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 incremental imports
11# - create tags
12# - instead of reading all files into a variable try to pipe from
13# - support p4 submit (hah!)
14# - don't hardcode the import to master
15#
16import os, string, sys, time
17import marshal, popen2
1819
if len(sys.argv) != 2:
20print "usage: %s //depot/path[@revRange]" % sys.argv[0]
21print "\n example:"
22print " %s //depot/my/project/ -- to import everything"
23print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
24print ""
25print " (a ... is not needed in the path p4 specification, it's added implicitly)"
26print ""
27sys.exit(1)
2829
prefix = sys.argv[1]
30changeRange = ""
31try:
32atIdx = prefix.index("@")
33changeRange = prefix[atIdx:]
34prefix = prefix[0:atIdx]
35except ValueError:
36changeRange = ""
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 p4FileSize(path):
61return int(p4Cmd("fstat -Ol \"%s\"" % path)["fileSize"])
6263
def getUserMap():
64users = {}
6566
for output in p4CmdList("users"):
67if not output.has_key("User"):
68continue
69users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
70return users
7172
users = getUserMap()
7374
output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
7576
changes = []
77for line in output:
78changeNum = line.split(" ")[1]
79changes.append(changeNum)
8081
changes.reverse()
8283
sys.stderr.write("\n")
8485
tz = - time.timezone / 36
8687
gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
8889
cnt = 1
90for change in changes:
91description = p4Cmd("describe %s" % change)
9293
sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
94cnt = cnt + 1
9596
epoch = description["time"]
97author = description["user"]
9899
gitStream.write("commit refs/heads/master\n")
100if author in users:
101gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
102else:
103gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
104gitStream.write("data <<EOT\n")
105gitStream.write(description["desc"])
106gitStream.write("EOT\n\n")
107108
fnum = 0
109while description.has_key("depotFile%s" % fnum):
110path = description["depotFile%s" % fnum]
111if not path.startswith(prefix):
112print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
113fnum = fnum + 1
114continue
115116
rev = description["rev%s" % fnum]
117depotPath = path + "#" + rev
118relPath = path[len(prefix):]
119action = description["action%s" % fnum]
120121
if action == "delete":
122gitStream.write("D %s\n" % relPath)
123else:
124fileSize = p4FileSize(depotPath)
125mode = 644
126if description["type%s" % fnum].startswith("x"):
127mode = 755
128129
gitStream.write("M %s inline %s\n" % (mode, relPath))
130gitStream.write("data %s\n" % fileSize)
131gitStream.write(os.popen("p4 print -q \"%s\"" % depotPath).read())
132gitStream.write("\n")
133134
fnum = fnum + 1
135136
gitStream.write("\n")
137138
gitStream.close()
139gitOutput.close()
140gitError.close()
141142
print ""