3ccef526eba4614e67ddaa72b0d551f39aec0cc0
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 describeOutput = p4Cmd("describe %s" % change)
55
56 author = describeOutput["user"]
57 epoch = describeOutput["time"]
58
59 log = describeOutput["desc"]
60
61 changed = []
62 removed = []
63
64 i = 0
65 while describeOutput.has_key("depotFile%s" % i):
66 path = describeOutput["depotFile%s" % i]
67 rev = describeOutput["rev%s" % i]
68 action = describeOutput["action%s" % i]
69 path = path + "#" + rev
70
71 if action == "delete":
72 removed.append(path)
73 else:
74 changed.append(path)
75
76 i = i + 1
77
78 return author, log, epoch, changed, removed
79
80def p4Stat(path):
81 output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
82 fileSize = 0
83 mode = 644
84 for line in output:
85 if line.startswith("... headType x"):
86 mode = 755
87 elif line.startswith("... fileSize "):
88 fileSize = long(line[12:])
89 return mode, fileSize
90
91def stripRevision(path):
92 hashPos = path.rindex("#")
93 return path[:hashPos]
94
95def getUserMap():
96 users = {}
97 output = os.popen("p4 users")
98 for line in output:
99 firstSpace = line.index(" ")
100 secondSpace = line.index(" ", firstSpace + 1)
101 key = line[:firstSpace]
102 email = line[firstSpace + 1:secondSpace]
103 openParenPos = line.index("(", secondSpace)
104 closedParenPos = line.index(")", openParenPos)
105 name = line[openParenPos + 1:closedParenPos]
106
107 users[key] = name + " " + email
108
109 return users
110
111users = getUserMap()
112
113output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
114
115changes = []
116for line in output:
117 changeNum = line.split(" ")[1]
118 changes.append(changeNum)
119
120changes.reverse()
121
122sys.stderr.write("\n")
123
124tz = - time.timezone / 36
125
126gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
127
128cnt = 1
129for change in changes:
130 [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
131 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
132 cnt = cnt + 1
133
134 gitStream.write("commit refs/heads/master\n")
135 if author in users:
136 gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
137 else:
138 gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
139 gitStream.write("data <<EOT\n")
140 gitStream.write(log)
141 gitStream.write("EOT\n\n")
142
143 for f in changedFiles:
144 if not f.startswith(prefix):
145 sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
146 continue
147 relpath = f[len(prefix):]
148
149 [mode, fileSize] = p4Stat(f)
150
151 gitStream.write("M %s inline %s\n" % (mode, stripRevision(relpath)))
152 gitStream.write("data %s\n" % fileSize)
153 gitStream.write(os.popen("p4 print -q \"%s\"" % f).read())
154 gitStream.write("\n")
155
156 for f in removedFiles:
157 if not f.startswith(prefix):
158 sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
159 continue
160 relpath = f[len(prefix):]
161 gitStream.write("D %s\n" % stripRevision(relpath))
162
163 gitStream.write("\n")
164
165gitStream.close()
166gitOutput.close()
167gitError.close()
168
169print ""