[Pkg-bazaar-commits] ./bzr/unstable r460: - new testing command compare-trees

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:19:19 UTC 2009


------------------------------------------------------------
revno: 460
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Wed 2005-05-11 14:18:51 +1000
message:
  - new testing command compare-trees
  - change operation of TreeDelta object a bit
    to specify which renamed files also have modified text
  - new TreeDelta.show() factored out
  - new compare_trees similar to compare_inventories
    but handling WorkingTrees that don't have a SHA-1 
    in the inventory but can get it from cache
  - new Inventory.get_file_kind
modified:
  bzrlib/commands.py
  bzrlib/diff.py
  bzrlib/inventory.py
  bzrlib/log.py
-------------- next part --------------
=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-05-11 02:24:55 +0000
+++ b/bzrlib/commands.py	2005-05-11 04:18:51 +0000
@@ -858,6 +858,16 @@
         statcache.update_cache(b.base, b.read_working_inventory())
 
 
+class cmd_compare_trees(Command):
+    """Show quick calculation of status."""
+    hidden = True
+    def run(self):
+        import diff
+        b = Branch('.')
+        delta = diff.compare_trees(b.basis_tree(), b.working_tree())
+        delta.show(sys.stdout, False)
+
+
 ######################################################################
 # main routine
 

=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py	2005-05-11 02:48:39 +0000
+++ b/bzrlib/diff.py	2005-05-11 04:18:51 +0000
@@ -27,17 +27,13 @@
     They may be in different branches and may be working or historical
     trees.
 
+    This only compares the versioned files, paying no attention to
+    files which are ignored or unknown.  Those can only be present in
+    working trees and can be reported on separately.
+
     Yields a sequence of (state, id, old_name, new_name, kind).
     Each filename and each id is listed only once.
     """
-
-    ## TODO: Compare files before diffing; only mention those that have changed
-
-    ## TODO: Set nice names in the headers, maybe include diffstat
-
-    ## TODO: Perhaps make this a generator rather than using
-    ## a callback object?
-
     ## TODO: Allow specifying a list of files to compare, rather than
     ## doing the whole tree?  (Not urgent.)
 
@@ -46,9 +42,6 @@
     ## stores to look for the files if diffing two branches.  That
     ## might imply this shouldn't be primarily a Branch method.
 
-    ## XXX: This doesn't report on unknown files; that can be done
-    ## from a separate method.
-
     sha_match_cnt = modified_cnt = 0
 
     old_it = old_tree.list_files()
@@ -263,15 +256,14 @@
     removed
         (path, id)
     renamed
-        (oldpath, newpath, id)
+        (oldpath, newpath, id, text_modified)
     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.
+    Each id is listed only once.
 
-    Files are listed in either modified or renamed, not both.  In
-    other words, renamed files may also be modified.
+    Files that are both modified and renamed are listed only in
+    renamed, with the text_modified flag true.
     """
     def __init__(self):
         self.added = []
@@ -279,6 +271,37 @@
         self.renamed = []
         self.modified = []
 
