More missing static
[git/spearce.git] / git-checkout.sh
blobd561c88dcbd3d5a67b4b6725e00683e613d80b9b
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 track=
16 newbranch=
17 newbranch_log=
18 merge=
19 quiet=
20 v=-v
21 LF='
23 while [ "$#" != "0" ]; do
24 arg="$1"
25 shift
26 case "$arg" in
27 "-b")
28 newbranch="$1"
29 shift
30 [ -z "$newbranch" ] &&
31 die "git checkout: -b needs a branch name"
32 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
33 die "git checkout: branch $newbranch already exists"
34 git-check-ref-format "heads/$newbranch" ||
35 die "git checkout: we do not like '$newbranch' as a branch name."
37 "-l")
38 newbranch_log=-l
40 "--track"|"--no-track")
41 track="$arg"
43 "-f")
44 force=1
46 -m)
47 merge=1
49 "-q")
50 quiet=1
53 --)
54 break
56 -*)
57 usage
60 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
61 then
62 if [ -z "$rev" ]; then
63 echo "unknown flag $arg"
64 exit 1
66 new="$rev"
67 new_name="$arg"
68 if git-show-ref --verify --quiet -- "refs/heads/$arg"
69 then
70 branch="$arg"
72 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
73 then
74 # checking out selected paths from a tree-ish.
75 new="$rev"
76 new_name="$arg^{tree}"
77 branch=
78 else
79 new=
80 new_name=
81 branch=
82 set x "$arg" "$@"
83 shift
85 case "$1" in
86 --)
87 shift ;;
88 esac
89 break
91 esac
92 done
94 case "$newbranch,$track" in
95 ,--*)
96 die "git checkout: --track and --no-track require -b"
97 esac
99 case "$force$merge" in
101 die "git checkout: -f and -m are incompatible"
102 esac
104 # The behaviour of the command with and without explicit path
105 # parameters is quite different.
107 # Without paths, we are checking out everything in the work tree,
108 # possibly switching branches. This is the traditional behaviour.
110 # With paths, we are _never_ switching branch, but checking out
111 # the named paths from either index (when no rev is given),
112 # or the named tree-ish (when rev is given).
114 if test "$#" -ge 1
115 then
116 hint=
117 if test "$#" -eq 1
118 then
119 hint="
120 Did you intend to checkout '$@' which can not be resolved as commit?"
122 if test '' != "$newbranch$force$merge"
123 then
124 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
126 if test '' != "$new"
127 then
128 # from a specific tree-ish; note that this is for
129 # rescuing paths and is never meant to remove what
130 # is not in the named tree-ish.
131 git-ls-tree --full-name -r "$new" "$@" |
132 git-update-index --index-info || exit $?
135 # Make sure the request is about existing paths.
136 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
137 git-ls-files -- "$@" |
138 git-checkout-index -f -u --stdin
139 exit $?
140 else
141 # Make sure we did not fall back on $arg^{tree} codepath
142 # since we are not checking out from an arbitrary tree-ish,
143 # but switching branches.
144 if test '' != "$new"
145 then
146 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
147 die "Cannot switch branch to a non-commit."
151 # We are switching branches and checking out trees, so
152 # we *NEED* to be at the toplevel.
153 cd_to_toplevel
155 [ -z "$new" ] && new=$old && new_name="$old_name"
157 # If we don't have an existing branch that we're switching to,
158 # and we don't have a new branch name for the target we
159 # are switching to, then we are detaching our HEAD from any
160 # branch. However, if "git checkout HEAD" detaches the HEAD
161 # from the current branch, even though that may be logically
162 # correct, it feels somewhat funny. More importantly, we do not
163 # want "git checkout" nor "git checkout -f" to detach HEAD.
165 detached=
166 detach_warn=
168 describe_detached_head () {
169 test -n "$quiet" || {
170 printf >&2 "$1 "
171 GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2"
175 if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
176 then
177 detached="$new"
178 if test -n "$oldbranch" && test -z "$quiet"
179 then
180 detach_warn="Note: moving to \"$new_name\" which isn't a local branch
181 If you want to create a new branch from this checkout, you may do so
182 (now or later) by using -b with the checkout command again. Example:
183 git checkout -b <new_branch_name>"
185 elif test -z "$oldbranch" && test "$new" != "$old"
186 then
187 describe_detached_head 'Previous HEAD position was' "$old"
190 if [ "X$old" = X ]
191 then
192 if test -z "$quiet"
193 then
194 echo >&2 "warning: You appear to be on a branch yet to be born."
195 echo >&2 "warning: Forcing checkout of $new_name."
197 force=1
200 if [ "$force" ]
201 then
202 git-read-tree $v --reset -u $new
203 else
204 git-update-index --refresh >/dev/null
205 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
206 case "$merge" in
208 echo >&2 "$merge_error"
209 exit 1 ;;
210 esac
212 # Match the index to the working tree, and do a three-way.
213 git diff-files --name-only | git update-index --remove --stdin &&
214 work=`git write-tree` &&
215 git read-tree $v --reset -u $new || exit
217 eval GITHEAD_$new='${new_name:-${branch:-$new}}' &&
218 eval GITHEAD_$work=local &&
219 export GITHEAD_$new GITHEAD_$work &&
220 git merge-recursive $old -- $new $work
222 # Do not register the cleanly merged paths in the index yet.
223 # this is not a real merge before committing, but just carrying
224 # the working tree changes along.
225 unmerged=`git ls-files -u`
226 git read-tree $v --reset $new
227 case "$unmerged" in
228 '') ;;
231 z40=0000000000000000000000000000000000000000
232 echo "$unmerged" |
233 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
234 echo "$unmerged"
235 ) | git update-index --index-info
237 esac
238 exit 0
240 saved_err=$?
241 if test "$saved_err" = 0 && test -z "$quiet"
242 then
243 git diff-index --name-status "$new"
245 (exit $saved_err)
249 # Switch the HEAD pointer to the new branch if we
250 # checked out a branch head, and remove any potential
251 # old MERGE_HEAD's (subsequent commits will clearly not
252 # be based on them, since we re-set the index)
254 if [ "$?" -eq 0 ]; then
255 if [ "$newbranch" ]; then
256 git-branch $track $newbranch_log "$newbranch" "$new_name" || exit
257 branch="$newbranch"
259 if test -n "$branch"
260 then
261 GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
262 if test -n "$quiet"
263 then
264 true # nothing
265 elif test "refs/heads/$branch" = "$oldbranch"
266 then
267 echo >&2 "Already on branch \"$branch\""
268 else
269 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
271 elif test -n "$detached"
272 then
273 git-update-ref --no-deref -m "checkout: moving to $arg" HEAD "$detached" ||
274 die "Cannot detach HEAD"
275 if test -n "$detach_warn"
276 then
277 echo >&2 "$detach_warn"
279 describe_detached_head 'HEAD is now at' HEAD
281 rm -f "$GIT_DIR/MERGE_HEAD"
282 else
283 exit 1