[PATCH 3 of 3] keyword: implement kwcat command

Christian Ebert blacktrash at gmx.net
Tue Jan 29 11:50:47 CST 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1201628457 -3600
# Node ID 049d05dabd71e4a04e8acdcd8471a9ccd7fc7da3
# Parent  49c5a3d714a6d0de97cfd5500c797cab8a8d9442
keyword: implement kwcat command

Note: If no [keyword] filenames are configured for expansion
      "hg kwcat" falls back silently to "hg cat"

Adapt test for kwcat.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -78,7 +78,7 @@
 "Log = {desc}" expands to the first line of the changeset description.
 '''
 
-from mercurial import commands, cmdutil, context, localrepo
+from mercurial import commands, cmdutil, context, filelog, localrepo
 from mercurial import patch, revlog, templater, util
 from mercurial.node import *
 from mercurial.i18n import _
@@ -135,6 +135,10 @@
             fl = self.repo.file(path)
             c = context.filectx(self.repo, path, fileid=fnode, filelog=fl)
             node = c.node()
+        elif subfunc == self.re_kw.sub:
+            # hg kwcat using kwfilelog.read
+            c = context.filectx(self.repo, path, fileid=node)
+            node = c.node()
 
         def kwsub(mobj):
             '''Substitutes keyword using corresponding template.'''
@@ -146,11 +150,11 @@
 
         return subfunc(kwsub, data)
 
-    def expand(self, path, data):
+    def expand(self, path, data, node):
         '''Returns data with keywords expanded.'''
         if util.binary(data):
             return data
-        return self.substitute(path, data, None, self.re_kw.sub)
+        return self.substitute(path, data, node, self.re_kw.sub)
 
     def process(self, path, data, expand, ctx, node):
         '''Returns a tuple: data, count.
@@ -168,6 +172,20 @@
         if util.binary(data):
             return data
         return self.re_kw.sub(r'$\1$', data)
+
+class kwfilelog(filelog.filelog):
+    '''
+    Subclass of filelog to hook into its read method for kwcat.
+    '''
+    def __init__(self, opener, path, kwt):
+        super(kwfilelog, self).__init__(opener, path)
+        self._kwt = kwt
+        self._path = path
+
+    def read(self, node):
+        '''Expands keywords when reading filelog.'''
+        data = super(kwfilelog, self).read(node)
+        return self._kwt.expand(self._path, data, node)
 
 # store original patch.patchfile.__init__
 _patchfile_init = patch.patchfile.__init__
@@ -222,6 +240,26 @@
     finally:
         del wlock, lock
 
