]> git.ipfire.org Git - thirdparty/git.git/blame - git-branch.sh
Documentation: more examples.
[thirdparty/git.git] / git-branch.sh
CommitLineData
37f1a519
LT
1#!/bin/sh
2
9cc2527c 3GIT_DIR=`git-rev-parse --git-dir` || exit $?
37f1a519 4
d165fa14
JH
5die () {
6 echo >&2 "$*"
7 exit 1
8}
9
a3b427b9 10usage () {
eb777612 11 echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]]
a3b427b9
AW
12
13If no arguments, show available branches and mark current branch with a star.
14If one argument, create a new branch <branchname> based off of current HEAD.
15If two arguments, create a new branch <branchname> based off of <start-point>.
16'
17 exit 1
18}
19
9cc2527c 20headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
eb777612 21
ba65af9c 22delete_branch () {
03feddd6
JH
23 option="$1"
24 shift
03feddd6
JH
25 for branch_name
26 do
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)
ba65af9c
JH
38 ;;
39 *)
03feddd6
JH
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.
45 ;;
46 *)
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
50 ;;
51 esac
ba65af9c
JH
52 ;;
53 esac
03feddd6
JH
54 rm -f "$GIT_DIR/refs/heads/$branch_name"
55 echo "Deleted branch $branch_name."
56 done
ba65af9c
JH
57 exit 0
58}
59
eb777612 60force=
ba65af9c
JH
61while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
62do
63 case "$1" in
64 -d | -D)
03feddd6 65 delete_branch "$@"
ba65af9c
JH
66 exit
67 ;;
eb777612
JH
68 -f)
69 force="$1"
70 ;;
ba65af9c
JH
71 --)
72 shift
73 break
74 ;;
75 -*)
76 usage
77 ;;
78 esac
79 shift
80done
81
e4aec26f
KV
82case "$#" in
830)
e4aec26f
KV
84 git-rev-parse --symbolic --all |
85 sed -ne 's|^refs/heads/||p' |
86 sort |
87 while read ref
88 do
89 if test "$headref" = "$ref"
90 then
91 pfx='*'
92 else
93 pfx=' '
94 fi
95 echo "$pfx $ref"
96 done
97 exit 0 ;;
981)
a38e7279 99 head=HEAD ;;
e4aec26f 1002)
a38e7279
JH
101 head="$2^0" ;;
102esac
e4aec26f 103branchname="$1"
a3b427b9 104
ff84d327 105rev=$(git-rev-parse --verify "$head") || exit
37f1a519 106
03feddd6
JH
107git-check-ref-format "heads/$branchname" ||
108 die "we do not like '$branchname' as a branch name."
37f1a519 109
eb777612
JH
110if [ -e "$GIT_DIR/refs/heads/$branchname" ]
111then
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."
118 fi
119fi
08db81a9 120git update-ref "refs/heads/$branchname" $rev