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
1415
branch = "refs/heads/p4"
16prefix = os.popen("git-repo-config --get p4.depotpath").read()
17if len(prefix) != 0:
18prefix = prefix[:-1]
1920
if len(sys.argv) == 1 and len(prefix) != 0:
21print "[using previously specified depot path %s]" % prefix
22elif len(sys.argv) != 2:
23print "usage: %s //depot/path[@revRange]" % sys.argv[0]
24print "\n example:"
25print " %s //depot/my/project/ -- to import everything"
26print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
27print ""
28print " (a ... is not needed in the path p4 specification, it's added implicitly)"
29print ""
30sys.exit(1)
31else:
32if len(prefix) != 0 and prefix != sys.argv[1]:
33print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix, sys.argv[1])
34sys.exit(1)
35prefix = sys.argv[1]
3637
changeRange = ""
38revision = ""
39users = {}
40initialParent = ""
4142
if prefix.find("@") != -1:
43atIdx = prefix.index("@")
44changeRange = prefix[atIdx:]
45if changeRange.find(",") == -1:
46revision = changeRange
47changeRange = ""
48prefix = prefix[0:atIdx]
49elif prefix.find("#") != -1:
50hashIdx = prefix.index("#")
51revision = prefix[hashIdx:]
52prefix = prefix[0:hashIdx]
5354
if prefix.endswith("..."):
55prefix = prefix[:-3]
5657
if not prefix.endswith("/"):
58prefix += "/"
5960
def p4CmdList(cmd):
61pipe = os.popen("p4 -G %s" % cmd, "rb")
62result = []
63try:
64while True:
65entry = marshal.load(pipe)
66result.append(entry)
67except EOFError:
68pass
69pipe.close()
70return result
7172
def p4Cmd(cmd):
73list = p4CmdList(cmd)
74result = {}
75for entry in list:
76result.update(entry)
77return result;
7879
def commit(details):
80global initialParent
81global users
8283
epoch = details["time"]
84author = details["user"]
8586
gitStream.write("commit %s\n" % branch)
87committer = ""
88if author in users:
89committer = "%s %s %s" % (users[author], epoch, tz)
90else:
91committer = "%s <a@b> %s %s" % (author, epoch, tz)
9293
gitStream.write("committer %s\n" % committer)
9495
gitStream.write("data <<EOT\n")
96gitStream.write(details["desc"])
97gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
98gitStream.write("EOT\n\n")
99100
if len(initialParent) > 0:
101gitStream.write("from %s\n" % initialParent)
102initialParent = ""
103104
fnum = 0
105while details.has_key("depotFile%s" % fnum):
106path = details["depotFile%s" % fnum]
107if not path.startswith(prefix):
108print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
109fnum = fnum + 1
110continue
111112
rev = details["rev%s" % fnum]
113depotPath = path + "#" + rev
114relPath = path[len(prefix):]
115action = details["action%s" % fnum]
116117
if action == "delete":
118gitStream.write("D %s\n" % relPath)
119else:
120mode = 644
121if details["type%s" % fnum].startswith("x"):
122mode = 755
123124
data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
125126
gitStream.write("M %s inline %s\n" % (mode, relPath))
127gitStream.write("data %s\n" % len(data))
128gitStream.write(data)
129gitStream.write("\n")
130131
fnum = fnum + 1
132133
gitStream.write("\n")
134135
gitStream.write("tag p4/%s\n" % details["change"])
136gitStream.write("from %s\n" % branch);
137gitStream.write("tagger %s\n" % committer);
138gitStream.write("data 0\n\n")
139140
141
def getUserMap():
142users = {}
143144
for output in p4CmdList("users"):
145if not output.has_key("User"):
146continue
147users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
148return users
149150
users = getUserMap()
151152
if len(changeRange) == 0:
153try:
154sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
155output = sout.read()
156tagIdx = output.index(" tags/p4/")
157caretIdx = output.index("^")
158rev = int(output[tagIdx + 9 : caretIdx]) + 1
159changeRange = "@%s,#head" % rev
160initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
161except:
162pass
163164
sys.stderr.write("\n")
165166
tz = - time.timezone / 36
167tzsign = ("%s" % tz)[0]
168if tzsign != '+' and tzsign != '-':
169tz = "+" + ("%s" % tz)
170171
if len(revision) > 0:
172print "Doing initial import of %s from revision %s" % (prefix, revision)
173174
details = { "user" : "git perforce import user", "time" : int(time.time()) }
175details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
176details["change"] = revision
177newestRevision = 0
178179
fileCnt = 0
180for info in p4CmdList("files %s...%s" % (prefix, revision)):
181change = info["change"]
182if change > newestRevision:
183newestRevision = change
184185
if info["action"] == "delete":
186continue
187188
for prop in [ "depotFile", "rev", "action", "type" ]:
189details["%s%s" % (prop, fileCnt)] = info[prop]
190191
fileCnt = fileCnt + 1
192193
details["change"] = newestRevision
194195
gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
196try:
197commit(details)
198except:
199print gitError.read()
200201
gitStream.close()
202gitOutput.close()
203gitError.close()
204else:
205output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
206207
changes = []
208for line in output:
209changeNum = line.split(" ")[1]
210changes.append(changeNum)
211212
changes.reverse()
213214
if len(changes) == 0:
215print "no changes to import!"
216sys.exit(1)
217218
gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
219220
cnt = 1
221for change in changes:
222description = p4Cmd("describe %s" % change)
223224
sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
225sys.stdout.flush()
226cnt = cnt + 1
227228
commit(description)
229230
gitStream.close()
231gitOutput.close()
232gitError.close()
233234
print ""
235236
os.popen("git-repo-config p4.depotpath %s" % prefix).read()
237