Skip to content

Commit 5a8e84c

Browse files
Pete Wyckoffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4749784 commit 5a8e84c

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

git-p4.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ def p4BranchesInGit(branchesAreInRemotes=True):
579579

580580
return branches
581581

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+
582593
def findUpstreamBranchPoint(head = "HEAD"):
583594
branches = p4BranchesInGit()
584595
# map from depot-path to branch name
@@ -2768,6 +2779,7 @@ def run(self, args):
27682779
print 'Syncing with origin first, using "git fetch origin"'
27692780
system("git fetch origin")
27702781

2782+
branch_arg_given = bool(self.branch)
27712783
if len(self.branch) == 0:
27722784
self.branch = self.refPrefix + "master"
27732785
if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
@@ -2961,8 +2973,21 @@ def run(self, args):
29612973
else:
29622974
# catch "git p4 sync" with no new branches, in a repo that
29632975
# 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+
29662991
if self.verbose:
29672992
print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
29682993
self.changeRange)

t/t9806-git-p4-options.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ test_expect_success 'clone --branch should checkout master' '
4040
)
4141
'
4242

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' '
4544
test_when_finished cleanup_git &&
45+
git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot@2 &&
4646
(
4747
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
5150
)
5251
'
5352

0 commit comments

Comments
 (0)