1#!/bin/sh 2 3USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r' 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=$(git-show-ref --verify --hash -- "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 45 git update-ref -d"refs/heads/$branch_name""$branch" 46echo"Deleted branch$branch_name." 47done 48exit0 49} 50 51ls_remote_branches () { 52 git-rev-parse --symbolic --all| 53sed-ne's|^refs/\(remotes/\)|\1|p'| 54sort 55} 56 57force= 58create_log= 59while case"$#,$1"in0,*)break;; *,-*) ;; *)break;;esac 60do 61case"$1"in 62-d|-D) 63 delete_branch "$@" 64exit 65;; 66-r) 67 ls_remote_branches 68exit 69;; 70-f) 71 force="$1" 72;; 73-l) 74 create_log="yes" 75;; 76--) 77shift 78break 79;; 80-*) 81 usage 82;; 83esac 84shift 85done 86 87case"$#"in 880) 89 git-rev-parse --symbolic --branches| 90sort| 91whileread ref 92do 93iftest"$headref"="$ref" 94then 95 pfx='*' 96else 97 pfx=' ' 98fi 99echo"$pfx$ref" 100done 101exit0;; 1021) 103head=HEAD ;; 1042) 105head="$2^0";; 106esac 107branchname="$1" 108 109rev=$(git-rev-parse --verify "$head")||exit 110 111git-check-ref-format"heads/$branchname"|| 112 die "we do not like '$branchname' as a branch name." 113 114prev='' 115if git-show-ref --verify --quiet --"refs/heads/$branchname" 116then 117iftest''="$force" 118then 119 die "$branchnamealready exists." 120eliftest"$branchname"="$headref" 121then 122 die "cannot force-update the current branch." 123fi 124 prev=`git rev-parse --verify "refs/heads/$branchname"` 125fi 126iftest"$create_log"='yes' 127then 128mkdir-p$(dirname "$GIT_DIR/logs/refs/heads/$branchname") 129touch"$GIT_DIR/logs/refs/heads/$branchname" 130fi 131git update-ref -m"branch: Created from$head""refs/heads/$branchname""$rev""$prev"