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
18
19if len(sys.argv) != 2:
20 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
21 print "\n example:"
22 print " %s //depot/my/project/ -- to import everything"
23 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
24 print ""
25 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
26 print ""
27 sys.exit(1)
28
29prefix = sys.argv[1]
30changeRange = ""
31try:
32 atIdx = prefix.index("@")
33 changeRange = prefix[atIdx:]
34 prefix = prefix[0:atIdx]
35except ValueError:
36 changeRange = ""
37
38if not prefix.endswith("/"):
39 prefix += "/"
40
41def p4Cmd(cmd):
42 pipe = os.popen("p4 -G %s" % cmd, "rb")
43 result = {}
44 try:
45 while True:
46 entry = marshal.load(pipe)
47 result.update(entry)
48 except EOFError:
49 pass
50 pipe.close()
51 return result
52
53def describe(change):
54 output = os.popen("p4 describe %s" % change).readlines()
55
56 firstLine = output[0]
57
58 splitted = firstLine.split(" ")
59 author = splitted[3]
60 author = author[:author.find("@")]
61 tm = time.strptime(splitted[5] + " " + splitted[6], "%Y/%m/%d %H:%M:%S ")
62 epoch = int(time.mktime(tm))
63
64 filesSection = 0
65 try:
66 filesSection = output.index("Affected files ...\n")
67 except ValueError:
68 sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
69 return [], [], [], [], []
70
71 differencesSection = 0
72 try:
73 differencesSection = output.index("Differences ...\n")
74 except ValueError:
75 sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
76 return [], [], [], [], []
77
78 log = output[2:filesSection - 1]
79
80 lines = output[filesSection + 2:differencesSection - 1]
81
82 changed = []
83 removed = []
84
85 for line in lines:
86 # chop off "... " and trailing newline
87 line = line[4:len(line) - 1]
88
89 lastSpace = line.rfind(" ")
90 if lastSpace == -1:
91 sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
92 continue
93
94 operation = line[lastSpace + 1:]
95 path = line[:lastSpace]
96
97 if operation == "delete":
98 removed.append(path)
99 else:
100 changed.append(path)
101
102 return author, log, epoch, changed, removed
103
104def p4Stat(path):
105 output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
106 fileSize = 0
107 mode = 644
108 for line in output:
109 if line.startswith("... headType x"):
110 mode = 755
111 elif line.startswith("... fileSize "):
112 fileSize = long(line[12:])
113 return mode, fileSize
114
115def stripRevision(path):
116 hashPos = path.rindex("#")
117 return path[:hashPos]
118
119def getUserMap():
120 users = {}
121 output = os.popen("p4 users")
122 for line in output:
123 firstSpace = line.index(" ")
124 secondSpace = line.index(" ", firstSpace + 1)
125 key = line[:firstSpace]
126 email = line[firstSpace + 1:secondSpace]
127 openParenPos = line.index("(", secondSpace)
128 closedParenPos = line.index(")", openParenPos)
129 name = line[openParenPos + 1:closedParenPos]
130
131 users[key] = name + " " + email
132
133 return users
134
135users = getUserMap()
136
137output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
138
139changes = []
140for line in output:
141 changeNum = line.split(" ")[1]
142 changes.append(changeNum)
143
144changes.reverse()
145
146sys.stderr.write("\n")
147
148tz = - time.timezone / 36
149
150gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
151
152cnt = 1
153for change in changes:
154 [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
155 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
156 cnt = cnt + 1
157
158 gitStream.write("commit refs/heads/master\n")
159 if author in users:
160 gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
161 else:
162 gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
163 gitStream.write("data <<EOT\n")
164 for l in log:
165 gitStream.write(l)
166 gitStream.write("EOT\n\n")
167
168 for f in changedFiles:
169 if not f.startswith(prefix):
170 sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
171 continue
172 relpath = f[len(prefix):]
173
174 [mode, fileSize] = p4Stat(f)
175
176 gitStream.write("M %s inline %s\n" % (mode, stripRevision(relpath)))
177 gitStream.write("data %s\n" % fileSize)
178 gitStream.write(os.popen("p4 print -q \"%s\"" % f).read())
179 gitStream.write("\n")
180
181 for f in removedFiles:
182 if not f.startswith(prefix):
183 sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
184 continue
185 relpath = f[len(prefix):]
186 gitStream.write("D %s\n" % stripRevision(relpath))
187
188 gitStream.write("\n")
189
190gitStream.close()
191gitOutput.close()
192gitError.close()
193
194print ""