small cleanup
[fast-export/rorcz.git] / p4-fast-export.py
blob8455c1f41c4b0e65846e408904b0d8207d91d288
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 users = {}
29 initialParent = ""
31 try:
32 atIdx = prefix.index("@")
33 changeRange = prefix[atIdx:]
34 prefix = prefix[0:atIdx]
35 except ValueError:
36 changeRange = ""
38 if prefix.endswith("..."):
39 prefix = prefix[:-3]
41 if not prefix.endswith("/"):
42 prefix += "/"
44 def p4CmdList(cmd):
45 pipe = os.popen("p4 -G %s" % cmd, "rb")
46 result = []
47 try:
48 while True:
49 entry = marshal.load(pipe)
50 result.append(entry)
51 except EOFError:
52 pass
53 pipe.close()
54 return result
56 def p4Cmd(cmd):
57 list = p4CmdList(cmd)
58 result = {}
59 for entry in list:
60 result.update(entry)
61 return result;
63 def commit(details):
64 global initialParent
65 global users
67 epoch = details["time"]
68 author = details["user"]
70 gitStream.write("commit %s\n" % branch)
71 committer = ""
72 if author in users:
73 committer = "%s %s %s" % (users[author], epoch, tz)
74 else:
75 committer = "%s <a@b> %s %s" % (author, epoch, tz)
77 gitStream.write("committer %s\n" % committer)
79 gitStream.write("data <<EOT\n")
80 gitStream.write(details["desc"])
81 gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
82 gitStream.write("EOT\n\n")
84 if len(initialParent) > 0:
85 gitStream.write("merge %s\n" % initialParent)
86 initialParent = ""
88 fnum = 0
89 while details.has_key("depotFile%s" % fnum):
90 path = details["depotFile%s" % fnum]
91 if not path.startswith(prefix):
92 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
93 fnum = fnum + 1
94 continue
96 rev = details["rev%s" % fnum]
97 depotPath = path + "#" + rev
98 relPath = path[len(prefix):]
99 action = details["action%s" % fnum]
101 if action == "delete":
102 gitStream.write("D %s\n" % relPath)
103 else:
104 mode = 644
105 if details["type%s" % fnum].startswith("x"):
106 mode = 755
108 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
110 gitStream.write("M %s inline %s\n" % (mode, relPath))
111 gitStream.write("data %s\n" % len(data))
112 gitStream.write(data)
113 gitStream.write("\n")
115 fnum = fnum + 1
117 gitStream.write("\n")
119 gitStream.write("tag p4/%s\n" % change)
120 gitStream.write("from %s\n" % branch);
121 gitStream.write("tagger %s\n" % committer);
122 gitStream.write("data 0\n\n")
125 def getUserMap():
126 users = {}
128 for output in p4CmdList("users"):
129 if not output.has_key("User"):
130 continue
131 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
132 return users
134 users = getUserMap()
136 if len(changeRange) == 0:
137 try:
138 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
139 output = sout.read()
140 tagIdx = output.index(" tags/p4/")
141 caretIdx = output.index("^")
142 revision = int(output[tagIdx + 9 : caretIdx]) + 1
143 changeRange = "@%s,#head" % revision
144 initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
145 except:
146 pass
148 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
150 changes = []
151 for line in output:
152 changeNum = line.split(" ")[1]
153 changes.append(changeNum)
155 changes.reverse()
157 if len(changes) == 0:
158 print "no changes to import!"
159 sys.exit(1)
161 sys.stderr.write("\n")
163 tz = - time.timezone / 36
165 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
167 cnt = 1
168 for change in changes:
169 description = p4Cmd("describe %s" % change)
171 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
172 sys.stdout.flush()
173 cnt = cnt + 1
175 commit(description)
177 gitStream.close()
178 gitOutput.close()
179 gitError.close()
181 print ""