[Pkg-bazaar-commits] ./bzr/unstable r909: - merge John's code to give the tree root an explicit file id

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


------------------------------------------------------------
revno: 909
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Mon 2005-07-11 17:33:45 +1000
message:
  - merge John's code to give the tree root an explicit file id
modified:
  bzrlib/branch.py
  bzrlib/check.py
  bzrlib/commands.py
  bzrlib/commit.py
  bzrlib/inventory.py
  bzrlib/log.py
  bzrlib/newinventory.py
  bzrlib/osutils.py
  bzrlib/store.py
  bzrlib/tree.py
-------------- next part --------------
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-07-11 07:05:34 +0000
+++ b/bzrlib/branch.py	2005-07-11 07:33:45 +0000
@@ -312,7 +312,7 @@
             self.controlfile(f, 'w').write('')
         mutter('created control directory in ' + self.base)
 
-        pack_xml(Inventory(), self.controlfile('inventory','w'))
+        pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
 
 
     def _check_format(self):
@@ -333,7 +333,22 @@
                            ['use a different bzr version',
                             'or remove the .bzr directory and "bzr init" again'])
 
+    def get_root_id(self):
+        """Return the id of this branches root"""
+        inv = self.read_working_inventory()
+        return inv.root.file_id
 
+    def set_root_id(self, file_id):
+        inv = self.read_working_inventory()
+        orig_root_id = inv.root.file_id
+        del inv._byid[inv.root.file_id]
+        inv.root.file_id = file_id
+        inv._byid[inv.root.file_id] = inv.root
+        for fid in inv:
+            entry = inv[fid]
+            if entry.parent_id in (None, orig_root_id):
+                entry.parent_id = inv.root.file_id
+        self._write_inventory(inv)
 
     def read_working_inventory(self):
         """Read the working inventory."""
@@ -521,7 +536,7 @@
     # FIXME: this doesn't need to be a branch method
     def set_inventory(self, new_inventory_list):
         from bzrlib.inventory import Inventory, InventoryEntry
-        inv = Inventory()
+        inv = Inventory(self.get_root_id())
         for path, file_id, parent, kind in new_inventory_list:
             name = os.path.basename(path)
             if name == "":
@@ -619,7 +634,7 @@
         # must be the same as its revision, so this is trivial.
         if revision_id == None:
             from bzrlib.inventory import Inventory
-            return Inventory()
+            return Inventory(self.get_root_id())
         else:
             return self.get_inventory(revision_id)
 
@@ -1011,7 +1026,7 @@
         # TODO: refactor this to use an existing revision object
         # so we don't need to read it in twice.
         if revision_id == None:
-            return EmptyTree()
+            return EmptyTree(self.get_root_id())
         else:
             inv = self.get_revision_inventory(revision_id)
             return RevisionTree(self.text_store, inv)
@@ -1031,7 +1046,7 @@
         from bzrlib.tree import EmptyTree, RevisionTree
         r = self.last_patch()
         if r == None:
-            return EmptyTree()
+            return EmptyTree(self.get_root_id())
         else:
             return RevisionTree(self.text_store, self.get_revision_inventory(r))
 
@@ -1354,3 +1369,9 @@
 
     s = hexlify(rand_bytes(8))
     return '-'.join((name, compact_date(time()), s))
+
+
+def gen_root_id():
+    """Return a new tree-root file id."""
+    return gen_file_id('TREE_ROOT')
+

