# HG changeset patch # User Christian Ebert # Date 1201624345 -3600 # Node ID b3caec747375c840e71eecdae23fb6235562d289 # Parent 8e3364294d0c3c8020bc47f98e29d3eab98ce7f2 Implement kwcat command diff -r 8e3364294d0c -r b3caec747375 hgkw/keyword.py --- a/hgkw/keyword.py Tue Jan 29 17:32:15 2008 +0100 +++ b/hgkw/keyword.py Tue Jan 29 17:32:25 2008 +0100 @@ -80,7 +80,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 _ @@ -137,6 +137,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.''' @@ -148,11 +152,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. @@ -171,6 +175,20 @@ 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__ @@ -224,6 +242,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 @@ -364,6 +402,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).''' @@ -378,12 +424,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, @@ -445,6 +491,9 @@ cmdtable = { + 'kwcat': + (cat, commands.table['cat'][1], + _('hg kwcat [OPTION]... FILE...')), 'kwdemo': (demo, [('d', 'default', None, _('show default keyword template maps')),