Skip to content

Commit

Permalink
Merge branch 'jc/branch-desc-typoavoidance'
Browse files Browse the repository at this point in the history
* jc/branch-desc-typoavoidance:
  branch --edit-description: protect against mistyped branch name
  • Loading branch information
Junio C Hamano committed Feb 10, 2012
2 parents e684e89 + c2d17ba commit d88698e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
15 changes: 15 additions & 0 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
with_commit, argv);
else if (edit_description) {
const char *branch_name;
struct strbuf branch_ref = STRBUF_INIT;

if (detached)
die("Cannot give description to detached HEAD");
if (!argc)
Expand All @@ -776,6 +778,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
branch_name = argv[0];
else
usage_with_options(builtin_branch_usage, options);

strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
if (!ref_exists(branch_ref.buf)) {
strbuf_release(&branch_ref);

if (!argc)
return error("No commit on branch '%s' yet.",
branch_name);
else
return error("No such branch '%s'.", branch_name);
}
strbuf_release(&branch_ref);

if (edit_branch_description(branch_name))
return 1;
} else if (rename) {
Expand Down
41 changes: 37 additions & 4 deletions t/t3200-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
# Copyright (c) 2005 Amos Waterland
#

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

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

test_expect_success \
Expand Down Expand Up @@ -620,4 +617,40 @@ test_expect_success 'use set-upstream on the current branch' '
'

test_expect_success 'use --edit-description' '
write_script editor <<-\EOF &&
echo "New contents" >"$1"
EOF
EDITOR=./editor git branch --edit-description &&
write_script editor <<-\EOF &&
git stripspace -s <"$1" >"EDITOR_OUTPUT"
EOF
EDITOR=./editor git branch --edit-description &&
echo "New contents" >expect &&
test_cmp EDITOR_OUTPUT expect
'

test_expect_success 'detect typo in branch name when using --edit-description' '
write_script editor <<-\EOF &&
echo "New contents" >"$1"
EOF
(
EDITOR=./editor &&
export EDITOR &&
test_must_fail git branch --edit-description no-such-branch
)
'

test_expect_success 'refuse --edit-description on unborn branch for now' '
write_script editor <<-\EOF &&
echo "New contents" >"$1"
EOF
git checkout --orphan unborn &&
(
EDITOR=./editor &&
export EDITOR &&
test_must_fail git branch --edit-description
)
'

test_done

0 comments on commit d88698e

Please sign in to comment.