Skip to content

Commit 8eb865b

Browse files
committed
Merge branch 'jc/branch-desc-typoavoidance' into maint
* jc/branch-desc-typoavoidance: branch --edit-description: protect against mistyped branch name tests: add write_script helper function
2 parents a78f558 + c2d17ba commit 8eb865b

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-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

t/test-lib.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,20 @@ test_config () {
381381
git config "$@"
382382
}
383383

384+
384385
test_config_global () {
385386
test_when_finished "test_unconfig --global '$1'" &&
386387
git config --global "$@"
387388
}
388389

390+
write_script () {
391+
{
392+
echo "#!${2-"$SHELL_PATH"}" &&
393+
cat
394+
} >"$1" &&
395+
chmod +x "$1"
396+
}
397+
389398
# Use test_set_prereq to tell that a particular prerequisite is available.
390399
# The prerequisite can later be checked for in two ways:
391400
#

0 commit comments

Comments
 (0)