branch: description for non-existent branch errors

When the repository does not yet have commits, some errors describe that there is no branch: $ git init -b first $ git branch --edit-description first error: No branch named 'first'. $ git branch --set-upstream-to=upstream fatal: branch 'first' does not exist $ git branch -c second error: refname refs/heads/first not found fatal: Branch copy failed That "first" branch is unborn but to say it doesn't exists is confusing. Options "-c" (copy) and "-m" (rename) show the same error when the origin branch doesn't exists: $ git branch -c non-existent-branch second error: refname refs/heads/non-existent-branch not found fatal: Branch copy failed $ git branch -m non-existent-branch second error: refname refs/heads/non-existent-branch not found fatal: Branch rename failed Note that "--edit-description" without an explicit argument is already considering the _empty repository_ circumstance in its error. Also note that "-m" on the initial branch it is an allowed operation. Make the error descriptions for those branch operations with unborn or non-existent branches, more informative. This is the result of the change: $ git init -b first $ git branch --edit-description first error: No commit on branch 'first' yet. $ git branch --set-upstream-to=upstream fatal: No commit on branch 'first' yet. $ git branch -c second fatal: No commit on branch 'first' yet. $ git branch [-c/-m] non-existent-branch second fatal: No branch named 'non-existent-branch'. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Oct 8, 2022 at 02:39 UTC bcfc82bd48f518c8939090748516ad21bb079720
2 files changed +58 -2
builtin/branch.c
+12 -2
@@ -538,6 +538,13 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
538 die(_("Invalid branch name: '%s'"), oldname);
539 }
540
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 +
548 /*
549 * A command like "git branch -M currentbranch currentbranch" cannot
550 * cause the worktree to become inconsistent with HEAD, so allow it.
@@ -805,7 +812,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
812 if (!ref_exists(branch_ref.buf)) {
813 strbuf_release(&branch_ref);
814
808 - if (!argc)
815 + if (!argc || !strcmp(head, branch_name))
816 return error(_("No commit on branch '%s' yet."),
817 branch_name);
818 else
@@ -848,8 +855,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
855 die(_("no such branch '%s'"), argv[0]);
856 }
857
851 - if (!ref_exists(branch->refname))
858 + if (!ref_exists(branch->refname)) {
859 + if (!argc || !strcmp(head, branch->name))
860 + die(_("No commit on branch '%s' yet."), branch->name);
861 die(_("branch '%s' does not exist"), branch->name);
862 + }
863
864 dwim_and_setup_tracking(the_repository, branch->name,
865 new_upstream, BRANCH_TRACK_OVERRIDE,
t/t3202-show-branch.sh
+46
@@ -7,6 +7,28 @@ test_description='test show-branch'
7 # arbitrary reference time: 2009-08-30 19:20:00
8 GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
9
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 +
32 test_expect_success 'setup' '
33 test_commit initial &&
34 for i in $(test_seq 1 10)
@@ -175,4 +197,28 @@ done <<\EOF
197 --reflog --current
198 EOF
199
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 +
224 test_done