[PATCH RFC] commands, context: allow to annotate working-copy revision

Yuya Nishihara yuya at tcha.org
Sun Feb 6 05:11:07 CST 2011


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1296990265 -32400
# Node ID 511aa87eeb262131bfd08655bab6fa94218750eb
# Parent  69e69b131458023d21ec40aa48fc5299e43ce69b
commands, context: allow to annotate working-copy revision

It changes 'hg annotate' command to annotate working-copy revision by default.
wctx rev and nodeid are displayed as blank in annotation result.

Since annotate now reads workingfctx.data(), keyword extension is updated to
unexpand substitutions while annotating.

Things I'm not sure:
- It might not be appropriate to change the default behavior of annotate
  command. But there's no way to specify working-copy revision explicitly.
- Is empty string good to denote working-copy rev and nodeid in annotation
  result?

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -90,7 +90,7 @@ import os, re, shutil, tempfile
 commands.optionalrepo += ' kwdemo'
 
 # hg commands that do not act on keywords
-nokwcommands = ('add addremove annotate bundle export grep incoming init log'
+nokwcommands = ('add addremove bundle export grep incoming init log'
                 ' outgoing push tip verify convert email glog')
 
 # hg commands that trigger expansion only when writing to working dir,
@@ -646,7 +646,12 @@ def reposetup(ui, repo):
             return self._filelog.cmp(self._filenode, fctx.data())
         return True
 
+    def kwfilectx_annotate(orig, self, follow=False, linenumber=None):
+        kwt.restrict = True
+        return orig(self, follow, linenumber)
+
     extensions.wrapfunction(context.filectx, 'cmp', kwfilectx_cmp)
+    extensions.wrapfunction(context.filectx, 'annotate', kwfilectx_annotate)
     extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init)
     extensions.wrapfunction(patch, 'diff', kw_diff)
     extensions.wrapfunction(cmdutil, 'copy', kw_copy)
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -107,8 +107,10 @@ def annotate(ui, repo, *pats, **opts):
         raise util.Abort(_('at least one filename or pattern is required'))
 
     opmap = [('user', lambda x: ui.shortuser(x[0].user())),
-             ('number', lambda x: str(x[0].rev())),
-             ('changeset', lambda x: short(x[0].node())),
+             ('number',
+              lambda x: x[0].rev() is not None and str(x[0].rev()) or ''),
+             ('changeset',
+              lambda x: x[0].node() is not None and short(x[0].node()) or ''),
              ('date', getdate),
              ('file', lambda x: x[0].path()),
             ]
@@ -126,7 +128,7 @@ def annotate(ui, repo, *pats, **opts):
         lastfunc = funcmap[-1]
         funcmap[-1] = lambda x: "%s:%s" % (lastfunc(x), x[1])
 
-    ctx = cmdutil.revsingle(repo, opts.get('rev'))
+    ctx = cmdutil.revsingle(repo, opts.get('rev'), None)
     m = cmdutil.match(repo, pats, opts)
     follow = not opts.get('no_follow')
     for abs in ctx.walk(m):
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -485,8 +485,14 @@ class filectx(object):
         for f in files:
             visit.extend(n for n in needed if n._path == f)
 
+        def revforsort(ctx):
+            if ctx.rev() is None:
+                # give the largest number to workingctx
+                return len(self._repo)
+            return ctx.rev()
+
         hist = {}
-        for f in sorted(visit, key=lambda x: x.rev()):
+        for f in sorted(visit, key=revforsort):
             curr = decorate(f.data(), f)
             for p in parents(f):
                 curr = pair(hist[p], curr)
@@ -907,6 +913,8 @@ class workingfilectx(filectx):
     def __repr__(self):
         return "<workingfilectx %s>" % str(self)
 
+    def linkrev(self):
+        return None
     def data(self):
         return self._repo.wread(self._path)
     def renamed(self):
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -228,3 +228,16 @@ annotate after ABA with follow
   $ hg annotate --follow foo
   foo: foo
 
+annotate dirty file
+
+  $ echo fooo >> foo
+  $ hg annotate -nc foo
+  8 94f2c296a83d: foo
+                : fooo
+
+annotate added file
+
+  $ echo baz > baz
+  $ hg add baz
+  $ hg annotate -nc baz
+   : baz


More information about the Mercurial-devel mailing list