+def cat(ui, repo, file1, *pats, **opts):
+    '''output the current or given revision of files expanding keywords
+
+    Print the specified files as they were at the given revision.
+    If no revision is given, the parent of the working directory is used,
+    or tip if no revision is checked out.
+
+    Output may be to a file, in which case the name of the file is
+    given using a format string.  The formatting rules are the same as
+    for the export command, with the following additions:
+
+    %s   basename of file being printed
+    %d   dirname of file being printed, or '.' if in repo root
+    %p   root-relative path name of file being printed
+    '''
+    try:
+        repo.file = repo._kwfile
+    except AttributeError:
+        pass
+    commands.cat(ui, repo, file1, *pats, **opts)
 
 def demo(ui, repo, *args, **opts):
     '''print [keywordmaps] configuration and an expansion example
@@ -362,6 +400,14 @@
         return
 
     class kwrepo(repo.__class__):
+        def _kwfile(self, f):
+            '''Returns filelog expanding keywords on read (for kwcat).'''
+            if f[0] == '/':
+                f = f[1:]
+            if self._kwt.matcher(f):
+                return kwfilelog(self.sopener, f, self._kwt)
+            return filelog.filelog(self.sopener, f)
+
         def _wreadkwct(self, filename, expand, ctx, node):
             '''Reads filename and returns tuple of data with keywords
             expanded/shrunk and count of keywords (for _overwrite).'''
@@ -376,12 +422,12 @@
 
         def wwrite(self, filename, data, flags, overwrite=False):
             if not overwrite and self._kwt.matcher(filename):
-                data = self._kwt.expand(filename, data)
+                data = self._kwt.expand(filename, data, None)
             super(kwrepo, self).wwrite(filename, data, flags)
 
         def wwritedata(self, filename, data):
             if self._kwt.matcher(filename):
-                data = self._kwt.expand(filename, data)
+                data = self._kwt.expand(filename, data, None)
             return super(kwrepo, self).wwritedata(filename, data)
 
         def commit(self, files=None, text='', user=None, date=None,
@@ -443,6 +489,9 @@
 
 
 cmdtable = {
+    'kwcat':
+        (cat, commands.table['cat'][1],
+         _('hg kwcat [OPTION]... FILE...')),
     'kwdemo':
         (demo,
          [('d', 'default', None, _('show default keyword template maps')),
diff --git a/tests/test-keyword b/tests/test-keyword
--- a/tests/test-keyword
+++ b/tests/test-keyword
@@ -57,8 +57,8 @@
 hg --quiet identify
 echo % cat
 cat sym a b
-echo % hg cat
-hg cat sym a b
+echo % hg kwcat
+hg kwcat sym a b
 
 echo
 echo % diff a hooktest
@@ -144,8 +144,8 @@
 
 echo % cat
 cat sym a b
-echo % hg cat
-hg cat sym a b
+echo % hg kwcat
+hg kwcat sym a b
 
 echo
 echo '$Xinfo$' >> a
@@ -167,8 +167,8 @@
 
 echo % cat
 cat sym a b
-echo % hg cat
-hg cat sym a b
+echo % hg kwcat
+hg kwcat sym a b
 echo
 
 echo % remove
@@ -242,12 +242,6 @@
 hg --verbose kwshrink
 echo % cat
 cat sym a b
-echo % hg cat
-hg cat sym a b
+echo % hg kwcat
+hg kwcat sym a b
 echo
-rm $HGRCPATH
-echo % cat
-cat sym a b
-echo % hg cat
-hg cat sym a b
-echo
diff --git a/tests/test-keyword.out b/tests/test-keyword.out
--- a/tests/test-keyword.out
+++ b/tests/test-keyword.out
@@ -46,6 +46,7 @@
 
 list of commands:
 
+ kwcat      output the current or given revision of files expanding keywords
  kwdemo     print [keywordmaps] configuration and an expansion example
  kwexpand   expand keywords in working directory
  kwfiles    print files currently configured for keyword expansion
@@ -130,7 +131,7 @@
 do not process $Id:
 xxx $
 ignore $Id$
-% hg cat
+% hg kwcat
 expand $Id: a,v f782df5f9602 1970/01/01 00:00:00 user $
 do not process $Id:
 xxx $
@@ -239,7 +240,7 @@
 do not process $Id:
 xxx $
 ignore $Id$
-% hg cat
+% hg kwcat
 expand $Id: a f782df5f9602 Thu, 01 Jan 1970 00:00:00 +0000 user $
 do not process $Id:
 xxx $
@@ -266,7 +267,7 @@
 xxx $
 $Xinfo: User Name <user at example.com>: firstline $
 ignore $Id$
-% hg cat
+% hg kwcat
 expand $Id: a 0729690beff6 Thu, 01 Jan 1970 00:00:02 +0000 user $
 do not process $Id:
 xxx $
@@ -359,27 +360,10 @@
 xxx $
 $Xinfo$
 ignore $Id$
-% hg cat
+% hg kwcat
 expand $Id: a 0729690beff6 Thu, 01 Jan 1970 00:00:02 +0000 user $
 do not process $Id:
 xxx $
 $Xinfo: User Name <user at example.com>: firstline $
 ignore $Id$
 a
-% cat
-expand $Id$
-do not process $Id:
-xxx $
-$Xinfo$
-expand $Id$
-do not process $Id:
-xxx $
-$Xinfo$
-ignore $Id$
-% hg cat
-expand $Id$
-do not process $Id:
-xxx $
-$Xinfo$
-ignore $Id$
-a


More information about the Mercurial-devel mailing list