Skip to content

Commit 4050354

Browse files
committed
Merge branch 'rj/branch-edit-desc-unborn'
"git branch --edit-description" on an unborh branch misleadingly said that no such branch exists, which has been corrected. * rj/branch-edit-desc-unborn: branch: description for non-existent branch errors
2 parents a2e618c + bcfc82b commit 4050354

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

builtin/branch.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
538538
die(_("Invalid branch name: '%s'"), oldname);
539539
}
540540

541+
if ((copy || strcmp(head, oldname)) && !ref_exists(oldref.buf)) {
542+
if (copy && !strcmp(head, oldname))
543+
die(_("No commit on branch '%s' yet."), oldname);
544+
else
545+
die(_("No branch named '%s'."), oldname);
546+
}
547+
541548
/*
542549
* A command like "git branch -M currentbranch currentbranch" cannot
543550
* cause the worktree to become inconsistent with HEAD, so allow it.
@@ -807,7 +814,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
807814
if (!ref_exists(branch_ref.buf)) {
808815
strbuf_release(&branch_ref);
809816

810-
if (!argc)
817+
if (!argc || !strcmp(head, branch_name))
811818
return error(_("No commit on branch '%s' yet."),
812819
branch_name);
813820
else
@@ -850,8 +857,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
850857
die(_("no such branch '%s'"), argv[0]);
851858
}
852859

853-
if (!ref_exists(branch->refname))
860+
if (!ref_exists(branch->refname)) {
861+
if (!argc || !strcmp(head, branch->name))
862+
die(_("No commit on branch '%s' yet."), branch->name);
854863
die(_("branch '%s' does not exist"), branch->name);
864+
}
855865

856866
dwim_and_setup_tracking(the_repository, branch->name,
857867
new_upstream, BRANCH_TRACK_OVERRIDE,

t/t3202-show-branch.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ test_description='test show-branch'
77
# arbitrary reference time: 2009-08-30 19:20:00
88
GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
99

10+
test_expect_success 'error descriptions on empty repository' '
11+
current=$(git branch --show-current) &&
12+
cat >expect <<-EOF &&
13+
error: No commit on branch '\''$current'\'' yet.
14+
EOF
15+
test_must_fail git branch --edit-description 2>actual &&
16+
test_cmp expect actual &&
17+
test_must_fail git branch --edit-description $current 2>actual &&
18+
test_cmp expect actual
19+
'
20+
21+
test_expect_success 'fatal descriptions on empty repository' '
22+
current=$(git branch --show-current) &&
23+
cat >expect <<-EOF &&
24+
fatal: No commit on branch '\''$current'\'' yet.
25+
EOF
26+
test_must_fail git branch --set-upstream-to=non-existent 2>actual &&
27+
test_cmp expect actual &&
28+
test_must_fail git branch -c new-branch 2>actual &&
29+
test_cmp expect actual
30+
'
31+
1032
test_expect_success 'setup' '
1133
test_commit initial &&
1234
for i in $(test_seq 1 10)
@@ -175,4 +197,28 @@ done <<\EOF
175197
--reflog --current
176198
EOF
177199

200+
test_expect_success 'error descriptions on non-existent branch' '
201+
cat >expect <<-EOF &&
202+
error: No branch named '\''non-existent'\'.'
203+
EOF
204+
test_must_fail git branch --edit-description non-existent 2>actual &&
205+
test_cmp expect actual
206+
'
207+
208+
test_expect_success 'fatal descriptions on non-existent branch' '
209+
cat >expect <<-EOF &&
210+
fatal: branch '\''non-existent'\'' does not exist
211+
EOF
212+
test_must_fail git branch --set-upstream-to=non-existent non-existent 2>actual &&
213+
test_cmp expect actual &&
214+
215+
cat >expect <<-EOF &&
216+
fatal: No branch named '\''non-existent'\''.
217+
EOF
218+
test_must_fail git branch -c non-existent new-branch 2>actual &&
219+
test_cmp expect actual &&
220+
test_must_fail git branch -m non-existent new-branch 2>actual &&
221+
test_cmp expect actual
222+
'
223+
178224
test_done

0 commit comments

Comments
 (0)