git-p4: create helper function importRevisions()
This makes it easier to try/catch around this block of code to ensure cleanup following p4 failures is handled properly. Signed-off-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Luke Diamand committed
Jan 29, 2020 at 11:12 UTC
ca5b5cce6290351f8cb63ee4ff466ed4a2285319
1 file changed
+68
-64
git-p4.py
+68
-64
@@ -3555,6 +3555,73 @@ class P4Sync(Command, P4UserMap):
3555
print("IO error details: {}".format(err))
3556
print(self.gitError.read())
3557
3558
+
3559
+ def importRevisions(self, args, branch_arg_given):
3560
+ changes = []
3561
+
3562
+ if len(self.changesFile) > 0:
3563
+ output = open(self.changesFile).readlines()
3564
+ changeSet = set()
3565
+ for line in output:
3566
+ changeSet.add(int(line))
3567
+
3568
+ for change in changeSet:
3569
+ changes.append(change)
3570
+
3571
+ changes.sort()
3572
+ else:
3573
+ # catch "git p4 sync" with no new branches, in a repo that
3574
+ # does not have any existing p4 branches
3575
+ if len(args) == 0:
3576
+ if not self.p4BranchesInGit:
3577
+ die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3578
+
3579
+ # The default branch is master, unless --branch is used to
3580
+ # specify something else. Make sure it exists, or complain
3581
+ # nicely about how to use --branch.
3582
+ if not self.detectBranches:
3583
+ if not branch_exists(self.branch):
3584
+ if branch_arg_given:
3585
+ die("Error: branch %s does not exist." % self.branch)
3586
+ else:
3587
+ die("Error: no branch %s; perhaps specify one with --branch." %
3588
+ self.branch)
3589
+
3590
+ if self.verbose:
3591
+ print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3592
+ self.changeRange))
3593
+ changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3594
+
3595
+ if len(self.maxChanges) > 0:
3596
+ changes = changes[:min(int(self.maxChanges), len(changes))]
3597
+
3598
+ if len(changes) == 0:
3599
+ if not self.silent:
3600
+ print("No changes to import!")
3601
+ else:
3602
+ if not self.silent and not self.detectBranches:
3603
+ print("Import destination: %s" % self.branch)
3604
+
3605
+ self.updatedBranches = set()
3606
+
3607
+ if not self.detectBranches:
3608
+ if args:
3609
+ # start a new branch
3610
+ self.initialParent = ""
3611
+ else:
3612
+ # build on a previous revision
3613
+ self.initialParent = parseRevision(self.branch)
3614
+
3615
+ self.importChanges(changes)
3616
+
3617
+ if not self.silent:
3618
+ print("")
3619
+ if len(self.updatedBranches) > 0:
3620
+ sys.stdout.write("Updated branches: ")
3621
+ for b in self.updatedBranches:
3622
+ sys.stdout.write("%s " % b)
3623
+ sys.stdout.write("\n")
3624
+
3625
def openStreams(self):
3626
self.importProcess = subprocess.Popen(["git", "fast-import"],
3627
stdin=subprocess.PIPE,
@@ -3761,70 +3828,7 @@ class P4Sync(Command, P4UserMap):
3828
if revision:
3829
self.importHeadRevision(revision)
3830
else:
3764
- changes = []
3765
-
3766
- if len(self.changesFile) > 0:
3767
- output = open(self.changesFile).readlines()
3768
- changeSet = set()
3769
- for line in output:
3770
- changeSet.add(int(line))
3771
-
3772
- for change in changeSet:
3773
- changes.append(change)
3774
-
3775
- changes.sort()
3776
- else:
3777
- # catch "git p4 sync" with no new branches, in a repo that
3778
- # does not have any existing p4 branches
3779
- if len(args) == 0:
3780
- if not self.p4BranchesInGit:
3781
- die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3782
-
3783
- # The default branch is master, unless --branch is used to
3784
- # specify something else. Make sure it exists, or complain
3785
- # nicely about how to use --branch.
3786
- if not self.detectBranches:
3787
- if not branch_exists(self.branch):
3788
- if branch_arg_given:
3789
- die("Error: branch %s does not exist." % self.branch)
3790
- else:
3791
- die("Error: no branch %s; perhaps specify one with --branch." %
3792
- self.branch)
3793
-
3794
- if self.verbose:
3795
- print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3796
- self.changeRange))
3797
- changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3798
-
3799
- if len(self.maxChanges) > 0:
3800
- changes = changes[:min(int(self.maxChanges), len(changes))]
3801
-
3802
- if len(changes) == 0:
3803
- if not self.silent:
3804
- print("No changes to import!")
3805
- else:
3806
- if not self.silent and not self.detectBranches:
3807
- print("Import destination: %s" % self.branch)
3808
-
3809
- self.updatedBranches = set()
3810
-
3811
- if not self.detectBranches:
3812
- if args:
3813
- # start a new branch
3814
- self.initialParent = ""
3815
- else:
3816
- # build on a previous revision
3817
- self.initialParent = parseRevision(self.branch)
3818
-
3819
- self.importChanges(changes)
3820
-
3821
- if not self.silent:
3822
- print("")
3823
- if len(self.updatedBranches) > 0:
3824
- sys.stdout.write("Updated branches: ")
3825
- for b in self.updatedBranches:
3826
- sys.stdout.write("%s " % b)
3827
- sys.stdout.write("\n")
3831
+ self.importRevisions(args, branch_arg_given)
3832
3833
if gitConfigBool("git-p4.importLabels"):
3834
self.importLabels = True