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