1#!/bin/sh 2. git-sh-setup|| 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 git-check-ref-format"heads/$newbranch"|| 21 die "we do not like '$newbranch' as a branch name." 22;; 23"-f") 24 force=1 25;; 26*) 27rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)|| 28 die "I don't know any '$arg'." 29if[-z"$rev"];then 30echo"unknown flag$arg" 31exit1 32fi 33if["$new"];then 34echo"Multiple revisions?" 35exit1 36fi 37 new="$rev" 38if[-f"$GIT_DIR/refs/heads/$arg"];then 39 branch="$arg" 40fi 41;; 42esac 43done 44[-z"$new"] && new=$old 45 46# 47# If we don't have an old branch that we're switching to, 48# and we don't have a new branch name for the target we 49# are switching to, then we'd better just be checking out 50# what we already had 51# 52[-z"$branch$newbranch"] && 53["$new"!="$old"] && 54 die "git checkout: you need to specify a new branch name" 55 56if["$force"] 57then 58 git-read-tree --reset$new&& 59 git-checkout-index -q -f -u -a 60else 61 git-update-index --refresh>/dev/null 62 git-read-tree -m -u$old $new 63fi 64 65# 66# Switch the HEAD pointer to the new branch if it we 67# checked out a branch head, and remove any potential 68# old MERGE_HEAD's (subsequent commits will clearly not 69# be based on them, since we re-set the index) 70# 71if["$?"-eq0];then 72if["$newbranch"];then 73echo$new>"$GIT_DIR/refs/heads/$newbranch" 74 branch="$newbranch" 75fi 76["$branch"] && 77 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" 78rm-f"$GIT_DIR/MERGE_HEAD" 79else 80exit1 81fi