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 prefix.endswith("..."):
39prefix = prefix[:-3]
4041
if not prefix.endswith("/"):
42prefix += "/"
4344
def p4CmdList(cmd):
45pipe = os.popen("p4 -G %s" % cmd, "rb")
46result = []
47try:
48while True:
49entry = marshal.load(pipe)
50result.append(entry)
51except EOFError:
52pass
53pipe.close()
54return result
5556
def p4Cmd(cmd):
57list = p4CmdList(cmd)
58result = {}
59for entry in list:
60result.update(entry)
61return result;
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:
124mode = 644
125if description["type%s" % fnum].startswith("x"):
126mode = 755
127128
data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
129130
gitStream.write("M %s inline %s\n" % (mode, relPath))
131gitStream.write("data %s\n" % len(data))
132gitStream.write(data)
133gitStream.write("\n")
134135
fnum = fnum + 1
136137
gitStream.write("\n")
138139
gitStream.close()
140gitOutput.close()
141gitError.close()
142143
print ""