]> git.ipfire.org Git - thirdparty/git.git/blame - git-branch.sh
[PATCH] Update git-clone documentation
[thirdparty/git.git] / git-branch.sh
CommitLineData
37f1a519
LT
1#!/bin/sh
2
215a7ad1 3. git-sh-setup || die "Not a git archive"
37f1a519 4
a3b427b9 5usage () {
ba65af9c 6 echo >&2 "usage: $(basename $0)"' [-d <branch>] | [<branch> [start-point]]
a3b427b9
AW
7
8If no arguments, show available branches and mark current branch with a star.
9If one argument, create a new branch <branchname> based off of current HEAD.
10If two arguments, create a new branch <branchname> based off of <start-point>.
11'
12 exit 1
13}
14
ba65af9c
JH
15delete_branch () {
16 option="$1" branch_name="$2"
8098a178
JH
17 headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
18 sed -e 's|^refs/heads/||')
ba65af9c
JH
19 case ",$headref," in
20 ",$branch_name,")
21 die "Cannot delete the branch you are on." ;;
22 ,,)
23 die "What branch are you on anyway?" ;;
24 esac
25 branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
26 branch=$(git-rev-parse --verify "$branch^0") ||
27 die "Seriously, what branch are you talking about?"
28 case "$option" in
29 -D)
30 ;;
31 *)
32 mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
33 case " $mbs " in
34 *' '$branch' '*)
35 # the merge base of branch and HEAD contains branch --
36 # which means that the HEAD contains everything in the HEAD.
37 ;;
38 *)
39 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
40If you are sure you want to delete it, run 'git branch -D $branch_name'."
41 exit 1
42 ;;
43 esac
44 ;;
45 esac
46 rm -f "$GIT_DIR/refs/heads/$branch_name"
47 echo "Deleted branch $branch_name."
48 exit 0
49}
50
51while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
52do
53 case "$1" in
54 -d | -D)
55 delete_branch "$1" "$2"
56 exit
57 ;;
58 --)
59 shift
60 break
61 ;;
62 -*)
63 usage
64 ;;
65 esac
66 shift
67done
68
e4aec26f
KV
69case "$#" in
700)
8098a178
JH
71 headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
72 sed -e 's|^refs/heads/||')
e4aec26f
KV
73 git-rev-parse --symbolic --all |
74 sed -ne 's|^refs/heads/||p' |
75 sort |
76 while read ref
77 do
78 if test "$headref" = "$ref"
79 then
80 pfx='*'
81 else
82 pfx=' '
83 fi
84 echo "$pfx $ref"
85 done
86 exit 0 ;;
871)
a38e7279 88 head=HEAD ;;
e4aec26f 892)
a38e7279
JH
90 head="$2^0" ;;
91esac
e4aec26f 92branchname="$1"
a3b427b9 93
ff84d327 94rev=$(git-rev-parse --verify "$head") || exit
37f1a519 95
37f1a519
LT
96[ -e "$GIT_DIR/refs/heads/$branchname" ] && die "$branchname already exists"
97
98echo $rev > "$GIT_DIR/refs/heads/$branchname"