Skip to content

Commit 6b79818

Browse files
jkuebartgitster
authored andcommitted
git-p4: speed up search for branch parent
For every new branch that git-p4 imports, it needs to find the commit where it branched off its parent branch. While p4 doesn't record this information explicitly, the first changelist on a branch is usually an identical copy of the parent branch. The method searchParent() tries to find a commit in the history of the given "parent" branch whose tree exactly matches the initial changelist of the new branch, "target". The code iterates through the parent commits and compares each of them to this initial changelist using diff-tree. Since we already know the tree object name we are looking for, spawning diff-tree for each commit is wasteful. Use the "--format" option of "rev-list" to find out the tree object name of each commit in the history, and find the tree whose name is exactly the same as the tree of the target commit to optimize this. This results in a considerable speed-up, at least on Windows. On one Windows machine with a fairly large repository of about 16000 commits in the parent branch, the current code takes over 7 minutes, while the new code only takes just over 10 seconds for the same changelist: Before: $ time git p4 sync Importing from/into multiple branches Depot paths: //depot Importing revision 31274 (100.0%) Updated branches: b1 real 7m41.458s user 0m0.000s sys 0m0.077s After: $ time git p4 sync Importing from/into multiple branches Depot paths: //depot Importing revision 31274 (100.0%) Updated branches: b1 real 0m10.235s user 0m0.000s sys 0m0.062s Signed-off-by: Joachim Kuebart <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Luke Diamand <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3ab088 commit 6b79818

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

git-p4.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,19 +3600,18 @@ def importNewBranch(self, branch, maxChange):
36003600
return True
36013601

36023602
def searchParent(self, parent, branch, target):
3603-
parentFound = False
3604-
for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3603+
targetTree = read_pipe(["git", "rev-parse",
3604+
"{}^{{tree}}".format(target)]).strip()
3605+
for line in read_pipe_lines(["git", "rev-list", "--format=%H %T",
36053606
"--no-merges", parent]):
3606-
blob = blob.strip()
3607-
if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3608-
parentFound = True
3607+
if line.startswith("commit "):
3608+
continue
3609+
commit, tree = line.strip().split(" ")
3610+
if tree == targetTree:
36093611
if self.verbose:
3610-
print("Found parent of %s in commit %s" % (branch, blob))
3611-
break
3612-
if parentFound:
3613-
return blob
3614-
else:
3615-
return None
3612+
print("Found parent of %s in commit %s" % (branch, commit))
3613+
return commit
3614+
return None
36163615

36173616
def importChanges(self, changes, origin_revision=0):
36183617
cnt = 1

0 commit comments

Comments
 (0)