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