git p4: fail gracefully on sync with no master branch
If --branch was used to build a repository with no refs/remotes/p4/master, future syncs will not know which branch to sync. Notice this situation and print a helpful error message. Signed-off-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pete Wyckoff committed
Jan 14, 2013 at 19:47 UTC
5a8e84cde3711076d3ad7260daa0a24ee40c8e07
2 files changed
+31
-7
git-p4.py
+27
-2
@@ -579,6 +579,17 @@ def p4BranchesInGit(branchesAreInRemotes=True):
579
580
return branches
581
582
+def branch_exists(branch):
583
+ """Make sure that the given ref name really exists."""
584
+
585
+ cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
586
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
587
+ out, _ = p.communicate()
588
+ if p.returncode:
589
+ return False
590
+ # expect exactly one line of output: the branch name
591
+ return out.rstrip() == branch
592
+
593
def findUpstreamBranchPoint(head = "HEAD"):
594
branches = p4BranchesInGit()
595
# map from depot-path to branch name
@@ -2768,6 +2779,7 @@ class P4Sync(Command, P4UserMap):
2779
print 'Syncing with origin first, using "git fetch origin"'
2780
system("git fetch origin")
2781
2782
+ branch_arg_given = bool(self.branch)
2783
if len(self.branch) == 0:
2784
self.branch = self.refPrefix + "master"
2785
if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
@@ -2961,8 +2973,21 @@ class P4Sync(Command, P4UserMap):
2973
else:
2974
# catch "git p4 sync" with no new branches, in a repo that
2975
# does not have any existing p4 branches
2964
- if len(args) == 0 and not self.p4BranchesInGit:
2965
- die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.");
2976
+ if len(args) == 0:
2977
+ if not self.p4BranchesInGit:
2978
+ die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
2979
+
2980
+ # The default branch is master, unless --branch is used to
2981
+ # specify something else. Make sure it exists, or complain
2982
+ # nicely about how to use --branch.
2983
+ if not self.detectBranches:
2984
+ if not branch_exists(self.branch):
2985
+ if branch_arg_given:
2986
+ die("Error: branch %s does not exist." % self.branch)
2987
+ else:
2988
+ die("Error: no branch %s; perhaps specify one with --branch." %
2989
+ self.branch)
2990
+
2991
if self.verbose:
2992
print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
2993
self.changeRange)
t/t9806-git-p4-options.sh
+4
-5
@@ -40,14 +40,13 @@ test_expect_success 'clone --branch should checkout master' '
40
)
41
'
42
43
-test_expect_failure 'sync when branch is not called master should work' '
44
- git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot@2 &&
43
+test_expect_success 'sync when no master branch prints a nice error' '
44
test_when_finished cleanup_git &&
45
+ git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot@2 &&
46
(
47
cd "$git" &&
48
- git p4 sync &&
49
- git show -s --format=%s refs/remotes/p4/sb >show &&
50
- grep "change 3" show
48
+ test_must_fail git p4 sync 2>err &&
49
+ grep "Error: no branch refs/remotes/p4/master" err
50
)
51
'
52