=== modified file 'bzrlib/check.py'
--- a/bzrlib/check.py	2005-07-04 12:26:02 +0000
+++ b/bzrlib/check.py	2005-07-11 07:33:45 +0000
@@ -16,7 +16,62 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
-
+def _update_store_entry(obj, obj_id, branch, store_name, store):
+    """This is just a meta-function, which handles both revision entries
+    and inventory entries.
+    """
+    from bzrlib.trace import mutter
+    import tempfile, os, errno
+    from osutils import rename
+    obj_tmp = tempfile.TemporaryFile()
+    obj.write_xml(obj_tmp)
+    obj_tmp.seek(0)
+
+    tmpfd, tmp_path = tempfile.mkstemp(prefix=obj_id, suffix='.gz',
+        dir=branch.controlfilename(store_name))
+    os.close(tmpfd)
+    try:
+        orig_obj_path = branch.controlfilename([store_name, obj_id+'.gz'])
+        # Remove the old entry out of the way
+        rename(orig_obj_path, tmp_path)
+        try:
+            # TODO: We may need to handle the case where the old
+            # entry was not compressed (and thus did not end with .gz)
+
+            store.add(obj_tmp, obj_id) # Add the new one
+            os.remove(tmp_path) # Remove the old name
+            mutter('    Updated %s entry {%s}' % (store_name, obj_id))
+        except:
+            # On any exception, restore the old entry
+            rename(tmp_path, orig_obj_path)
+            raise
+    finally:
+        if os.path.exists(tmp_path):
+            # Unfortunately, the next command might throw
+            # an exception, which will mask a previous exception.
+            os.remove(tmp_path)
+        obj_tmp.close()
+
+def _update_revision_entry(rev, branch):
+    """After updating the values in a revision, make sure to
+    write out the data, but try to do it in an atomic manner.
+
+    :param rev:    The Revision object to store
+    :param branch: The Branch object where this Revision is to be stored.
+    """
+    _update_store_entry(rev, rev.revision_id, branch,
+            'revision-store', branch.revision_store)
+
+def _update_inventory_entry(inv, inv_id, branch):
+    """When an inventory has been modified (such as by adding a unique tree root)
+    this atomically re-generates the file.
+
+    :param inv:     The Inventory
+    :param inv_id:  The inventory id for this inventory
+    :param branch:  The Branch where this entry will be stored.
+    """
+    _update_store_entry(inv, inv_id, branch,
+            'inventory-store', branch.inventory_store)
 
 def check(branch):
     """Run consistency checks on a branch.
@@ -29,6 +84,8 @@
     from bzrlib.errors import BzrCheckError
     from bzrlib.osutils import fingerprint_file
     from bzrlib.progress import ProgressBar
+    from bzrlib.inventory import ROOT_ID
+    from bzrlib.branch import gen_root_id
 
     branch.lock_read()
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-07-11 07:25:42 +0000
+++ b/bzrlib/commands.py	2005-07-11 07:33:45 +0000
@@ -617,14 +617,18 @@
                     raise
             br_to = Branch(to_location, init=True)
 
-            revno = br_to.lookup_revision(revision[0])
-            try:
-                br_to.update_revisions(br_from, stop_revision=revno)
-            except NoSuchRevision:
-                rmtree(to_location)
-                msg = "The branch %s has no revision %d." % (from_location,
-                                                             revno)
-                raise BzrCommandError(msg)
+            br_to.set_root_id(br_from.get_root_id())
+
+            if revision:
+                revno = br_to.lookup_revision(revision[0])
+                try:
+                    br_to.update_revisions(br_from, stop_revision=revno)
+                except NoSuchRevision:
+                    rmtree(to_location)
+                    msg = "The branch %s has no revision %d." % (from_location,
+                                                                 revno)
+                    raise BzrCommandError(msg)
+            
             merge((to_location, -1), (to_location, 0), this_dir=to_location,
                   check_clean=False, ignore_zero=True)
             from_location = pull_loc(br_from)

=== modified file 'bzrlib/commit.py'
--- a/bzrlib/commit.py	2005-07-11 03:40:02 +0000
+++ b/bzrlib/commit.py	2005-07-11 07:33:45 +0000
@@ -220,7 +220,7 @@
     from bzrlib.trace import mutter, note
 
     any_changes = False
-    inv = Inventory()
+    inv = Inventory(work_inv.root.file_id)
     missing_ids = []
     
     for path, entry in work_inv.iter_entries():

=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2005-07-06 14:07:06 +0000
+++ b/bzrlib/inventory.py	2005-07-11 07:33:45 +0000
@@ -277,8 +277,10 @@
 
     >>> [x[0] for x in inv.iter_entries()]
     ['hello.c']
+    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
+    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
     """
