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!)
11 # - emulate p4's delete behavior: if a directory becomes empty delete it. continue
12 # with parent dir until non-empty dir is found.
14 import os
, string
, sys
, time
, os
.path
15 import marshal
, popen2
, getopt
, sha
22 createdBranches
= Set()
23 committedChanges
= Set()
24 branch
= "refs/heads/master"
25 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
26 detectBranches
= False
28 if len(globalPrefix
) != 0:
29 globalPrefix
= globalPrefix
[:-1]
32 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches", "changesfile=", "silent", "known-branches=",
34 except getopt
.GetoptError
:
35 print "fixme, syntax error"
40 branch
= "refs/heads/" + a
41 elif o
== "--detect-branches":
43 elif o
== "--changesfile":
47 elif o
== "--known-branches":
48 for branch
in open(a
).readlines():
49 knownBranches
.add(branch
[:-1])
50 elif o
== "--cache-debug":
53 if len(args
) == 0 and len(globalPrefix
) != 0:
55 print "[using previously specified depot path %s]" % globalPrefix
57 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
59 print " %s //depot/my/project/ -- to import the current head"
60 print " %s //depot/my/project/@all -- to import everything"
61 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
63 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
67 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
68 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
70 globalPrefix
= args
[0]
79 if globalPrefix
.find("@") != -1:
80 atIdx
= globalPrefix
.index("@")
81 changeRange
= globalPrefix
[atIdx
:]
82 if changeRange
== "@all":
84 elif changeRange
.find(",") == -1:
85 revision
= changeRange
87 globalPrefix
= globalPrefix
[0:atIdx
]
88 elif globalPrefix
.find("#") != -1:
89 hashIdx
= globalPrefix
.index("#")
90 revision
= globalPrefix
[hashIdx
:]
91 globalPrefix
= globalPrefix
[0:hashIdx
]
92 elif len(previousDepotPath
) == 0:
95 if globalPrefix
.endswith("..."):
96 globalPrefix
= globalPrefix
[:-3]
98 if not globalPrefix
.endswith("/"):
101 def p4File(depotPath
):
102 cacheKey
= "/tmp/p4cache/data-" + sha
.new(depotPath
).hexdigest()
108 data
= open(cacheKey
, "rb").read()
110 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
112 open(cacheKey
, "wb").write(data
)
117 fullCmd
= "p4 -G %s" % cmd
;
119 cacheKey
= sha
.new(fullCmd
).hexdigest()
120 cacheKey
= "/tmp/p4cache/cmd-" + cacheKey
127 pipe
= open(cacheKey
, "rb")
130 pipe
= os
.popen(fullCmd
, "rb")
135 entry
= marshal
.load(pipe
)
141 if not cached
and cacheDebug
:
142 pipe
= open(cacheKey
, "wb")
144 marshal
.dump(r
, pipe
)
150 list = p4CmdList(cmd
)
156 def extractFilesFromCommit(commit
):
159 while commit
.has_key("depotFile%s" % fnum
):
160 path
= commit
["depotFile%s" % fnum
]
161 if not path
.startswith(globalPrefix
):
163 # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
169 file["rev"] = commit
["rev%s" % fnum
]
170 file["action"] = commit
["action%s" % fnum
]
171 file["type"] = commit
["type%s" % fnum
]
176 def isSubPathOf(first
, second
):
177 if not first
.startswith(second
):
181 return first
[len(second
)] == "/"
183 def branchesForCommit(files
):
188 relativePath
= file["path"][len(globalPrefix
):]
189 # strip off the filename
190 relativePath
= relativePath
[0:relativePath
.rfind("/")]
192 # if len(branches) == 0:
193 # branches.add(relativePath)
194 # knownBranches.add(relativePath)
197 ###### this needs more testing :)
199 for branch
in branches
:
200 if relativePath
== branch
:
203 # if relativePath.startswith(branch):
204 if isSubPathOf(relativePath
, branch
):
207 # if branch.startswith(relativePath):
208 if isSubPathOf(branch
, relativePath
):
209 branches
.remove(branch
)
215 for branch
in knownBranches
:
216 #if relativePath.startswith(branch):
217 if isSubPathOf(relativePath
, branch
):
218 if len(branches
) == 0:
219 relativePath
= branch
227 branches
.add(relativePath
)
228 knownBranches
.add(relativePath
)
232 def findBranchParent(branchPrefix
, files
):
235 if not path
.startswith(branchPrefix
):
237 action
= file["action"]
238 if action
!= "integrate" and action
!= "branch":
241 depotPath
= path
+ "#" + rev
243 log
= p4CmdList("filelog \"%s\"" % depotPath
)
245 print "eek! I got confused by the filelog of %s" % depotPath
249 if log
["action0"] != action
:
250 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
253 branchAction
= log
["how0,0"]
254 # if branchAction == "branch into" or branchAction == "ignored":
255 # continue # ignore for branching
257 if not branchAction
.endswith(" from"):
258 continue # ignore for branching
259 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
262 source
= log
["file0,0"]
263 if source
.startswith(branchPrefix
):
266 lastSourceRev
= log
["erev0,0"]
268 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
269 if len(sourceLog
) != 1:
270 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
272 sourceLog
= sourceLog
[0]
274 relPath
= source
[len(globalPrefix
):]
275 # strip off the filename
276 relPath
= relPath
[0:relPath
.rfind("/")]
278 for branch
in knownBranches
:
279 if isSubPathOf(relPath
, branch
):
280 # print "determined parent branch branch %s due to change in file %s" % (branch, source)
283 # print "%s is not a subpath of branch %s" % (relPath, branch)
287 def commit(details
, files
, branch
, branchPrefix
, parent
, merged
= ""):
290 global committedChanges
292 epoch
= details
["time"]
293 author
= details
["user"]
295 gitStream
.write("commit %s\n" % branch
)
296 # gitStream.write("mark :%s\n" % details["change"])
297 committedChanges
.add(int(details
["change"]))
300 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
302 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
304 gitStream
.write("committer %s\n" % committer
)
306 gitStream
.write("data <<EOT\n")
307 gitStream
.write(details
["desc"])
308 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
309 gitStream
.write("EOT\n\n")
312 gitStream
.write("from %s\n" % parent
)
315 gitStream
.write("merge %s\n" % merged
)
319 if not path
.startswith(branchPrefix
):
321 # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
324 depotPath
= path
+ "#" + rev
325 relPath
= path
[len(branchPrefix
):]
326 action
= file["action"]
328 if action
== "delete":
329 gitStream
.write("D %s\n" % relPath
)
332 if file["type"].startswith("x"):
335 data
= p4File(depotPath
)
337 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
338 gitStream
.write("data %s\n" % len(data
))
339 gitStream
.write(data
)
340 gitStream
.write("\n")
342 gitStream
.write("\n")
344 lastChange
= int(details
["change"])
346 def extractFilesInCommitToBranch(files
, branchPrefix
):
351 if path
.startswith(branchPrefix
):
352 newFiles
.append(file)
356 def findBranchSourceHeuristic(files
, branch
, branchPrefix
):
358 action
= file["action"]
359 if action
!= "integrate" and action
!= "branch":
363 depotPath
= path
+ "#" + rev
365 log
= p4CmdList("filelog \"%s\"" % depotPath
)
367 print "eek! I got confused by the filelog of %s" % depotPath
371 if log
["action0"] != action
:
372 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
375 branchAction
= log
["how0,0"]
377 if not branchAction
.endswith(" from"):
378 continue # ignore for branching
379 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
382 source
= log
["file0,0"]
383 if source
.startswith(branchPrefix
):
386 lastSourceRev
= log
["erev0,0"]
388 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
389 if len(sourceLog
) != 1:
390 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
392 sourceLog
= sourceLog
[0]
394 relPath
= source
[len(globalPrefix
):]
395 # strip off the filename
396 relPath
= relPath
[0:relPath
.rfind("/")]
398 for candidate
in knownBranches
:
399 if isSubPathOf(relPath
, candidate
) and candidate
!= branch
:
404 def changeIsBranchMerge(sourceBranch
, destinationBranch
, change
):
406 for file in p4CmdList("files %s...@%s" % (globalPrefix
+ sourceBranch
+ "/", change
)):
407 if file["action"] == "delete":
409 sourceFiles
[file["depotFile"]] = file
411 destinationFiles
= {}
412 for file in p4CmdList("files %s...@%s" % (globalPrefix
+ destinationBranch
+ "/", change
)):
413 destinationFiles
[file["depotFile"]] = file
415 for fileName
in sourceFiles
.keys():
419 for integration
in p4CmdList("integrated \"%s\"" % fileName
):
420 toFile
= integration
["fromFile"] # yes, it's true, it's fromFile
421 if not toFile
in destinationFiles
:
423 destFile
= destinationFiles
[toFile
]
424 if destFile
["action"] == "delete":
425 # print "file %s has been deleted in %s" % (fileName, toFile)
428 integrationCount
+= 1
429 if integration
["how"] == "branch from":
432 if int(integration
["change"]) == change
:
433 integrations
.append(integration
)
435 if int(integration
["change"]) > change
:
438 destRev
= int(destFile
["rev"])
440 startRev
= integration
["startFromRev"][1:]
441 if startRev
== "none":
444 startRev
= int(startRev
)
446 endRev
= integration
["endFromRev"][1:]
452 initialBranch
= (destRev
== 1 and integration
["how"] != "branch into")
453 inRange
= (destRev
>= startRev
and destRev
<= endRev
)
454 newer
= (destRev
> startRev
and destRev
> endRev
)
456 if initialBranch
or inRange
or newer
:
457 integrations
.append(integration
)
462 if len(integrations
) == 0 and integrationCount
> 1:
463 print "file %s was not integrated from %s into %s" % (fileName
, sourceBranch
, destinationBranch
)
471 for output
in p4CmdList("users"):
472 if not output
.has_key("User"):
474 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
479 if len(changeRange
) == 0:
481 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
483 if output
.endswith("\n"):
485 tagIdx
= output
.index(" tags/p4/")
486 caretIdx
= output
.find("^")
490 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
491 changeRange
= "@%s,#head" % rev
492 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
493 initialTag
= "p4/%s" % (int(rev
) - 1)
497 tz
= - time
.timezone
/ 36
498 tzsign
= ("%s" % tz
)[0]
499 if tzsign
!= '+' and tzsign
!= '-':
500 tz
= "+" + ("%s" % tz
)
502 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
504 if len(revision
) > 0:
505 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
507 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
508 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
509 details
["change"] = revision
513 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
514 change
= int(info
["change"])
515 if change
> newestRevision
:
516 newestRevision
= change
518 if info
["action"] == "delete":
521 for prop
in [ "depotFile", "rev", "action", "type" ]:
522 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
524 fileCnt
= fileCnt
+ 1
526 details
["change"] = newestRevision
529 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
531 print gitError
.read()
536 if len(changesFile
) > 0:
537 output
= open(changesFile
).readlines()
540 changeSet
.add(int(line
))
542 for change
in changeSet
:
543 changes
.append(change
)
547 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
550 changeNum
= line
.split(" ")[1]
551 changes
.append(changeNum
)
555 if len(changes
) == 0:
557 print "no changes to import!"
561 for change
in changes
:
562 description
= p4Cmd("describe %s" % change
)
565 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
570 files
= extractFilesFromCommit(description
)
572 for branch
in branchesForCommit(files
):
573 knownBranches
.add(branch
)
574 branchPrefix
= globalPrefix
+ branch
+ "/"
576 filesForCommit
= extractFilesInCommitToBranch(files
, branchPrefix
)
580 ########### remove cnt!!!
581 if branch
not in createdBranches
and cnt
> 2:
582 createdBranches
.add(branch
)
583 parent
= findBranchParent(branchPrefix
, files
)
586 # elif len(parent) > 0:
587 # print "%s branched off of %s" % (branch, parent)
590 merged
= findBranchSourceHeuristic(filesForCommit
, branch
, branchPrefix
)
592 print "change %s could be a merge from %s into %s" % (description
["change"], merged
, branch
)
593 if not changeIsBranchMerge(merged
, branch
, int(description
["change"])):
596 branch
= "refs/heads/" + branch
598 parent
= "refs/heads/" + parent
600 merged
= "refs/heads/" + merged
601 commit(description
, files
, branch
, branchPrefix
, parent
, merged
)
603 commit(description
, filesForCommit
, branch
, globalPrefix
, initialParent
)
606 print gitError
.read()
612 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
613 gitStream
.write("from %s\n\n" % branch
);
620 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
621 if len(initialTag
) > 0:
622 os
.popen("git tag -d %s" % initialTag
).read()