[PATCH 8 of 8 RFC] annotate: add option to annotate working-directory files

Yuya Nishihara yuya at tcha.org
Tue Aug 19 17:56:22 CDT 2014


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1408179055 -32400
#      Sat Aug 16 17:50:55 2014 +0900
# Node ID b13b2433ce7f33fba9f80da642fcff2bdd76b229
# Parent  c8ad2080d3f4031851a84f12cee37439da405048
annotate: add option to annotate working-directory files

Working revision is displayed with "+" suffix.  Instead of the output of this
patch:

    1+ 0123456789ab+ foo
     0  cdef01234567 bar

it might be better to handle "+" suffix specially:

     1+ 0123456789ab foo
     0  cdef01234567 bar

If we prefer the latter, some of the previous patches would be useless and
should be dropped.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -258,6 +258,9 @@ def annotate(ui, repo, *pats, **opts):
     anyway, although the results will probably be neither useful
     nor desirable.
 
+    By default, annotate files in the parent of the working directory.
+    Use -r workingrev to annotate the working directory files.
+
     Returns 0 on success.
     """
     if not pats:
@@ -268,6 +271,11 @@ def annotate(ui, repo, *pats, **opts):
         # to mimic the behavior of Mercurial before version 1.5
         opts['file'] = True
 
+    def getnumber(x):
+        if x[0].uncommitted():
+            return '%d+' % x[0].changectx().p1().rev()
+        else:
+            return str(x[0].rev())
     if ui.debugflag:
         getchangeset = lambda x: x[0].hex()
     else:
@@ -276,7 +284,7 @@ def annotate(ui, repo, *pats, **opts):
     getdate = util.cachefunc(lambda x: datefunc(x[0].date()))
 
     opmap = [('user', ' ', lambda x: ui.shortuser(x[0].user())),
-             ('number', ' ', lambda x: str(x[0].rev())),
+             ('number', ' ', getnumber),
              ('changeset', ' ', getchangeset),
              ('date', ' ', getdate),
              ('file', ' ', lambda x: x[0].path()),
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -812,7 +812,9 @@ class basefilectx(object):
             return pl
 
         # use linkrev to find the first changeset where self appeared
-        if self.rev() != self.linkrev():
+        if self.uncommitted():
+            base = self  # uncommitted file has no linkrev
+        elif self.rev() != self.linkrev():
             base = self.filectx(self.filenode())
         else:
             base = self
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -378,6 +378,54 @@ and its ancestor by overriding "repo._fi
   20: 4 baz:4
   16: 5
 
+annotate clean file
+
+  $ hg annotate -ncr workingdir foo
+  11 472b18db256d: foo
+
+annotate modified file
+
+  $ echo foofoo >> foo
+  $ hg annotate -ncr workingdir foo
+   11  472b18db256d: foo
+  20+ b6bedd5477e7+: foofoo
+
+  $ hg annotate --debug -ncr workingdir foo
+   11  472b18db256d1e8282064eab4bfdaf48cbfe83cd: foo
+  20+ b6bedd5477e797f25e568a6402d4697f3f895a72+: foofoo
+
+  $ hg annotate -udr workingdir foo
+  test Thu Jan 01 00:00:00 1970 +0000: foo
+  test [A-Za-z0-9:+ ]+: foofoo (re)
+
+annotate added file
+
+  $ echo bar > bar
+  $ hg add bar
+  $ hg annotate -ncr workingdir bar
+  20+ b6bedd5477e7+: bar
+
+annotate renamed file
+
+  $ hg rename foo renamefoo2
+  $ hg annotate -ncr workingdir renamefoo2
+   11  472b18db256d: foo
+  20+ b6bedd5477e7+: foofoo
+
+annotate missing file
+
+  $ rm baz
+  $ hg annotate -ncr workingdir baz
+  abort: No such file or directory: $TESTTMP/repo/baz
+  [255]
+
+annotate removed file
+
+  $ hg rm baz
+  $ hg annotate -ncr workingdir baz
+  abort: No such file or directory: $TESTTMP/repo/baz
+  [255]
+
 Test annotate with whitespace options
 
   $ cd ..


More information about the Mercurial-devel mailing list