Revert "check_packed_git_idx(): check integrity of the idx file itself."
[git/mingw/4msysgit/wingit-dll.git] / git-checkout.sh
blob36308d22c6a72b9ad53ced60bd3c70a2e17520b6
1 #!/bin/sh
3 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
4 . git-sh-setup
6 old=$(git-rev-parse HEAD)
7 new=
8 force=
9 branch=
10 newbranch=
11 while [ "$#" != "0" ]; do
12 arg="$1"
13 shift
14 case "$arg" in
15 "-b")
16 newbranch="$1"
17 shift
18 [ -z "$newbranch" ] &&
19 die "git checkout: -b needs a branch name"
20 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
21 die "git checkout: branch $newbranch already exists"
22 git-check-ref-format "heads/$newbranch" ||
23 die "we do not like '$newbranch' as a branch name."
25 "-f")
26 force=1
28 --)
29 break
31 -*)
32 usage
35 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
36 then
37 if [ -z "$rev" ]; then
38 echo "unknown flag $arg"
39 exit 1
41 new="$rev"
42 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
43 branch="$arg"
45 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
46 then
47 # checking out selected paths from a tree-ish.
48 new="$rev"
49 branch=
50 else
51 new=
52 branch=
53 set x "$arg" "$@"
54 shift
56 break
58 esac
59 done
61 # The behaviour of the command with and without explicit path
62 # parameters is quite different.
64 # Without paths, we are checking out everything in the work tree,
65 # possibly switching branches. This is the traditional behaviour.
67 # With paths, we are _never_ switching branch, but checking out
68 # the named paths from either index (when no rev is given),
69 # or the named tree-ish (when rev is given).
71 if test "$#" -ge 1
72 then
73 if test '' != "$newbranch$force"
74 then
75 die "updating paths and switching branches or forcing are incompatible."
77 if test '' != "$new"
78 then
79 # from a specific tree-ish; note that this is for
80 # rescuing paths and is never meant to remove what
81 # is not in the named tree-ish.
82 git-ls-tree -r "$new" "$@" |
83 git-update-index --index-info || exit $?
85 git-checkout-index -f -u -- "$@"
86 exit $?
87 else
88 # Make sure we did not fall back on $arg^{tree} codepath
89 # since we are not checking out from an arbitrary tree-ish,
90 # but switching branches.
91 if test '' != "$new"
92 then
93 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
94 die "Cannot switch branch to a non-commit."
98 [ -z "$new" ] && new=$old
100 # If we don't have an old branch that we're switching to,
101 # and we don't have a new branch name for the target we
102 # are switching to, then we'd better just be checking out
103 # what we already had
105 [ -z "$branch$newbranch" ] &&
106 [ "$new" != "$old" ] &&
107 die "git checkout: you need to specify a new branch name"
109 if [ "$force" ]
110 then
111 git-read-tree --reset $new &&
112 git-checkout-index -q -f -u -a
113 else
114 git-update-index --refresh >/dev/null
115 git-read-tree -m -u $old $new
119 # Switch the HEAD pointer to the new branch if we
120 # checked out a branch head, and remove any potential
121 # old MERGE_HEAD's (subsequent commits will clearly not
122 # be based on them, since we re-set the index)
124 if [ "$?" -eq 0 ]; then
125 if [ "$newbranch" ]; then
126 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
127 mkdir -p "$GIT_DIR/$leading" &&
128 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
129 branch="$newbranch"
131 [ "$branch" ] &&
132 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
133 rm -f "$GIT_DIR/MERGE_HEAD"
134 else
135 exit 1