More work on the incremental importing of multiple branches. Improved error detection by checking the exit code of git-fast-import.
More work on the incremental importing of multiple branches. Improved error detection by checking the exit code of git-fast-import. Signed-off-by: Simon Hausmann <shausman@trolltech.com>
Simon Hausmann committed
May 19, 2007 at 10:23 UTC
29bdbac1cd5fc4126b62c9a030403d56ae43c204
1 file changed
+76
-14
contrib/fast-import/git-p4
+76
-14
@@ -612,8 +612,7 @@ class P4Sync(Command):
612
self.labels[newestChange] = [output, revisions]
613
614
def getBranchMapping(self):
615
- # map from branch depot path to parent branch
616
- self.knownBranches = {}
615
+ self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:]
616
617
for info in p4CmdList("branches"):
618
details = p4Cmd("branch -o %s" % info["branch"])
@@ -629,16 +628,34 @@ class P4Sync(Command):
628
if source.startswith(self.depotPath) and destination.startswith(self.depotPath):
629
source = source[len(self.depotPath):-4]
630
destination = destination[len(self.depotPath):-4]
632
- self.knownBranches[destination] = source
633
- self.knownBranches[source] = source
631
+ if destination not in self.knownBranches:
632
+ self.knownBranches[destination] = source
633
+ if source not in self.knownBranches:
634
+ self.knownBranches[source] = source
635
+
636
+ def listExistingP4GitBranches(self):
637
+ self.p4BranchesInGit = []
638
+
639
+ for line in mypopen("git rev-parse --symbolic --remotes").readlines():
640
+ if line.startswith("p4/") and line != "p4/HEAD\n":
641
+ branch = line[3:-1]
642
+ self.p4BranchesInGit.append(branch)
643
+ self.initialParents["refs/remotes/p4/" + branch] = parseRevision(line[:-1])
644
645
def run(self, args):
646
self.depotPath = ""
647
self.changeRange = ""
648
self.initialParent = ""
649
self.previousDepotPath = ""
650
+ # map from branch depot path to parent branch
651
+ self.knownBranches = {}
652
+ self.initialParents = {}
653
+
654
+ self.listExistingP4GitBranches()
655
+
656
+ if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master") and not self.detectBranches:
657
+ ### needs to be ported to multi branch import
658
641
- if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master"):
659
print "Syncing with origin first as requested by calling git fetch origin"
660
system("git fetch origin")
661
[originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
@@ -662,7 +679,8 @@ class P4Sync(Command):
679
system("git symbolic-ref refs/remotes/p4/HEAD %s" % self.branch)
680
681
if len(args) == 0:
665
- if not gitBranchExists(self.branch) and gitBranchExists("origin"):
682
+ if not gitBranchExists(self.branch) and gitBranchExists("origin") and not self.detectBranches:
683
+ ### needs to be ported to multi branch import
684
if not self.silent:
685
print "Creating %s branch in git repository based on origin" % self.branch
686
branch = self.branch
@@ -670,11 +688,33 @@ class P4Sync(Command):
688
branch = "refs/heads/" + branch
689
system("git update-ref %s origin" % branch)
690
673
- [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
674
- if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
675
- p4Change = int(p4Change) + 1
691
+ if self.verbose:
692
+ print "branches: %s" % self.p4BranchesInGit
693
+
694
+ p4Change = 0
695
+ for branch in self.p4BranchesInGit:
696
+ depotPath, change = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("refs/remotes/p4/" + branch))
697
+
698
+ if self.verbose:
699
+ print "path %s change %s" % (depotPath, change)
700
+
701
+ if len(depotPath) > 0 and len(change) > 0:
702
+ change = int(change) + 1
703
+ p4Change = max(p4Change, change)
704
+
705
+ if len(self.previousDepotPath) == 0:
706
+ self.previousDepotPath = depotPath
707
+ else:
708
+ i = 0
709
+ l = min(len(self.previousDepotPath), len(depotPath))
710
+ while i < l and self.previousDepotPath[i] == depotPath[i]:
711
+ i = i + 1
712
+ self.previousDepotPath = self.previousDepotPath[:i]
713
+
714
+ if p4Change > 0:
715
self.depotPath = self.previousDepotPath
677
- self.changeRange = "@%s,#head" % p4Change
716
+ #self.changeRange = "@%s,#head" % p4Change
717
+ self.changeRange = "@%s,%s" % (p4Change, p4Change + 10)
718
self.initialParent = parseRevision(self.branch)
719
if not self.silent:
720
print "Performing incremental import into %s git branch" % self.branch
@@ -729,7 +769,13 @@ class P4Sync(Command):
769
770
if self.detectBranches:
771
self.getBranchMapping();
732
- self.branchPrefix = self.depotPath[self.depotPath[:-1].rfind("/") + 1:]
772
+ if self.verbose:
773
+ print "p4-git branches: %s" % self.p4BranchesInGit
774
+ print "initial parents: %s" % self.initialParents
775
+ for b in self.p4BranchesInGit:
776
+ if b != "master":
777
+ b = b[len(self.projectName):]
778
+ self.createdBranches.add(b)
779
780
self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
781
@@ -784,6 +830,8 @@ class P4Sync(Command):
830
831
changes.sort()
832
else:
833
+ if self.verbose:
834
+ print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange)
835
output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines()
836
837
for line in output:
@@ -816,26 +864,39 @@ class P4Sync(Command):
864
865
filesForCommit = self.extractFilesInCommitToBranch(files, branch)
866
867
+ if self.verbose:
868
+ print "branch is %s" % branch
869
+
870
if branch not in self.createdBranches:
871
self.createdBranches.add(branch)
872
parent = self.knownBranches[branch]
873
if parent == branch:
874
parent = ""
875
+ elif self.verbose:
876
+ print "parent determined through known branches: %s" % parent
877
878
# main branch? use master
879
if branch == "main":
880
branch = "master"
881
else:
829
- branch = self.branchPrefix + branch
882
+ branch = self.projectName + branch
883
884
if parent == "main":
885
parent = "master"
886
elif len(parent) > 0:
834
- parent = self.branchPrefix + parent
887
+ parent = self.projectName + parent
888
889
branch = "refs/remotes/p4/" + branch
890
if len(parent) > 0:
891
parent = "refs/remotes/p4/" + parent
892
+
893
+ if self.verbose:
894
+ print "looking for initial parent for %s; current parent is %s" % (branch, parent)
895
+
896
+ if len(parent) == 0 and branch in self.initialParents:
897
+ parent = self.initialParents[branch]
898
+ del self.initialParents[branch]
899
+
900
self.commit(description, files, branch, branchPrefix, parent)
901
else:
902
self.commit(description, files, self.branch, self.depotPath, self.initialParent)
@@ -849,9 +910,10 @@ class P4Sync(Command):
910
911
912
self.gitStream.close()
913
+ if importProcess.wait() != 0:
914
+ die("fast-import failed: %s" % self.gitError.read())
915
self.gitOutput.close()
916
self.gitError.close()
854
- importProcess.wait()
917
918
return True
919