1#!/bin/sh 2 3GIT_DIR=`git-rev-parse --git-dir`||exit $? 4 5usage () { 6echo>&2"usage:$(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]] 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' 12exit1 13} 14 15headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||') 16 17delete_branch () { 18 option="$1" 19shift 20for branch_name 21do 22case",$headref,"in 23",$branch_name,") 24 die "Cannot delete the branch you are on.";; 25,,) 26 die "What branch are you on anyway?";; 27esac 28 branch=$(cat "$GIT_DIR/refs/heads/$branch_name")&& 29 branch=$(git-rev-parse --verify "$branch^0")|| 30 die "Seriously, what branch are you talking about?" 31case"$option"in 32-D) 33;; 34*) 35 mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ') 36case"$mbs"in 37*' '$branch' '*) 38# the merge base of branch and HEAD contains branch -- 39# which means that the HEAD contains everything in the HEAD. 40;; 41*) 42echo>&2"The branch '$branch_name' is not a strict subset of your current HEAD. 43 If you are sure you want to delete it, run 'git branch -D$branch_name'." 44exit1 45;; 46esac 47;; 48esac 49rm-f"$GIT_DIR/refs/heads/$branch_name" 50echo"Deleted branch$branch_name." 51done 52exit0 53} 54 55force= 56while case"$#,$1"in0,*)break;; *,-*) ;; *)break;;esac 57do 58case"$1"in 59-d|-D) 60 delete_branch "$@" 61exit 62;; 63-f) 64 force="$1" 65;; 66--) 67shift 68break 69;; 70-*) 71 usage 72;; 73esac 74shift 75done 76 77case"$#"in 780) 79 git-rev-parse --symbolic --all| 80sed-ne's|^refs/heads/||p'| 81sort| 82whileread ref 83do 84iftest"$headref"="$ref" 85then 86 pfx='*' 87else 88 pfx=' ' 89fi 90echo"$pfx$ref" 91done 92exit0;; 931) 94head=HEAD ;; 952) 96head="$2^0";; 97esac 98branchname="$1" 99 100rev=$(git-rev-parse --verify "$head")||exit 101 102git-check-ref-format"heads/$branchname"|| 103 die "we do not like '$branchname' as a branch name." 104 105if[-e"$GIT_DIR/refs/heads/$branchname"] 106then 107iftest''="$force" 108then 109 die "$branchnamealready exists." 110eliftest"$branchname"="$headref" 111then 112 die "cannot force-update the current branch." 113fi 114fi 115git update-ref"refs/heads/$branchname"$rev