this is very much in progress, but at least the branch importing works a little bit
[fast-export/rorcz.git] / p4-fast-export.py
blobb5dc6f676761e268f7e445ddf53d3ec591a1fd15
1 #!/usr/bin/python
3 # p4-fast-export.py
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
8 # TODO:
9 # - support integrations (at least p4i)
10 # - support p4 submit (hah!)
12 import os, string, sys, time
13 import marshal, popen2, getopt
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]
24 try:
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 for o, a in opts:
31 if o == "--branch":
32 branch = "refs/heads/" + a
33 elif o == "--detect-branches":
34 detectBranches = True
35 elif o == "--changesfile":
36 changesFile = a
38 if len(args) == 0 and len(globalPrefix) != 0:
39 print "[using previously specified depot path %s]" % globalPrefix
40 elif len(args) != 1:
41 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
42 print "\n example:"
43 print " %s //depot/my/project/ -- to import the current head"
44 print " %s //depot/my/project/@all -- to import everything"
45 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
46 print ""
47 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
48 print ""
49 sys.exit(1)
50 else:
51 if len(globalPrefix) != 0 and globalPrefix != args[0]:
52 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix, args[0])
53 sys.exit(1)
54 globalPrefix = args[0]
56 changeRange = ""
57 revision = ""
58 users = {}
59 initialParent = ""
60 lastChange = 0
61 initialTag = ""
63 if globalPrefix.find("@") != -1:
64 atIdx = globalPrefix.index("@")
65 changeRange = globalPrefix[atIdx:]
66 if changeRange == "@all":
67 changeRange = ""
68 elif changeRange.find(",") == -1:
69 revision = changeRange
70 changeRange = ""
71 globalPrefix = globalPrefix[0:atIdx]
72 elif globalPrefix.find("#") != -1:
73 hashIdx = globalPrefix.index("#")
74 revision = globalPrefix[hashIdx:]
75 globalPrefix = globalPrefix[0:hashIdx]
76 elif len(previousDepotPath) == 0:
77 revision = "#head"
79 if globalPrefix.endswith("..."):
80 globalPrefix = globalPrefix[:-3]
82 if not globalPrefix.endswith("/"):
83 globalPrefix += "/"
85 def p4CmdList(cmd):
86 pipe = os.popen("p4 -G %s" % cmd, "rb")
87 result = []
88 try:
89 while True:
90 entry = marshal.load(pipe)
91 result.append(entry)
92 except EOFError:
93 pass
94 pipe.close()
95 return result
97 def p4Cmd(cmd):
98 list = p4CmdList(cmd)
99 result = {}
100 for entry in list:
101 result.update(entry)
102 return result;
104 def extractFilesFromCommit(commit):
105 files = []
106 fnum = 0
107 while commit.has_key("depotFile%s" % fnum):
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
114 file = {}
115 file["path"] = path
116 file["rev"] = commit["rev%s" % fnum]
117 file["action"] = commit["action%s" % fnum]
118 file["type"] = commit["type%s" % fnum]
119 files.append(file)
120 fnum = fnum + 1
121 return files
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)] == "/"
130 def branchesForCommit(files):
131 global knownBranches
132 branches = set()
134 for file in files:
135 relativePath = file["path"][len(globalPrefix):]
136 # strip off the filename
137 relativePath = relativePath[0:relativePath.rfind("/")]
139 # if len(branches) == 0:
140 # branches.add(relativePath)
141 # knownBranches.add(relativePath)
142 # continue
144 ###### this needs more testing :)
145 knownBranch = False
146 for branch in branches:
147 if relativePath == branch:
148 knownBranch = True
149 break
150 # if relativePath.startswith(branch):
151 if isSubPathOf(relativePath, branch):
152 knownBranch = True
153 break
154 # if branch.startswith(relativePath):
155 if isSubPathOf(branch, relativePath):
156 branches.remove(branch)
157 break
159 if knownBranch:
160 continue
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
171 if knownBranch:
172 continue
174 branches.add(relativePath)
175 knownBranches.add(relativePath)
177 return branches
179 def commit(details, files, branch, branchPrefix):
180 global initialParent
181 global users
182 global lastChange
183 global committedChanges
185 epoch = details["time"]
186 author = details["user"]
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)
194 else:
195 committer = "%s <a@b> %s %s" % (author, epoch, tz)
197 gitStream.write("committer %s\n" % committer)
199 gitStream.write("data <<EOT\n")
200 gitStream.write(details["desc"])
201 gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
202 gitStream.write("EOT\n\n")
204 if len(initialParent) > 0:
205 gitStream.write("from %s\n" % initialParent)
206 initialParent = ""
208 #mergedBranches = set()
209 merges = set()
211 for file in files:
212 if lastChange == 0:
213 continue
214 path = file["path"]
215 if not path.startswith(branchPrefix):
216 continue
217 action = file["action"]
218 if action != "integrate" and action != "branch":
219 continue
220 rev = file["rev"]
221 depotPath = path + "#" + rev
223 log = p4CmdList("filelog \"%s\"" % depotPath)
224 if len(log) != 1:
225 print "eek! I got confused by the filelog of %s" % depotPath
226 sys.exit(1);
228 log = log[0]
229 if log["action0"] != action:
230 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
231 sys.exit(1);
233 branchAction = log["how0,0"]
234 # if branchAction == "branch into" or branchAction == "ignored":
235 # continue # ignore for branching
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);
242 source = log["file0,0"]
243 if source.startswith(branchPrefix):
244 continue
246 lastSourceRev = log["erev0,0"]
248 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
249 if len(sourceLog) != 1:
250 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
251 sys.exit(1);
252 sourceLog = sourceLog[0]
254 change = int(sourceLog["change0"])
255 merges.add(change)
257 # relPath = source[len(globalPrefix):]
259 # for branch in knownBranches:
260 # if relPath.startswith(branch) and branch not in mergedBranches:
261 # gitStream.write("merge refs/heads/%s\n" % branch)
262 # mergedBranches.add(branch)
263 # break
265 for merge in merges:
266 if merge in committedChanges:
267 gitStream.write("merge :%s\n" % merge)
269 for file in files:
270 path = file["path"]
271 if not path.startswith(branchPrefix):
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
276 relPath = path[len(branchPrefix):]
277 action = file["action"]
279 if action == "delete":
280 gitStream.write("D %s\n" % relPath)
281 else:
282 mode = 644
283 if file["type"].startswith("x"):
284 mode = 755
286 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
288 gitStream.write("M %s inline %s\n" % (mode, relPath))
289 gitStream.write("data %s\n" % len(data))
290 gitStream.write(data)
291 gitStream.write("\n")
293 gitStream.write("\n")
295 lastChange = int(details["change"])
297 def getUserMap():
298 users = {}
300 for output in p4CmdList("users"):
301 if not output.has_key("User"):
302 continue
303 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
304 return users
306 users = getUserMap()
308 if len(changeRange) == 0:
309 try:
310 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
311 output = sout.read()
312 if output.endswith("\n"):
313 output = output[:-1]
314 tagIdx = output.index(" tags/p4/")
315 caretIdx = output.find("^")
316 endPos = len(output)
317 if caretIdx != -1:
318 endPos = caretIdx
319 rev = int(output[tagIdx + 9 : endPos]) + 1
320 changeRange = "@%s,#head" % rev
321 initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
322 initialTag = "p4/%s" % (int(rev) - 1)
323 except:
324 pass
326 sys.stderr.write("\n")
328 tz = - time.timezone / 36
329 tzsign = ("%s" % tz)[0]
330 if tzsign != '+' and tzsign != '-':
331 tz = "+" + ("%s" % tz)
333 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
335 if len(revision) > 0:
336 print "Doing initial import of %s from revision %s" % (globalPrefix, revision)
338 details = { "user" : "git perforce import user", "time" : int(time.time()) }
339 details["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix, revision)
340 details["change"] = revision
341 newestRevision = 0
343 fileCnt = 0
344 for info in p4CmdList("files %s...%s" % (globalPrefix, revision)):
345 change = int(info["change"])
346 if change > newestRevision:
347 newestRevision = change
349 if info["action"] == "delete":
350 continue
352 for prop in [ "depotFile", "rev", "action", "type" ]:
353 details["%s%s" % (prop, fileCnt)] = info[prop]
355 fileCnt = fileCnt + 1
357 details["change"] = newestRevision
359 try:
360 commit(details, extractFilesFromCommit(details), branch, globalPrefix)
361 except:
362 print gitError.read()
364 else:
365 changes = []
367 if len(changesFile) > 0:
368 output = open(changesFile).readlines()
369 changeSet = set()
370 for line in output:
371 changeSet.add(int(line))
373 for change in changeSet:
374 changes.append(change)
376 changes.sort()
377 else:
378 output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
380 for line in output:
381 changeNum = line.split(" ")[1]
382 changes.append(changeNum)
384 changes.reverse()
386 if len(changes) == 0:
387 print "no changes to import!"
388 sys.exit(1)
390 cnt = 1
391 for change in changes:
392 description = p4Cmd("describe %s" % change)
394 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
395 sys.stdout.flush()
396 cnt = cnt + 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)
412 print ""
414 gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
415 gitStream.write("from %s\n\n" % branch);
418 gitStream.close()
419 gitOutput.close()
420 gitError.close()
422 os.popen("git-repo-config p4.depotpath %s" % globalPrefix).read()
423 if len(initialTag) > 0:
424 os.popen("git tag -d %s" % initialTag).read()
426 sys.exit(0)