[Pkg-bazaar-commits] ./bzr/unstable r379: - Simpler compare_inventories() to possibly replace diff_trees

Martin Pool mbp at sourcefrog.net
Fri Apr 10 07:43:41 UTC 2009


------------------------------------------------------------
revno: 379
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Thu 2005-05-05 19:26:27 +1000
message:
  - Simpler compare_inventories() to possibly replace diff_trees
  - New TreeDelta class
  - Use this in show_log()
modified:
  NEWS
  bzrlib/diff.py
  bzrlib/log.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-05-05 08:26:20 +0000
+++ b/NEWS	2005-05-05 09:26:27 +0000
@@ -20,6 +20,8 @@
     * New usage ``bzr log FILENAME`` shows only revisions that
       affected that file.
 
+    * Changed format for describing changes in ``bzr log -v``.
+
   TESTING:
 
     * Converted black-box test suites from Bourne shell into Python;
@@ -41,6 +43,8 @@
     * New internal function find_touching_revisions() and hidden
       command touching-revisions trace the changes to a given file.
 
+    * Simpler and faster compare_inventories() function.
+
 bzr-0.0.4  2005-04-22
 
   ENHANCEMENTS:

=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py	2005-05-05 05:41:45 +0000
+++ b/bzrlib/diff.py	2005-05-05 09:26:27 +0000
@@ -248,3 +248,65 @@
             raise BzrError("can't represent state %s {%s}" % (file_state, fid))
 
 
+
+class TreeDelta:
+    """Describes changes from one tree to another.
+
+    Contains four lists:
+
+    added
+        (path, id)
+    removed
+        (path, id)
+    renamed
+        (oldpath, newpath, id)
+    modified
+        (path, id)
+
+    A path may occur in more than one list if it was e.g. deleted
+    under an old id and renamed into place in a new id.
+
+    Files are listed in either modified or renamed, not both.  In
+    other words, renamed files may also be modified.
+    """
+    def __init__(self):
+        self.added = []
+        self.removed = []
+        self.renamed = []
+        self.modified = []
+
+
+def compare_inventories(old_inv, new_inv):
+    """Return a TreeDelta object describing changes between inventories.
+
+    This only describes changes in the shape of the tree, not the
+    actual texts.
+
+    This is an alternative to diff_trees() and should probably
+    eventually replace it.
+    """
+    old_ids = old_inv.id_set()
+    new_ids = new_inv.id_set()
+    delta = TreeDelta()
+
+    delta.removed = [(old_inv.id2path(fid), fid) for fid in (old_ids - new_ids)]
+    delta.removed.sort()
+
+    delta.added = [(new_inv.id2path(fid), fid) for fid in (new_ids - old_ids)]
+    delta.added.sort()
+
+    for fid in old_ids & new_ids:
+        old_ie = old_inv[fid]
+        new_ie = new_inv[fid]
+        old_path = old_inv.id2path(fid)
+        new_path = new_inv.id2path(fid)
+
+        if old_path != new_path:
+            delta.renamed.append((old_path, new_path, fid))
+        elif old_ie.text_sha1 != new_ie.text_sha1:
+            delta.modified.append((new_path, fid))
+
+    delta.modified.sort()
+    delta.renamed.sort()    
+
+    return delta

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2005-05-05 08:26:20 +0000
+++ b/bzrlib/log.py	2005-05-05 09:26:27 +0000
@@ -94,8 +94,9 @@
     """
     from osutils import format_date
     from errors import BzrCheckError
-    from diff import diff_trees
+    from diff import compare_inventories
     from textui import show_status
+    from inventory import Inventory
 
     if to_file == None:
         import sys
@@ -113,6 +114,8 @@
         
     branch._need_readlock()
     precursor = None
+    if verbose:
+        prev_inv = Inventory()
     for revno, revision_id in which_revs():
         print >>to_file,  '-' * 60
         print >>to_file,  'revno:', revno
@@ -137,22 +140,28 @@
         # Don't show a list of changed files if we were asked about
         # one specific file.
 
-        if verbose and precursor and not filename:
-            # TODO: Group as added/deleted/renamed instead
-            # TODO: Show file ids
-            print >>to_file, 'changed files:'
-            tree = branch.revision_tree(revision_id)
-            prevtree = branch.revision_tree(precursor)
-
-            for file_state, fid, old_name, new_name, kind in \
-                                    diff_trees(prevtree, tree, ):
-                if file_state == 'A' or file_state == 'M':
-                    show_status(file_state, kind, new_name)
-                elif file_state == 'D':
-                    show_status(file_state, kind, old_name)
-                elif file_state == 'R':
-                    show_status(file_state, kind,
-                        old_name + ' => ' + new_name)
+        if verbose and not filename:
+            this_inv = branch.get_inventory(rev.inventory_id)
+            delta = compare_inventories(prev_inv, this_inv)
+
+            if delta.removed:
+                print >>to_file, 'removed files:'
+                for path, fid in delta.removed:
+                    print >>to_file, '  ' + path
+            if delta.added:
+                print >>to_file, 'added files:'
+                for path, fid in delta.added:
+                    print >>to_file, '  ' + path
+            if delta.renamed:
+                print >>to_file, 'renamed files:'
+                for oldpath, newpath, fid in delta.renamed:
+                    print >>to_file, '  %s => %s' % (oldpath, newpath)
+            if delta.modified:
+                print >>to_file, 'modified files:'
+                for path, fid in delta.modified:
+                    print >>to_file, '  ' + path
+
+            prev_inv = this_inv
 
         precursor = revision_id
 



More information about the Pkg-bazaar-commits mailing list