5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 # - support integrations (at least p4i)
10 # - support p4 submit (hah!)
12 import os
, string
, sys
, time
13 import marshal
, popen2
, getopt
16 branch
= "refs/heads/master"
17 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
18 detectBranches
= False
19 if len(globalPrefix
) != 0:
20 globalPrefix
= globalPrefix
[:-1]
23 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches" ])
24 except getopt
.GetoptError
:
25 print "fixme, syntax error"
30 branch
= "refs/heads/" + a
31 elif o
== "--detect-branches":
34 if len(args
) == 0 and len(globalPrefix
) != 0:
35 print "[using previously specified depot path %s]" % globalPrefix
37 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
39 print " %s //depot/my/project/ -- to import the current head"
40 print " %s //depot/my/project/@all -- to import everything"
41 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
43 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
47 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
48 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
50 globalPrefix
= args
[0]
59 if globalPrefix
.find("@") != -1:
60 atIdx
= globalPrefix
.index("@")
61 changeRange
= globalPrefix
[atIdx
:]
62 if changeRange
== "@all":
64 elif changeRange
.find(",") == -1:
65 revision
= changeRange
67 globalPrefix
= globalPrefix
[0:atIdx
]
68 elif globalPrefix
.find("#") != -1:
69 hashIdx
= globalPrefix
.index("#")
70 revision
= globalPrefix
[hashIdx
:]
71 globalPrefix
= globalPrefix
[0:hashIdx
]
72 elif len(previousDepotPath
) == 0:
75 if globalPrefix
.endswith("..."):
76 globalPrefix
= globalPrefix
[:-3]
78 if not globalPrefix
.endswith("/"):
82 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
86 entry
= marshal
.load(pipe
)
100 def extractFilesFromCommit(commit
):
103 while commit
.has_key("depotFile%s" % fnum
):
104 path
= commit
["depotFile%s" % fnum
]
105 if not path
.startswith(globalPrefix
):
106 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, globalPrefix
, change
)
111 file["rev"] = commit
["rev%s" % fnum
]
112 file["action"] = commit
["action%s" % fnum
]
113 file["type"] = commit
["type%s" % fnum
]
118 def branchesForCommit(files
):
122 relativePath
= file["path"][len(globalPrefix
):]
123 # strip off the filename
124 relativePath
= relativePath
[0:relativePath
.rfind("/")]
126 if len(branches
) == 0:
127 branches
.add(relativePath
)
130 ###### this needs more testing :)
132 for branch
in branches
:
133 if relativePath
== branch
:
136 if relativePath
.startswith(branch
):
139 if branch
.startswith(relativePath
):
140 branches
.remove(branch
)
144 branches
.add(relativePath
)
148 def commit(details
, files
, branch
, branchPrefix
):
153 epoch
= details
["time"]
154 author
= details
["user"]
156 gitStream
.write("commit %s\n" % branch
)
157 gitStream
.write("mark :%s\n" % details
["change"])
160 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
162 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
164 gitStream
.write("committer %s\n" % committer
)
166 gitStream
.write("data <<EOT\n")
167 gitStream
.write(details
["desc"])
168 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
169 gitStream
.write("EOT\n\n")
171 if len(initialParent
) > 0:
172 gitStream
.write("from %s\n" % initialParent
)
175 #mergedBranches = set()
180 if not path
.startswith(branchPrefix
):
182 action
= file["action"]
183 if action
!= "integrate" and action
!= "branch":
186 depotPath
= path
+ "#" + rev
188 log
= p4CmdList("filelog \"%s\"" % depotPath
)
190 print "eek! I got confused by the filelog of %s" % depotPath
194 if log
["action0"] != action
:
195 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
198 if not log
["how0,0"].endswith(" from"):
199 print "eek! file %s was not branched but instead: %s" % (depotPath
, log
["how0,0"])
202 source
= log
["file0,0"]
203 if source
.startswith(branchPrefix
):
206 lastSourceRev
= log
["erev0,0"]
208 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
209 if len(sourceLog
) != 1:
210 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
212 sourceLog
= sourceLog
[0]
214 change
= int(sourceLog
["change0"])
218 # relPath = source[len(globalPrefix):]
220 # for branch in knownBranches:
221 # if relPath.startswith(branch) and branch not in mergedBranches:
222 # gitStream.write("merge refs/heads/%s\n" % branch)
223 # mergedBranches.add(branch)
227 gitStream
.write("merge :%s\n" % merge
)
231 if not path
.startswith(branchPrefix
):
232 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path
, branchPrefix
, change
)
235 depotPath
= path
+ "#" + rev
236 relPath
= path
[len(branchPrefix
):]
237 action
= file["action"]
239 if action
== "delete":
240 gitStream
.write("D %s\n" % relPath
)
243 if file["type"].startswith("x"):
246 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
248 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
249 gitStream
.write("data %s\n" % len(data
))
250 gitStream
.write(data
)
251 gitStream
.write("\n")
253 gitStream
.write("\n")
255 lastChange
= details
["change"]
260 for output
in p4CmdList("users"):
261 if not output
.has_key("User"):
263 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
268 if len(changeRange
) == 0:
270 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
272 if output
.endswith("\n"):
274 tagIdx
= output
.index(" tags/p4/")
275 caretIdx
= output
.find("^")
279 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
280 changeRange
= "@%s,#head" % rev
281 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
282 initialTag
= "p4/%s" % (int(rev
) - 1)
286 sys
.stderr
.write("\n")
288 tz
= - time
.timezone
/ 36
289 tzsign
= ("%s" % tz
)[0]
290 if tzsign
!= '+' and tzsign
!= '-':
291 tz
= "+" + ("%s" % tz
)
293 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
295 if len(revision
) > 0:
296 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
298 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
299 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
300 details
["change"] = revision
304 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
305 change
= int(info
["change"])
306 if change
> newestRevision
:
307 newestRevision
= change
309 if info
["action"] == "delete":
312 for prop
in [ "depotFile", "rev", "action", "type" ]:
313 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
315 fileCnt
= fileCnt
+ 1
317 details
["change"] = newestRevision
320 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
322 print gitError
.read()
325 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
329 changeNum
= line
.split(" ")[1]
330 changes
.append(changeNum
)
334 if len(changes
) == 0:
335 print "no changes to import!"
339 for change
in changes
:
340 description
= p4Cmd("describe %s" % change
)
342 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
347 files
= extractFilesFromCommit(description
)
349 for branch
in branchesForCommit(files
):
350 knownBranches
.add(branch
)
351 branchPrefix
= globalPrefix
+ branch
+ "/"
352 branch
= "refs/heads/" + branch
353 commit(description
, files
, branch
, branchPrefix
)
355 commit(description
, files
, branch
, globalPrefix
)
357 print gitError
.read()
362 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
363 gitStream
.write("from %s\n\n" % branch
);
370 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
371 if len(initialTag
) > 0:
372 os
.popen("git tag -d %s" % initialTag
).read()