Merge http://www.kernel.org/pub/scm/gitk/gitk
[git.git] / git-branch.sh
1 #!/bin/sh
2
3 . git-sh-setup
4
5 usage () {
6     echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <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.
10 If two arguments, create a new branch <branchname> based off of <start-point>.
11 '
12     exit 1
13 }
14
15 headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
16         sed -e 's|^refs/heads/||')
17
18 delete_branch () {
19     option="$1"
20     shift
21     for branch_name
22     do
23         case ",$headref," in
24         ",$branch_name,")
25             die "Cannot delete the branch you are on." ;;
26         ,,)
27             die "What branch are you on anyway?" ;;
28         esac
29         branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
30             branch=$(git-rev-parse --verify "$branch^0") ||
31                 die "Seriously, what branch are you talking about?"
32         case "$option" in
33         -D)
34             ;;
35         *)
36             mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
37             case " $mbs " in
38             *' '$branch' '*)
39                 # the merge base of branch and HEAD contains branch --
40                 # which means that the HEAD contains everything in the HEAD.
41                 ;;
42             *)
43                 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
44     If you are sure you want to delete it, run 'git branch -D $branch_name'."
45                 exit 1
46                 ;;
47             esac
48             ;;
49         esac
50         rm -f "$GIT_DIR/refs/heads/$branch_name"
51         echo "Deleted branch $branch_name."
52     done
53     exit 0
54 }
55
56 force=
57 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
58 do
59         case "$1" in
60         -d | -D)
61                 delete_branch "$@"
62                 exit
63                 ;;
64         -f)
65                 force="$1"
66                 ;;
67         --)
68                 shift
69                 break
70                 ;;
71         -*)
72                 usage
73                 ;;
74         esac
75         shift
76 done
77
78 case "$#" in
79 0)
80         git-rev-parse --symbolic --all |
81         sed -ne 's|^refs/heads/||p' |
82         sort |
83         while read ref
84         do
85                 if test "$headref" = "$ref"
86                 then
87                         pfx='*'
88                 else
89                         pfx=' '
90                 fi
91                 echo "$pfx $ref"
92         done
93         exit 0 ;;
94 1)
95         head=HEAD ;;
96 2)
97         head="$2^0" ;;
98 esac
99 branchname="$1"
100
101 rev=$(git-rev-parse --verify "$head") || exit
102
103 git-check-ref-format "heads/$branchname" ||
104         die "we do not like '$branchname' as a branch name."
105
106 if [ -e "$GIT_DIR/refs/heads/$branchname" ]
107 then
108         if test '' = "$force"
109         then
110                 die "$branchname already exists."
111         elif test "$branchname" = "$headref"
112         then
113                 die "cannot force-update the current branch."
114         fi
115 fi
116 git update-ref "refs/heads/$branchname" $rev
117