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
if len(sys.argv) != 2:
16print "usage: %s //depot/path[@revRange]" % sys.argv[0]
17print "\n example:"
18print " %s //depot/my/project/ -- to import everything"
19print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
20print ""
21print " (a ... is not needed in the path p4 specification, it's added implicitly)"
22print ""
23sys.exit(1)
2425
branch = "refs/heads/p4"
26prefix = sys.argv[1]
27changeRange = ""
28try:
29atIdx = prefix.index("@")
30changeRange = prefix[atIdx:]
31prefix = prefix[0:atIdx]
32except ValueError:
33changeRange = ""
3435
if prefix.endswith("..."):
36prefix = prefix[:-3]
3738
if not prefix.endswith("/"):
39prefix += "/"
4041
def p4CmdList(cmd):
42pipe = os.popen("p4 -G %s" % cmd, "rb")
43result = []
44try:
45while True:
46entry = marshal.load(pipe)
47result.append(entry)
48except EOFError:
49pass
50pipe.close()
51return result
5253
def p4Cmd(cmd):
54list = p4CmdList(cmd)
55result = {}
56for entry in list:
57result.update(entry)
58return result;
5960
def getUserMap():
61users = {}
6263
for output in p4CmdList("users"):
64if not output.has_key("User"):
65continue
66users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
67return users
6869
users = getUserMap()
70topMerge = ""
7172
if len(changeRange) == 0:
73try:
74sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
75output = sout.read()
76tagIdx = output.index(" tags/p4/")
77caretIdx = output.index("^")
78revision = int(output[tagIdx + 9 : caretIdx]) + 1
79changeRange = "@%s,#head" % revision
80topMerge = os.popen("git-rev-parse %s" % branch).read()[:-1]
81except:
82pass
8384
output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
8586
changes = []
87for line in output:
88changeNum = line.split(" ")[1]
89changes.append(changeNum)
9091
changes.reverse()
9293
if len(changes) == 0:
94print "no changes to import!"
95sys.exit(1)
9697
sys.stderr.write("\n")
9899
tz = - time.timezone / 36
100101
gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
102103
cnt = 1
104for change in changes:
105description = p4Cmd("describe %s" % change)
106107
sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
108sys.stdout.flush()
109cnt = cnt + 1
110111
epoch = description["time"]
112author = description["user"]
113114
gitStream.write("commit %s\n" % branch)
115committer = ""
116if author in users:
117committer = "%s %s %s" % (users[author], epoch, tz)
118else:
119committer = "%s <a@b> %s %s" % (author, epoch, tz)
120121
gitStream.write("committer %s\n" % committer)
122123
gitStream.write("data <<EOT\n")
124gitStream.write(description["desc"])
125gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
126gitStream.write("EOT\n\n")
127128
if len(topMerge) > 0:
129gitStream.write("merge %s\n" % topMerge)
130topMerge = ""
131132
fnum = 0
133while description.has_key("depotFile%s" % fnum):
134path = description["depotFile%s" % fnum]
135if not path.startswith(prefix):
136print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
137fnum = fnum + 1
138continue
139140
rev = description["rev%s" % fnum]
141depotPath = path + "#" + rev
142relPath = path[len(prefix):]
143action = description["action%s" % fnum]
144145
if action == "delete":
146gitStream.write("D %s\n" % relPath)
147else:
148mode = 644
149if description["type%s" % fnum].startswith("x"):
150mode = 755
151152
data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
153154
gitStream.write("M %s inline %s\n" % (mode, relPath))
155gitStream.write("data %s\n" % len(data))
156gitStream.write(data)
157gitStream.write("\n")
158159
fnum = fnum + 1
160161
gitStream.write("\n")
162163
gitStream.write("tag p4/%s\n" % change)
164gitStream.write("from %s\n" % branch);
165gitStream.write("tagger %s\n" % committer);
166gitStream.write("data 0\n\n")
167168
169
gitStream.close()
170gitOutput.close()
171gitError.close()
172173
print ""