Add options to annotate for user/rev/changeset
authormpm@selenic.com
Wed, 25 May 2005 08:53:01 -0800
changeset 150 8029ee2949b8
parent 149 8cd45e152c83
child 151 1f6c61a60586
Add options to annotate for user/rev/changeset This eliminates the blame command and makes annotate more flexible.
hg
--- a/hg	Tue May 24 23:34:06 2005 -0800
+++ b/hg	Wed May 25 08:53:01 2005 -0800
@@ -23,23 +23,22 @@
     print """\
  commands:
 
- init                  create a new repository in this directory
+ add [files...]        add the given files in the next commit
+ addremove             add all new files, delete all missing files
+ annotate [files...]   show changeset number per file line
  branch <path>         create a branch of <path> in this directory
- merge <path>          merge changes from <path> into local repository
  checkout [changeset]  checkout the latest or given changeset
- status                show new, missing, and changed files in working dir
- add [files...]        add the given files in the next commit
- remove [files...]     remove the given files in the next commit
- addremove             add all new files, delete all missing files
  commit                commit all changes to the repository
- history               show changeset history
- log <file>            show revision history of a single file
+ diff [files...]       diff working directory (or selected files)
  dump <file> [rev]     dump the latest or given revision of a file
  dumpmanifest [rev]    dump the latest or given revision of the manifest
- diff [files...]       diff working directory (or selected files)
+ history               show changeset history
+ init                  create a new repository in this directory
+ log <file>            show revision history of a single file
+ merge <path>          merge changes from <path> into local repository
+ remove [files...]     remove the given files in the next commit
+ status                show new, missing, and changed files in working dir
  tags                  show current changeset tags
- annotate [files...]   show changeset number per file line
- blame [files...]      show commit user per file line
 """
 
 def filterfiles(list, files):
@@ -215,47 +214,57 @@
     diff(args, *revs)
 
 elif cmd == "annotate":
+    bcache = {}
+
+    def getnode(rev):
+        return hg.short(repo.changelog.node(rev))
+
+    def getname(rev):
+        try:
+            return bcache[rev]
+        except KeyError:
+            cl = repo.changelog.read(repo.changelog.node(rev))
+            name = cl[1]
+            f = name.find('@')
+            if f >= 0:
+                name = name[:f]
+            bcache[rev] = name
+            return name
+    
     aoptions = {}
-    opts = [('r', 'revision', '', 'revision')]
+    opts = [('r', 'revision', '', 'revision'),
+            ('u', 'user', None, 'show user'),
+            ('n', 'number', None, 'show revision number'),
+            ('c', 'changeset', None, 'show changeset')]
+
     args = fancyopts.fancyopts(args, opts, aoptions,
-                               'hg annotate [-r id] [files]')
+                               'hg annotate [-u] [-c] [-n] [-r id] [files]')
+
+    opmap = [['user', getname], ['number', str], ['changeset', getnode]]
+    if not aoptions['user'] and not aoptions['changeset']:
+        aoptions['number'] = 1
 
     if args:
         if relpath: args = [ os.path.join(relpath, x) for x in args ]
-
         node = repo.current
         if aoptions['revision']:
             node = repo.changelog.lookup(aoptions['revision'])
         change = repo.changelog.read(node)
         mmap = repo.manifest.read(change[0])
+        maxuserlen = 0
+        maxchangelen = 0
         for f in args:
-            for n, l in repo.file(f).annotate(mmap[f]):
-                sys.stdout.write("% 6s:%s"%(n, l))
+            lines = repo.file(f).annotate(mmap[f])
+            pieces = []
 
-elif cmd == "blame":
-    aoptions = {}
-    opts = [('r', 'revision', '', 'revision')]
-    args = fancyopts.fancyopts(args, opts, aoptions,
-                               'hg blame [-r id] [files]')
-    if args:
-        bcache = {}
-        node = repo.current
-        if aoptions['revision']:
-            node = repo.changelog.lookup(aoptions['revision'])
-        change = repo.changelog.read(node)
-        mmap = repo.manifest.read(change[0])
-        for f in args:
-            for n, l in repo.file(f).annotate(mmap[f]):
-                try:
-                    name = bcache[n]
-                except KeyError:
-                    cl = repo.changelog.read(repo.changelog.node(n))
-                    name = cl[1]
-                    f = name.find('@')
-                    if f >= 0:
-                        name = name[:f]
-                    bcache[n] = name
-                sys.stdout.write("% 10s:%s"%(name, l))
+            for o, f in opmap:
+                if aoptions[o]:
+                    l = [ f(n) for n,t in lines ]
+                    m = max(map(len, l))
+                    pieces.append([ "%*s" % (m, x) for x in l])
+
+            for p,l in zip(zip(*pieces), lines):
+                sys.stdout.write(" ".join(p) + ": " + l[1])
 
 elif cmd == "export":
     node = repo.lookup(args[0])