]> git.ipfire.org Git - thirdparty/git.git/blame - git-branch.sh
git-branch: -f to forcibly reset branch head.
[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 () {
eb777612 6 echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <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
eb777612
JH
15headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
16 sed -e 's|^refs/heads/||')
17
ba65af9c 18delete_branch () {
03feddd6
JH
19 option="$1"
20 shift
03feddd6
JH
21 for branch_name
22 do
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)
ba65af9c
JH
34 ;;
35 *)
03feddd6
JH
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.
41 ;;
42 *)
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
46 ;;
47 esac
ba65af9c
JH
48 ;;
49 esac
03feddd6
JH
50 rm -f "$GIT_DIR/refs/heads/$branch_name"
51 echo "Deleted branch $branch_name."
52 done
ba65af9c
JH
53 exit 0
54}
55
eb777612 56force=
ba65af9c
JH
57while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
58do
59 case "$1" in
60 -d | -D)
03feddd6 61 delete_branch "$@"
ba65af9c
JH
62 exit
63 ;;
eb777612
JH
64 -f)
65 force="$1"
66 ;;
ba65af9c
JH
67 --)
68 shift
69 break
70 ;;
71 -*)
72 usage
73 ;;
74 esac
75 shift
76done
77
e4aec26f
KV
78case "$#" in
790)
e4aec26f
KV
80 git-rev-parse --symbolic --all |
81 sed -ne 's|^refs/heads/||p' |
82 sort |
83 while read ref
84 do
85 if test "$headref" = "$ref"
86 then
87 pfx='*'
88 else
89 pfx=' '
90 fi
91 echo "$pfx $ref"
92 done
93 exit 0 ;;
941)
a38e7279 95 head=HEAD ;;
e4aec26f 962)
a38e7279
JH
97 head="$2^0" ;;
98esac
e4aec26f 99branchname="$1"
a3b427b9 100
ff84d327 101rev=$(git-rev-parse --verify "$head") || exit
37f1a519 102
03feddd6
JH
103git-check-ref-format "heads/$branchname" ||
104 die "we do not like '$branchname' as a branch name."
37f1a519 105
eb777612
JH
106if [ -e "$GIT_DIR/refs/heads/$branchname" ]
107then
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."
114 fi
115fi
08db81a9
AR
116git update-ref "refs/heads/$branchname" $rev
117