]> git.ipfire.org Git - thirdparty/git.git/blame - git-branch.sh
GIT 1.1.1
[thirdparty/git.git] / git-branch.sh
CommitLineData
37f1a519
LT
1#!/bin/sh
2
d025524d
FK
3USAGE='[-d <branch>] | [[-f] <branch> [start-point]]'
4LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
a3b427b9 5If one argument, create a new branch <branchname> based off of current HEAD.
d025524d
FK
6If two arguments, create a new branch <branchname> based off of <start-point>.'
7
8SUBDIRECTORY_OK='Yes'
9. git-sh-setup
a3b427b9 10
9cc2527c 11headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
eb777612 12
ba65af9c 13delete_branch () {
03feddd6
JH
14 option="$1"
15 shift
03feddd6
JH
16 for branch_name
17 do
18 case ",$headref," in
19 ",$branch_name,")
20 die "Cannot delete the branch you are on." ;;
21 ,,)
22 die "What branch are you on anyway?" ;;
23 esac
24 branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
25 branch=$(git-rev-parse --verify "$branch^0") ||
26 die "Seriously, what branch are you talking about?"
27 case "$option" in
28 -D)
ba65af9c
JH
29 ;;
30 *)
03feddd6
JH
31 mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
32 case " $mbs " in
33 *' '$branch' '*)
34 # the merge base of branch and HEAD contains branch --
01385e27 35 # which means that the HEAD contains everything in both.
03feddd6
JH
36 ;;
37 *)
38 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
01385e27 39If you are sure you want to delete it, run 'git branch -D $branch_name'."
03feddd6
JH
40 exit 1
41 ;;
42 esac
ba65af9c
JH
43 ;;
44 esac
03feddd6
JH
45 rm -f "$GIT_DIR/refs/heads/$branch_name"
46 echo "Deleted branch $branch_name."
47 done
ba65af9c
JH
48 exit 0
49}
50
eb777612 51force=
ba65af9c
JH
52while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
53do
54 case "$1" in
55 -d | -D)
03feddd6 56 delete_branch "$@"
ba65af9c
JH
57 exit
58 ;;
eb777612
JH
59 -f)
60 force="$1"
61 ;;
ba65af9c
JH
62 --)
63 shift
64 break
65 ;;
66 -*)
67 usage
68 ;;
69 esac
70 shift
71done
72
e4aec26f
KV
73case "$#" in
740)
e4aec26f
KV
75 git-rev-parse --symbolic --all |
76 sed -ne 's|^refs/heads/||p' |
77 sort |
78 while read ref
79 do
80 if test "$headref" = "$ref"
81 then
82 pfx='*'
83 else
84 pfx=' '
85 fi
86 echo "$pfx $ref"
87 done
88 exit 0 ;;
891)
a38e7279 90 head=HEAD ;;
e4aec26f 912)
a38e7279
JH
92 head="$2^0" ;;
93esac
e4aec26f 94branchname="$1"
a3b427b9 95
ff84d327 96rev=$(git-rev-parse --verify "$head") || exit
37f1a519 97
03feddd6
JH
98git-check-ref-format "heads/$branchname" ||
99 die "we do not like '$branchname' as a branch name."
37f1a519 100
eb777612
JH
101if [ -e "$GIT_DIR/refs/heads/$branchname" ]
102then
103 if test '' = "$force"
104 then
105 die "$branchname already exists."
106 elif test "$branchname" = "$headref"
107 then
108 die "cannot force-update the current branch."
109 fi
110fi
08db81a9 111git update-ref "refs/heads/$branchname" $rev