]> git.ipfire.org Git - thirdparty/git.git/blame - git-checkout.sh
git-checkout: do not warn detaching HEAD when it is already detached.
[thirdparty/git.git] / git-checkout.sh
CommitLineData
303e5f4c 1#!/bin/sh
b33e9666 2
1be0659e 3USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
104f3e03 4SUBDIRECTORY_OK=Sometimes
806f36d4 5. git-sh-setup
b0bafe03 6
969d326d 7old_name=HEAD
5a03e7f2 8old=$(git-rev-parse --verify $old_name 2>/dev/null)
64886104 9oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
e8b11749 10new=
969d326d 11new_name=
a79944d7 12force=
e8b11749 13branch=
91dcdfd3 14newbranch=
969d326d 15newbranch_log=
1be0659e 16merge=
e8b11749
LT
17while [ "$#" != "0" ]; do
18 arg="$1"
19 shift
20 case "$arg" in
91dcdfd3
LT
21 "-b")
22 newbranch="$1"
23 shift
24 [ -z "$newbranch" ] &&
25 die "git checkout: -b needs a branch name"
305e22c3 26 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
91dcdfd3 27 die "git checkout: branch $newbranch already exists"
03feddd6 28 git-check-ref-format "heads/$newbranch" ||
babfaf8d 29 die "git checkout: we do not like '$newbranch' as a branch name."
91dcdfd3 30 ;;
969d326d
SP
31 "-l")
32 newbranch_log=1
33 ;;
303e5f4c 34 "-f")
e8b11749 35 force=1
303e5f4c 36 ;;
1be0659e
JH
37 -m)
38 merge=1
39 ;;
4aaa7027
JH
40 --)
41 break
42 ;;
b0bafe03
CS
43 -*)
44 usage
45 ;;
303e5f4c 46 *)
4aaa7027
JH
47 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
48 then
49 if [ -z "$rev" ]; then
50 echo "unknown flag $arg"
51 exit 1
52 fi
53 new="$rev"
969d326d 54 new_name="$arg^0"
305e22c3
LT
55 if git-show-ref --verify --quiet -- "refs/heads/$arg"
56 then
4aaa7027
JH
57 branch="$arg"
58 fi
59 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
60 then
61 # checking out selected paths from a tree-ish.
62 new="$rev"
969d326d 63 new_name="$arg^{tree}"
4aaa7027
JH
64 branch=
65 else
66 new=
969d326d 67 new_name=
4aaa7027
JH
68 branch=
69 set x "$arg" "$@"
70 shift
e8b11749 71 fi
2608003f
JH
72 case "$1" in
73 --)
74 shift ;;
75 esac
4aaa7027 76 break
e8b11749 77 ;;
303e5f4c 78 esac
303e5f4c
LT
79done
80
897643cc
JH
81case "$force$merge" in
8211)
83 die "git checkout: -f and -m are incompatible"
84esac
85
4aaa7027
JH
86# The behaviour of the command with and without explicit path
87# parameters is quite different.
88#
89# Without paths, we are checking out everything in the work tree,
90# possibly switching branches. This is the traditional behaviour.
91dcdfd3 91#
4aaa7027
JH
92# With paths, we are _never_ switching branch, but checking out
93# the named paths from either index (when no rev is given),
94# or the named tree-ish (when rev is given).
95
96if test "$#" -ge 1
97then
babfaf8d
JW
98 hint=
99 if test "$#" -eq 1
100 then
101 hint="
102Did you intend to checkout '$@' which can not be resolved as commit?"
103 fi
1be0659e 104 if test '' != "$newbranch$force$merge"
4aaa7027 105 then
babfaf8d 106 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
4aaa7027
JH
107 fi
108 if test '' != "$new"
109 then
110 # from a specific tree-ish; note that this is for
111 # rescuing paths and is never meant to remove what
112 # is not in the named tree-ish.
d0d14cf3 113 git-ls-tree --full-name -r "$new" "$@" |
4aaa7027
JH
114 git-update-index --index-info || exit $?
115 fi
bf7e1472
JH
116
117 # Make sure the request is about existing paths.
118 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
119 git-ls-files -- "$@" |
120 git-checkout-index -f -u --stdin
4aaa7027
JH
121 exit $?
122else
123 # Make sure we did not fall back on $arg^{tree} codepath
124 # since we are not checking out from an arbitrary tree-ish,
125 # but switching branches.
126 if test '' != "$new"
127 then
128 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
129 die "Cannot switch branch to a non-commit."
130 fi
131fi
132
104f3e03
JH
133# We are switching branches and checking out trees, so
134# we *NEED* to be at the toplevel.
135cdup=$(git-rev-parse --show-cdup)
136if test ! -z "$cdup"
137then
138 cd "$cdup"
139fi
140
969d326d 141[ -z "$new" ] && new=$old && new_name="$old_name"
4aaa7027 142
91dcdfd3
LT
143# If we don't have an old branch that we're switching to,
144# and we don't have a new branch name for the target we
145# are switching to, then we'd better just be checking out
146# what we already had
4aaa7027 147
c847f537
JH
148if test -z "$branch$newbranch" && test "$new" != "$old"
149then
150 # NEEDSWORK: we would want to have this command here
151 # that allows us to detach the HEAD atomically.
152 # git update-ref --detach HEAD "$new"
64886104
JH
153 echo "$new" >"$GIT_DIR/HEAD.new" &&
154 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" || die "Cannot detach HEAD"
155
156 if test -n "$oldbranch"
157 then
158 echo >&2 "WARNING: you are not on ANY branch anymore.
c847f537
JH
159If you meant to create a new branch from the commit, you need -b to
160associate a new branch with the wanted checkout. Example:
0a576174
NP
161 git checkout -b <new_branch_name> $arg
162"
64886104 163 fi
c847f537 164fi
91dcdfd3 165
5a03e7f2
SP
166if [ "X$old" = X ]
167then
168 echo "warning: You do not appear to currently be on a branch." >&2
169 echo "warning: Forcing checkout of $new_name." >&2
170 force=1
171fi
172
a79944d7 173if [ "$force" ]
303e5f4c 174then
4170af82 175 git-read-tree --reset -u $new
303e5f4c 176else
7f88c846 177 git-update-index --refresh >/dev/null
11271480 178 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
1be0659e
JH
179 case "$merge" in
180 '')
181 echo >&2 "$merge_error"
182 exit 1 ;;
19205acf
JH
183 esac
184
1be0659e
JH
185 # Match the index to the working tree, and do a three-way.
186 git diff-files --name-only | git update-index --remove --stdin &&
19205acf 187 work=`git write-tree` &&
abc02670 188 git read-tree --reset -u $new &&
11271480
JH
189 git read-tree -m -u --aggressive --exclude-per-directory=.gitignore $old $new $work ||
190 exit
1be0659e 191
19205acf
JH
192 if result=`git write-tree 2>/dev/null`
193 then
1be0659e
JH
194 echo >&2 "Trivially automerged."
195 else
196 git merge-index -o git-merge-one-file -a
19205acf 197 fi
1be0659e
JH
198
199 # Do not register the cleanly merged paths in the index yet.
200 # this is not a real merge before committing, but just carrying
201 # the working tree changes along.
202 unmerged=`git ls-files -u`
203 git read-tree --reset $new
204 case "$unmerged" in
205 '') ;;
206 *)
207 (
208 z40=0000000000000000000000000000000000000000
209 echo "$unmerged" |
210 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
211 echo "$unmerged"
212 ) | git update-index --index-info
213 ;;
214 esac
215 exit 0
19205acf 216 )
980d8ce5 217 saved_err=$?
504fe714
JH
218 if test "$saved_err" = 0
219 then
220 test "$new" = "$old" || git diff-index --name-status "$new"
221 fi
980d8ce5 222 (exit $saved_err)
ef0bfa25
LT
223fi
224
225#
01385e27 226# Switch the HEAD pointer to the new branch if we
ef0bfa25
LT
227# checked out a branch head, and remove any potential
228# old MERGE_HEAD's (subsequent commits will clearly not
229# be based on them, since we re-set the index)
230#
231if [ "$?" -eq 0 ]; then
91dcdfd3 232 if [ "$newbranch" ]; then
969d326d 233 if [ "$newbranch_log" ]; then
d7fb7a37
SP
234 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
235 touch "$GIT_DIR/logs/refs/heads/$newbranch"
969d326d
SP
236 fi
237 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
91dcdfd3
LT
238 branch="$newbranch"
239 fi
8098a178
JH
240 [ "$branch" ] &&
241 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
ef0bfa25 242 rm -f "$GIT_DIR/MERGE_HEAD"
ab22707f
TL
243else
244 exit 1
ef0bfa25 245fi