arguments cleanup and some formatting
[git/dscho.git] / git-branch.sh
blob2594518e9f7a812a77aaba5b10f3194a7eced423
1 #!/bin/sh
3 . git-sh-setup || die "Not a git archive"
5 usage () {
6 echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]]
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>.
12 exit 1
15 headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
16 sed -e 's|^refs/heads/||')
18 delete_branch () {
19 option="$1"
20 shift
21 for branch_name
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)
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.
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
47 esac
49 esac
50 rm -f "$GIT_DIR/refs/heads/$branch_name"
51 echo "Deleted branch $branch_name."
52 done
53 exit 0
56 force=
57 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
59 case "$1" in
60 -d | -D)
61 delete_branch "$@"
62 exit
64 -f)
65 force="$1"
67 --)
68 shift
69 break
71 -*)
72 usage
74 esac
75 shift
76 done
78 case "$#" in
80 git-rev-parse --symbolic --all |
81 sed -ne 's|^refs/heads/||p' |
82 sort |
83 while read ref
85 if test "$headref" = "$ref"
86 then
87 pfx='*'
88 else
89 pfx=' '
91 echo "$pfx $ref"
92 done
93 exit 0 ;;
95 head=HEAD ;;
97 head="$2^0" ;;
98 esac
99 branchname="$1"
101 rev=$(git-rev-parse --verify "$head") || exit
103 git-check-ref-format "heads/$branchname" ||
104 die "we do not like '$branchname' as a branch name."
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."
116 git update-ref "refs/heads/$branchname" $rev