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>
Simon Hausmann committed
Aug 26, 2007 at 17:36 UTC
1ca3d71069620f1438d9f89165a3e69e8d47d302
1 file changed
+74
-2
contrib/fast-import/git-p4
+74
-2
@@ -1127,6 +1127,67 @@ class P4Sync(Command):
1127
1128
return self.refPrefix + self.projectName + branch
1129
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
+
1191
def importChanges(self, changes):
1192
cnt = 1
1193
for change in changes:
@@ -1159,8 +1220,19 @@ class P4Sync(Command):
1220
parent = self.knownBranches[branch]
1221
if parent == branch:
1222
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
1236
1237
branch = self.gitRefForBranch(branch)
1238
parent = self.gitRefForBranch(parent)