Update README.MinGW.
[git/mingw.git] / git-checkout.sh
blob3e09f4d92b0267ba57d26b53eeafd5f41c65b827
1 #!/bin/sh
3 USAGE='[-q] [-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
5 . git-sh-setup
6 require_work_tree
8 old_name=HEAD
9 old=$(git-rev-parse --verify $old_name 2>/dev/null)
10 oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
11 new=
12 new_name=
13 force=
14 branch=
15 newbranch=
16 newbranch_log=
17 merge=
18 quiet=
19 LF='
21 while [ "$#" != "0" ]; do
22 arg="$1"
23 shift
24 case "$arg" in
25 "-b")
26 newbranch="$1"
27 shift
28 [ -z "$newbranch" ] &&
29 die "git checkout: -b needs a branch name"
30 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
31 die "git checkout: branch $newbranch already exists"
32 git-check-ref-format "heads/$newbranch" ||
33 die "git checkout: we do not like '$newbranch' as a branch name."
35 "-l")
36 newbranch_log=1
38 "-f")
39 force=1
41 -m)
42 merge=1
44 "-q")
45 quiet=1
47 --)
48 break
50 -*)
51 usage
54 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
55 then
56 if [ -z "$rev" ]; then
57 echo "unknown flag $arg"
58 exit 1
60 new="$rev"
61 new_name="$arg"
62 if git-show-ref --verify --quiet -- "refs/heads/$arg"
63 then
64 branch="$arg"
66 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
67 then
68 # checking out selected paths from a tree-ish.
69 new="$rev"
70 new_name="$arg^{tree}"
71 branch=
72 else
73 new=
74 new_name=
75 branch=
76 set x "$arg" "$@"
77 shift
79 case "$1" in
80 --)
81 shift ;;
82 esac
83 break
85 esac
86 done
88 case "$force$merge" in
89 11)
90 die "git checkout: -f and -m are incompatible"
91 esac
93 # The behaviour of the command with and without explicit path
94 # parameters is quite different.
96 # Without paths, we are checking out everything in the work tree,
97 # possibly switching branches. This is the traditional behaviour.
99 # With paths, we are _never_ switching branch, but checking out
100 # the named paths from either index (when no rev is given),
101 # or the named tree-ish (when rev is given).
103 if test "$#" -ge 1
104 then
105 hint=
106 if test "$#" -eq 1
107 then
108 hint="
109 Did you intend to checkout '$@' which can not be resolved as commit?"
111 if test '' != "$newbranch$force$merge"
112 then
113 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
115 if test '' != "$new"
116 then
117 # from a specific tree-ish; note that this is for
118 # rescuing paths and is never meant to remove what
119 # is not in the named tree-ish.
120 git-ls-tree --full-name -r "$new" "$@" |
121 git-update-index --index-info || exit $?
124 # Make sure the request is about existing paths.
125 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
126 git-ls-files -- "$@" |
127 git-checkout-index -f -u --stdin
128 exit $?
129 else
130 # Make sure we did not fall back on $arg^{tree} codepath
131 # since we are not checking out from an arbitrary tree-ish,
132 # but switching branches.
133 if test '' != "$new"
134 then
135 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
136 die "Cannot switch branch to a non-commit."
140 # We are switching branches and checking out trees, so
141 # we *NEED* to be at the toplevel.
142 cd_to_toplevel
144 [ -z "$new" ] && new=$old && new_name="$old_name"
146 # If we don't have an existing branch that we're switching to,
147 # and we don't have a new branch name for the target we
148 # are switching to, then we are detaching our HEAD from any
149 # branch. However, if "git checkout HEAD" detaches the HEAD
150 # from the current branch, even though that may be logically
151 # correct, it feels somewhat funny. More importantly, we do not
152 # want "git checkout" nor "git checkout -f" to detach HEAD.
154 detached=
155 detach_warn=
157 if test -z "$branch$newbranch" && test "$new" != "$old"
158 then
159 detached="$new"
160 if test -n "$oldbranch" && test -z "$quiet"
161 then
162 detach_warn="Note: moving to \"$new_name\" which isn't a local branch
163 If you want to create a new branch from this checkout, you may do so
164 (now or later) by using -b with the checkout command again. Example:
165 git checkout -b <new_branch_name>"
167 elif test -z "$oldbranch" && test -z "$quiet"
168 then
169 echo >&2 "Previous HEAD position was $old"
172 if [ "X$old" = X ]
173 then
174 if test -z "$quiet"
175 then
176 echo >&2 "warning: You appear to be on a branch yet to be born."
177 echo >&2 "warning: Forcing checkout of $new_name."
179 force=1
182 if [ "$force" ]
183 then
184 git-read-tree --reset -u $new
185 else
186 git-update-index --refresh >/dev/null
187 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
188 case "$merge" in
190 echo >&2 "$merge_error"
191 exit 1 ;;
192 esac
194 # First stash away the local changes
195 git diff-index --binary -p HEAD >"$GIT_DIR"/LOCAL_DIFF
197 # Match the index to the working tree, and do a three-way.
198 git diff-files --name-only | git update-index --remove --stdin &&
199 work=`git write-tree` &&
200 git read-tree --reset -u $new || exit
202 eval GITHEAD_$new=${new_name:-${branch:-$new}} &&
203 eval GITHEAD_$work=local &&
204 export GITHEAD_$new GITHEAD_$work &&
205 git merge-recursive $old -- $new $work
207 # Do not register the cleanly merged paths in the index yet.
208 # this is not a real merge before committing, but just carrying
209 # the working tree changes along.
210 unmerged=`git ls-files -u`
211 git read-tree --reset $new
212 case "$unmerged" in
213 '') ;;
216 z40=0000000000000000000000000000000000000000
217 echo "$unmerged" |
218 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
219 echo "$unmerged"
220 ) | git update-index --index-info
222 echo >&2 "Conflicts in locally modified files:"
223 git diff --name-only --diff-filter=U >&2
224 echo >&2 "Your local changes are found in $GIT_DIR/LOCAL_DIFF"
226 esac
227 exit 0
229 saved_err=$?
230 if test "$saved_err" = 0 && test -z "$quiet"
231 then
232 git diff-index --name-status "$new"
234 (exit $saved_err)
238 # Switch the HEAD pointer to the new branch if we
239 # checked out a branch head, and remove any potential
240 # old MERGE_HEAD's (subsequent commits will clearly not
241 # be based on them, since we re-set the index)
243 if [ "$?" -eq 0 ]; then
244 if [ "$newbranch" ]; then
245 if [ "$newbranch_log" ]; then
246 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
247 touch "$GIT_DIR/logs/refs/heads/$newbranch"
249 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
250 branch="$newbranch"
252 if test -n "$branch"
253 then
254 GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
255 if test -z "$quiet"
256 then
257 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
259 elif test -n "$detached"
260 then
261 # NEEDSWORK: we would want a command to detach the HEAD
262 # atomically, instead of this handcrafted command sequence.
263 # Perhaps:
264 # git update-ref --detach HEAD $new
265 # or something like that...
267 git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
268 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
269 git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
270 die "Cannot detach HEAD"
271 if test -n "$detach_warn"
272 then
273 echo >&2 "$detach_warn"
276 rm -f "$GIT_DIR/MERGE_HEAD"
277 else
278 exit 1