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