@matheusbsbs / matheus-bagliano / commits / 5ca4461728
Fork 0

git-p4: Make 'git-p4 branches' work after an initial clone with git clone from an origin-updated repository.

After a clone with "git clone" of a repository the p4 branches are only in remotes/origin/p4/* and not in remotes/p4/*. Separate the code for detection and creation out of the P4Sync command class into standalone methods and use them from the P4Branches command. Signed-off-by: Simon Hausmann <simon@lst.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Simon Hausmann committed Aug 24, 2007 at 17:44 UTC 5ca4461728adadf22b5b8833693702ab09e556db
1 file changed +55 -49
contrib/fast-import/git-p4
+55 -49
@@ -231,6 +231,56 @@ def findUpstreamBranchPoint(head = "HEAD"):
231
232 return ["", settings]
233
234 +def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
235 + if not silent:
236 + print ("Creating/updating branch(es) in %s based on origin branch(es)"
237 + % localRefPrefix)
238 +
239 + originPrefix = "origin/p4/"
240 +
241 + for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
242 + line = line.strip()
243 + if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
244 + continue
245 +
246 + headName = line[len(originPrefix):]
247 + remoteHead = localRefPrefix + headName
248 + originHead = line
249 +
250 + original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
251 + if (not original.has_key('depot-paths')
252 + or not original.has_key('change')):
253 + continue
254 +
255 + update = False
256 + if not gitBranchExists(remoteHead):
257 + if verbose:
258 + print "creating %s" % remoteHead
259 + update = True
260 + else:
261 + settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
262 + if settings.has_key('change') > 0:
263 + if settings['depot-paths'] == original['depot-paths']:
264 + originP4Change = int(original['change'])
265 + p4Change = int(settings['change'])
266 + if originP4Change > p4Change:
267 + print ("%s (%s) is newer than %s (%s). "
268 + "Updating p4 branch from origin."
269 + % (originHead, originP4Change,
270 + remoteHead, p4Change))
271 + update = True
272 + else:
273 + print ("Ignoring: %s was imported from %s while "
274 + "%s was imported from %s"
275 + % (originHead, ','.join(original['depot-paths']),
276 + remoteHead, ','.join(settings['depot-paths'])))
277 +
278 + if update:
279 + system("git update-ref %s %s" % (remoteHead, originHead))
280 +
281 +def originP4BranchesExist():
282 + return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
283 +
284 class Command:
285 def __init__(self):
286 self.usage = "usage: %prog [options]"
@@ -1041,53 +1091,6 @@ class P4Sync(Command):
1091 for branch in branches.keys():
1092 self.initialParents[self.refPrefix + branch] = branches[branch]
1093
1044 - def createOrUpdateBranchesFromOrigin(self):
1045 - if not self.silent:
1046 - print ("Creating/updating branch(es) in %s based on origin branch(es)"
1047 - % self.refPrefix)
1048 -
1049 - originPrefix = "origin/p4/"
1050 -
1051 - for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
1052 - line = line.strip()
1053 - if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
1054 - continue
1055 -
1056 - headName = line[len(originPrefix):]
1057 - remoteHead = self.refPrefix + headName
1058 - originHead = line
1059 -
1060 - original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
1061 - if (not original.has_key('depot-paths')
1062 - or not original.has_key('change')):
1063 - continue
1064 -
1065 - update = False
1066 - if not gitBranchExists(remoteHead):
1067 - if self.verbose:
1068 - print "creating %s" % remoteHead
1069 - update = True
1070 - else:
1071 - settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
1072 - if settings.has_key('change') > 0:
1073 - if settings['depot-paths'] == original['depot-paths']:
1074 - originP4Change = int(original['change'])
1075 - p4Change = int(settings['change'])
1076 - if originP4Change > p4Change:
1077 - print ("%s (%s) is newer than %s (%s). "
1078 - "Updating p4 branch from origin."
1079 - % (originHead, originP4Change,
1080 - remoteHead, p4Change))
1081 - update = True
1082 - else:
1083 - print ("Ignoring: %s was imported from %s while "
1084 - "%s was imported from %s"
1085 - % (originHead, ','.join(original['depot-paths']),
1086 - remoteHead, ','.join(settings['depot-paths'])))
1087 -
1088 - if update:
1089 - system("git update-ref %s %s" % (remoteHead, originHead))
1090 -
1094 def updateOptionDict(self, d):
1095 option_keys = {}
1096 if self.keepRepoPath:
@@ -1108,7 +1111,7 @@ class P4Sync(Command):
1111 # map from branch depot path to parent branch
1112 self.knownBranches = {}
1113 self.initialParents = {}
1111 - self.hasOrigin = gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
1114 + self.hasOrigin = originP4BranchesExist()
1115 if not self.syncWithOrigin:
1116 self.hasOrigin = False
1117
@@ -1135,7 +1138,7 @@ class P4Sync(Command):
1138 # merge with previous imports, if possible.
1139 if args == []:
1140 if self.hasOrigin:
1138 - self.createOrUpdateBranchesFromOrigin()
1141 + createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
1142 self.listExistingP4GitBranches()
1143
1144 if len(self.p4BranchesInGit) > 1:
@@ -1518,6 +1521,9 @@ class P4Branches(Command):
1521 self.verbose = False
1522
1523 def run(self, args):
1524 + if originP4BranchesExist():
1525 + createOrUpdateBranchesFromOrigin()
1526 +
1527 cmdline = "git rev-parse --symbolic "
1528 cmdline += " --remotes"
1529