-    def __init__(self):
+    def __init__(self, root_id=ROOT_ID):
         """Create or read an inventory.
 
         If a working directory is specified, the inventory is read
@@ -288,7 +290,11 @@
         The inventory is created with a default root directory, with
         an id of None.
         """
-        self.root = RootEntry(ROOT_ID)
+        # We are letting Branch(init=True) create a unique inventory
+        # root id. Rather than generating a random one here.
+        #if root_id is None:
+        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
+        self.root = RootEntry(root_id)
         self._byid = {self.root.file_id: self.root}
 
 
@@ -400,6 +406,9 @@
         if entry.file_id in self._byid:
             raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
 
+        if entry.parent_id == ROOT_ID or entry.parent_id is None:
+            entry.parent_id = self.root.file_id
+
         try:
             parent = self._byid[entry.parent_id]
         except KeyError:
@@ -469,6 +478,8 @@
         
         e = Element('inventory')
         e.text = '\n'
+        if self.root.file_id not in (None, ROOT_ID):
+            e.set('file_id', self.root.file_id)
         for path, ie in self.iter_entries():
             e.append(ie.to_element())
         return e
@@ -486,9 +497,13 @@
         """
         # XXXX: doctest doesn't run this properly under python2.3
         assert elt.tag == 'inventory'
-        o = cls()
+        root_id = elt.get('file_id') or ROOT_ID
+        o = cls(root_id)
         for e in elt:
-            o.add(InventoryEntry.from_element(e))
+            ie = InventoryEntry.from_element(e)
+            if ie.parent_id == ROOT_ID:
+                ie.parent_id = root_id
+            o.add(ie)
         return o
         
     from_element = classmethod(from_element)

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2005-07-11 06:41:00 +0000
+++ b/bzrlib/log.py	2005-07-11 07:33:45 +0000
@@ -184,6 +184,8 @@
         if last_revno:
             yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
 
+        this_tree = EmptyTree(branch.get_root_id())
+
         last_revno = revno
         last_revision = this_revision
         last_tree = this_tree
@@ -212,6 +214,8 @@
     from diff import compare_trees
 
     last_revno = last_revision_id = last_tree = None
+    prev_tree = EmptyTree(branch.get_root_id())
+
     for revno, revision_id in which_revs:
         this_tree = branch.revision_tree(revision_id)
         this_revision = branch.get_revision(revision_id)

=== modified file 'bzrlib/newinventory.py'
--- a/bzrlib/newinventory.py	2005-06-28 03:02:31 +0000
+++ b/bzrlib/newinventory.py	2005-07-11 07:33:45 +0000
@@ -139,6 +139,6 @@
     root_el = inv_el[0]
     assert root_el.tag == 'root_directory'
 
-    inv = Inventory()
+    inv = Inventory(inv_el.get('file_id'))
     for el in root_el:
         descend(inv.root, el)

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2005-06-24 11:04:55 +0000
+++ b/bzrlib/osutils.py	2005-07-11 07:33:45 +0000
@@ -98,6 +98,17 @@
     finally:
         outf.close()
 
+def rename(path_from, path_to):
+    """Basically the same as os.rename() just special for win32"""
+    if sys.platform == 'win32':
+        try:
+            os.remove(path_to)
+        except OSError, e:
+            if e.errno != e.ENOENT:
+                raise
+    os.rename(path_from, path_to)
+
+
 
 
 

=== modified file 'bzrlib/store.py'
--- a/bzrlib/store.py	2005-06-27 01:36:22 +0000
+++ b/bzrlib/store.py	2005-07-11 07:33:45 +0000
@@ -90,6 +90,7 @@
             
         p = self._path(fileid)
         if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
+            from bzrlib.errors import bailout
             raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
 
         fn = p

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2005-07-07 02:07:03 +0000
+++ b/bzrlib/tree.py	2005-07-11 07:33:45 +0000
@@ -143,9 +143,9 @@
 
 
 class EmptyTree(Tree):
-    def __init__(self):
+    def __init__(self, root_id):
         from bzrlib.inventory import Inventory
-        self._inventory = Inventory()
+        self._inventory = Inventory(root_id)
 
     def has_filename(self, filename):
         return False



More information about the Pkg-bazaar-commits mailing list