Rev 1: Trivial command to check if one branch contains the ancestry of the other. in http://bzr.arbash-meinel.com/plugins/share_ancestry

John Arbash Meinel john at arbash-meinel.com
Wed Feb 10 14:38:21 GMT 2010


At http://bzr.arbash-meinel.com/plugins/share_ancestry

------------------------------------------------------------
revno: 1
revision-id: john at arbash-meinel.com-20100210143800-wzk2naoe7xrzfr8a
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: share_ancestry
timestamp: Wed 2010-02-10 08:38:00 -0600
message:
  Trivial command to check if one branch contains the ancestry of the other.
-------------- next part --------------
=== added file '__init__.py'
--- a/__init__.py	1970-01-01 00:00:00 +0000
+++ b/__init__.py	2010-02-10 14:38:00 +0000
@@ -0,0 +1,69 @@
+# Copyright (C) 2010 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""Determine if one branch shares ancestry with another."""
+
+from bzrlib import (
+    commands,
+    errors,
+    option,
+    )
+
+
+class cmd_share_ancestry(commands.Command):
+    """Do BRANCH1 and BRANCH2 share ancestry?"""
+
+    takes_args = ['branch1', 'branch2']
+
+    def run(self, branch1, branch2):
+        from bzrlib import branch
+        b1 = branch.Branch.open(branch1)
+        b2 = branch.Branch.open(branch2)
+        b1.lock_read()
+        b2.lock_read()
+        try:
+            self.outf.write('is %s ancestry in %s: ' % (branch1, branch2))
+            if self._is_ancestry_in_other(b1, b2):
+                self.outf.write('yes\n')
+            else:
+                self.outf.write('no\n')
+            #self.outf.write('is %s ancestry in %s: ' % (branch2, branch1))
+            #if self._is_ancestry_in_other(b2, b1):
+            #    self.outf.write('yes\n')
+            #else:
+            #    self.outf.write('no\n')
+        finally:
+            b2.unlock()
+            b1.unlock()
+
+    def _is_ancestry_in_other(self, this, other):
+        """Does the ancestry of 'other' contain the ancestry of 'this'?"""
+        # Do a quick check using the tip revisions
+        dotted_revno = None
+        rev_id = this.last_revision()
+        try:
+            dotted_revno = other.revision_id_to_dotted_revno(rev_id)
+        except errors.NoSuchRevision:
+            # The tip revision isn't present, check an older rev
+            rev_id = this.revision_history()[0]
+            try:
+                dotted_revno = other.revision_id_to_dotted_revno(rev_id)
+            except errors.NoSuchRevision:
+                pass
+        return (dotted_revno is not None)
+
+
+commands.register_command(cmd_share_ancestry)



More information about the bazaar-commits mailing list