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