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