Skip to content

Commit c2d17ba

Browse files
committed
branch --edit-description: protect against mistyped branch name
It is very easy to mistype the branch name when editing its description, e.g. $ git checkout -b my-topic master : work work work : now we are at a good point to switch working something else $ git checkout master : ah, let's write it down before we forget what we were doing $ git branch --edit-description my-tpoic The command does not notice that branch 'my-tpoic' does not exist. It is not lost (it becomes description of an unborn my-tpoic branch), but is not very useful. So detect such a case and error out to reduce the grief factor from this common mistake. This incidentally also errors out --edit-description when the HEAD points at an unborn branch (immediately after "init", or "checkout --orphan"), because at that point, you do not even have any commit that is part of your history and there is no point in describing how this particular branch is different from the branch it forked off of, which is the useful bit of information the branch description is designed to capture. We may want to special case the unborn case later, but that is outside the scope of this patch to prevent more common mistakes before 1.7.9 series gains too much widespread use. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 840c519 commit c2d17ba

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

builtin/branch.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
768768
with_commit, argv);
769769
else if (edit_description) {
770770
const char *branch_name;
771+
struct strbuf branch_ref = STRBUF_INIT;
772+
771773
if (detached)
772774
die("Cannot give description to detached HEAD");
773775
if (!argc)
@@ -776,6 +778,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
776778
branch_name = argv[0];
777779
else
778780
usage_with_options(builtin_branch_usage, options);
781+
782+
strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
783+
if (!ref_exists(branch_ref.buf)) {
784+
strbuf_release(&branch_ref);
785+
786+
if (!argc)
787+
return error("No commit on branch '%s' yet.",
788+
branch_name);
789+
else
790+
return error("No such branch '%s'.", branch_name);
791+
}
792+
strbuf_release(&branch_ref);
793+
779794
if (edit_branch_description(branch_name))
780795
return 1;
781796
} else if (rename) {

t/t3200-branch.sh

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
# Copyright (c) 2005 Amos Waterland
44
#
55

6-
test_description='git branch --foo should not create bogus branch
6+
test_description='git branch assorted tests'
77

8-
This test runs git branch --help and checks that the argument is properly
9-
handled. Specifically, that a bogus branch is not created.
10-
'
118
. ./test-lib.sh
129

1310
test_expect_success \
@@ -620,4 +617,40 @@ test_expect_success 'use set-upstream on the current branch' '
620617
621618
'
622619

620+
test_expect_success 'use --edit-description' '
621+
write_script editor <<-\EOF &&
622+
echo "New contents" >"$1"
623+
EOF
624+
EDITOR=./editor git branch --edit-description &&
625+
write_script editor <<-\EOF &&
626+
git stripspace -s <"$1" >"EDITOR_OUTPUT"
627+
EOF
628+
EDITOR=./editor git branch --edit-description &&
629+
echo "New contents" >expect &&
630+
test_cmp EDITOR_OUTPUT expect
631+
'
632+
633+
test_expect_success 'detect typo in branch name when using --edit-description' '
634+
write_script editor <<-\EOF &&
635+
echo "New contents" >"$1"
636+
EOF
637+
(
638+
EDITOR=./editor &&
639+
export EDITOR &&
640+
test_must_fail git branch --edit-description no-such-branch
641+
)
642+
'
643+
644+
test_expect_success 'refuse --edit-description on unborn branch for now' '
645+
write_script editor <<-\EOF &&
646+
echo "New contents" >"$1"
647+
EOF
648+
git checkout --orphan unborn &&
649+
(
650+
EDITOR=./editor &&
651+
export EDITOR &&
652+
test_must_fail git branch --edit-description
653+
)
654+
'
655+
623656
test_done

0 commit comments

Comments
 (0)