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 #
16 import os, string, sys, time
17 import marshal, popen2
19 if 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)
29 prefix = sys.argv[1]
30 changeRange = ""
31 try:
32 atIdx = prefix.index("@")
33 changeRange = prefix[atIdx:]
34 prefix = prefix[0:atIdx]
35 except ValueError:
36 changeRange = ""
38 if not prefix.endswith("/"):
39 prefix += "/"
41 def 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
53 def describe(change):
54 output = os.popen("p4 describe %s" % change).readlines()
56 firstLine = output[0]
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))
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 [], [], [], [], []
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 [], [], [], [], []
78 log = output[2:filesSection - 1]
80 lines = output[filesSection + 2:differencesSection - 1]
82 changed = []
83 removed = []
85 for line in lines:
86 # chop off "... " and trailing newline
87 line = line[4:len(line) - 1]
89 lastSpace = line.rfind(" ")
90 if lastSpace == -1:
91 sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
92 continue
94 operation = line[lastSpace + 1:]
95 path = line[:lastSpace]
97 if operation == "delete":
98 removed.append(path)
99 else:
100 changed.append(path)
102 return author, log, epoch, changed, removed
104 def 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
115 def stripRevision(path):
116 hashPos = path.rindex("#")
117 return path[:hashPos]
119 def 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]
131 users[key] = name + " " + email
133 return users
135 users = getUserMap()
137 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
139 changes = []
140 for line in output:
141 changeNum = line.split(" ")[1]
142 changes.append(changeNum)
144 changes.reverse()
146 sys.stderr.write("\n")
148 tz = - time.timezone / 36
150 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
152 cnt = 1
153 for 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
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")
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):]
174 [mode, fileSize] = p4Stat(f)
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")
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))
188 gitStream.write("\n")
190 gitStream.close()
191 gitOutput.close()
192 gitError.close()
194 print ""