git-branch -d <branch>: delete unused branch.
The new flag '-d' lets you delete a branch. For safety, it does not lets you delete the branch you are currently on, nor a branch that has been fully merged into your current branch. The credit for the safety check idea goes to Daniel Barkalow. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 14, 2005 at 01:43 UTC
ba65af9c1f67461c0dfa6582c911d6a893d80284
1 file changed
+54
-6
git-branch.sh
+54
-6
@@ -3,7 +3,7 @@
3
. git-sh-setup || die "Not a git archive"
4
5
usage () {
6
- echo >&2 "usage: $(basename $0)"' [<branchname> [start-point]]
6
+ echo >&2 "usage: $(basename $0)"' [-d <branch>] | [<branch> [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.
@@ -12,6 +12,59 @@ If two arguments, create a new branch <branchname> based off of <start-point>.
12
exit 1
13
}
14
15
+delete_branch () {
16
+ option="$1" branch_name="$2"
17
+ headref=$(readlink "$GIT_DIR/HEAD" | sed -e 's|^refs/heads/||')
18
+ case ",$headref," in
19
+ ",$branch_name,")
20
+ die "Cannot delete the branch you are on." ;;
21
+ ,,)
22
+ die "What branch are you on anyway?" ;;
23
+ esac
24
+ branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
25
+ branch=$(git-rev-parse --verify "$branch^0") ||
26
+ die "Seriously, what branch are you talking about?"
27
+ case "$option" in
28
+ -D)
29
+ ;;
30
+ *)
31
+ mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
32
+ case " $mbs " in
33
+ *' '$branch' '*)
34
+ # the merge base of branch and HEAD contains branch --
35
+ # which means that the HEAD contains everything in the HEAD.
36
+ ;;
37
+ *)
38
+ echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
39
+If you are sure you want to delete it, run 'git branch -D $branch_name'."
40
+ exit 1
41
+ ;;
42
+ esac
43
+ ;;
44
+ esac
45
+ rm -f "$GIT_DIR/refs/heads/$branch_name"
46
+ echo "Deleted branch $branch_name."
47
+ exit 0
48
+}
49
+
50
+while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
51
+do
52
+ case "$1" in
53
+ -d | -D)
54
+ delete_branch "$1" "$2"
55
+ exit
56
+ ;;
57
+ --)
58
+ shift
59
+ break
60
+ ;;
61
+ -*)
62
+ usage
63
+ ;;
64
+ esac
65
+ shift
66
+done
67
+
68
case "$#" in
69
0)
70
headref=$(readlink "$GIT_DIR/HEAD" | sed -e 's|^refs/heads/||')
@@ -36,11 +89,6 @@ case "$#" in
89
esac
90
branchname="$1"
91
39
-case "$branchname" in
40
--*)
41
- usage;;
42
-esac
43
-
92
rev=$(git-rev-parse --verify "$head") || exit
93
94
[ -e "$GIT_DIR/refs/heads/$branchname" ] && die "$branchname already exists"