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