Skip to content

Commit ac53196

Browse files
committed
Merge branch 'jh/maint-do-not-track-non-branches' into maint
* jh/maint-do-not-track-non-branches: branch/checkout --track: Ensure that upstream branch is indeed a branch
2 parents f35bb7c + 21b5b1e commit ac53196

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

branch.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,14 @@ void create_branch(const char *head,
175175
die("Cannot setup tracking information; starting point is not a branch.");
176176
break;
177177
case 1:
178-
/* Unique completion -- good, only if it is a real ref */
179-
if (explicit_tracking && !strcmp(real_ref, "HEAD"))
180-
die("Cannot setup tracking information; starting point is not a branch.");
178+
/* Unique completion -- good, only if it is a real branch */
179+
if (prefixcmp(real_ref, "refs/heads/") &&
180+
prefixcmp(real_ref, "refs/remotes/")) {
181+
if (explicit_tracking)
182+
die("Cannot setup tracking information; starting point is not a branch.");
183+
else
184+
real_ref = NULL;
185+
}
181186
break;
182187
default:
183188
die("Ambiguous object name: '%s'.", start_name);

t/t3200-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ test_expect_success \
223223
'branch from non-branch HEAD w/--track causes failure' \
224224
'test_must_fail git branch --track my10 HEAD^'
225225

226+
test_expect_success \
227+
'branch from tag w/--track causes failure' \
228+
'git tag foobar &&
229+
test_must_fail git branch --track my11 foobar'
230+
226231
# Keep this test last, as it changes the current branch
227232
cat >expect <<EOF
228233
0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
@@ -488,6 +493,15 @@ test_expect_success 'autosetuprebase always on an untracked remote branch' '
488493
test "z$(git config branch.myr20.rebase)" = z
489494
'
490495

496+
test_expect_success 'autosetuprebase always on detached HEAD' '
497+
git config branch.autosetupmerge always &&
498+
test_when_finished git checkout master &&
499+
git checkout HEAD^0 &&
500+
git branch my11 &&
501+
test -z "$(git config branch.my11.remote)" &&
502+
test -z "$(git config branch.my11.merge)"
503+
'
504+
491505
test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
492506
git config branch.autosetuprebase garbage &&
493507
test_must_fail git branch

t/t6040-tracking-info.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ test_expect_success 'status' '
7474
grep "have 1 and 1 different" actual
7575
'
7676

77-
test_expect_success 'status when tracking lightweight tags' '
77+
test_expect_success 'fail to track lightweight tags' '
7878
git checkout master &&
7979
git tag light &&
80-
git branch --track lighttrack light >actual &&
81-
grep "set up to track" actual &&
82-
git checkout lighttrack
80+
test_must_fail git branch --track lighttrack light >actual &&
81+
test_must_fail grep "set up to track" actual &&
82+
test_must_fail git checkout lighttrack
8383
'
8484

85-
test_expect_success 'status when tracking annotated tags' '
85+
test_expect_success 'fail to track annotated tags' '
8686
git checkout master &&
8787
git tag -m heavy heavy &&
88-
git branch --track heavytrack heavy >actual &&
89-
grep "set up to track" actual &&
90-
git checkout heavytrack
88+
test_must_fail git branch --track heavytrack heavy >actual &&
89+
test_must_fail grep "set up to track" actual &&
90+
test_must_fail git checkout heavytrack
9191
'
9292

9393
test_expect_success 'setup tracking with branch --set-upstream on existing branch' '

t/t7201-co.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ test_expect_success 'checkout w/--track from non-branch HEAD fails' '
408408
test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
409409
'
410410

411+
test_expect_success 'checkout w/--track from tag fails' '
412+
git checkout master^0 &&
413+
test_must_fail git symbolic-ref HEAD &&
414+
test_must_fail git checkout --track -b track frotz &&
415+
test_must_fail git rev-parse --verify track &&
416+
test_must_fail git symbolic-ref HEAD &&
417+
test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
418+
'
419+
411420
test_expect_success 'detach a symbolic link HEAD' '
412421
git checkout master &&
413422
git config --bool core.prefersymlinkrefs yes &&
@@ -423,7 +432,6 @@ test_expect_success 'detach a symbolic link HEAD' '
423432
test_expect_success \
424433
'checkout with --track fakes a sensible -b <name>' '
425434
git update-ref refs/remotes/origin/koala/bear renamer &&
426-
git update-ref refs/new/koala/bear renamer &&
427435
428436
git checkout --track origin/koala/bear &&
429437
test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
@@ -439,12 +447,6 @@ test_expect_success \
439447
440448
git checkout --track remotes/origin/koala/bear &&
441449
test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
442-
test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
443-
444-
git checkout master && git branch -D koala/bear &&
445-
446-
git checkout --track refs/new/koala/bear &&
447-
test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
448450
test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
449451
'
450452

0 commit comments

Comments
 (0)