Skip to content

Commit 1ca3d71

Browse files
committed
git-p4: Added support for automatically importing newly appearing perforce branches.
If a change in a p4 "branch" appears that hasn't seen any previous commit and that has a known branch mapping we now try to import it properly. First we find the p4 change of the source branch that the new p4 branch is based on. Then we using git rev-list --bisect to locate the corresponding git commit to that change. Finally we import all changes in the new p4 branch up to the current change and resume with the regular import. Signed-off-by: Simon Hausmann <simon@lst.de>
1 parent 8134f69 commit 1ca3d71

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

contrib/fast-import/git-p4

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,67 @@ class P4Sync(Command):
11271127

11281128
return self.refPrefix + self.projectName + branch
11291129

1130+
def gitCommitByP4Change(self, ref, change):
1131+
if self.verbose:
1132+
print "looking in ref " + ref + " for change %s using bisect..." % change
1133+
1134+
earliestCommit = ""
1135+
latestCommit = parseRevision(ref)
1136+
1137+
while True:
1138+
if self.verbose:
1139+
print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
1140+
next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
1141+
if len(next) == 0:
1142+
if self.verbose:
1143+
print "argh"
1144+
return ""
1145+
log = extractLogMessageFromGitCommit(next)
1146+
settings = extractSettingsGitLog(log)
1147+
currentChange = int(settings['change'])
1148+
if self.verbose:
1149+
print "current change %s" % currentChange
1150+
1151+
if currentChange == change:
1152+
if self.verbose:
1153+
print "found %s" % next
1154+
return next
1155+
1156+
if currentChange < change:
1157+
earliestCommit = "^%s" % next
1158+
else:
1159+
latestCommit = "%s" % next
1160+
1161+
return ""
1162+
1163+
def importNewBranch(self, branch, maxChange):
1164+
# make fast-import flush all changes to disk and update the refs using the checkpoint
1165+
# command so that we can try to find the branch parent in the git history
1166+
self.gitStream.write("checkpoint\n\n");
1167+
self.gitStream.flush();
1168+
branchPrefix = self.depotPaths[0] + branch + "/"
1169+
range = "@1,%s" % maxChange
1170+
#print "prefix" + branchPrefix
1171+
changes = p4ChangesForPaths([branchPrefix], range)
1172+
if len(changes) <= 0:
1173+
return False
1174+
firstChange = changes[0]
1175+
#print "first change in branch: %s" % firstChange
1176+
sourceBranch = self.knownBranches[branch]
1177+
sourceDepotPath = self.depotPaths[0] + sourceBranch
1178+
sourceRef = self.gitRefForBranch(sourceBranch)
1179+
#print "source " + sourceBranch
1180+
1181+
branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"])
1182+
#print "branch parent: %s" % branchParentChange
1183+
gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
1184+
if len(gitParent) > 0:
1185+
self.initialParents[self.gitRefForBranch(branch)] = gitParent
1186+
#print "parent git commit: %s" % gitParent
1187+
1188+
self.importChanges(changes)
1189+
return True
1190+
11301191
def importChanges(self, changes):
11311192
cnt = 1
11321193
for change in changes:
@@ -1159,8 +1220,19 @@ class P4Sync(Command):
11591220
parent = self.knownBranches[branch]
11601221
if parent == branch:
11611222
parent = ""
1162-
elif self.verbose:
1163-
print "parent determined through known branches: %s" % parent
1223+
else:
1224+
fullBranch = self.projectName + branch
1225+
if fullBranch not in self.p4BranchesInGit:
1226+
if not self.silent:
1227+
print("\n Importing new branch %s" % fullBranch);
1228+
if self.importNewBranch(branch, change - 1):
1229+
parent = ""
1230+
self.p4BranchesInGit.append(fullBranch)
1231+
if not self.silent:
1232+
print("\n Resuming with change %s" % change);
1233+
1234+
if self.verbose:
1235+
print "parent determined through known branches: %s" % parent
11641236

11651237
branch = self.gitRefForBranch(branch)
11661238
parent = self.gitRefForBranch(parent)

0 commit comments

Comments
 (0)