]> git.ipfire.org Git - thirdparty/git.git/blame - git-checkout.sh
Add Documentation/cmd-list.made to .gitignore
[thirdparty/git.git] / git-checkout.sh
CommitLineData
303e5f4c 1#!/bin/sh
b33e9666 2
6124aee5 3USAGE='[-q] [-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
104f3e03 4SUBDIRECTORY_OK=Sometimes
806f36d4 5. git-sh-setup
7eff28a9 6require_work_tree
b0bafe03 7
969d326d 8old_name=HEAD
5a03e7f2 9old=$(git-rev-parse --verify $old_name 2>/dev/null)
64886104 10oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
e8b11749 11new=
969d326d 12new_name=
a79944d7 13force=
e8b11749 14branch=
0746d19a 15track=
91dcdfd3 16newbranch=
969d326d 17newbranch_log=
1be0659e 18merge=
6124aee5 19quiet=
ead80606
JH
20LF='
21'
e8b11749
LT
22while [ "$#" != "0" ]; do
23 arg="$1"
24 shift
25 case "$arg" in
91dcdfd3
LT
26 "-b")
27 newbranch="$1"
28 shift
29 [ -z "$newbranch" ] &&
30 die "git checkout: -b needs a branch name"
305e22c3 31 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
91dcdfd3 32 die "git checkout: branch $newbranch already exists"
03feddd6 33 git-check-ref-format "heads/$newbranch" ||
babfaf8d 34 die "git checkout: we do not like '$newbranch' as a branch name."
91dcdfd3 35 ;;
969d326d 36 "-l")
0746d19a
PB
37 newbranch_log=-l
38 ;;
39 "--track"|"--no-track")
40 track="$arg"
969d326d 41 ;;
303e5f4c 42 "-f")
e8b11749 43 force=1
303e5f4c 44 ;;
1be0659e
JH
45 -m)
46 merge=1
47 ;;
6124aee5
NP
48 "-q")
49 quiet=1
50 ;;
4aaa7027
JH
51 --)
52 break
53 ;;
b0bafe03
CS
54 -*)
55 usage
56 ;;
303e5f4c 57 *)
4aaa7027
JH
58 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
59 then
60 if [ -z "$rev" ]; then
61 echo "unknown flag $arg"
62 exit 1
63 fi
64 new="$rev"
8d78b5af 65 new_name="$arg"
305e22c3
LT
66 if git-show-ref --verify --quiet -- "refs/heads/$arg"
67 then
4aaa7027
JH
68 branch="$arg"
69 fi
70 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
71 then
72 # checking out selected paths from a tree-ish.
73 new="$rev"
969d326d 74 new_name="$arg^{tree}"
4aaa7027
JH
75 branch=
76 else
77 new=
969d326d 78 new_name=
4aaa7027
JH
79 branch=
80 set x "$arg" "$@"
81 shift
e8b11749 82 fi
2608003f
JH
83 case "$1" in
84 --)
85 shift ;;
86 esac
4aaa7027 87 break
e8b11749 88 ;;
303e5f4c 89 esac
303e5f4c
LT
90done
91
9debc324 92case "$newbranch,$track" in
0746d19a
PB
93,--*)
94 die "git checkout: --track and --no-track require -b"
95esac
96
897643cc
JH
97case "$force$merge" in
9811)
99 die "git checkout: -f and -m are incompatible"
100esac
101
4aaa7027
JH
102# The behaviour of the command with and without explicit path
103# parameters is quite different.
104#
105# Without paths, we are checking out everything in the work tree,
106# possibly switching branches. This is the traditional behaviour.
91dcdfd3 107#
4aaa7027
JH
108# With paths, we are _never_ switching branch, but checking out
109# the named paths from either index (when no rev is given),
110# or the named tree-ish (when rev is given).
111
112if test "$#" -ge 1
113then
babfaf8d
JW
114 hint=
115 if test "$#" -eq 1
116 then
117 hint="
118Did you intend to checkout '$@' which can not be resolved as commit?"
119 fi
1be0659e 120 if test '' != "$newbranch$force$merge"
4aaa7027 121 then
babfaf8d 122 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
4aaa7027
JH
123 fi
124 if test '' != "$new"
125 then
126 # from a specific tree-ish; note that this is for
127 # rescuing paths and is never meant to remove what
128 # is not in the named tree-ish.
d0d14cf3 129 git-ls-tree --full-name -r "$new" "$@" |
4aaa7027
JH
130 git-update-index --index-info || exit $?
131 fi
bf7e1472
JH
132
133 # Make sure the request is about existing paths.
134 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
135 git-ls-files -- "$@" |
136 git-checkout-index -f -u --stdin
4aaa7027
JH
137 exit $?
138else
139 # Make sure we did not fall back on $arg^{tree} codepath
140 # since we are not checking out from an arbitrary tree-ish,
141 # but switching branches.
142 if test '' != "$new"
143 then
144 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
145 die "Cannot switch branch to a non-commit."
146 fi
147fi
148
104f3e03
JH
149# We are switching branches and checking out trees, so
150# we *NEED* to be at the toplevel.
514c09fd 151cd_to_toplevel
104f3e03 152
969d326d 153[ -z "$new" ] && new=$old && new_name="$old_name"
4aaa7027 154
73c838e4 155# If we don't have an existing branch that we're switching to,
91dcdfd3 156# and we don't have a new branch name for the target we
73c838e4
JH
157# are switching to, then we are detaching our HEAD from any
158# branch. However, if "git checkout HEAD" detaches the HEAD
159# from the current branch, even though that may be logically
160# correct, it feels somewhat funny. More importantly, we do not
161# want "git checkout" nor "git checkout -f" to detach HEAD.
4aaa7027 162
bfbbb8f8
JH
163detached=
164detach_warn=
165
abba9dbb
JH
166describe_detached_head () {
167 test -n "$quiet" || {
168 printf >&2 "$1 "
169 GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2"
170 }
171}
172
c847f537
JH
173if test -z "$branch$newbranch" && test "$new" != "$old"
174then
bfbbb8f8 175 detached="$new"
6124aee5 176 if test -n "$oldbranch" && test -z "$quiet"
64886104 177 then
92cf9569 178 detach_warn="Note: moving to \"$new_name\" which isn't a local branch
d117452a
NP
179If you want to create a new branch from this checkout, you may do so
180(now or later) by using -b with the checkout command again. Example:
eb3204df 181 git checkout -b <new_branch_name>"
64886104 182 fi
abba9dbb 183elif test -z "$oldbranch"
ead80606 184then
abba9dbb 185 describe_detached_head 'Previous HEAD position was' "$old"
c847f537 186fi
91dcdfd3 187
5a03e7f2
SP
188if [ "X$old" = X ]
189then
6124aee5
NP
190 if test -z "$quiet"
191 then
192 echo >&2 "warning: You appear to be on a branch yet to be born."
193 echo >&2 "warning: Forcing checkout of $new_name."
194 fi
5a03e7f2
SP
195 force=1
196fi
197
a79944d7 198if [ "$force" ]
303e5f4c 199then
4170af82 200 git-read-tree --reset -u $new
303e5f4c 201else
7f88c846 202 git-update-index --refresh >/dev/null
11271480 203 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
1be0659e
JH
204 case "$merge" in
205 '')
206 echo >&2 "$merge_error"
207 exit 1 ;;
19205acf
JH
208 esac
209
1be0659e
JH
210 # Match the index to the working tree, and do a three-way.
211 git diff-files --name-only | git update-index --remove --stdin &&
19205acf 212 work=`git write-tree` &&
d7ebd53d 213 git read-tree --reset -u $new || exit
1be0659e 214
41f5d733 215 eval GITHEAD_$new='${new_name:-${branch:-$new}}' &&
d7ebd53d
JH
216 eval GITHEAD_$work=local &&
217 export GITHEAD_$new GITHEAD_$work &&
218 git merge-recursive $old -- $new $work
1be0659e
JH
219
220 # Do not register the cleanly merged paths in the index yet.
221 # this is not a real merge before committing, but just carrying
222 # the working tree changes along.
223 unmerged=`git ls-files -u`
224 git read-tree --reset $new
225 case "$unmerged" in
226 '') ;;
227 *)
228 (
229 z40=0000000000000000000000000000000000000000
230 echo "$unmerged" |
231 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
232 echo "$unmerged"
233 ) | git update-index --index-info
234 ;;
235 esac
236 exit 0
19205acf 237 )
980d8ce5 238 saved_err=$?
6124aee5 239 if test "$saved_err" = 0 && test -z "$quiet"
504fe714 240 then
e4b0e4ab 241 git diff-index --name-status "$new"
504fe714 242 fi
980d8ce5 243 (exit $saved_err)
ef0bfa25
LT
244fi
245
246#
01385e27 247# Switch the HEAD pointer to the new branch if we
ef0bfa25
LT
248# checked out a branch head, and remove any potential
249# old MERGE_HEAD's (subsequent commits will clearly not
250# be based on them, since we re-set the index)
251#
252if [ "$?" -eq 0 ]; then
91dcdfd3 253 if [ "$newbranch" ]; then
0746d19a 254 git-branch $track $newbranch_log "$newbranch" "$new_name" || exit
91dcdfd3
LT
255 branch="$newbranch"
256 fi
bfbbb8f8
JH
257 if test -n "$branch"
258 then
a7e4fbf9 259 GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
57216856 260 if test -n "$quiet"
e4b0e4ab 261 then
57216856
NP
262 true # nothing
263 elif test "refs/heads/$branch" = "$oldbranch"
264 then
265 echo >&2 "Already on branch \"$branch\""
266 else
6124aee5 267 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
e4b0e4ab 268 fi
bfbbb8f8
JH
269 elif test -n "$detached"
270 then
271 # NEEDSWORK: we would want a command to detach the HEAD
272 # atomically, instead of this handcrafted command sequence.
273 # Perhaps:
274 # git update-ref --detach HEAD $new
275 # or something like that...
276 #
e1dde3d0
NP
277 git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
278 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
279 git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
bfbbb8f8
JH
280 die "Cannot detach HEAD"
281 if test -n "$detach_warn"
282 then
283 echo >&2 "$detach_warn"
284 fi
abba9dbb 285 describe_detached_head 'HEAD is now at' HEAD
bfbbb8f8 286 fi
ef0bfa25 287 rm -f "$GIT_DIR/MERGE_HEAD"
ab22707f
TL
288else
289 exit 1
ef0bfa25 290fi