Cleanups, remove unused variable.
[fast-export.git] / contrib / fast-import / p4-fast-export.py
blob61b9e67e54b4bdb3808ed8c02eb668a0cb0db1cf
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, getopt
15 branch = "refs/heads/master"
16 prefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
17 if len(prefix) != 0:
18 prefix = prefix[:-1]
20 try:
21 opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ])
22 except getopt.GetoptError:
23 print "fixme, syntax error"
24 sys.exit(1)
26 for o, a in opts:
27 if o == "--branch":
28 branch = "refs/heads/" + a
30 if len(args) == 0 and len(prefix) != 0:
31 print "[using previously specified depot path %s]" % prefix
32 elif len(args) != 1:
33 print "usage: %s //depot/path[@revRange]" % sys.argv[0]
34 print "\n example:"
35 print " %s //depot/my/project/ -- to import the current head"
36 print " %s //depot/my/project/@all -- to import everything"
37 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
38 print ""
39 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
40 print ""
41 sys.exit(1)
42 else:
43 if len(prefix) != 0 and prefix != args[0]:
44 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix, args[0])
45 sys.exit(1)
46 prefix = args[0]
48 changeRange = ""
49 revision = ""
50 users = {}
51 initialParent = ""
52 lastChange = ""
53 initialTag = ""
55 if prefix.find("@") != -1:
56 atIdx = prefix.index("@")
57 changeRange = prefix[atIdx:]
58 if changeRange == "@all":
59 changeRange = ""
60 elif changeRange.find(",") == -1:
61 revision = changeRange
62 changeRange = ""
63 prefix = prefix[0:atIdx]
64 elif prefix.find("#") != -1:
65 hashIdx = prefix.index("#")
66 revision = prefix[hashIdx:]
67 prefix = prefix[0:hashIdx]
68 elif len(previousDepotPath) == 0:
69 revision = "#head"
71 if prefix.endswith("..."):
72 prefix = prefix[:-3]
74 if not prefix.endswith("/"):
75 prefix += "/"
77 def p4CmdList(cmd):
78 pipe = os.popen("p4 -G %s" % cmd, "rb")
79 result = []
80 try:
81 while True:
82 entry = marshal.load(pipe)
83 result.append(entry)
84 except EOFError:
85 pass
86 pipe.close()
87 return result
89 def p4Cmd(cmd):
90 list = p4CmdList(cmd)
91 result = {}
92 for entry in list:
93 result.update(entry)
94 return result;
96 def commit(details):
97 global initialParent
98 global users
99 global lastChange
101 epoch = details["time"]
102 author = details["user"]
104 gitStream.write("commit %s\n" % branch)
105 committer = ""
106 if author in users:
107 committer = "%s %s %s" % (users[author], epoch, tz)
108 else:
109 committer = "%s <a@b> %s %s" % (author, epoch, tz)
111 gitStream.write("committer %s\n" % committer)
113 gitStream.write("data <<EOT\n")
114 gitStream.write(details["desc"])
115 gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
116 gitStream.write("EOT\n\n")
118 if len(initialParent) > 0:
119 gitStream.write("from %s\n" % initialParent)
120 initialParent = ""
122 fnum = 0
123 while details.has_key("depotFile%s" % fnum):
124 path = details["depotFile%s" % fnum]
125 if not path.startswith(prefix):
126 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
127 fnum = fnum + 1
128 continue
130 rev = details["rev%s" % fnum]
131 depotPath = path + "#" + rev
132 relPath = path[len(prefix):]
133 action = details["action%s" % fnum]
135 if action == "delete":
136 gitStream.write("D %s\n" % relPath)
137 else:
138 mode = 644
139 if details["type%s" % fnum].startswith("x"):
140 mode = 755
142 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
144 gitStream.write("M %s inline %s\n" % (mode, relPath))
145 gitStream.write("data %s\n" % len(data))
146 gitStream.write(data)
147 gitStream.write("\n")
149 fnum = fnum + 1
151 gitStream.write("\n")
153 lastChange = details["change"]
155 def getUserMap():
156 users = {}
158 for output in p4CmdList("users"):
159 if not output.has_key("User"):
160 continue
161 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
162 return users
164 users = getUserMap()
166 if len(changeRange) == 0:
167 try:
168 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
169 output = sout.read()
170 if output.endswith("\n"):
171 output = output[:-1]
172 tagIdx = output.index(" tags/p4/")
173 caretIdx = output.find("^")
174 endPos = len(output)
175 if caretIdx != -1:
176 endPos = caretIdx
177 rev = int(output[tagIdx + 9 : endPos]) + 1
178 changeRange = "@%s,#head" % rev
179 initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
180 initialTag = "p4/%s" % (int(rev) - 1)
181 except:
182 pass
184 sys.stderr.write("\n")
186 tz = - time.timezone / 36
187 tzsign = ("%s" % tz)[0]
188 if tzsign != '+' and tzsign != '-':
189 tz = "+" + ("%s" % tz)
191 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
193 if len(revision) > 0:
194 print "Doing initial import of %s from revision %s" % (prefix, revision)
196 details = { "user" : "git perforce import user", "time" : int(time.time()) }
197 details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
198 details["change"] = revision
199 newestRevision = 0
201 fileCnt = 0
202 for info in p4CmdList("files %s...%s" % (prefix, revision)):
203 change = int(info["change"])
204 if change > newestRevision:
205 newestRevision = change
207 if info["action"] == "delete":
208 continue
210 for prop in [ "depotFile", "rev", "action", "type" ]:
211 details["%s%s" % (prop, fileCnt)] = info[prop]
213 fileCnt = fileCnt + 1
215 details["change"] = newestRevision
217 try:
218 commit(details)
219 except:
220 print gitError.read()
222 else:
223 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
225 changes = []
226 for line in output:
227 changeNum = line.split(" ")[1]
228 changes.append(changeNum)
230 changes.reverse()
232 if len(changes) == 0:
233 print "no changes to import!"
234 sys.exit(1)
236 cnt = 1
237 for change in changes:
238 description = p4Cmd("describe %s" % change)
240 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
241 sys.stdout.flush()
242 cnt = cnt + 1
244 try:
245 commit(description)
246 except:
247 print gitError.read()
248 sys.exit(1)
250 print ""
252 gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
253 gitStream.write("from %s\n\n" % branch);
256 gitStream.close()
257 gitOutput.close()
258 gitError.close()
260 os.popen("git-repo-config p4.depotpath %s" % prefix).read()
261 if len(initialTag) > 0:
262 os.popen("git tag -d %s" % initialTag).read()
264 sys.exit(0)