fix incremental imports
[fast-export/rorcz.git] / p4-fast-export.py
blob3e573cd7863a8b3dff67d3d3ae08b71e5ffb7eb1
1 #!/usr/bin/python
3 # p4-fast-export.py
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
8 # TODO:
9 # - support integrations (at least p4i)
10 # - support p4 submit (hah!)
12 import os, string, sys, time
13 import marshal, popen2
15 if 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)
25 branch = "refs/heads/p4"
26 prefix = sys.argv[1]
27 changeRange = ""
28 revision = ""
29 users = {}
30 initialParent = ""
32 if prefix.find("@") != -1:
33 atIdx = prefix.index("@")
34 changeRange = prefix[atIdx:]
35 if changeRange.find(",") == -1:
36 revision = changeRange
37 changeRange = ""
38 prefix = prefix[0:atIdx]
39 elif prefix.find("#") != -1:
40 hashIdx = prefix.index("#")
41 revision = prefix[hashIdx:]
42 prefix = prefix[0:hashIdx]
44 if prefix.endswith("..."):
45 prefix = prefix[:-3]
47 if not prefix.endswith("/"):
48 prefix += "/"
50 def p4CmdList(cmd):
51 pipe = os.popen("p4 -G %s" % cmd, "rb")
52 result = []
53 try:
54 while True:
55 entry = marshal.load(pipe)
56 result.append(entry)
57 except EOFError:
58 pass
59 pipe.close()
60 return result
62 def p4Cmd(cmd):
63 list = p4CmdList(cmd)
64 result = {}
65 for entry in list:
66 result.update(entry)
67 return result;
69 def commit(details):
70 global initialParent
71 global users
73 epoch = details["time"]
74 author = details["user"]
76 gitStream.write("commit %s\n" % branch)
77 committer = ""
78 if author in users:
79 committer = "%s %s %s" % (users[author], epoch, tz)
80 else:
81 committer = "%s <a@b> %s %s" % (author, epoch, tz)
83 gitStream.write("committer %s\n" % committer)
85 gitStream.write("data <<EOT\n")
86 gitStream.write(details["desc"])
87 gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
88 gitStream.write("EOT\n\n")
90 if len(initialParent) > 0:
91 gitStream.write("from %s\n" % initialParent)
92 initialParent = ""
94 fnum = 0
95 while details.has_key("depotFile%s" % fnum):
96 path = details["depotFile%s" % fnum]
97 if not path.startswith(prefix):
98 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
99 fnum = fnum + 1
100 continue
102 rev = details["rev%s" % fnum]
103 depotPath = path + "#" + rev
104 relPath = path[len(prefix):]
105 action = details["action%s" % fnum]
107 if action == "delete":
108 gitStream.write("D %s\n" % relPath)
109 else:
110 mode = 644
111 if details["type%s" % fnum].startswith("x"):
112 mode = 755
114 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
116 gitStream.write("M %s inline %s\n" % (mode, relPath))
117 gitStream.write("data %s\n" % len(data))
118 gitStream.write(data)
119 gitStream.write("\n")
121 fnum = fnum + 1
123 gitStream.write("\n")
125 gitStream.write("tag p4/%s\n" % details["change"])
126 gitStream.write("from %s\n" % branch);
127 gitStream.write("tagger %s\n" % committer);
128 gitStream.write("data 0\n\n")
131 def getUserMap():
132 users = {}
134 for output in p4CmdList("users"):
135 if not output.has_key("User"):
136 continue
137 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
138 return users
140 users = getUserMap()
142 if len(changeRange) == 0:
143 try:
144 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
145 output = sout.read()
146 tagIdx = output.index(" tags/p4/")
147 caretIdx = output.index("^")
148 rev = int(output[tagIdx + 9 : caretIdx]) + 1
149 changeRange = "@%s,#head" % rev
150 initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
151 except:
152 pass
154 sys.stderr.write("\n")
156 tz = - time.timezone / 36
157 tzsign = ("%s" % tz)[0]
158 if tzsign != '+' and tzsign != '-':
159 tz = "+" + ("%s" % tz)
161 if len(revision) > 0:
162 print "Doing initial import of %s from revision %s" % (prefix, revision)
164 details = { "user" : "git perforce import user", "time" : int(time.time()) }
165 details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
166 details["change"] = revision
167 newestRevision = 0
169 fileCnt = 0
170 for info in p4CmdList("files %s...%s" % (prefix, revision)):
171 change = info["change"]
172 if change > newestRevision:
173 newestRevision = change
175 if info["action"] == "delete":
176 continue
178 for prop in [ "depotFile", "rev", "action", "type" ]:
179 details["%s%s" % (prop, fileCnt)] = info[prop]
181 fileCnt = fileCnt + 1
183 details["change"] = newestRevision
185 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
186 try:
187 commit(details)
188 except:
189 print gitError.read()
191 gitStream.close()
192 gitOutput.close()
193 gitError.close()
194 else:
195 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
197 changes = []
198 for line in output:
199 changeNum = line.split(" ")[1]
200 changes.append(changeNum)
202 changes.reverse()
204 if len(changes) == 0:
205 print "no changes to import!"
206 sys.exit(1)
208 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
210 cnt = 1
211 for change in changes:
212 description = p4Cmd("describe %s" % change)
214 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
215 sys.stdout.flush()
216 cnt = cnt + 1
218 commit(description)
220 gitStream.close()
221 gitOutput.close()
222 gitError.close()
224 print ""