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=$(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/logs/refs/heads/$branch_name" 46rm-f"$GIT_DIR/refs/heads/$branch_name" 47echo"Deleted branch$branch_name." 48done 49exit0 50} 51 52ls_remote_branches () { 53 git-rev-parse --symbolic --all| 54sed-ne's|^refs/\(remotes/\)|\1|p'| 55sort 56} 57 58force= 59create_log= 60while case"$#,$1"in0,*)break;; *,-*) ;; *)break;;esac 61do 62case"$1"in 63-d|-D) 64 delete_branch "$@" 65exit 66;; 67-r) 68 ls_remote_branches 69exit 70;; 71-f) 72 force="$1" 73;; 74-l) 75 create_log="yes" 76;; 77--) 78shift 79break 80;; 81-*) 82 usage 83;; 84esac 85shift 86done 87 88case"$#"in 890) 90 git-rev-parse --symbolic --branches| 91sort| 92whileread ref 93do 94iftest"$headref"="$ref" 95then 96 pfx='*' 97else 98 pfx=' ' 99fi 100echo"$pfx$ref" 101done 102exit0;; 1031) 104head=HEAD ;; 1052) 106head="$2^0";; 107esac 108branchname="$1" 109 110rev=$(git-rev-parse --verify "$head")||exit 111 112git-check-ref-format"heads/$branchname"|| 113 die "we do not like '$branchname' as a branch name." 114 115if[-d"$GIT_DIR/refs/heads/$branchname"] 116then 117for refdir in`cd "$GIT_DIR" && \ 118 find "refs/heads/$branchname" -type d | sort -r` 119do 120rmdir"$GIT_DIR/$refdir"|| \ 121 die "Could not delete '$refdir', there may still be a ref there." 122done 123fi 124 125if[-e"$GIT_DIR/refs/heads/$branchname"] 126then 127iftest''="$force" 128then 129 die "$branchnamealready exists." 130eliftest"$branchname"="$headref" 131then 132 die "cannot force-update the current branch." 133fi 134fi 135iftest"$create_log"='yes' 136then 137mkdir-p$(dirname "$GIT_DIR/logs/refs/heads/$branchname") 138touch"$GIT_DIR/logs/refs/heads/$branchname" 139fi 140git update-ref -m"branch: Created from$head""refs/heads/$branchname"$rev