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 <gitster@pobox.com>

Junio C Hamano committed Feb 5, 2012 at 17:13 UTC c2d17ba3db0d2b14daf04e69a8c5ec73b023c1fe
2 files changed +52 -4
builtin/branch.c
+15
@@ -768,6 +768,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
768 with_commit, argv);
769 else if (edit_description) {
770 const char *branch_name;
771 + struct strbuf branch_ref = STRBUF_INIT;
772 +
773 if (detached)
774 die("Cannot give description to detached HEAD");
775 if (!argc)
@@ -776,6 +778,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
778 branch_name = argv[0];
779 else
780 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 +
794 if (edit_branch_description(branch_name))
795 return 1;
796 } else if (rename) {
t/t3200-branch.sh
+37 -4
@@ -3,11 +3,8 @@
3 # Copyright (c) 2005 Amos Waterland
4 #
5
6 -test_description='git branch --foo should not create bogus branch
6 +test_description='git branch assorted tests'
7
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 -'
8 . ./test-lib.sh
9
10 test_expect_success \
@@ -620,4 +617,40 @@ test_expect_success 'use set-upstream on the current branch' '
617
618 '
619
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 +
656 test_done