[Pkg-bazaar-commits] ./bzr/unstable r462: - New form 'file_id in tree' to check if the file is present

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


------------------------------------------------------------
revno: 462
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Wed 2005-05-11 14:57:13 +1000
message:
  - New form 'file_id in tree' to check if the file is present
  - Rewrite show_info to work on compare_trees (much faster)
  - New form 'for file_id in tree' to iterate through files there.
modified:
  bzrlib/diff.py
  bzrlib/info.py
  bzrlib/tree.py
  bzrlib/workingtree.py
-------------- next part --------------
=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py	2005-05-11 04:25:35 +0000
+++ b/bzrlib/diff.py	2005-05-11 04:57:13 +0000
@@ -307,8 +307,8 @@
     old_inv = old_tree.inventory
     new_inv = new_tree.inventory
     delta = TreeDelta()
-    for file_id in old_inv:
-        if file_id in new_inv:
+    for file_id in old_tree:
+        if file_id in new_tree:
             old_path = old_inv.id2path(file_id)
             new_path = new_inv.id2path(file_id)
 

=== modified file 'bzrlib/info.py'
--- a/bzrlib/info.py	2005-04-26 23:44:41 +0000
+++ b/bzrlib/info.py	2005-05-11 04:57:13 +0000
@@ -19,10 +19,21 @@
 from sets import Set
 import time
 
-import bzrlib
 from osutils import format_date
 
+
+def _countiter(it):
+    # surely there's a builtin for this?
+    i = 0
+    for j in it:
+        i += 1
+    return i        
+
+
+
 def show_info(b):
+    import diff
+    
     print 'branch format:', b.controlfile('branch-format', 'r').readline().rstrip('\n')
 
     def plural(n, base='', pl=None):
@@ -35,22 +46,36 @@
 
     count_version_dirs = 0
 
-    count_status = {'A': 0, 'D': 0, 'M': 0, 'R': 0, '?': 0, 'I': 0, '.': 0}
-    for st_tup in bzrlib.diff_trees(b.basis_tree(), b.working_tree()):
-        fs = st_tup[0]
-        count_status[fs] += 1
-        if fs not in ['I', '?'] and st_tup[4] == 'directory':
-            count_version_dirs += 1
-
+    basis = b.basis_tree()
+    working = b.working_tree()
+    work_inv = working.inventory
+    delta = diff.compare_trees(basis, working)
+    
     print
     print 'in the working tree:'
-    for name, fs in (('unchanged', '.'),
-                     ('modified', 'M'), ('added', 'A'), ('removed', 'D'),
-                     ('renamed', 'R'), ('unknown', '?'), ('ignored', 'I'),
-                     ):
-        print '  %8d %s' % (count_status[fs], name)
-    print '  %8d versioned subdirector%s' % (count_version_dirs,
-                                             plural(count_version_dirs, 'y', 'ies'))
+    print '  %8s unchanged' % '?'
+    print '  %8d modified' % len(delta.modified)
+    print '  %8d added' % len(delta.added)
+    print '  %8d removed' % len(delta.removed)
+    print '  %8d renamed' % len(delta.renamed)
+
+    ignore_cnt = unknown_cnt = 0
+    for path in working.extras():
+        if working.is_ignored(path):
+            ignore_cnt += 1
+        else:
+            unknown_cnt += 1
+
+    print '  %8d unknown' % unknown_cnt
+    print '  %8d ignored' % ignore_cnt
+
+    dir_cnt = 0
+    for file_id in work_inv:
+        if work_inv.get_file_kind(file_id) == 'directory':
+            dir_cnt += 1
+    print '  %8d versioned %s' \
+          % (dir_cnt,
+             plural(dir_cnt, 'subdirectory', 'subdirectories'))
 
     print
     print 'branch history:'

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2005-05-11 02:22:26 +0000
+++ b/bzrlib/tree.py	2005-05-11 04:57:13 +0000
@@ -61,10 +61,15 @@
     def has_id(self, file_id):
         return self.inventory.has_id(file_id)
 
+    __contains__ = has_id
+
     def id_set(self):
         """Return set of all ids in this tree."""
         return self.inventory.id_set()
 
+    def __iter__(self):
+        return iter(self.inventory)
+
     def id2path(self, file_id):
         return self.inventory.id2path(file_id)
 

=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py	2005-05-11 02:22:26 +0000
+++ b/bzrlib/workingtree.py	2005-05-11 04:57:13 +0000
@@ -38,6 +38,26 @@
         self.basedir = basedir
         self.path2id = inv.path2id
 
+    def __iter__(self):
+        """Iterate through file_ids for this tree.
+
+        file_ids are in a WorkingTree if they are in the working inventory
+        and the working file exists.
+        """
+        self._update_statcache()
+        inv = self._inventory
+        for file_id in self._inventory:
+            # TODO: This is slightly redundant; we should be able to just
+            # check the statcache but it only includes regular files.
+            # only include files which still exist on disk
+            ie = inv[file_id]
+            if ie.kind == 'file':
+                if ((file_id in self._statcache)
+                    or (os.path.exists(self.abspath(inv.id2path(file_id))))):
+                    yield file_id
+
+
+
     def __repr__(self):
         return "<%s of %s>" % (self.__class__.__name__,
                                self.basedir)
@@ -58,12 +78,24 @@
         ## XXX: badly named; this isn't in the store at all
         return self.abspath(self.id2path(file_id))
 
+                
     def has_id(self, file_id):
         # files that have been deleted are excluded
         if not self.inventory.has_id(file_id):
             return False
-        import os
-        return os.access(self.abspath(self.inventory.id2path(file_id)), os.F_OK)
+        self._update_statcache()
+        if file_id in self._statcache:
+            return True
+        return os.path.exists(self.abspath(self.id2path(file_id)))
+
+
+    __contains__ = has_id
+    
+
+    def _update_statcache(self):
+        import statcache
+        if not self._statcache:
+            self._statcache = statcache.update_cache(self.basedir, self.inventory)
 
     def get_file_size(self, file_id):
         import os, stat
@@ -72,9 +104,7 @@
 
     def get_file_sha1(self, file_id):
         import statcache
-        if not self._statcache:
-            self._statcache = statcache.update_cache(self.basedir, self.inventory)
-
+        self._update_statcache()
         return self._statcache[file_id][statcache.SC_SHA1]
 
 



More information about the Pkg-bazaar-commits mailing list