1#!/usr/bin/python
2#
3# p4-fast-export.py
4#
5# Author: Simon Hausmann <hausmann@kde.org>
6# License: MIT <http://www.opensource.org/licenses/mit-license.php>
7#
8# TODO:
9# - support integrations (at least p4i)
10# - support p4 submit (hah!)
11#
12import os, string, sys, time
13import marshal, popen2, getopt
14
15knownBranches = set()
16committedChanges = set()
17branch = "refs/heads/master"
18globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
19detectBranches = False
20changesFile = ""
21if len(globalPrefix) != 0:
22 globalPrefix = globalPrefix[:-1]
23
24try:
25 opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
26except getopt.GetoptError:
27 print "fixme, syntax error"
28 sys.exit(1)
29
30for 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
37
38if len(args) == 0 and len(globalPrefix) != 0:
39 print "[using previously specified depot path %s]" % globalPrefix
40elif 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)
50else:
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]
55
56changeRange = ""
57revision = ""
58users = {}
59initialParent = ""
60lastChange = 0
61initialTag = ""
62
63if 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]
72elif globalPrefix.find("#") != -1:
73 hashIdx = globalPrefix.index("#")
74 revision = globalPrefix[hashIdx:]
75 globalPrefix = globalPrefix[0:hashIdx]
76elif len(previousDepotPath) == 0:
77 revision = "#head"
78
79if globalPrefix.endswith("..."):
80 globalPrefix = globalPrefix[:-3]
81
82if not globalPrefix.endswith("/"):
83 globalPrefix += "/"
84
85def 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
96
97def p4Cmd(cmd):
98 list = p4CmdList(cmd)
99 result = {}
100 for entry in list:
101 result.update(entry)
102 return result;
103
104def 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
113
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
122
123def 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
130def branchesForCommit(files):
131 global knownBranches
132 branches = set()
133
134 for file in files:
135 relativePath = file["path"][len(globalPrefix):]
136 # strip off the filename
137 relativePath = relativePath[0:relativePath.rfind("/")]
138
139# if len(branches) == 0:
140# branches.add(relativePath)
141# knownBranches.add(relativePath)
142# continue
143
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
158
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
179def 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)
194 else:
195 committer = "%s <a@b> %s %s" % (author, epoch, tz)
196
197 gitStream.write("committer %s\n" % committer)
198
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")
203
204 if len(initialParent) > 0:
205 gitStream.write("from %s\n" % initialParent)
206 initialParent = ""
207
208 #mergedBranches = set()
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
217 action = file["action"]
218 if action != "integrate" and action != "branch":
219 continue
220 rev = file["rev"]
221 depotPath = path + "#" + rev
222
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);
227
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);
232
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):
244 continue
245
246 lastSourceRev = log["erev0,0"]
247
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]
253
254 change = int(sourceLog["change0"])
255 merges.add(change)
256
257# relPath = source[len(globalPrefix):]
258#
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
264
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):
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"]
278
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
285
286 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
287
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")
292
293 gitStream.write("\n")
294
295 lastChange = int(details["change"])
296
297def getUserMap():
298 users = {}
299
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
305
306users = getUserMap()
307
308if 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
325
326sys.stderr.write("\n")
327
328tz = - time.timezone / 36
329tzsign = ("%s" % tz)[0]
330if tzsign != '+' and tzsign != '-':
331 tz = "+" + ("%s" % tz)
332
333gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
334
335if len(revision) > 0:
336 print "Doing initial import of %s from revision %s" % (globalPrefix, revision)
337
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
342
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
348
349 if info["action"] == "delete":
350 continue
351
352 for prop in [ "depotFile", "rev", "action", "type" ]:
353 details["%s%s" % (prop, fileCnt)] = info[prop]
354
355 fileCnt = fileCnt + 1
356
357 details["change"] = newestRevision
358
359 try:
360 commit(details, extractFilesFromCommit(details), branch, globalPrefix)
361 except:
362 print gitError.read()
363
364else:
365 changes = []
366
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!"
388 sys.exit(1)
389
390 cnt = 1
391 for change in changes:
392 description = p4Cmd("describe %s" % change)
393
394 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
395 sys.stdout.flush()
396 cnt = cnt + 1
397
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
412print ""
413
414gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
415gitStream.write("from %s\n\n" % branch);
416
417
418gitStream.close()
419gitOutput.close()
420gitError.close()
421
422os.popen("git-repo-config p4.depotpath %s" % globalPrefix).read()
423if len(initialTag) > 0:
424 os.popen("git tag -d %s" % initialTag).read()
425
426sys.exit(0)