[PATCH] Annotate update

Goffredo Baroncelli kreijack at alice.it
Thu Oct 13 21:51:02 BST 2005


Hi all,

the patch below adds two option to the 'bzr annotate' command:

  $ bzr annotate --help
  usage: bzr annotate FILENAME
  aliases: blame, praise

  Show the origin of each line in a file.

  This prints out the given file with an annotation on the
  left side indicating which revision, author and date introduced the
  change.

  options:
      --verbose, -v
      --long, -l


** the --long|-l option show a more verbose annotation

  $ bzr annotate NEWS | head -3
   1266 mbp at sou | UNRELEASED CHANGES
                |
                |   INTERNALS:

  $ bzr annotate --long NEWS | head -3
   1266 mbp at sourcefr 20051013 | UNRELEASED CHANGES
                              |
                              |   INTERNALS:

** the --verbose|-v options show the annotation for every line

  $ bzr annotate --verbose NEWS | head -3

   1266 mbp at sou | UNRELEASED CHANGES
   1266 mbp at sou |
   1266 mbp at sou |   INTERNALS:


Moreover the patch separate the annotate engine from the code which performs the
output. So now it is possible to reuse the annotate code in other place; in fact I 
need this code in my web interface to implement the browsing of the source 
on annotation basis. You can see a preview at this URL

http://goffredo-baroncelli.homelinux.net/bzr/?cmd=content;rev=1217;path=bzrlib/plugins/webserve/hgweb.py

In order to get the annotation on the web interface I had to duplicate the code from 
the bzrlib/annotate.py file as you can see

http://goffredo-baroncelli.homelinux.net/bzr/?cmd=revision;rev=1217



Please apply
Goffredo




 annotate.py |   38 +++++++++++++++++++++++++++-----------
 builtins.py |    7 ++++---
 2 files changed, 31 insertions(+), 14 deletions(-)


=== modified file 'bzrlib/annotate.py'
--- bzrlib/annotate.py
+++ bzrlib/annotate.py
@@ -18,8 +18,6 @@

 # TODO: Choice of more or less verbose formats:
 #
-# short: just show revno
-# long: revno, author, date
 # interposed: show more details between blocks of modified lines

 # TODO: Show which revision caused a line to merge into the parent
@@ -30,21 +28,39 @@

 import bzrlib.weave

-def annotate_file(branch, rev_id, file_id, to_file=None):
+
+def annotate_file(branch, rev_id, file_id, verbose=False, full=False,
+        to_file=None):
     if to_file is None:
         to_file = sys.stdout
+
+    prevanno=''
+    for (revno_str, author, date_str, line_rev_id, text ) in \
+            _annotate_file(branch, rev_id, file_id ):
+
+        if verbose:
+            anno = '%5s %-12s %8s ' % (revno_str, author[:12], date_str)
+        else:
+            anno = "%5s %-7s " % ( revno_str, author[:7] )
+
+        if anno.lstrip() == "" and full: anno = prevanno
+        print >>to_file, '%s| %s' % (anno, text)
+        prevanno=anno
+
+def _annotate_file(branch, rev_id, file_id ):
+
     rh = branch.revision_history()
     w = branch.weave_store.get_weave(file_id, branch.get_transaction())
     last_origin = None
     for origin, text in w.annotate_iter(rev_id):
         text = text.rstrip('\r\n')
         if origin == last_origin:
-            anno = ''
+            (revno_str, author, date_str) = ('','','')
         else:
             last_origin = origin
             line_rev_id = w.idx_to_name(origin)
             if not branch.has_revision(line_rev_id):
-                anno = '???'
+                (revno_str, author, date_str) = ('?','?','?')
             else:
                 if line_rev_id in rh:
                     revno_str = str(rh.index(line_rev_id) + 1)
@@ -57,11 +73,11 @@
             # a lazy way to get something like the email address
             # TODO: Get real email address
             author = line_rev_id
-            if '@' in author:
-                author = author[:author.index('@')]
-            author = author[:12]
-            anno = '%5s %-12s %8s' % (revno_str, author, date_str)
-        print '%-27.27s | %s' % (anno, text)
+            m = author.rfind('-')
+            if m >= 0: m = author.rfind('-',0,m-1)
+            if m >= 0: author = author[:m]
+
+        yield (revno_str, author, date_str, line_rev_id, text)


 if __name__ == '__main__':
@@ -74,4 +90,4 @@
     tree = b.revision_tree(b.last_revision())
     file_id = tree.inventory.path2id(rp)
     file_version = tree.inventory[file_id].revision
-    annotate_file(b, file_version, file_id, sys.stdout)
+    annotate_file(b, file_version, file_id, to_file = sys.stdout)

=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py
+++ bzrlib/builtins.py
@@ -1453,8 +1453,9 @@
     # TODO: annotate a previous version of a file
     aliases = ['blame', 'praise']
     takes_args = ['filename']
-
-    def run(self, filename):
+    takes_options = ['verbose','long']
+
+    def run(self, filename, verbose = False, long = False):
         from bzrlib.annotate import annotate_file
         b = Branch.open_containing(filename)
         b.lock_read()
@@ -1463,7 +1464,7 @@
             tree = b.revision_tree(b.last_revision())
             file_id = tree.inventory.path2id(rp)
             file_version = tree.inventory[file_id].revision
-            annotate_file(b, file_version, file_id, sys.stdout)
+            annotate_file(b, file_version, file_id, long, verbose, sys.stdout)
         finally:
             b.unlock()


-- 
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijack at inwind _DOT_ it>
Key fingerprint = CE3C 7E01 6782 30A3 5B87  87C0 BB86 505C 6B2A CFF9




More information about the bazaar mailing list