1#!/bin/sh 2 3USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]' 4SUBDIRECTORY_OK=Sometimes 5. git-sh-setup 6 7old_name=HEAD 8old=$(git-rev-parse --verify $old_name 2>/dev/null) 9new= 10new_name= 11force= 12branch= 13newbranch= 14newbranch_log= 15merge= 16while["$#"!="0"];do 17 arg="$1" 18shift 19case"$arg"in 20"-b") 21 newbranch="$1" 22shift 23[-z"$newbranch"] && 24 die "git checkout: -b needs a branch name" 25 git-show-ref --verify --quiet --"refs/heads/$newbranch"&& 26 die "git checkout: branch$newbranchalready exists" 27 git-check-ref-format"heads/$newbranch"|| 28 die "git checkout: we do not like '$newbranch' as a branch name." 29;; 30"-l") 31 newbranch_log=1 32;; 33"-f") 34 force=1 35;; 36-m) 37 merge=1 38;; 39--) 40break 41;; 42-*) 43 usage 44;; 45*) 46ifrev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) 47then 48if[-z"$rev"];then 49echo"unknown flag$arg" 50exit1 51fi 52 new="$rev" 53 new_name="$arg^0" 54if git-show-ref --verify --quiet --"refs/heads/$arg" 55then 56 branch="$arg" 57fi 58elifrev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null) 59then 60# checking out selected paths from a tree-ish. 61 new="$rev" 62 new_name="$arg^{tree}" 63 branch= 64else 65 new= 66 new_name= 67 branch= 68set x "$arg""$@" 69shift 70fi 71case"$1"in 72--) 73shift;; 74esac 75break 76;; 77esac 78done 79 80# The behaviour of the command with and without explicit path 81# parameters is quite different. 82# 83# Without paths, we are checking out everything in the work tree, 84# possibly switching branches. This is the traditional behaviour. 85# 86# With paths, we are _never_ switching branch, but checking out 87# the named paths from either index (when no rev is given), 88# or the named tree-ish (when rev is given). 89 90iftest"$#"-ge1 91then 92 hint= 93iftest"$#"-eq1 94then 95 hint=" 96Did you intend to checkout '$@' which can not be resolved as commit?" 97fi 98iftest''!="$newbranch$force$merge" 99then 100 die "git checkout: updating paths is incompatible with switching branches/forcing$hint" 101fi 102iftest''!="$new" 103then 104# from a specific tree-ish; note that this is for 105# rescuing paths and is never meant to remove what 106# is not in the named tree-ish. 107 git-ls-tree --full-name -r"$new""$@"| 108 git-update-index --index-info||exit $? 109fi 110 git-checkout-index -f -u --"$@" 111exit $? 112else 113# Make sure we did not fall back on $arg^{tree} codepath 114# since we are not checking out from an arbitrary tree-ish, 115# but switching branches. 116iftest''!="$new" 117then 118 git-rev-parse --verify"$new^{commit}">/dev/null 2>&1|| 119 die "Cannot switch branch to a non-commit." 120fi 121fi 122 123# We are switching branches and checking out trees, so 124# we *NEED* to be at the toplevel. 125cdup=$(git-rev-parse --show-cdup) 126iftest!-z"$cdup" 127then 128cd"$cdup" 129fi 130 131[-z"$new"] && new=$old&& new_name="$old_name" 132 133# If we don't have an old branch that we're switching to, 134# and we don't have a new branch name for the target we 135# are switching to, then we'd better just be checking out 136# what we already had 137 138[-z"$branch$newbranch"] && 139["$new"!="$old"] && 140 die "git checkout: to checkout the requested commit you need to specify 141 a name for a new branch which is created and switched to" 142 143if["X$old"= X ] 144then 145echo"warning: You do not appear to currently be on a branch.">&2 146echo"warning: Forcing checkout of$new_name.">&2 147 force=1 148fi 149 150if["$force"] 151then 152 git-read-tree --reset -u$new 153else 154 git-update-index --refresh>/dev/null 155 merge_error=$(git-read-tree -m -u $old $new 2>&1)|| ( 156case"$merge"in 157'') 158echo>&2"$merge_error" 159exit1;; 160esac 161 162# Match the index to the working tree, and do a three-way. 163 git diff-files --name-only| git update-index --remove --stdin&& 164 work=`git write-tree`&& 165 git read-tree --reset -u$new&& 166 git read-tree -m -u --aggressive$old $new $work||exit 167 168if result=`git write-tree 2>/dev/null` 169then 170echo>&2"Trivially automerged." 171else 172 git merge-index -o git-merge-one-file -a 173fi 174 175# Do not register the cleanly merged paths in the index yet. 176# this is not a real merge before committing, but just carrying 177# the working tree changes along. 178 unmerged=`git ls-files -u` 179 git read-tree --reset$new 180case"$unmerged"in 181'') ;; 182*) 183( 184 z40=0000000000000000000000000000000000000000 185echo"$unmerged"| 186sed-e's/^[0-7]* [0-9a-f]* /'"0$z40/" 187echo"$unmerged" 188) | git update-index --index-info 189;; 190esac 191exit0 192) 193 saved_err=$? 194iftest"$saved_err"=0 195then 196test"$new"="$old"|| git diff-index --name-status"$new" 197fi 198(exit$saved_err) 199fi 200 201# 202# Switch the HEAD pointer to the new branch if we 203# checked out a branch head, and remove any potential 204# old MERGE_HEAD's (subsequent commits will clearly not 205# be based on them, since we re-set the index) 206# 207if["$?"-eq0];then 208if["$newbranch"];then 209if["$newbranch_log"];then 210mkdir-p$(dirname "$GIT_DIR/logs/refs/heads/$newbranch") 211touch"$GIT_DIR/logs/refs/heads/$newbranch" 212fi 213 git-update-ref -m"checkout: Created from$new_name""refs/heads/$newbranch"$new||exit 214 branch="$newbranch" 215fi 216["$branch"] && 217 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" 218rm-f"$GIT_DIR/MERGE_HEAD" 219else 220exit1 221fi