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