Documentation/git-verify-pack.txt: added documentation for --.
[git/dscho.git] / git-branch.sh
blob5306b2719ffbedfa077f4d5489ab46448ef5c186
1 #!/bin/sh
3 GIT_DIR=`git-rev-parse --git-dir` || exit $?
5 die () {
6 echo >&2 "$*"
7 exit 1
10 usage () {
11 echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]]
13 If no arguments, show available branches and mark current branch with a star.
14 If one argument, create a new branch <branchname> based off of current HEAD.
15 If two arguments, create a new branch <branchname> based off of <start-point>.
17 exit 1
20 headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
22 delete_branch () {
23 option="$1"
24 shift
25 for branch_name
27 case ",$headref," in
28 ",$branch_name,")
29 die "Cannot delete the branch you are on." ;;
30 ,,)
31 die "What branch are you on anyway?" ;;
32 esac
33 branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
34 branch=$(git-rev-parse --verify "$branch^0") ||
35 die "Seriously, what branch are you talking about?"
36 case "$option" in
37 -D)
40 mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
41 case " $mbs " in
42 *' '$branch' '*)
43 # the merge base of branch and HEAD contains branch --
44 # which means that the HEAD contains everything in the HEAD.
47 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
48 If you are sure you want to delete it, run 'git branch -D $branch_name'."
49 exit 1
51 esac
53 esac
54 rm -f "$GIT_DIR/refs/heads/$branch_name"
55 echo "Deleted branch $branch_name."
56 done
57 exit 0
60 force=
61 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
63 case "$1" in
64 -d | -D)
65 delete_branch "$@"
66 exit
68 -f)
69 force="$1"
71 --)
72 shift
73 break
75 -*)
76 usage
78 esac
79 shift
80 done
82 case "$#" in
84 git-rev-parse --symbolic --all |
85 sed -ne 's|^refs/heads/||p' |
86 sort |
87 while read ref
89 if test "$headref" = "$ref"
90 then
91 pfx='*'
92 else
93 pfx=' '
95 echo "$pfx $ref"
96 done
97 exit 0 ;;
99 head=HEAD ;;
101 head="$2^0" ;;
102 esac
103 branchname="$1"
105 rev=$(git-rev-parse --verify "$head") || exit
107 git-check-ref-format "heads/$branchname" ||
108 die "we do not like '$branchname' as a branch name."
110 if [ -e "$GIT_DIR/refs/heads/$branchname" ]
111 then
112 if test '' = "$force"
113 then
114 die "$branchname already exists."
115 elif test "$branchname" = "$headref"
116 then
117 die "cannot force-update the current branch."
120 git update-ref "refs/heads/$branchname" $rev