]>
git.ipfire.org Git - thirdparty/git.git/blob - contrib/fast-import/git-p4
3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # Copyright: 2007 Simon Hausmann <hausmann@kde.org>
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
11 import optparse
, sys
, os
, marshal
, popen2
, shelve
12 import tempfile
, getopt
, sha
, os
.path
, time
15 gitdir
= os
.environ
.get("GIT_DIR", "")
18 cmd
= "p4 -G %s" % cmd
19 pipe
= os
.popen(cmd
, "rb")
24 entry
= marshal
.load(pipe
)
40 sys
.stderr
.write(msg
+ "\n")
43 def currentGitBranch():
44 return os
.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
46 def isValidGitDir(path
):
47 if os
.path
.exists(path
+ "/HEAD") and os
.path
.exists(path
+ "/refs") and os
.path
.exists(path
+ "/objects"):
52 if os
.system(cmd
) != 0:
53 die("command failed: %s" % cmd
)
55 def extractLogMessageFromGitCommit(commit
):
58 for log
in os
.popen("git-cat-file commit %s" % commit
).readlines():
67 def extractDepotPathAndChangeFromGitLog(log
):
69 for line
in log
.split("\n"):
71 if line
.startswith("[git-p4:") and line
.endswith("]"):
72 line
= line
[8:-1].strip()
73 for assignment
in line
.split(":"):
74 variable
= assignment
.strip()
76 equalPos
= assignment
.find("=")
78 variable
= assignment
[:equalPos
].strip()
79 value
= assignment
[equalPos
+ 1:].strip()
80 if value
.startswith("\"") and value
.endswith("\""):
82 values
[variable
] = value
84 return values
.get("depot-path"), values
.get("change")
86 def gitBranchExists(branch
):
87 if os
.system("git-rev-parse %s 2>/dev/null >/dev/null" % branch
) == 0:
93 self
.usage
= "usage: %prog [options]"
95 class P4Debug(Command
):
97 Command
.__init
__(self
)
100 self
.description
= "A tool to debug the output of p4 -G."
103 for output
in p4CmdList(" ".join(args
)):
107 class P4CleanTags(Command
):
109 Command
.__init
__(self
)
111 # optparse.make_option("--branch", dest="branch", default="refs/heads/master")
113 self
.description
= "A tool to remove stale unused tags from incremental perforce imports."
115 branch
= currentGitBranch()
116 print "Cleaning out stale p4 import tags..."
117 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
120 tagIdx
= output
.index(" tags/p4/")
122 print "Cannot find any p4/* tag. Nothing to do."
126 caretIdx
= output
.index("^")
128 caretIdx
= len(output
) - 1
129 rev
= int(output
[tagIdx
+ 9 : caretIdx
])
131 allTags
= os
.popen("git tag -l p4/").readlines()
132 for i
in range(len(allTags
)):
133 allTags
[i
] = int(allTags
[i
][3:-1])
140 print os
.popen("git tag -d p4/%s" % rev
).read()
142 print "%s tags removed." % len(allTags
)
145 class P4Sync(Command
):
147 Command
.__init
__(self
)
149 optparse
.make_option("--continue", action
="store_false", dest
="firstTime"),
150 optparse
.make_option("--origin", dest
="origin"),
151 optparse
.make_option("--reset", action
="store_true", dest
="reset"),
152 optparse
.make_option("--master", dest
="master"),
153 optparse
.make_option("--log-substitutions", dest
="substFile"),
154 optparse
.make_option("--noninteractive", action
="store_false"),
155 optparse
.make_option("--dry-run", action
="store_true"),
156 optparse
.make_option("--apply-as-patch", action
="store_true", dest
="applyAsPatch")
158 self
.description
= "Submit changes from git to the perforce depot."
159 self
.firstTime
= True
161 self
.interactive
= True
164 self
.firstTime
= True
165 self
.origin
= "origin"
167 self
.applyAsPatch
= True
169 self
.logSubstitutions
= {}
170 self
.logSubstitutions
["<enter description here>"] = "%log%"
171 self
.logSubstitutions
["\tDetails:"] = "\tDetails: %log%"
174 if len(p4CmdList("opened ...")) > 0:
175 die("You have files opened with perforce! Close them before starting the sync.")
178 if len(self
.config
) > 0 and not self
.reset
:
179 die("Cannot start sync. Previous sync config found at %s" % self
.configFile
)
182 for line
in os
.popen("git-rev-list --no-merges %s..%s" % (self
.origin
, self
.master
)).readlines():
183 commits
.append(line
[:-1])
186 self
.config
["commits"] = commits
188 if not self
.applyAsPatch
:
189 print "Creating temporary p4-sync branch from %s ..." % self
.origin
190 system("git checkout -f -b p4-sync %s" % self
.origin
)
192 def prepareLogMessage(self
, template
, message
):
195 for line
in template
.split("\n"):
196 if line
.startswith("#"):
197 result
+= line
+ "\n"
201 for key
in self
.logSubstitutions
.keys():
202 if line
.find(key
) != -1:
203 value
= self
.logSubstitutions
[key
]
204 value
= value
.replace("%log%", message
)
205 if value
!= "@remove@":
206 result
+= line
.replace(key
, value
) + "\n"
211 result
+= line
+ "\n"
216 print "Applying %s" % (os
.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
217 diff
= os
.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
219 filesToDelete
= set()
222 path
= line
[1:].strip()
224 system("p4 edit %s" % path
)
225 elif modifier
== "A":
227 if path
in filesToDelete
:
228 filesToDelete
.remove(path
)
229 elif modifier
== "D":
230 filesToDelete
.add(path
)
231 if path
in filesToAdd
:
232 filesToAdd
.remove(path
)
234 die("unknown modifier %s for %s" % (modifier
, path
))
236 if self
.applyAsPatch
:
237 system("git-diff-tree -p --diff-filter=ACMRTUXB \"%s^\" \"%s\" | patch -p1" % (id, id))
239 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
240 system("git cherry-pick --no-commit \"%s\"" % id)
243 system("p4 add %s" % f
)
244 for f
in filesToDelete
:
245 system("p4 revert %s" % f
)
246 system("p4 delete %s" % f
)
248 logMessage
= extractLogMessageFromGitCommit(id)
249 logMessage
= logMessage
.replace("\n", "\n\t")
250 logMessage
= logMessage
[:-1]
252 template
= os
.popen("p4 change -o").read()
255 submitTemplate
= self
.prepareLogMessage(template
, logMessage
)
256 diff
= os
.popen("p4 diff -du ...").read()
258 for newFile
in filesToAdd
:
259 diff
+= "==== new file ====\n"
260 diff
+= "--- /dev/null\n"
261 diff
+= "+++ %s\n" % newFile
262 f
= open(newFile
, "r")
263 for line
in f
.readlines():
267 separatorLine
= "######## everything below this line is just the diff #######\n"
270 firstIteration
= True
271 while response
== "e":
272 if not firstIteration
:
273 response
= raw_input("Do you want to submit this change (y/e/n)? ")
274 firstIteration
= False
276 [handle
, fileName
] = tempfile
.mkstemp()
277 tmpFile
= os
.fdopen(handle
, "w+")
278 tmpFile
.write(submitTemplate
+ separatorLine
+ diff
)
280 editor
= os
.environ
.get("EDITOR", "vi")
281 system(editor
+ " " + fileName
)
282 tmpFile
= open(fileName
, "r")
283 message
= tmpFile
.read()
286 submitTemplate
= message
[:message
.index(separatorLine
)]
288 if response
== "y" or response
== "yes":
291 raw_input("Press return to continue...")
293 pipe
= os
.popen("p4 submit -i", "w")
294 pipe
.write(submitTemplate
)
297 print "Not submitting!"
298 self
.interactive
= False
300 fileName
= "submit.txt"
301 file = open(fileName
, "w+")
302 file.write(self
.prepareLogMessage(template
, logMessage
))
304 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName
, fileName
)
308 self
.firstTime
= True
310 if len(self
.substFile
) > 0:
311 for line
in open(self
.substFile
, "r").readlines():
312 tokens
= line
[:-1].split("=")
313 self
.logSubstitutions
[tokens
[0]] = tokens
[1]
315 if len(self
.master
) == 0:
316 self
.master
= currentGitBranch()
317 if len(self
.master
) == 0 or not os
.path
.exists("%s/refs/heads/%s" % (gitdir
, self
.master
)):
318 die("Detecting current git branch failed!")
321 self
.configFile
= gitdir
+ "/p4-git-sync.cfg"
322 self
.config
= shelve
.open(self
.configFile
, writeback
=True)
327 commits
= self
.config
.get("commits", [])
329 while len(commits
) > 0:
330 self
.firstTime
= False
332 commits
= commits
[1:]
333 self
.config
["commits"] = commits
335 if not self
.interactive
:
340 if len(commits
) == 0:
342 print "No changes found to apply between %s and current HEAD" % self
.origin
344 print "All changes applied!"
345 if not self
.applyAsPatch
:
346 print "Deleting temporary p4-sync branch and going back to %s" % self
.master
347 system("git checkout %s" % self
.master
)
348 system("git branch -D p4-sync")
349 print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert ..."
350 system("p4 edit ... >/dev/null")
351 system("p4 revert ... >/dev/null")
352 os
.remove(self
.configFile
)
356 class GitSync(Command
):
358 Command
.__init
__(self
)
360 optparse
.make_option("--branch", dest
="branch"),
361 optparse
.make_option("--detect-branches", dest
="detectBranches", action
="store_true"),
362 optparse
.make_option("--changesfile", dest
="changesFile"),
363 optparse
.make_option("--silent", dest
="silent", action
="store_true"),
364 optparse
.make_option("--known-branches", dest
="knownBranches"),
365 optparse
.make_option("--cache", dest
="doCache", action
="store_true"),
366 optparse
.make_option("--command-cache", dest
="commandCache", action
="store_true")
368 self
.description
= """Imports from Perforce into a git repository.\n
370 //depot/my/project/ -- to import the current head
371 //depot/my/project/@all -- to import everything
372 //depot/my/project/@1,6 -- to import only from revision 1 to 6
374 (a ... is not needed in the path p4 specification, it's added implicitly)"""
376 self
.usage
+= " //depot/path[@revRange]"
378 self
.dataCache
= False
379 self
.commandCache
= False
381 self
.knownBranches
= Set()
382 self
.createdBranches
= Set()
383 self
.committedChanges
= Set()
385 self
.detectBranches
= False
386 self
.changesFile
= ""
388 def p4File(self
, depotPath
):
389 return os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
391 def extractFilesFromCommit(self
, commit
):
394 while commit
.has_key("depotFile%s" % fnum
):
395 path
= commit
["depotFile%s" % fnum
]
396 if not path
.startswith(self
.globalPrefix
):
397 # if not self.silent:
398 # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.globalPrefix, change)
404 file["rev"] = commit
["rev%s" % fnum
]
405 file["action"] = commit
["action%s" % fnum
]
406 file["type"] = commit
["type%s" % fnum
]
411 def isSubPathOf(self
, first
, second
):
412 if not first
.startswith(second
):
416 return first
[len(second
)] == "/"
418 def branchesForCommit(self
, files
):
422 relativePath
= file["path"][len(self
.globalPrefix
):]
423 # strip off the filename
424 relativePath
= relativePath
[0:relativePath
.rfind("/")]
426 # if len(branches) == 0:
427 # branches.add(relativePath)
428 # knownBranches.add(relativePath)
431 ###### this needs more testing :)
433 for branch
in branches
:
434 if relativePath
== branch
:
437 # if relativePath.startswith(branch):
438 if self
.isSubPathOf(relativePath
, branch
):
441 # if branch.startswith(relativePath):
442 if self
.isSubPathOf(branch
, relativePath
):
443 branches
.remove(branch
)
449 for branch
in knownBranches
:
450 #if relativePath.startswith(branch):
451 if self
.isSubPathOf(relativePath
, branch
):
452 if len(branches
) == 0:
453 relativePath
= branch
461 branches
.add(relativePath
)
462 self
.knownBranches
.add(relativePath
)
466 def findBranchParent(self
, branchPrefix
, files
):
469 if not path
.startswith(branchPrefix
):
471 action
= file["action"]
472 if action
!= "integrate" and action
!= "branch":
475 depotPath
= path
+ "#" + rev
477 log
= p4CmdList("filelog \"%s\"" % depotPath
)
479 print "eek! I got confused by the filelog of %s" % depotPath
483 if log
["action0"] != action
:
484 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
487 branchAction
= log
["how0,0"]
488 # if branchAction == "branch into" or branchAction == "ignored":
489 # continue # ignore for branching
491 if not branchAction
.endswith(" from"):
492 continue # ignore for branching
493 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
496 source
= log
["file0,0"]
497 if source
.startswith(branchPrefix
):
500 lastSourceRev
= log
["erev0,0"]
502 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
503 if len(sourceLog
) != 1:
504 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
506 sourceLog
= sourceLog
[0]
508 relPath
= source
[len(self
.globalPrefix
):]
509 # strip off the filename
510 relPath
= relPath
[0:relPath
.rfind("/")]
512 for branch
in self
.knownBranches
:
513 if self
.isSubPathOf(relPath
, branch
):
514 # print "determined parent branch branch %s due to change in file %s" % (branch, source)
517 # print "%s is not a subpath of branch %s" % (relPath, branch)
521 def commit(self
, details
, files
, branch
, branchPrefix
, parent
= "", merged
= ""):
522 epoch
= details
["time"]
523 author
= details
["user"]
525 self
.gitStream
.write("commit %s\n" % branch
)
526 # gitStream.write("mark :%s\n" % details["change"])
527 self
.committedChanges
.add(int(details
["change"]))
529 if author
in self
.users
:
530 committer
= "%s %s %s" % (self
.users
[author
], epoch
, self
.tz
)
532 committer
= "%s <a@b> %s %s" % (author
, epoch
, self
.tz
)
534 self
.gitStream
.write("committer %s\n" % committer
)
536 self
.gitStream
.write("data <<EOT\n")
537 self
.gitStream
.write(details
["desc"])
538 self
.gitStream
.write("\n[git-p4: depot-path = \"%s\": change = %s]\n" % (branchPrefix
, details
["change"]))
539 self
.gitStream
.write("EOT\n\n")
542 self
.gitStream
.write("from %s\n" % parent
)
545 self
.gitStream
.write("merge %s\n" % merged
)
549 if not path
.startswith(branchPrefix
):
551 # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
554 depotPath
= path
+ "#" + rev
555 relPath
= path
[len(branchPrefix
):]
556 action
= file["action"]
558 if file["type"] == "apple":
559 print "\nfile %s is a strange apple file that forks. Ignoring!" % path
562 if action
== "delete":
563 self
.gitStream
.write("D %s\n" % relPath
)
566 if file["type"].startswith("x"):
569 data
= self
.p4File(depotPath
)
571 self
.gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
572 self
.gitStream
.write("data %s\n" % len(data
))
573 self
.gitStream
.write(data
)
574 self
.gitStream
.write("\n")
576 self
.gitStream
.write("\n")
578 self
.lastChange
= int(details
["change"])
580 def extractFilesInCommitToBranch(self
, files
, branchPrefix
):
585 if path
.startswith(branchPrefix
):
586 newFiles
.append(file)
590 def findBranchSourceHeuristic(self
, files
, branch
, branchPrefix
):
592 action
= file["action"]
593 if action
!= "integrate" and action
!= "branch":
597 depotPath
= path
+ "#" + rev
599 log
= p4CmdList("filelog \"%s\"" % depotPath
)
601 print "eek! I got confused by the filelog of %s" % depotPath
605 if log
["action0"] != action
:
606 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
609 branchAction
= log
["how0,0"]
611 if not branchAction
.endswith(" from"):
612 continue # ignore for branching
613 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
616 source
= log
["file0,0"]
617 if source
.startswith(branchPrefix
):
620 lastSourceRev
= log
["erev0,0"]
622 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
623 if len(sourceLog
) != 1:
624 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
626 sourceLog
= sourceLog
[0]
628 relPath
= source
[len(self
.globalPrefix
):]
629 # strip off the filename
630 relPath
= relPath
[0:relPath
.rfind("/")]
632 for candidate
in self
.knownBranches
:
633 if self
.isSubPathOf(relPath
, candidate
) and candidate
!= branch
:
638 def changeIsBranchMerge(self
, sourceBranch
, destinationBranch
, change
):
640 for file in p4CmdList("files %s...@%s" % (self
.globalPrefix
+ sourceBranch
+ "/", change
)):
641 if file["action"] == "delete":
643 sourceFiles
[file["depotFile"]] = file
645 destinationFiles
= {}
646 for file in p4CmdList("files %s...@%s" % (self
.globalPrefix
+ destinationBranch
+ "/", change
)):
647 destinationFiles
[file["depotFile"]] = file
649 for fileName
in sourceFiles
.keys():
653 for integration
in p4CmdList("integrated \"%s\"" % fileName
):
654 toFile
= integration
["fromFile"] # yes, it's true, it's fromFile
655 if not toFile
in destinationFiles
:
657 destFile
= destinationFiles
[toFile
]
658 if destFile
["action"] == "delete":
659 # print "file %s has been deleted in %s" % (fileName, toFile)
662 integrationCount
+= 1
663 if integration
["how"] == "branch from":
666 if int(integration
["change"]) == change
:
667 integrations
.append(integration
)
669 if int(integration
["change"]) > change
:
672 destRev
= int(destFile
["rev"])
674 startRev
= integration
["startFromRev"][1:]
675 if startRev
== "none":
678 startRev
= int(startRev
)
680 endRev
= integration
["endFromRev"][1:]
686 initialBranch
= (destRev
== 1 and integration
["how"] != "branch into")
687 inRange
= (destRev
>= startRev
and destRev
<= endRev
)
688 newer
= (destRev
> startRev
and destRev
> endRev
)
690 if initialBranch
or inRange
or newer
:
691 integrations
.append(integration
)
696 if len(integrations
) == 0 and integrationCount
> 1:
697 print "file %s was not integrated from %s into %s" % (fileName
, sourceBranch
, destinationBranch
)
702 def getUserMap(self
):
705 for output
in p4CmdList("users"):
706 if not output
.has_key("User"):
708 self
.users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
711 self
.globalPrefix
= ""
712 self
.changeRange
= ""
713 self
.initialParent
= ""
714 self
.tagLastChange
= True
716 if len(self
.branch
) == 0:
719 if not gitBranchExists(self
.branch
) and gitBranchExists("origin"):
721 print "Creating %s branch in git repository based on origin" % self
.branch
722 system("git branch %s origin" % self
.branch
)
724 [self
.previousDepotPath
, p4Change
] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self
.branch
))
725 if len(self
.previousDepotPath
) > 0 and len(p4Change
) > 0:
726 p4Change
= int(p4Change
) + 1
727 self
.globalPrefix
= self
.previousDepotPath
728 self
.changeRange
= "@%s,#head" % p4Change
729 self
.initialParent
= self
.branch
730 self
.tagLastChange
= False
732 print "Performing incremental import into %s git branch" % self
.branch
734 self
.branch
= "refs/heads/" + self
.branch
736 if len(self
.globalPrefix
) == 0:
737 self
.globalPrefix
= self
.previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
739 if len(self
.globalPrefix
) != 0:
740 self
.globalPrefix
= self
.globalPrefix
[:-1]
742 if len(args
) == 0 and len(self
.globalPrefix
) != 0:
744 print "Depot path: %s" % self
.globalPrefix
748 if len(self
.globalPrefix
) != 0 and self
.globalPrefix
!= args
[0]:
749 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self
.globalPrefix
, args
[0])
751 self
.globalPrefix
= args
[0]
758 if self
.globalPrefix
.find("@") != -1:
759 atIdx
= self
.globalPrefix
.index("@")
760 self
.changeRange
= self
.globalPrefix
[atIdx
:]
761 if self
.changeRange
== "@all":
762 self
.changeRange
= ""
763 elif self
.changeRange
.find(",") == -1:
764 self
.revision
= self
.changeRange
765 self
.changeRange
= ""
766 self
.globalPrefix
= self
.globalPrefix
[0:atIdx
]
767 elif self
.globalPrefix
.find("#") != -1:
768 hashIdx
= self
.globalPrefix
.index("#")
769 self
.revision
= self
.globalPrefix
[hashIdx
:]
770 self
.globalPrefix
= self
.globalPrefix
[0:hashIdx
]
771 elif len(self
.previousDepotPath
) == 0:
772 self
.revision
= "#head"
774 if self
.globalPrefix
.endswith("..."):
775 self
.globalPrefix
= self
.globalPrefix
[:-3]
777 if not self
.globalPrefix
.endswith("/"):
778 self
.globalPrefix
+= "/"
782 if len(self
.changeRange
) == 0:
784 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % self
.branch
)
786 if output
.endswith("\n"):
788 tagIdx
= output
.index(" tags/p4/")
789 caretIdx
= output
.find("^")
793 self
.rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
794 self
.changeRange
= "@%s,#head" % self
.rev
795 self
.initialParent
= os
.popen("git-rev-parse %s" % self
.branch
).read()[:-1]
796 self
.initialTag
= "p4/%s" % (int(self
.rev
) - 1)
800 self
.tz
= - time
.timezone
/ 36
801 tzsign
= ("%s" % self
.tz
)[0]
802 if tzsign
!= '+' and tzsign
!= '-':
803 self
.tz
= "+" + ("%s" % self
.tz
)
805 self
.gitOutput
, self
.gitStream
, self
.gitError
= popen2
.popen3("git-fast-import")
807 if len(self
.revision
) > 0:
808 print "Doing initial import of %s from revision %s" % (self
.globalPrefix
, self
.revision
)
810 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
811 details
["desc"] = "Initial import of %s from the state at revision %s" % (self
.globalPrefix
, self
.revision
)
812 details
["change"] = self
.revision
816 for info
in p4CmdList("files %s...%s" % (self
.globalPrefix
, self
.revision
)):
817 change
= int(info
["change"])
818 if change
> newestRevision
:
819 newestRevision
= change
821 if info
["action"] == "delete":
822 fileCnt
= fileCnt
+ 1
825 for prop
in [ "depotFile", "rev", "action", "type" ]:
826 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
828 fileCnt
= fileCnt
+ 1
830 details
["change"] = newestRevision
833 self
.commit(details
, self
.extractFilesFromCommit(details
), self
.branch
, self
.globalPrefix
)
835 print self
.gitError
.read()
840 if len(self
.changesFile
) > 0:
841 output
= open(self
.changesFile
).readlines()
844 changeSet
.add(int(line
))
846 for change
in changeSet
:
847 changes
.append(change
)
851 output
= os
.popen("p4 changes %s...%s" % (self
.globalPrefix
, self
.changeRange
)).readlines()
854 changeNum
= line
.split(" ")[1]
855 changes
.append(changeNum
)
859 if len(changes
) == 0:
861 print "no changes to import!"
865 for change
in changes
:
866 description
= p4Cmd("describe %s" % change
)
869 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
874 files
= self
.extractFilesFromCommit(description
)
875 if self
.detectBranches
:
876 for branch
in self
.branchesForCommit(files
):
877 self
.knownBranches
.add(branch
)
878 branchPrefix
= self
.globalPrefix
+ branch
+ "/"
880 filesForCommit
= self
.extractFilesInCommitToBranch(files
, branchPrefix
)
884 ########### remove cnt!!!
885 if branch
not in self
.createdBranches
and cnt
> 2:
886 self
.createdBranches
.add(branch
)
887 parent
= self
.findBranchParent(branchPrefix
, files
)
890 # elif len(parent) > 0:
891 # print "%s branched off of %s" % (branch, parent)
894 merged
= self
.findBranchSourceHeuristic(filesForCommit
, branch
, branchPrefix
)
896 print "change %s could be a merge from %s into %s" % (description
["change"], merged
, branch
)
897 if not self
.changeIsBranchMerge(merged
, branch
, int(description
["change"])):
900 branch
= "refs/heads/" + branch
902 parent
= "refs/heads/" + parent
904 merged
= "refs/heads/" + merged
905 self
.commit(description
, files
, branch
, branchPrefix
, parent
, merged
)
907 self
.commit(description
, files
, self
.branch
, self
.globalPrefix
, self
.initialParent
)
908 self
.initialParent
= ""
910 print self
.gitError
.read()
916 if self
.tagLastChange
:
917 self
.gitStream
.write("reset refs/tags/p4/%s\n" % self
.lastChange
)
918 self
.gitStream
.write("from %s\n\n" % self
.branch
);
921 self
.gitStream
.close()
922 self
.gitOutput
.close()
923 self
.gitError
.close()
925 os
.popen("git-repo-config p4.depotpath %s" % self
.globalPrefix
).read()
926 if len(self
.initialTag
) > 0:
927 os
.popen("git tag -d %s" % self
.initialTag
).read()
931 class HelpFormatter(optparse
.IndentedHelpFormatter
):
933 optparse
.IndentedHelpFormatter
.__init
__(self
)
935 def format_description(self
, description
):
937 return description
+ "\n"
941 def printUsage(commands
):
942 print "usage: %s <command> [options]" % sys
.argv
[0]
944 print "valid commands: %s" % ", ".join(commands
)
946 print "Try %s <command> --help for command specific help." % sys
.argv
[0]
951 "clean-tags" : P4CleanTags(),
956 if len(sys
.argv
[1:]) == 0:
957 printUsage(commands
.keys())
961 cmdName
= sys
.argv
[1]
963 cmd
= commands
[cmdName
]
965 print "unknown command %s" % cmdName
967 printUsage(commands
.keys())
970 options
= cmd
.options
972 options
.append(optparse
.make_option("--git-dir", dest
="gitdir"))
974 parser
= optparse
.OptionParser(cmd
.usage
.replace("%prog", "%prog " + cmdName
),
976 description
= cmd
.description
,
977 formatter
= HelpFormatter())
979 (cmd
, args
) = parser
.parse_args(sys
.argv
[2:], cmd
);
984 if not isValidGitDir(gitdir
):
985 cdup
= os
.popen("git-rev-parse --show-cdup").read()[:-1]
986 if isValidGitDir(cdup
+ "/" + gitdir
):
989 if not isValidGitDir(gitdir
):
990 if isValidGitDir(gitdir
+ "/.git"):
993 die("fatal: cannot locate git repository at %s" % gitdir
)
995 os
.environ
["GIT_DIR"] = gitdir
997 if not cmd
.run(args
):