GIT 0.99.8f
[git.git] / git-checkout.sh
blob2c053a33c3c78f27150cdd397ad28641787abb3a
1 #!/bin/sh
2 . git-sh-setup || die "Not a git archive"
4 old=$(git-rev-parse HEAD)
5 new=
6 force=
7 branch=
8 newbranch=
9 while [ "$#" != "0" ]; do
10 arg="$1"
11 shift
12 case "$arg" in
13 "-b")
14 newbranch="$1"
15 shift
16 [ -z "$newbranch" ] &&
17 die "git checkout: -b needs a branch name"
18 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
19 die "git checkout: branch $newbranch already exists"
20 git-check-ref-format "heads/$newbranch" ||
21 die "we do not like '$newbranch' as a branch name."
23 "-f")
24 force=1
27 rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) ||
28 die "I don't know any '$arg'."
29 if [ -z "$rev" ]; then
30 echo "unknown flag $arg"
31 exit 1
33 if [ "$new" ]; then
34 echo "Multiple revisions?"
35 exit 1
37 new="$rev"
38 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
39 branch="$arg"
42 esac
43 done
44 [ -z "$new" ] && new=$old
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
52 [ -z "$branch$newbranch" ] &&
53 [ "$new" != "$old" ] &&
54 die "git checkout: you need to specify a new branch name"
56 if [ "$force" ]
57 then
58 git-read-tree --reset $new &&
59 git-checkout-index -q -f -u -a
60 else
61 git-update-index --refresh >/dev/null
62 git-read-tree -m -u $old $new
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)
71 if [ "$?" -eq 0 ]; then
72 if [ "$newbranch" ]; then
73 echo $new > "$GIT_DIR/refs/heads/$newbranch"
74 branch="$newbranch"
76 [ "$branch" ] &&
77 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
78 rm -f "$GIT_DIR/MERGE_HEAD"
79 else
80 exit 1