1#!/bin/sh 2 3GIT_DIR=`git-rev-parse --git-dir`||exit $? 4 5die () { 6echo>&2"$*" 7exit1 8} 9 10usage () { 11echo>&2"usage:$(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]] 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' 17exit1 18} 19 20headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||') 21 22delete_branch () { 23 option="$1" 24shift 25for branch_name 26do 27case",$headref,"in 28",$branch_name,") 29 die "Cannot delete the branch you are on.";; 30,,) 31 die "What branch are you on anyway?";; 32esac 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?" 36case"$option"in 37-D) 38;; 39*) 40 mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ') 41case"$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*) 47echo>&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'." 49exit1 50;; 51esac 52;; 53esac 54rm-f"$GIT_DIR/refs/heads/$branch_name" 55echo"Deleted branch$branch_name." 56done 57exit0 58} 59 60force= 61while case"$#,$1"in0,*)break;; *,-*) ;; *)break;;esac 62do 63case"$1"in 64-d|-D) 65 delete_branch "$@" 66exit 67;; 68-f) 69 force="$1" 70;; 71--) 72shift 73break 74;; 75-*) 76 usage 77;; 78esac 79shift 80done 81 82case"$#"in 830) 84 git-rev-parse --symbolic --all| 85sed-ne's|^refs/heads/||p'| 86sort| 87whileread ref 88do 89iftest"$headref"="$ref" 90then 91 pfx='*' 92else 93 pfx=' ' 94fi 95echo"$pfx$ref" 96done 97exit0;; 981) 99head=HEAD ;; 1002) 101head="$2^0";; 102esac 103branchname="$1" 104 105rev=$(git-rev-parse --verify "$head")||exit 106 107git-check-ref-format"heads/$branchname"|| 108 die "we do not like '$branchname' as a branch name." 109 110if[-e"$GIT_DIR/refs/heads/$branchname"] 111then 112iftest''="$force" 113then 114 die "$branchnamealready exists." 115eliftest"$branchname"="$headref" 116then 117 die "cannot force-update the current branch." 118fi 119fi 120git update-ref"refs/heads/$branchname"$rev