@matheusbsbs / matheus-bagliano / commits / 0563a4538a
Fork 0

Make it possible to specify the p4 changes to import through a text file (for debugging) and made various improvements to the branch/merge heuristic detection.

Signed-off-by: Simon Hausmann <hausmann@kde.org>

Simon Hausmann committed Feb 14, 2007 at 17:13 UTC 0563a4538a33c7ae9da41628d6f8849c38a2f935
1 file changed +90 -38
contrib/fast-import/p4-fast-export.py
+90 -38
@@ -13,14 +13,16 @@ import os, string, sys, time
13 import marshal, popen2, getopt
14
15 knownBranches = set()
16 +committedChanges = set()
17 branch = "refs/heads/master"
18 globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
19 detectBranches = False
20 +changesFile = ""
21 if len(globalPrefix) != 0:
22 globalPrefix = globalPrefix[:-1]
23
24 try:
23 - opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches" ])
25 + opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
26 except getopt.GetoptError:
27 print "fixme, syntax error"
28 sys.exit(1)
@@ -30,6 +32,8 @@ for o, a in opts:
32 branch = "refs/heads/" + a
33 elif o == "--detect-branches":
34 detectBranches = True
35 + elif o == "--changesfile":
36 + changesFile = a
37
38 if len(args) == 0 and len(globalPrefix) != 0:
39 print "[using previously specified depot path %s]" % globalPrefix
@@ -53,7 +57,7 @@ changeRange = ""
57 revision = ""
58 users = {}
59 initialParent = ""
56 -lastChange = ""
60 +lastChange = 0
61 initialTag = ""
62
63 if globalPrefix.find("@") != -1:
@@ -104,6 +108,7 @@ def extractFilesFromCommit(commit):
108 path = commit["depotFile%s" % fnum]
109 if not path.startswith(globalPrefix):
110 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
111 + fnum = fnum + 1
112 continue
113
114 file = {}
@@ -115,7 +120,15 @@ def extractFilesFromCommit(commit):
120 fnum = fnum + 1
121 return files
122
123 +def isSubPathOf(first, second):
124 + if not first.startswith(second):
125 + return False
126 + if first == second:
127 + return True
128 + return first[len(second)] == "/"
129 +
130 def branchesForCommit(files):
131 + global knownBranches
132 branches = set()
133
134 for file in files:
@@ -123,9 +136,10 @@ def branchesForCommit(files):
136 # strip off the filename
137 relativePath = relativePath[0:relativePath.rfind("/")]
138
126 - if len(branches) == 0:
127 - branches.add(relativePath)
128 - continue
139 +# if len(branches) == 0:
140 +# branches.add(relativePath)
141 +# knownBranches.add(relativePath)
142 +# continue
143
144 ###### this needs more testing :)
145 knownBranch = False
@@ -133,15 +147,32 @@ def branchesForCommit(files):
147 if relativePath == branch:
148 knownBranch = True
149 break
136 - if relativePath.startswith(branch):
150 +# if relativePath.startswith(branch):
151 + if isSubPathOf(relativePath, branch):
152 knownBranch = True
153 break
139 - if branch.startswith(relativePath):
154 +# if branch.startswith(relativePath):
155 + if isSubPathOf(branch, relativePath):
156 branches.remove(branch)
157 break
158
143 - if not knownBranch:
144 - branches.add(relativePath)
159 + if knownBranch:
160 + continue
161 +
162 + for branch in knownBranches:
163 + #if relativePath.startswith(branch):
164 + if isSubPathOf(relativePath, branch):
165 + if len(branches) == 0:
166 + relativePath = branch
167 + else:
168 + knownBranch = True
169 + break
170 +
171 + if knownBranch:
172 + continue
173 +
174 + branches.add(relativePath)
175 + knownBranches.add(relativePath)
176
177 return branches
178
@@ -149,12 +180,14 @@ def commit(details, files, branch, branchPrefix):
180 global initialParent
181 global users
182 global lastChange
183 + global committedChanges
184
185 epoch = details["time"]
186 author = details["user"]
187
188 gitStream.write("commit %s\n" % branch)
189 gitStream.write("mark :%s\n" % details["change"])
190 + committedChanges.add(int(details["change"]))
191 committer = ""
192 if author in users:
193 committer = "%s %s %s" % (users[author], epoch, tz)
@@ -173,9 +206,11 @@ def commit(details, files, branch, branchPrefix):
206 initialParent = ""
207
208 #mergedBranches = set()
176 - merge = 0
209 + merges = set()
210
211 for file in files:
212 + if lastChange == 0:
213 + continue
214 path = file["path"]
215 if not path.startswith(branchPrefix):
216 continue
@@ -195,9 +230,14 @@ def commit(details, files, branch, branchPrefix):
230 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
231 sys.exit(1);
232
198 - if not log["how0,0"].endswith(" from"):
199 - print "eek! file %s was not branched but instead: %s" % (depotPath, log["how0,0"])
200 - sys.exit(1);
233 + branchAction = log["how0,0"]
234 +# if branchAction == "branch into" or branchAction == "ignored":
235 +# continue # ignore for branching
236 +
237 + if not branchAction.endswith(" from"):
238 + continue # ignore for branching
239 +# print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
240 +# sys.exit(1);
241
242 source = log["file0,0"]
243 if source.startswith(branchPrefix):
@@ -212,8 +252,7 @@ def commit(details, files, branch, branchPrefix):
252 sourceLog = sourceLog[0]
253
254 change = int(sourceLog["change0"])
215 - if change > merge:
216 - merge = change
255 + merges.add(change)
256
257 # relPath = source[len(globalPrefix):]
258 #
@@ -223,13 +262,14 @@ def commit(details, files, branch, branchPrefix):
262 # mergedBranches.add(branch)
263 # break
264
226 - if merge != 0:
227 - gitStream.write("merge :%s\n" % merge)
265 + for merge in merges:
266 + if merge in committedChanges:
267 + gitStream.write("merge :%s\n" % merge)
268
269 for file in files:
270 path = file["path"]
271 if not path.startswith(branchPrefix):
232 - print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, change)
272 + print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
273 continue
274 rev = file["rev"]
275 depotPath = path + "#" + rev
@@ -252,7 +292,7 @@ def commit(details, files, branch, branchPrefix):
292
293 gitStream.write("\n")
294
255 - lastChange = details["change"]
295 + lastChange = int(details["change"])
296
297 def getUserMap():
298 users = {}
@@ -322,14 +362,26 @@ if len(revision) > 0:
362 print gitError.read()
363
364 else:
325 - output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
326 -
365 changes = []
328 - for line in output:
329 - changeNum = line.split(" ")[1]
330 - changes.append(changeNum)
366
332 - changes.reverse()
367 + if len(changesFile) > 0:
368 + output = open(changesFile).readlines()
369 + changeSet = set()
370 + for line in output:
371 + changeSet.add(int(line))
372 +
373 + for change in changeSet:
374 + changes.append(change)
375 +
376 + changes.sort()
377 + else:
378 + output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
379 +
380 + for line in output:
381 + changeNum = line.split(" ")[1]
382 + changes.append(changeNum)
383 +
384 + changes.reverse()
385
386 if len(changes) == 0:
387 print "no changes to import!"
@@ -343,19 +395,19 @@ else:
395 sys.stdout.flush()
396 cnt = cnt + 1
397
346 - try:
347 - files = extractFilesFromCommit(description)
348 - if detectBranches:
349 - for branch in branchesForCommit(files):
350 - knownBranches.add(branch)
351 - branchPrefix = globalPrefix + branch + "/"
352 - branch = "refs/heads/" + branch
353 - commit(description, files, branch, branchPrefix)
354 - else:
355 - commit(description, files, branch, globalPrefix)
356 - except:
357 - print gitError.read()
358 - sys.exit(1)
398 +# try:
399 + files = extractFilesFromCommit(description)
400 + if detectBranches:
401 + for branch in branchesForCommit(files):
402 + knownBranches.add(branch)
403 + branchPrefix = globalPrefix + branch + "/"
404 + branch = "refs/heads/" + branch
405 + commit(description, files, branch, branchPrefix)
406 + else:
407 + commit(description, files, branch, globalPrefix)
408 +# except:
409 +# print gitError.read()
410 +# sys.exit(1)
411
412 print ""
413