Skip to content

Commit 307d68e

Browse files
committed
Merge branch 'nd/branch-error-cases' into maint
"git branch" had more cases where it did not bother to check nonsense command line parameters. * nd/branch-error-cases: branch: segfault fixes and validation
2 parents 6201eb3 + 8efb889 commit 307d68e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

builtin/branch.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
889889
} else if (new_upstream) {
890890
struct branch *branch = branch_get(argv[0]);
891891

892+
if (argc > 1)
893+
die(_("too many branches to set new upstream"));
894+
895+
if (!branch) {
896+
if (!argc || !strcmp(argv[0], "HEAD"))
897+
die(_("could not set upstream of HEAD to %s when "
898+
"it does not point to any branch."),
899+
new_upstream);
900+
die(_("no such branch '%s'"), argv[0]);
901+
}
902+
892903
if (!ref_exists(branch->refname))
893904
die(_("branch '%s' does not exist"), branch->name);
894905

@@ -901,6 +912,16 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
901912
struct branch *branch = branch_get(argv[0]);
902913
struct strbuf buf = STRBUF_INIT;
903914

915+
if (argc > 1)
916+
die(_("too many branches to unset upstream"));
917+
918+
if (!branch) {
919+
if (!argc || !strcmp(argv[0], "HEAD"))
920+
die(_("could not unset upstream of HEAD when "
921+
"it does not point to any branch."));
922+
die(_("no such branch '%s'"), argv[0]);
923+
}
924+
904925
if (!branch_has_merge_config(branch)) {
905926
die(_("Branch '%s' has no upstream information"), branch->name);
906927
}
@@ -916,6 +937,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
916937
int branch_existed = 0, remote_tracking = 0;
917938
struct strbuf buf = STRBUF_INIT;
918939

940+
if (!strcmp(argv[0], "HEAD"))
941+
die(_("it does not make sense to create 'HEAD' manually"));
942+
943+
if (!branch)
944+
die(_("no such branch '%s'"), argv[0]);
945+
919946
if (kinds != REF_LOCAL_BRANCH)
920947
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
921948

t/t3200-branch.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ test_expect_success \
4242
'git branch a/b/c should create a branch' \
4343
'git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c'
4444

45+
test_expect_success \
46+
'git branch HEAD should fail' \
47+
'test_must_fail git branch HEAD'
48+
4549
cat >expect <<EOF
4650
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
4751
EOF
@@ -388,6 +392,14 @@ test_expect_success \
388392
'git tag foobar &&
389393
test_must_fail git branch --track my11 foobar'
390394

395+
test_expect_success '--set-upstream-to fails on multiple branches' \
396+
'test_must_fail git branch --set-upstream-to master a b c'
397+
398+
test_expect_success '--set-upstream-to fails on detached HEAD' \
399+
'git checkout HEAD^{} &&
400+
test_must_fail git branch --set-upstream-to master &&
401+
git checkout -'
402+
391403
test_expect_success 'use --set-upstream-to modify HEAD' \
392404
'test_config branch.master.remote foo &&
393405
test_config branch.master.merge foo &&
@@ -417,6 +429,15 @@ test_expect_success 'test --unset-upstream on HEAD' \
417429
test_must_fail git branch --unset-upstream
418430
'
419431

432+
test_expect_success '--unset-upstream should fail on multiple branches' \
433+
'test_must_fail git branch --unset-upstream a b c'
434+
435+
test_expect_success '--unset-upstream should fail on detached HEAD' \
436+
'git checkout HEAD^{} &&
437+
test_must_fail git branch --unset-upstream &&
438+
git checkout -
439+
'
440+
420441
test_expect_success 'test --unset-upstream on a particular branch' \
421442
'git branch my15
422443
git branch --set-upstream-to master my14 &&

0 commit comments

Comments
 (0)