@matheusbsbs / matheus-bagliano / commits / dcacf8b447
Fork 0

More fixes in heuristic p4 branch detection based on common path components.

Signed-off-by: Simon Hausmann <simon@lst.de>

Simon Hausmann committed Feb 12, 2007 at 21:41 UTC dcacf8b447be74efaa5335f42c502ee1c397c436
1 file changed +74 -34
contrib/fast-import/p4-fast-export.py
+74 -34
@@ -12,11 +12,12 @@
12 import os, string, sys, time
13 import marshal, popen2, getopt
14
15 +knownBranches = set()
16 branch = "refs/heads/master"
16 -prefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
17 +globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
18 detectBranches = False
18 -if len(prefix) != 0:
19 - prefix = prefix[:-1]
19 +if len(globalPrefix) != 0:
20 + globalPrefix = globalPrefix[:-1]
21
22 try:
23 opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches" ])
@@ -30,8 +31,8 @@ for o, a in opts:
31 elif o == "--detect-branches":
32 detectBranches = True
33
33 -if len(args) == 0 and len(prefix) != 0:
34 - print "[using previously specified depot path %s]" % prefix
34 +if len(args) == 0 and len(globalPrefix) != 0:
35 + print "[using previously specified depot path %s]" % globalPrefix
36 elif len(args) != 1:
37 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
38 print "\n example:"
@@ -43,10 +44,10 @@ elif len(args) != 1:
44 print ""
45 sys.exit(1)
46 else:
46 - if len(prefix) != 0 and prefix != args[0]:
47 - print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix, args[0])
47 + if len(globalPrefix) != 0 and globalPrefix != args[0]:
48 + print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix, args[0])
49 sys.exit(1)
49 - prefix = args[0]
50 + globalPrefix = args[0]
51
52 changeRange = ""
53 revision = ""
@@ -55,27 +56,27 @@ initialParent = ""
56 lastChange = ""
57 initialTag = ""
58
58 -if prefix.find("@") != -1:
59 - atIdx = prefix.index("@")
60 - changeRange = prefix[atIdx:]
59 +if globalPrefix.find("@") != -1:
60 + atIdx = globalPrefix.index("@")
61 + changeRange = globalPrefix[atIdx:]
62 if changeRange == "@all":
63 changeRange = ""
64 elif changeRange.find(",") == -1:
65 revision = changeRange
66 changeRange = ""
66 - prefix = prefix[0:atIdx]
67 -elif prefix.find("#") != -1:
68 - hashIdx = prefix.index("#")
69 - revision = prefix[hashIdx:]
70 - prefix = prefix[0:hashIdx]
67 + globalPrefix = globalPrefix[0:atIdx]
68 +elif globalPrefix.find("#") != -1:
69 + hashIdx = globalPrefix.index("#")
70 + revision = globalPrefix[hashIdx:]
71 + globalPrefix = globalPrefix[0:hashIdx]
72 elif len(previousDepotPath) == 0:
73 revision = "#head"
74
74 -if prefix.endswith("..."):
75 - prefix = prefix[:-3]
75 +if globalPrefix.endswith("..."):
76 + globalPrefix = globalPrefix[:-3]
77
77 -if not prefix.endswith("/"):
78 - prefix += "/"
78 +if not globalPrefix.endswith("/"):
79 + globalPrefix += "/"
80
81 def p4CmdList(cmd):
82 pipe = os.popen("p4 -G %s" % cmd, "rb")
@@ -101,8 +102,8 @@ def extractFilesFromCommit(commit):
102 fnum = 0
103 while commit.has_key("depotFile%s" % fnum):
104 path = commit["depotFile%s" % fnum]
104 - if not path.startswith(prefix):
105 - print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
105 + if not path.startswith(globalPrefix):
106 + print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
107 continue
108
109 file = {}
@@ -118,7 +119,7 @@ def branchesForCommit(files):
119 branches = set()
120
121 for file in files:
121 - relativePath = file["path"][len(prefix):]
122 + relativePath = file["path"][len(globalPrefix):]
123 # strip off the filename
124 relativePath = relativePath[0:relativePath.rfind("/")]
125
@@ -144,7 +145,7 @@ def branchesForCommit(files):
145
146 return branches
147
147 -def commit(details, files, branch, prefix):
148 +def commit(details, files, branch, branchPrefix):
149 global initialParent
150 global users
151 global lastChange
@@ -163,7 +164,7 @@ def commit(details, files, branch, prefix):
164
165 gitStream.write("data <<EOT\n")
166 gitStream.write(details["desc"])
166 - gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
167 + gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
168 gitStream.write("EOT\n\n")
169
170 if len(initialParent) > 0:
@@ -172,9 +173,47 @@ def commit(details, files, branch, prefix):
173
174 for file in files:
175 path = file["path"]
176 + if not path.startswith(branchPrefix):
177 + continue
178 + action = file["action"]
179 + if action != "integrate" and action != "branch":
180 + continue
181 + rev = file["rev"]
182 + depotPath = path + "#" + rev
183 +
184 + log = p4CmdList("filelog \"%s\"" % depotPath)
185 + if len(log) != 1:
186 + print "eek! I got confused by the filelog of %s" % depotPath
187 + sys.exit(1);
188 +
189 + log = log[0]
190 + if log["action0"] != action:
191 + print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
192 + sys.exit(1);
193 +
194 + if not log["how0,0"].endswith(" from"):
195 + print "eek! file %s was not branched but instead: %s" % (depotPath, log["how0,0"])
196 + sys.exit(1);
197 +
198 + source = log["file0,0"]
199 + if source.startswith(branchPrefix):
200 + continue
201 +
202 + relPath = source[len(globalPrefix):]
203 +
204 + for branch in knownBranches:
205 + if relPath.startswith(branch):
206 + gitStream.write("merge refs/heads/%s\n" % branch)
207 + break
208 +
209 + for file in files:
210 + path = file["path"]
211 + if not path.startswith(branchPrefix):
212 + print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, change)
213 + continue
214 rev = file["rev"]
215 depotPath = path + "#" + rev
177 - relPath = path[len(prefix):]
216 + relPath = path[len(branchPrefix):]
217 action = file["action"]
218
219 if action == "delete":
@@ -234,15 +273,15 @@ if tzsign != '+' and tzsign != '-':
273 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
274
275 if len(revision) > 0:
237 - print "Doing initial import of %s from revision %s" % (prefix, revision)
276 + print "Doing initial import of %s from revision %s" % (globalPrefix, revision)
277
278 details = { "user" : "git perforce import user", "time" : int(time.time()) }
240 - details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
279 + details["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix, revision)
280 details["change"] = revision
281 newestRevision = 0
282
283 fileCnt = 0
245 - for info in p4CmdList("files %s...%s" % (prefix, revision)):
284 + for info in p4CmdList("files %s...%s" % (globalPrefix, revision)):
285 change = int(info["change"])
286 if change > newestRevision:
287 newestRevision = change
@@ -258,12 +297,12 @@ if len(revision) > 0:
297 details["change"] = newestRevision
298
299 try:
261 - commit(details, extractFilesFromCommit(details), branch, prefix)
300 + commit(details, extractFilesFromCommit(details), branch, globalPrefix)
301 except:
302 print gitError.read()
303
304 else:
266 - output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
305 + output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
306
307 changes = []
308 for line in output:
@@ -288,11 +327,12 @@ else:
327 files = extractFilesFromCommit(description)
328 if detectBranches:
329 for branch in branchesForCommit(files):
291 - branchPrefix = prefix + branch + "/"
330 + knownBranches.add(branch)
331 + branchPrefix = globalPrefix + branch + "/"
332 branch = "refs/heads/" + branch
333 commit(description, files, branch, branchPrefix)
334 else:
295 - commit(description, files, branch, prefix)
335 + commit(description, files, branch, globalPrefix)
336 except:
337 print gitError.read()
338 sys.exit(1)
@@ -307,7 +347,7 @@ gitStream.close()
347 gitOutput.close()
348 gitError.close()
349
310 -os.popen("git-repo-config p4.depotpath %s" % prefix).read()
350 +os.popen("git-repo-config p4.depotpath %s" % globalPrefix).read()
351 if len(initialTag) > 0:
352 os.popen("git tag -d %s" % initialTag).read()
353