Fix "--abbrev=xyz" for revision listing
[git/spearce.git] / git-branch.sh
blob134e68cf7f0389d73af000c4303c4dda27f89d57
1 #!/bin/sh
3 USAGE='[(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
4 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
5 If one argument, create a new branch <branchname> based off of current HEAD.
6 If two arguments, create a new branch <branchname> based off of <start-point>.'
8 SUBDIRECTORY_OK='Yes'
9 . git-sh-setup
11 headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
13 delete_branch () {
14 option="$1"
15 shift
16 for branch_name
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)
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 --
35 # which means that the HEAD contains everything in both.
38 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
39 If you are sure you want to delete it, run 'git branch -D $branch_name'."
40 exit 1
42 esac
44 esac
45 rm -f "$GIT_DIR/refs/heads/$branch_name"
46 echo "Deleted branch $branch_name."
47 done
48 exit 0
51 ls_remote_branches () {
52 git-rev-parse --symbolic --all |
53 sed -ne 's|^refs/\(remotes/\)|\1|p' |
54 sort
57 force=
58 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
60 case "$1" in
61 -d | -D)
62 delete_branch "$@"
63 exit
65 -r)
66 ls_remote_branches
67 exit
69 -f)
70 force="$1"
72 --)
73 shift
74 break
76 -*)
77 usage
79 esac
80 shift
81 done
83 case "$#" in
85 git-rev-parse --symbolic --branches |
86 sort |
87 while read ref
89 if test "$headref" = "$ref"
90 then
91 pfx='*'
92 else
93 pfx=' '
95 echo "$pfx $ref"
96 done
97 exit 0 ;;
99 head=HEAD ;;
101 head="$2^0" ;;
102 esac
103 branchname="$1"
105 rev=$(git-rev-parse --verify "$head") || exit
107 git-check-ref-format "heads/$branchname" ||
108 die "we do not like '$branchname' as a branch name."
110 if [ -e "$GIT_DIR/refs/heads/$branchname" ]
111 then
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."
120 git update-ref "refs/heads/$branchname" $rev