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
14
15if len(sys.argv) != 2:
16 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
17 print "\n example:"
18 print " %s //depot/my/project/ -- to import everything"
19 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
20 print ""
21 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
22 print ""
23 sys.exit(1)
24
25branch = "refs/heads/p4"
26prefix = sys.argv[1]
27changeRange = ""
28try:
29 atIdx = prefix.index("@")
30 changeRange = prefix[atIdx:]
31 prefix = prefix[0:atIdx]
32except ValueError:
33 changeRange = ""
34
35if prefix.endswith("..."):
36 prefix = prefix[:-3]
37
38if not prefix.endswith("/"):
39 prefix += "/"
40
41def p4CmdList(cmd):
42 pipe = os.popen("p4 -G %s" % cmd, "rb")
43 result = []
44 try:
45 while True:
46 entry = marshal.load(pipe)
47 result.append(entry)
48 except EOFError:
49 pass
50 pipe.close()
51 return result
52
53def p4Cmd(cmd):
54 list = p4CmdList(cmd)
55 result = {}
56 for entry in list:
57 result.update(entry)
58 return result;
59
60def getUserMap():
61 users = {}
62
63 for output in p4CmdList("users"):
64 if not output.has_key("User"):
65 continue
66 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
67 return users
68
69users = getUserMap()
70topMerge = ""
71
72if len(changeRange) == 0:
73 try:
74 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
75 output = sout.read()
76 tagIdx = output.index(" tags/p4/")
77 caretIdx = output.index("^")
78 revision = int(output[tagIdx + 9 : caretIdx]) + 1
79 changeRange = "@%s,#head" % revision
80 topMerge = os.popen("git-rev-parse %s" % branch).read()[:-1]
81 except:
82 pass
83
84output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
85
86changes = []
87for line in output:
88 changeNum = line.split(" ")[1]
89 changes.append(changeNum)
90
91changes.reverse()
92
93if len(changes) == 0:
94 print "no changes to import!"
95 sys.exit(1)
96
97sys.stderr.write("\n")
98
99tz = - time.timezone / 36
100
101gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
102
103cnt = 1
104for change in changes:
105 description = p4Cmd("describe %s" % change)
106
107 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
108 sys.stdout.flush()
109 cnt = cnt + 1
110
111 epoch = description["time"]
112 author = description["user"]
113
114 gitStream.write("commit %s\n" % branch)
115 committer = ""
116 if author in users:
117 committer = "%s %s %s" % (users[author], epoch, tz)
118 else:
119 committer = "%s <a@b> %s %s" % (author, epoch, tz)
120
121 gitStream.write("committer %s\n" % committer)
122
123 gitStream.write("data <<EOT\n")
124 gitStream.write(description["desc"])
125 gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
126 gitStream.write("EOT\n\n")
127
128 if len(topMerge) > 0:
129 gitStream.write("merge %s\n" % topMerge)
130 topMerge = ""
131
132 fnum = 0
133 while description.has_key("depotFile%s" % fnum):
134 path = description["depotFile%s" % fnum]
135 if not path.startswith(prefix):
136 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
137 fnum = fnum + 1
138 continue
139
140 rev = description["rev%s" % fnum]
141 depotPath = path + "#" + rev
142 relPath = path[len(prefix):]
143 action = description["action%s" % fnum]
144
145 if action == "delete":
146 gitStream.write("D %s\n" % relPath)
147 else:
148 mode = 644
149 if description["type%s" % fnum].startswith("x"):
150 mode = 755
151
152 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
153
154 gitStream.write("M %s inline %s\n" % (mode, relPath))
155 gitStream.write("data %s\n" % len(data))
156 gitStream.write(data)
157 gitStream.write("\n")
158
159 fnum = fnum + 1
160
161 gitStream.write("\n")
162
163 gitStream.write("tag p4/%s\n" % change)
164 gitStream.write("from %s\n" % branch);
165 gitStream.write("tagger %s\n" % committer);
166 gitStream.write("data 0\n\n")
167
168
169gitStream.close()
170gitOutput.close()
171gitError.close()
172
173print ""