@matheusbsbs / matheus-bagliano / commits / 4b97ffb1e4
Fork 0

Started rewriting the branch detection, based on "p4 branches" and "p4 branch -o foo".

Signed-off-by: Simon Hausmann <shausman@trolltech.com>

Simon Hausmann committed May 18, 2007 at 21:45 UTC 4b97ffb1e462c2334b38047fbf2b7e8b24c36aed
1 file changed +45 -242
contrib/fast-import/git-p4
+45 -242
@@ -414,9 +414,9 @@ class P4Sync(Command):
414 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
415 optparse.make_option("--changesfile", dest="changesFile"),
416 optparse.make_option("--silent", dest="silent", action="store_true"),
417 - optparse.make_option("--known-branches", dest="knownBranches"),
417 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
419 - optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true")
418 + optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true"),
419 + optparse.make_option("--verbose", dest="verbose", action="store_true")
420 ]
421 self.description = """Imports from Perforce into a git repository.\n
422 example:
@@ -429,7 +429,6 @@ class P4Sync(Command):
429 self.usage += " //depot/path[@revRange]"
430
431 self.silent = False
432 - self.knownBranches = Set()
432 self.createdBranches = Set()
433 self.committedChanges = Set()
434 self.branch = ""
@@ -437,6 +436,7 @@ class P4Sync(Command):
436 self.detectLabels = False
437 self.changesFile = ""
438 self.syncWithOrigin = False
439 + self.verbose = False
440
441 def p4File(self, depotPath):
442 return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
@@ -461,120 +461,25 @@ class P4Sync(Command):
461 fnum = fnum + 1
462 return files
463
464 - def isSubPathOf(self, first, second):
465 - if not first.startswith(second):
466 - return False
467 - if first == second:
468 - return True
469 - return first[len(second)] == "/"
470 -
464 def branchesForCommit(self, files):
465 branches = Set()
466
467 for file in files:
475 - relativePath = file["path"][len(self.depotPath):]
476 - # strip off the filename
477 - relativePath = relativePath[0:relativePath.rfind("/")]
478 -
479 - # if len(branches) == 0:
480 - # branches.add(relativePath)
481 - # knownBranches.add(relativePath)
482 - # continue
483 -
484 - ###### this needs more testing :)
485 - knownBranch = False
486 - for branch in branches:
487 - if relativePath == branch:
488 - knownBranch = True
489 - break
490 - # if relativePath.startswith(branch):
491 - if self.isSubPathOf(relativePath, branch):
492 - knownBranch = True
493 - break
494 - # if branch.startswith(relativePath):
495 - if self.isSubPathOf(branch, relativePath):
496 - branches.remove(branch)
497 - break
498 -
499 - if knownBranch:
500 - continue
501 -
502 - for branch in self.knownBranches:
503 - #if relativePath.startswith(branch):
504 - if self.isSubPathOf(relativePath, branch):
505 - if len(branches) == 0:
506 - relativePath = branch
507 - else:
508 - knownBranch = True
509 - break
468 + path = file["path"][len(self.depotPath):]
469
511 - if knownBranch:
512 - continue
513 -
514 - branches.add(relativePath)
515 - self.knownBranches.add(relativePath)
470 + for branch in self.knownBranches.keys():
471 + if path.startswith(branch):
472 + branches.add(branch)
473
474 return branches
475
519 - def findBranchParent(self, branchPrefix, files):
520 - for file in files:
521 - path = file["path"]
522 - if not path.startswith(branchPrefix):
523 - continue
524 - action = file["action"]
525 - if action != "integrate" and action != "branch":
526 - continue
527 - rev = file["rev"]
528 - depotPath = path + "#" + rev
529 -
530 - log = p4CmdList("filelog \"%s\"" % depotPath)
531 - if len(log) != 1:
532 - print "eek! I got confused by the filelog of %s" % depotPath
533 - sys.exit(1);
534 -
535 - log = log[0]
536 - if log["action0"] != action:
537 - print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
538 - sys.exit(1);
539 -
540 - branchAction = log["how0,0"]
541 - # if branchAction == "branch into" or branchAction == "ignored":
542 - # continue # ignore for branching
543 -
544 - if not branchAction.endswith(" from"):
545 - continue # ignore for branching
546 - # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
547 - # sys.exit(1);
548 -
549 - source = log["file0,0"]
550 - if source.startswith(branchPrefix):
551 - continue
552 -
553 - lastSourceRev = log["erev0,0"]
554 -
555 - sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
556 - if len(sourceLog) != 1:
557 - print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
558 - sys.exit(1);
559 - sourceLog = sourceLog[0]
560 -
561 - relPath = source[len(self.depotPath):]
562 - # strip off the filename
563 - relPath = relPath[0:relPath.rfind("/")]
564 -
565 - for branch in self.knownBranches:
566 - if self.isSubPathOf(relPath, branch):
567 - # print "determined parent branch branch %s due to change in file %s" % (branch, source)
568 - return branch
569 - # else:
570 - # print "%s is not a subpath of branch %s" % (relPath, branch)
571 -
572 - return ""
573 -
574 - def commit(self, details, files, branch, branchPrefix, parent = "", merged = ""):
476 + def commit(self, details, files, branch, branchPrefix, parent = ""):
477 epoch = details["time"]
478 author = details["user"]
479
480 + if self.verbose:
481 + print "commit into %s" % branch
482 +
483 self.gitStream.write("commit %s\n" % branch)
484 # gitStream.write("mark :%s\n" % details["change"])
485 self.committedChanges.add(int(details["change"]))
@@ -592,11 +497,10 @@ class P4Sync(Command):
497 self.gitStream.write("EOT\n\n")
498
499 if len(parent) > 0:
500 + if self.verbose:
501 + print "parent %s" % parent
502 self.gitStream.write("from %s\n" % parent)
503
597 - if len(merged) > 0:
598 - self.gitStream.write("merge %s\n" % merged)
599 -
504 for file in files:
505 path = file["path"]
506 if not path.startswith(branchPrefix):
@@ -680,118 +584,6 @@ class P4Sync(Command):
584
585 return newFiles
586
683 - def findBranchSourceHeuristic(self, files, branch, branchPrefix):
684 - for file in files:
685 - action = file["action"]
686 - if action != "integrate" and action != "branch":
687 - continue
688 - path = file["path"]
689 - rev = file["rev"]
690 - depotPath = path + "#" + rev
691 -
692 - log = p4CmdList("filelog \"%s\"" % depotPath)
693 - if len(log) != 1:
694 - print "eek! I got confused by the filelog of %s" % depotPath
695 - sys.exit(1);
696 -
697 - log = log[0]
698 - if log["action0"] != action:
699 - print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
700 - sys.exit(1);
701 -
702 - branchAction = log["how0,0"]
703 -
704 - if not branchAction.endswith(" from"):
705 - continue # ignore for branching
706 - # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
707 - # sys.exit(1);
708 -
709 - source = log["file0,0"]
710 - if source.startswith(branchPrefix):
711 - continue
712 -
713 - lastSourceRev = log["erev0,0"]
714 -
715 - sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
716 - if len(sourceLog) != 1:
717 - print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
718 - sys.exit(1);
719 - sourceLog = sourceLog[0]
720 -
721 - relPath = source[len(self.depotPath):]
722 - # strip off the filename
723 - relPath = relPath[0:relPath.rfind("/")]
724 -
725 - for candidate in self.knownBranches:
726 - if self.isSubPathOf(relPath, candidate) and candidate != branch:
727 - return candidate
728 -
729 - return ""
730 -
731 - def changeIsBranchMerge(self, sourceBranch, destinationBranch, change):
732 - sourceFiles = {}
733 - for file in p4CmdList("files %s...@%s" % (self.depotPath + sourceBranch + "/", change)):
734 - if file["action"] == "delete":
735 - continue
736 - sourceFiles[file["depotFile"]] = file
737 -
738 - destinationFiles = {}
739 - for file in p4CmdList("files %s...@%s" % (self.depotPath + destinationBranch + "/", change)):
740 - destinationFiles[file["depotFile"]] = file
741 -
742 - for fileName in sourceFiles.keys():
743 - integrations = []
744 - deleted = False
745 - integrationCount = 0
746 - for integration in p4CmdList("integrated \"%s\"" % fileName):
747 - toFile = integration["fromFile"] # yes, it's true, it's fromFile
748 - if not toFile in destinationFiles:
749 - continue
750 - destFile = destinationFiles[toFile]
751 - if destFile["action"] == "delete":
752 - # print "file %s has been deleted in %s" % (fileName, toFile)
753 - deleted = True
754 - break
755 - integrationCount += 1
756 - if integration["how"] == "branch from":
757 - continue
758 -
759 - if int(integration["change"]) == change:
760 - integrations.append(integration)
761 - continue
762 - if int(integration["change"]) > change:
763 - continue
764 -
765 - destRev = int(destFile["rev"])
766 -
767 - startRev = integration["startFromRev"][1:]
768 - if startRev == "none":
769 - startRev = 0
770 - else:
771 - startRev = int(startRev)
772 -
773 - endRev = integration["endFromRev"][1:]
774 - if endRev == "none":
775 - endRev = 0
776 - else:
777 - endRev = int(endRev)
778 -
779 - initialBranch = (destRev == 1 and integration["how"] != "branch into")
780 - inRange = (destRev >= startRev and destRev <= endRev)
781 - newer = (destRev > startRev and destRev > endRev)
782 -
783 - if initialBranch or inRange or newer:
784 - integrations.append(integration)
785 -
786 - if deleted:
787 - continue
788 -
789 - if len(integrations) == 0 and integrationCount > 1:
790 - print "file %s was not integrated from %s into %s" % (fileName, sourceBranch, destinationBranch)
791 - return False
792 -
793 - return True
794 -
587 def getUserMap(self):
588 self.users = {}
589
@@ -819,6 +611,27 @@ class P4Sync(Command):
611
612 self.labels[newestChange] = [output, revisions]
613
614 + def getBranchMapping(self):
615 + # map from branch depot path to parent branch
616 + self.knownBranches = {}
617 +
618 + for info in p4CmdList("branches"):
619 + details = p4Cmd("branch -o %s" % info["branch"])
620 + viewIdx = 0
621 + while details.has_key("View%s" % viewIdx):
622 + paths = details["View%s" % viewIdx].split(" ")
623 + viewIdx = viewIdx + 1
624 + # require standard //depot/foo/... //depot/bar/... mapping
625 + if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
626 + continue
627 + source = paths[0]
628 + destination = paths[1]
629 + if source.startswith(self.depotPath) and destination.startswith(self.depotPath):
630 + source = source[len(self.depotPath):-4]
631 + destination = destination[len(self.depotPath):-4]
632 + self.knownBranches[destination] = source
633 + self.knownBranches[source] = source
634 +
635 def run(self, args):
636 self.depotPath = ""
637 self.changeRange = ""
@@ -914,6 +727,9 @@ class P4Sync(Command):
727 if self.detectLabels:
728 self.getLabels();
729
730 + if self.detectBranches:
731 + self.getBranchMapping();
732 +
733 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
734
735 importProcess = subprocess.Popen(["git", "fast-import"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE);
@@ -993,35 +809,22 @@ class P4Sync(Command):
809 files = self.extractFilesFromCommit(description)
810 if self.detectBranches:
811 for branch in self.branchesForCommit(files):
996 - self.knownBranches.add(branch)
812 branchPrefix = self.depotPath + branch + "/"
813
999 - filesForCommit = self.extractFilesInCommitToBranch(files, branchPrefix)
1000 -
1001 - merged = ""
814 parent = ""
1003 - ########### remove cnt!!!
1004 - if branch not in self.createdBranches and cnt > 2:
815 +
816 + filesForCommit = self.extractFilesInCommitToBranch(files, branch)
817 +
818 + if branch not in self.createdBranches :
819 self.createdBranches.add(branch)
1006 - parent = self.findBranchParent(branchPrefix, files)
820 + parent = self.knownBranches[branch]
821 if parent == branch:
822 parent = ""
1009 - # elif len(parent) > 0:
1010 - # print "%s branched off of %s" % (branch, parent)
1011 -
1012 - if len(parent) == 0:
1013 - merged = self.findBranchSourceHeuristic(filesForCommit, branch, branchPrefix)
1014 - if len(merged) > 0:
1015 - print "change %s could be a merge from %s into %s" % (description["change"], merged, branch)
1016 - if not self.changeIsBranchMerge(merged, branch, int(description["change"])):
1017 - merged = ""
823
1019 - branch = "refs/heads/" + branch
824 + branch = "refs/remotes/p4/" + branch
825 if len(parent) > 0:
1021 - parent = "refs/heads/" + parent
1022 - if len(merged) > 0:
1023 - merged = "refs/heads/" + merged
1024 - self.commit(description, files, branch, branchPrefix, parent, merged)
826 + parent = "refs/remotes/p4/" + parent
827 + self.commit(description, files, branch, branchPrefix, parent)
828 else:
829 self.commit(description, files, self.branch, self.depotPath, self.initialParent)
830 self.initialParent = ""