Rev 2: Basis. in file:///v/home/vila/.bazaar/plugins/lpto/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Sep 12 18:40:29 BST 2008


At file:///v/home/vila/.bazaar/plugins/lpto/

------------------------------------------------------------
revno: 2
revision-id: v.ladeuil+lp at free.fr-20080912174029-2cfhczgx8a6gr2uf
parent: v.ladeuil+lp at free.fr-20080912160152-37fev9u2dgcn7d9c
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: lpto
timestamp: Fri 2008-09-12 19:40:29 +0200
message:
  Basis.
  
  * tests/test_lpto.py: 
  Basic tests.
  
  * __init__.py:
  (cmd_lpto.run): First shot at calculating longest path to origin.
modified:
  __init__.py                    __init__.py-20080912160131-v30mt2ve4pzprxqp-1
  tests/test_lpto.py             test_lpto.py-20080912160131-v30mt2ve4pzprxqp-4
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2008-09-12 16:01:52 +0000
+++ b/__init__.py	2008-09-12 17:40:29 +0000
@@ -19,11 +19,68 @@
 from bzrlib import commands
 
 class cmd_lpto(commands.Command):
-    def run(self):
-        pass
+    """Calculate longest path to origin starting at a given revision.
+
+    By default the current branch is used and starts at the tip.
+
+    """
+
+    takes_args = ['location?']
+
+    def run(self, location=u'.'):
+        from bzrlib import branch
+        br = branch.Branch.open_containing(location)[0]
+        br.lock_read()
+        try:
+            return self._run(br)
+        finally:
+            br.unlock()
+
+    def _run(self, br):
+        for lpto, revno, revid in calculate_lptos(br):
+            print '%r:%r:%r' % (lpto, revno, revid)
+
+
+def calculate_lptos(br):
+    from bzrlib import (
+        graph,
+        log,
+        repository,
+        tsort,
+        )
+    last_rev =  br.last_revision()
+    gr = br.repository.get_graph(br.repository)
+    rev_graph = repository._old_get_graph(br.repository, last_rev)
+    merge_sorted_revisions = tsort.merge_sort(rev_graph, last_rev, None,
+                                         generate_revno=True)
+
+    merge_sorted_revisions = log.reverse_by_depth(merge_sorted_revisions)
+    revs = []
+    lptos = dict()
+    previous = None
+    for seq, revid, depth, revno, end_of_merge in merge_sorted_revisions:
+        if previous is None:
+            lpto = 1
+        else:
+            parents = gr.get_parent_map([revid]).get(revid)
+            if not parents:
+                # Ghost, we are doomed
+                import pdb; pdb.set_trace()
+            else:
+                lpto = 0
+                for p in parents:
+                    parent_lpto = lptos[p]
+                    if  parent_lpto > lpto:
+                        lpto = parent_lpto + 1
+        lptos[revid] = lpto
+        revs.append((lpto, revno, revid))
+        previous = revid
+
+    return revs
 
 commands.register_command(cmd_lpto)
 
+
 def load_tests(basic_tests, module, loader):
     # This module shouldn't define any tests but I don't know how to report
     # that. I prefer to update basic_tests with the other tests to detect

=== modified file 'tests/test_lpto.py'
--- a/tests/test_lpto.py	2008-09-12 16:01:52 +0000
+++ b/tests/test_lpto.py	2008-09-12 17:40:29 +0000
@@ -19,3 +19,29 @@
     )
 
 from bzrlib.plugins import lpto
+
+class TestCalculateLptos(tests.TestCaseWithMemoryTransport):
+
+    def _get_lptos(self, br):
+        br.lock_read()
+        try:
+            return lpto.calculate_lptos(br)
+        finally:
+            br.unlock()
+
+    def test_empty_branch(self):
+        builder = self.make_branch_builder('path')
+        self.assertEqual([], self._get_lptos(builder.get_branch()))
+
+    def test_simple_mainline(self):
+        builder = self.make_branch_builder('path')
+        builder.build_snapshot('A-id', None,
+                               [('add', ('', 'a-root-id', 'directory', None))])
+        builder.build_snapshot('B-id', ['A-id'], [])
+        builder.build_snapshot('C-id', ['B-id'], [])
+        # FIXME: Since we use not tree, the following fails, that's a bug.
+        # builder.finish_series()
+        self.assertEqual([(1, (1,), 'A-id'),
+                          (2, (2,), 'B-id'),
+                          (3, (3,), 'C-id')],
+                         self._get_lptos(builder.get_branch()))



More information about the bazaar-commits mailing list