1#!/bin/sh
23
USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
4LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
5If one argument, create a new branch <branchname> based off of current HEAD.
6If two arguments, create a new branch <branchname> based off of <start-point>.'
78
SUBDIRECTORY_OK='Yes'
9. git-sh-setup
1011
headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
1213
delete_branch () {
14option="$1"
15shift
16for branch_name
17do
18case ",$headref," in
19",$branch_name,")
20die "Cannot delete the branch you are on." ;;
21,,)
22die "What branch are you on anyway?" ;;
23esac
24branch=$(git-show-ref --verify --hash -- "refs/heads/$branch_name") &&
25branch=$(git-rev-parse --verify "$branch^0") ||
26die "Seriously, what branch are you talking about?"
27case "$option" in
28-D)
29;;
30*)
31mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
32case " $mbs " in
33*' '$branch' '*)
34# the merge base of branch and HEAD contains branch --
35# which means that the HEAD contains everything in both.
36;;
37*)
38echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
39If you are sure you want to delete it, run 'git branch -D $branch_name'."
40exit 1
41;;
42esac
43;;
44esac
45git update-ref -d "refs/heads/$branch_name" "$branch"
46echo "Deleted branch $branch_name."
47done
48exit 0
49}
5051
ls_remote_branches () {
52git-rev-parse --symbolic --all |
53sed -ne 's|^refs/\(remotes/\)|\1|p' |
54sort
55}
5657
force=
58create_log=
59while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
60do
61case "$1" in
62-d | -D)
63delete_branch "$@"
64exit
65;;
66-r)
67ls_remote_branches
68exit
69;;
70-f)
71force="$1"
72;;
73-l)
74create_log="yes"
75;;
76--)
77shift
78break
79;;
80-*)
81usage
82;;
83esac
84shift
85done
8687
case "$#" in
880)
89git-rev-parse --symbolic --branches |
90sort |
91while read ref
92do
93if test "$headref" = "$ref"
94then
95pfx='*'
96else
97pfx=' '
98fi
99echo "$pfx $ref"
100done
101exit 0 ;;
1021)
103head=HEAD ;;
1042)
105head="$2^0" ;;
106esac
107branchname="$1"
108109
rev=$(git-rev-parse --verify "$head") || exit
110111
git-check-ref-format "heads/$branchname" ||
112die "we do not like '$branchname' as a branch name."
113114
if [ -d "$GIT_DIR/refs/heads/$branchname" ]
115then
116for refdir in `cd "$GIT_DIR" && \
117find "refs/heads/$branchname" -type d | sort -r`
118do
119rmdir "$GIT_DIR/$refdir" || \
120die "Could not delete '$refdir', there may still be a ref there."
121done
122fi
123124
branchdir=$(dirname $branchname)
125while test "$branchdir" != "."
126do
127if git-show-ref --verify --quiet -- "refs/heads/$branchdir"
128then
129die "$branchdir already exists."
130fi
131branchdir=$(dirname $branchdir)
132done
133134
prev=''
135if git-show-ref --verify --quiet -- "refs/heads/$branchname"
136then
137if test '' = "$force"
138then
139die "$branchname already exists."
140elif test "$branchname" = "$headref"
141then
142die "cannot force-update the current branch."
143fi
144prev=`git rev-parse --verify "refs/heads/$branchname"`
145fi
146if test "$create_log" = 'yes'
147then
148mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
149touch "$GIT_DIR/logs/refs/heads/$branchname"
150fi
151git update-ref -m "branch: Created from $head" "refs/heads/$branchname" "$rev" "$prev"