changeset 795:1350e43e662f

git2hg.find_incoming: move graph traversal into a function This is preparation for upcoming changes to find_incoming that will allow it to import certain Git branches as Mercurial named branches.
author Siddharth Agarwal <sid0@fb.com>
date Wed, 15 Oct 2014 14:21:09 -0700
parents 388944fca782
children c19835c3c60d
files hggit/git2hg.py
diffstat 1 files changed, 32 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/git2hg.py
+++ b/hggit/git2hg.py
@@ -36,33 +36,39 @@
         todo.sort(key=commitdate, reverse=True)
         return todo
 
-    todo = get_heads(refs)
+    def get_unseen_commits(todo):
+        '''get all unseen commits reachable from todo in topological order
 
-    # traverse the heads getting a list of all the unique commits in
-    # topological order
-    commits = []
-    while todo:
-        sha = todo[-1]
-        if sha in done or sha in git_map:
-            todo.pop()
-            continue
-        assert isinstance(sha, str)
-        if sha in commit_cache:
-            obj = commit_cache[sha]
-        else:
-            obj = git_object_store[sha]
-            commit_cache[sha] = obj
-        assert isinstance(obj, Commit)
-        for p in obj.parents:
-            if p not in done and p not in git_map:
-                todo.append(p)
-                # process parents of a commit before processing the
-                # commit itself, and come back to this commit later
-                break
-        else:
-            commits.append(sha)
-            done.add(sha)
-            todo.pop()
+        'unseen' means not reachable from the done set and not in the git map.
+        Mutates todo and the done set in the process.'''
+        commits = []
+        while todo:
+            sha = todo[-1]
+            if sha in done or sha in git_map:
+                todo.pop()
+                continue
+            assert isinstance(sha, str)
+            if sha in commit_cache:
+                obj = commit_cache[sha]
+            else:
+                obj = git_object_store[sha]
+                commit_cache[sha] = obj
+            assert isinstance(obj, Commit)
+            for p in obj.parents:
+                if p not in done and p not in git_map:
+                    todo.append(p)
+                    # process parents of a commit before processing the
+                    # commit itself, and come back to this commit later
+                    break
+            else:
+                commits.append(sha)
+                done.add(sha)
+                todo.pop()
+
+        return commits
+
+    todo = get_heads(refs)
+    commits = get_unseen_commits(todo)
 
     return GitIncomingResult(commits, commit_cache)