1#!/bin/sh 2. git-sh-setup-script|| die "Not a git archive" 3 4old=$(git-rev-parse HEAD) 5new= 6force= 7branch= 8newbranch= 9while["$#"!="0"];do 10 arg="$1" 11shift 12case"$arg"in 13"-b") 14 newbranch="$1" 15shift 16[-z"$newbranch"] && 17 die "git checkout: -b needs a branch name" 18[-e"$GIT_DIR/refs/heads/$newbranch"] && 19 die "git checkout: branch$newbranchalready exists" 20;; 21"-f") 22 force=1 23;; 24*) 25rev=$(git-rev-parse --verify "$arg^0")||exit 26if[-z"$rev"];then 27echo"unknown flag$arg" 28exit1 29fi 30if["$new"];then 31echo"Multiple revisions?" 32exit1 33fi 34 new="$rev" 35if[-f"$GIT_DIR/refs/heads/$arg"];then 36 branch="$arg" 37fi 38;; 39esac 40done 41[-z"$new"] && new=$old 42 43# 44# If we don't have an old branch that we're switching to, 45# and we don't have a new branch name for the target we 46# are switching to, then we'd better just be checking out 47# what we already had 48# 49[-z"$branch$newbranch"] && 50["$new"!="$old"] && 51 die "git checkout: you need to specify a new branch name" 52 53if["$force"] 54then 55 git-read-tree --reset$new&& 56 git-checkout-cache -q -f -u -a 57else 58 git-read-tree -m -u$old $new 59fi 60 61# 62# Switch the HEAD pointer to the new branch if it we 63# checked out a branch head, and remove any potential 64# old MERGE_HEAD's (subsequent commits will clearly not 65# be based on them, since we re-set the index) 66# 67if["$?"-eq0];then 68if["$newbranch"];then 69echo$new>"$GIT_DIR/refs/heads/$newbranch" 70 branch="$newbranch" 71fi 72["$branch"] &&ln-sf"refs/heads/$branch""$GIT_DIR/HEAD" 73rm-f"$GIT_DIR/MERGE_HEAD" 74else 75exit1 76fi