@matheusbsbs / matheus-bagliano / commits / a3b427b9fa
Fork 0

[PATCH] Do not create bogus branch from flag to git branch

If you run `git branch --help', you will unexpectedly have created a new branch named "--help". This simple patch adds logic and a usage statement to catch this and similar problems, and adds a testcase for it. Signed-off-by: Amos Waterland <apw@rossby.metr.ou.edu> Signed-off-by: Junio C Hamano <junkio@cox.net>

Amos Waterland committed Sep 7, 2005 at 21:13 UTC a3b427b9fa1fdfd6d0c119d1f9db075ff92ffdf7
2 files changed +43
git-branch.sh
+16
@@ -2,6 +2,16 @@
2
3 . git-sh-setup || die "Not a git archive"
4
5 +usage () {
6 + echo >&2 "usage: $(basename $0)"' [<branchname> [start-point]]
7 +
8 +If no arguments, show available branches and mark current branch with a star.
9 +If one argument, create a new branch <branchname> based off of current HEAD.
10 +If two arguments, create a new branch <branchname> based off of <start-point>.
11 +'
12 + exit 1
13 +}
14 +
15 case "$#" in
16 0)
17 headref=$(readlink "$GIT_DIR/HEAD" | sed -e 's|^refs/heads/||')
@@ -25,6 +35,12 @@ case "$#" in
35 head="$2^0" ;;
36 esac
37 branchname="$1"
38 +
39 +case "$branchname" in
40 +-*)
41 + usage;;
42 +esac
43 +
44 rev=$(git-rev-parse --verify "$head") || exit
45
46 [ -e "$GIT_DIR/refs/heads/$branchname" ] && die "$branchname already exists"
t/t3200-branch.sh new
+27
@@ -0,0 +1,27 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Amos Waterland
4 +#
5 +
6 +test_description='git branch --foo should not create bogus branch
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 +'
11 +. ./test-lib.sh
12 +
13 +test_expect_success \
14 + 'prepare an trivial repository' \
15 + 'echo Hello > A &&
16 + ../../git-update-index --add A &&
17 + ../../git-commit.sh -m "Initial commit."'
18 +
19 +test_expect_failure \
20 + 'git branch --help should return error code' \
21 + '../../git-branch.sh --help'
22 +
23 +test_expect_failure \
24 + 'git branch --help should not have created a bogus branch' \
25 + 'test -f .git/refs/heads/--help'
26 +
27 +test_done