another todo item
[fast-export/rorcz.git] / p4-fast-export.py
blob6fd86cf7351b680121295495f2cba7559d803d00
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!)
11 # - emulate p4's delete behavior: if a directory becomes empty delete it. continue
12 # with parent dir until non-empty dir is found.
14 import os, string, sys, time
15 import marshal, popen2, getopt
16 from sets import Set;
18 knownBranches = Set()
19 committedChanges = Set()
20 branch = "refs/heads/master"
21 globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
22 detectBranches = False
23 changesFile = ""
24 if len(globalPrefix) != 0:
25 globalPrefix = globalPrefix[:-1]
27 try:
28 opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
29 except getopt.GetoptError:
30 print "fixme, syntax error"
31 sys.exit(1)
33 for o, a in opts:
34 if o == "--branch":
35 branch = "refs/heads/" + a
36 elif o == "--detect-branches":
37 detectBranches = True
38 elif o == "--changesfile":
39 changesFile = a
41 if len(args) == 0 and len(globalPrefix) != 0:
42 print "[using previously specified depot path %s]" % globalPrefix
43 elif len(args) != 1:
44 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
45 print "\n example:"
46 print " %s //depot/my/project/ -- to import the current head"
47 print " %s //depot/my/project/@all -- to import everything"
48 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
49 print ""
50 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
51 print ""
52 sys.exit(1)
53 else:
54 if len(globalPrefix) != 0 and globalPrefix != args[0]:
55 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix, args[0])
56 sys.exit(1)
57 globalPrefix = args[0]
59 changeRange = ""
60 revision = ""
61 users = {}
62 initialParent = ""
63 lastChange = 0
64 initialTag = ""
66 if globalPrefix.find("@") != -1:
67 atIdx = globalPrefix.index("@")
68 changeRange = globalPrefix[atIdx:]
69 if changeRange == "@all":
70 changeRange = ""
71 elif changeRange.find(",") == -1:
72 revision = changeRange
73 changeRange = ""
74 globalPrefix = globalPrefix[0:atIdx]
75 elif globalPrefix.find("#") != -1:
76 hashIdx = globalPrefix.index("#")
77 revision = globalPrefix[hashIdx:]
78 globalPrefix = globalPrefix[0:hashIdx]
79 elif len(previousDepotPath) == 0:
80 revision = "#head"
82 if globalPrefix.endswith("..."):
83 globalPrefix = globalPrefix[:-3]
85 if not globalPrefix.endswith("/"):
86 globalPrefix += "/"
88 def p4CmdList(cmd):
89 pipe = os.popen("p4 -G %s" % cmd, "rb")
90 result = []
91 try:
92 while True:
93 entry = marshal.load(pipe)
94 result.append(entry)
95 except EOFError:
96 pass
97 pipe.close()
98 return result
100 def p4Cmd(cmd):
101 list = p4CmdList(cmd)
102 result = {}
103 for entry in list:
104 result.update(entry)
105 return result;
107 def extractFilesFromCommit(commit):
108 files = []
109 fnum = 0
110 while commit.has_key("depotFile%s" % fnum):
111 path = commit["depotFile%s" % fnum]
112 if not path.startswith(globalPrefix):
113 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
114 fnum = fnum + 1
115 continue
117 file = {}
118 file["path"] = path
119 file["rev"] = commit["rev%s" % fnum]
120 file["action"] = commit["action%s" % fnum]
121 file["type"] = commit["type%s" % fnum]
122 files.append(file)
123 fnum = fnum + 1
124 return files
126 def isSubPathOf(first, second):
127 if not first.startswith(second):
128 return False
129 if first == second:
130 return True
131 return first[len(second)] == "/"
133 def branchesForCommit(files):
134 global knownBranches
135 branches = Set()
137 for file in files:
138 relativePath = file["path"][len(globalPrefix):]
139 # strip off the filename
140 relativePath = relativePath[0:relativePath.rfind("/")]
142 # if len(branches) == 0:
143 # branches.add(relativePath)
144 # knownBranches.add(relativePath)
145 # continue
147 ###### this needs more testing :)
148 knownBranch = False
149 for branch in branches:
150 if relativePath == branch:
151 knownBranch = True
152 break
153 # if relativePath.startswith(branch):
154 if isSubPathOf(relativePath, branch):
155 knownBranch = True
156 break
157 # if branch.startswith(relativePath):
158 if isSubPathOf(branch, relativePath):
159 branches.remove(branch)
160 break
162 if knownBranch:
163 continue
165 for branch in knownBranches:
166 #if relativePath.startswith(branch):
167 if isSubPathOf(relativePath, branch):
168 if len(branches) == 0:
169 relativePath = branch
170 else:
171 knownBranch = True
172 break
174 if knownBranch:
175 continue
177 branches.add(relativePath)
178 knownBranches.add(relativePath)
180 return branches
182 def commit(details, files, branch, branchPrefix):
183 global initialParent
184 global users
185 global lastChange
186 global committedChanges
188 epoch = details["time"]
189 author = details["user"]
191 gitStream.write("commit %s\n" % branch)
192 gitStream.write("mark :%s\n" % details["change"])
193 committedChanges.add(int(details["change"]))
194 committer = ""
195 if author in users:
196 committer = "%s %s %s" % (users[author], epoch, tz)
197 else:
198 committer = "%s <a@b> %s %s" % (author, epoch, tz)
200 gitStream.write("committer %s\n" % committer)
202 gitStream.write("data <<EOT\n")
203 gitStream.write(details["desc"])
204 gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
205 gitStream.write("EOT\n\n")
207 if len(initialParent) > 0:
208 gitStream.write("from %s\n" % initialParent)
209 initialParent = ""
211 #mergedBranches = Set()
212 merges = Set()
214 for file in files:
215 if lastChange == 0 or not detectBranches:
216 continue
217 path = file["path"]
218 if not path.startswith(branchPrefix):
219 continue
220 action = file["action"]
221 if action != "integrate" and action != "branch":
222 continue
223 rev = file["rev"]
224 depotPath = path + "#" + rev
226 log = p4CmdList("filelog \"%s\"" % depotPath)
227 if len(log) != 1:
228 print "eek! I got confused by the filelog of %s" % depotPath
229 sys.exit(1);
231 log = log[0]
232 if log["action0"] != action:
233 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
234 sys.exit(1);
236 branchAction = log["how0,0"]
237 # if branchAction == "branch into" or branchAction == "ignored":
238 # continue # ignore for branching
240 if not branchAction.endswith(" from"):
241 continue # ignore for branching
242 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
243 # sys.exit(1);
245 source = log["file0,0"]
246 if source.startswith(branchPrefix):
247 continue
249 lastSourceRev = log["erev0,0"]
251 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
252 if len(sourceLog) != 1:
253 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
254 sys.exit(1);
255 sourceLog = sourceLog[0]
257 change = int(sourceLog["change0"])
258 merges.add(change)
260 # relPath = source[len(globalPrefix):]
262 # for branch in knownBranches:
263 # if relPath.startswith(branch) and branch not in mergedBranches:
264 # gitStream.write("merge refs/heads/%s\n" % branch)
265 # mergedBranches.add(branch)
266 # break
268 for merge in merges:
269 if merge in committedChanges:
270 gitStream.write("merge :%s\n" % merge)
272 for file in files:
273 path = file["path"]
274 if not path.startswith(branchPrefix):
275 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
276 continue
277 rev = file["rev"]
278 depotPath = path + "#" + rev
279 relPath = path[len(branchPrefix):]
280 action = file["action"]
282 if action == "delete":
283 gitStream.write("D %s\n" % relPath)
284 else:
285 mode = 644
286 if file["type"].startswith("x"):
287 mode = 755
289 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
291 gitStream.write("M %s inline %s\n" % (mode, relPath))
292 gitStream.write("data %s\n" % len(data))
293 gitStream.write(data)
294 gitStream.write("\n")
296 gitStream.write("\n")
298 lastChange = int(details["change"])
300 def getUserMap():
301 users = {}
303 for output in p4CmdList("users"):
304 if not output.has_key("User"):
305 continue
306 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
307 return users
309 users = getUserMap()
311 if len(changeRange) == 0:
312 try:
313 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
314 output = sout.read()
315 if output.endswith("\n"):
316 output = output[:-1]
317 tagIdx = output.index(" tags/p4/")
318 caretIdx = output.find("^")
319 endPos = len(output)
320 if caretIdx != -1:
321 endPos = caretIdx
322 rev = int(output[tagIdx + 9 : endPos]) + 1
323 changeRange = "@%s,#head" % rev
324 initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
325 initialTag = "p4/%s" % (int(rev) - 1)
326 except:
327 pass
329 sys.stderr.write("\n")
331 tz = - time.timezone / 36
332 tzsign = ("%s" % tz)[0]
333 if tzsign != '+' and tzsign != '-':
334 tz = "+" + ("%s" % tz)
336 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
338 if len(revision) > 0:
339 print "Doing initial import of %s from revision %s" % (globalPrefix, revision)
341 details = { "user" : "git perforce import user", "time" : int(time.time()) }
342 details["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix, revision)
343 details["change"] = revision
344 newestRevision = 0
346 fileCnt = 0
347 for info in p4CmdList("files %s...%s" % (globalPrefix, revision)):
348 change = int(info["change"])
349 if change > newestRevision:
350 newestRevision = change
352 if info["action"] == "delete":
353 continue
355 for prop in [ "depotFile", "rev", "action", "type" ]:
356 details["%s%s" % (prop, fileCnt)] = info[prop]
358 fileCnt = fileCnt + 1
360 details["change"] = newestRevision
362 try:
363 commit(details, extractFilesFromCommit(details), branch, globalPrefix)
364 except:
365 print gitError.read()
367 else:
368 changes = []
370 if len(changesFile) > 0:
371 output = open(changesFile).readlines()
372 changeSet = Set()
373 for line in output:
374 changeSet.add(int(line))
376 for change in changeSet:
377 changes.append(change)
379 changes.sort()
380 else:
381 output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
383 for line in output:
384 changeNum = line.split(" ")[1]
385 changes.append(changeNum)
387 changes.reverse()
389 if len(changes) == 0:
390 print "no changes to import!"
391 sys.exit(1)
393 cnt = 1
394 for change in changes:
395 description = p4Cmd("describe %s" % change)
397 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
398 sys.stdout.flush()
399 cnt = cnt + 1
401 # try:
402 files = extractFilesFromCommit(description)
403 if detectBranches:
404 for branch in branchesForCommit(files):
405 knownBranches.add(branch)
406 branchPrefix = globalPrefix + branch + "/"
407 branch = "refs/heads/" + branch
408 commit(description, files, branch, branchPrefix)
409 else:
410 commit(description, files, branch, globalPrefix)
411 # except:
412 # print gitError.read()
413 # sys.exit(1)
415 print ""
417 gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
418 gitStream.write("from %s\n\n" % branch);
421 gitStream.close()
422 gitOutput.close()
423 gitError.close()
425 os.popen("git-repo-config p4.depotpath %s" % globalPrefix).read()
426 if len(initialTag) > 0:
427 os.popen("git tag -d %s" % initialTag).read()
429 sys.exit(0)