+    def show(self, to_file, show_ids):
+        if self.removed:
+            print >>to_file, 'removed files:'
+            for path, fid in self.removed:
+                if show_ids:
+                    print >>to_file, '  %-30s %s' % (path, fid)
+                else:
+                    print >>to_file, ' ', path
+        if self.added:
+            print >>to_file, 'added files:'
+            for path, fid in self.added:
+                if show_ids:
+                    print >>to_file, '  %-30s %s' % (path, fid)
+                else:
+                    print >>to_file, '  ' + path
+        if self.renamed:
+            print >>to_file, 'renamed files:'
+            for oldpath, newpath, fid, text_modified in self.renamed:
+                if show_ids:
+                    print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
+                else:
+                    print >>to_file, '  %s => %s' % (oldpath, newpath)
+        if self.modified:
+            print >>to_file, 'modified files:'
+            for path, fid in self.modified:
+                if show_ids:
+                    print >>to_file, '  %-30s %s' % (path, fid)
+                else:
+                    print >>to_file, '  ' + path
+
+        
 
 def compare_inventories(old_inv, new_inv):
     """Return a TreeDelta object describing changes between inventories.
@@ -305,12 +328,55 @@
         old_path = old_inv.id2path(fid)
         new_path = new_inv.id2path(fid)
 
+        text_modified = (old_ie.text_sha1 != new_ie.text_sha1)
+
         if old_path != new_path:
-            delta.renamed.append((old_path, new_path, fid))
-        elif old_ie.text_sha1 != new_ie.text_sha1:
+            delta.renamed.append((old_path, new_path, fid, text_modified))
+        elif text_modified:
             delta.modified.append((new_path, fid))
 
     delta.modified.sort()
     delta.renamed.sort()    
 
     return delta
+
+
+
+
+def compare_trees(old_tree, new_tree):
+    old_inv = old_tree.inventory
+    new_inv = new_tree.inventory
+    delta = TreeDelta()
+    for file_id in old_inv:
+        if file_id in new_inv:
+            old_path = old_inv.id2path(file_id)
+            new_path = new_inv.id2path(file_id)
+
+            kind = old_inv.get_file_kind(file_id)
+            assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
+                   'invalid file kind %r' % kind
+            if kind == 'file':
+                old_sha1 = old_tree.get_file_sha1(file_id)
+                new_sha1 = new_tree.get_file_sha1(file_id)
+                text_modified = (old_sha1 != new_sha1)
+            else:
+                ## mutter("no text to check for %r %r" % (file_id, kind))
+                text_modified = False
+            
+            if old_path != new_path:
+                delta.renamed.append((old_path, new_path, file_id, text_modified))
+            elif text_modified:
+                delta.modified.append((new_path, file_id))
+        else:
+            delta.removed.append((old_inv.id2path(file_id), file_id))
+    for file_id in new_inv:
+        if file_id in old_inv:
+            continue
+        delta.added.append((new_inv.id2path(file_id), file_id))
+            
+    delta.removed.sort()
+    delta.added.sort()
+    delta.renamed.sort()
+    delta.modified.sort()
+
+    return delta

=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2005-05-08 23:11:45 +0000
+++ b/bzrlib/inventory.py	2005-05-11 04:18:51 +0000
@@ -380,6 +380,9 @@
                 raise BzrError("file_id {%s} not in inventory" % file_id)
 
 
+    def get_file_kind(self, file_id):
+        return self._byid[file_id].kind
+
     def get_child(self, parent_id, filename):
         return self[parent_id].children.get(filename)
 

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2005-05-05 09:57:17 +0000
+++ b/bzrlib/log.py	2005-05-11 04:18:51 +0000
@@ -143,36 +143,7 @@
         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:
-                    if show_ids:
-                        print >>to_file, '  %-30s %s' % (path, fid)
-                    else:
-                        print >>to_file, ' ', path
-            if delta.added:
-                print >>to_file, 'added files:'
-                for path, fid in delta.added:
-                    if show_ids:
-                        print >>to_file, '  %-30s %s' % (path, fid)
-                    else:
-                        print >>to_file, '  ' + path
-            if delta.renamed:
-                print >>to_file, 'renamed files:'
-                for oldpath, newpath, fid in delta.renamed:
-                    if show_ids:
-                        print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
-                    else:
-                        print >>to_file, '  %s => %s' % (oldpath, newpath)
-            if delta.modified:
-                print >>to_file, 'modified files:'
-                for path, fid in delta.modified:
-                    if show_ids:
-                        print >>to_file, '  %-30s %s' % (path, fid)
-                    else:
-                        print >>to_file, '  ' + path
-
+            delta.show(to_file, show_ids)
             prev_inv = this_inv
 
         precursor = revision_id



More information about the Pkg-bazaar-commits mailing list