]>
Commit | Line | Data |
---|---|---|
f8adbec9 | 1 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
782c2d65 | 2 | #include "builtin.h" |
b2141fc1 | 3 | #include "config.h" |
7c85a87c | 4 | #include "checkout.h" |
697cc8ef | 5 | #include "lockfile.h" |
782c2d65 DB |
6 | #include "parse-options.h" |
7 | #include "refs.h" | |
cbd53a21 | 8 | #include "object-store.h" |
782c2d65 DB |
9 | #include "commit.h" |
10 | #include "tree.h" | |
11 | #include "tree-walk.h" | |
b9d37a54 | 12 | #include "cache-tree.h" |
782c2d65 DB |
13 | #include "unpack-trees.h" |
14 | #include "dir.h" | |
15 | #include "run-command.h" | |
16 | #include "merge-recursive.h" | |
17 | #include "branch.h" | |
18 | #include "diff.h" | |
19 | #include "revision.h" | |
79a1e6b4 | 20 | #include "remote.h" |
0cf8581e JH |
21 | #include "blob.h" |
22 | #include "xdiff-interface.h" | |
23 | #include "ll-merge.h" | |
cfc5789a | 24 | #include "resolve-undo.h" |
851e18c3 | 25 | #include "submodule-config.h" |
175f6e59 | 26 | #include "submodule.h" |
ad8d5104 | 27 | #include "advice.h" |
782c2d65 | 28 | |
fa655d84 BP |
29 | static int checkout_optimize_new_branch; |
30 | ||
782c2d65 | 31 | static const char * const checkout_usage[] = { |
9c9b4f2f AH |
32 | N_("git checkout [<options>] <branch>"), |
33 | N_("git checkout [<options>] [<branch>] -- <file>..."), | |
782c2d65 DB |
34 | NULL, |
35 | }; | |
36 | ||
db941099 | 37 | struct checkout_opts { |
e51e3057 | 38 | int patch_mode; |
db941099 JH |
39 | int quiet; |
40 | int merge; | |
41 | int force; | |
32669671 | 42 | int force_detach; |
38901a48 | 43 | int writeout_stage; |
c1d7036b | 44 | int overwrite_ignore; |
08d595dc | 45 | int ignore_skipworktree; |
1d0fa898 | 46 | int ignore_other_worktrees; |
870ebdb9 | 47 | int show_progress; |
0f086e6d | 48 | int count_checkout_paths; |
091e04bc | 49 | int overlay_mode; |
fa655d84 BP |
50 | /* |
51 | * If new checkout options are added, skip_merge_working_tree | |
52 | * should be updated accordingly. | |
53 | */ | |
db941099 JH |
54 | |
55 | const char *new_branch; | |
02ac9837 | 56 | const char *new_branch_force; |
9db5ebf4 | 57 | const char *new_orphan_branch; |
db941099 JH |
58 | int new_branch_log; |
59 | enum branch_track track; | |
175f6e59 | 60 | struct diff_options diff_options; |
e51e3057 NTND |
61 | |
62 | int branch_exists; | |
63 | const char *prefix; | |
817b345a | 64 | struct pathspec pathspec; |
e51e3057 | 65 | struct tree *source_tree; |
db941099 JH |
66 | }; |
67 | ||
c8a3ea1f | 68 | static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, |
782c2d65 DB |
69 | int changed) |
70 | { | |
15048f8a | 71 | return run_hook_le(NULL, "post-checkout", |
c8a3ea1f BW |
72 | oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid), |
73 | oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid), | |
15048f8a | 74 | changed ? "1" : "0", NULL); |
c8a3ea1f | 75 | /* "new_commit" can be NULL when checking out from the index before |
2292ce47 | 76 | a commit exists. */ |
ae98a008 | 77 | |
782c2d65 DB |
78 | } |
79 | ||
df46d77e | 80 | static int update_some(const struct object_id *oid, struct strbuf *base, |
671f0707 | 81 | const char *pathname, unsigned mode, int stage, void *context) |
782c2d65 DB |
82 | { |
83 | int len; | |
84 | struct cache_entry *ce; | |
c5326bd6 | 85 | int pos; |
782c2d65 | 86 | |
782c2d65 DB |
87 | if (S_ISDIR(mode)) |
88 | return READ_TREE_RECURSIVE; | |
89 | ||
6a0b0b6d | 90 | len = base->len + strlen(pathname); |
a849735b | 91 | ce = make_empty_cache_entry(&the_index, len); |
df46d77e | 92 | oidcpy(&ce->oid, oid); |
6a0b0b6d NTND |
93 | memcpy(ce->name, base->buf, base->len); |
94 | memcpy(ce->name + base->len, pathname, len - base->len); | |
b60e188c TG |
95 | ce->ce_flags = create_ce_flags(0) | CE_UPDATE; |
96 | ce->ce_namelen = len; | |
782c2d65 | 97 | ce->ce_mode = create_ce_mode(mode); |
c5326bd6 JK |
98 | |
99 | /* | |
100 | * If the entry is the same as the current index, we can leave the old | |
101 | * entry in place. Whether it is UPTODATE or not, checkout_entry will | |
102 | * do the right thing. | |
103 | */ | |
104 | pos = cache_name_pos(ce->name, ce->ce_namelen); | |
105 | if (pos >= 0) { | |
106 | struct cache_entry *old = active_cache[pos]; | |
107 | if (ce->ce_mode == old->ce_mode && | |
4a7e27e9 | 108 | oideq(&ce->oid, &old->oid)) { |
c5326bd6 | 109 | old->ce_flags |= CE_UPDATE; |
a849735b | 110 | discard_cache_entry(ce); |
c5326bd6 JK |
111 | return 0; |
112 | } | |
113 | } | |
114 | ||
782c2d65 DB |
115 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
116 | return 0; | |
117 | } | |
118 | ||
18e4f405 | 119 | static int read_tree_some(struct tree *tree, const struct pathspec *pathspec) |
782c2d65 | 120 | { |
e092073d NTND |
121 | read_tree_recursive(the_repository, tree, "", 0, 0, |
122 | pathspec, update_some, NULL); | |
782c2d65 | 123 | |
782c2d65 DB |
124 | /* update the index with the given tree's info |
125 | * for all args, expanding wildcards, and exit | |
126 | * with any non-zero return code. | |
127 | */ | |
128 | return 0; | |
129 | } | |
130 | ||
9c5e6c80 | 131 | static int skip_same_name(const struct cache_entry *ce, int pos) |
8fdcf312 JH |
132 | { |
133 | while (++pos < active_nr && | |
134 | !strcmp(active_cache[pos]->name, ce->name)) | |
135 | ; /* skip */ | |
136 | return pos; | |
137 | } | |
138 | ||
091e04bc TG |
139 | static int check_stage(int stage, const struct cache_entry *ce, int pos, |
140 | int overlay_mode) | |
38901a48 JH |
141 | { |
142 | while (pos < active_nr && | |
143 | !strcmp(active_cache[pos]->name, ce->name)) { | |
144 | if (ce_stage(active_cache[pos]) == stage) | |
145 | return 0; | |
146 | pos++; | |
147 | } | |
091e04bc TG |
148 | if (!overlay_mode) |
149 | return 0; | |
9f97ab08 ÆAB |
150 | if (stage == 2) |
151 | return error(_("path '%s' does not have our version"), ce->name); | |
152 | else | |
153 | return error(_("path '%s' does not have their version"), ce->name); | |
38901a48 JH |
154 | } |
155 | ||
9c5e6c80 | 156 | static int check_stages(unsigned stages, const struct cache_entry *ce, int pos) |
0cf8581e | 157 | { |
fbbccd0a JH |
158 | unsigned seen = 0; |
159 | const char *name = ce->name; | |
160 | ||
161 | while (pos < active_nr) { | |
162 | ce = active_cache[pos]; | |
163 | if (strcmp(name, ce->name)) | |
164 | break; | |
165 | seen |= (1 << ce_stage(ce)); | |
166 | pos++; | |
167 | } | |
168 | if ((stages & seen) != stages) | |
169 | return error(_("path '%s' does not have all necessary versions"), | |
170 | name); | |
0cf8581e JH |
171 | return 0; |
172 | } | |
173 | ||
ce25e4c7 | 174 | static int checkout_stage(int stage, const struct cache_entry *ce, int pos, |
7d0c1f45 JH |
175 | const struct checkout *state, int *nr_checkouts, |
176 | int overlay_mode) | |
38901a48 JH |
177 | { |
178 | while (pos < active_nr && | |
179 | !strcmp(active_cache[pos]->name, ce->name)) { | |
180 | if (ce_stage(active_cache[pos]) == stage) | |
0f086e6d NTND |
181 | return checkout_entry(active_cache[pos], state, |
182 | NULL, nr_checkouts); | |
38901a48 JH |
183 | pos++; |
184 | } | |
091e04bc TG |
185 | if (!overlay_mode) { |
186 | unlink_entry(ce); | |
187 | return 0; | |
188 | } | |
9f97ab08 ÆAB |
189 | if (stage == 2) |
190 | return error(_("path '%s' does not have our version"), ce->name); | |
191 | else | |
192 | return error(_("path '%s' does not have their version"), ce->name); | |
38901a48 | 193 | } |
8fdcf312 | 194 | |
0f086e6d | 195 | static int checkout_merged(int pos, const struct checkout *state, int *nr_checkouts) |
0cf8581e JH |
196 | { |
197 | struct cache_entry *ce = active_cache[pos]; | |
198 | const char *path = ce->name; | |
199 | mmfile_t ancestor, ours, theirs; | |
200 | int status; | |
60af7691 | 201 | struct object_id oid; |
0cf8581e | 202 | mmbuffer_t result_buf; |
60af7691 | 203 | struct object_id threeway[3]; |
335c6e40 | 204 | unsigned mode = 0; |
0cf8581e | 205 | |
fbbccd0a JH |
206 | memset(threeway, 0, sizeof(threeway)); |
207 | while (pos < active_nr) { | |
208 | int stage; | |
209 | stage = ce_stage(ce); | |
210 | if (!stage || strcmp(path, ce->name)) | |
211 | break; | |
60af7691 | 212 | oidcpy(&threeway[stage - 1], &ce->oid); |
fbbccd0a JH |
213 | if (stage == 2) |
214 | mode = create_ce_mode(ce->ce_mode); | |
215 | pos++; | |
216 | ce = active_cache[pos]; | |
217 | } | |
60af7691 | 218 | if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2])) |
fbbccd0a | 219 | return error(_("path '%s' does not have necessary versions"), path); |
0cf8581e | 220 | |
d449347d | 221 | read_mmblob(&ancestor, &threeway[0]); |
222 | read_mmblob(&ours, &threeway[1]); | |
223 | read_mmblob(&theirs, &threeway[2]); | |
0cf8581e | 224 | |
18b037a5 JN |
225 | /* |
226 | * NEEDSWORK: re-create conflicts from merges with | |
227 | * merge.renormalize set, too | |
228 | */ | |
f0531a29 | 229 | status = ll_merge(&result_buf, path, &ancestor, "base", |
32eaa468 NTND |
230 | &ours, "ours", &theirs, "theirs", |
231 | state->istate, NULL); | |
0cf8581e JH |
232 | free(ancestor.ptr); |
233 | free(ours.ptr); | |
234 | free(theirs.ptr); | |
235 | if (status < 0 || !result_buf.ptr) { | |
236 | free(result_buf.ptr); | |
e8a8a4d7 | 237 | return error(_("path '%s': cannot merge"), path); |
0cf8581e JH |
238 | } |
239 | ||
240 | /* | |
241 | * NEEDSWORK: | |
242 | * There is absolutely no reason to write this as a blob object | |
514e8039 JS |
243 | * and create a phony cache entry. This hack is primarily to get |
244 | * to the write_entry() machinery that massages the contents to | |
245 | * work-tree format and writes out which only allows it for a | |
246 | * cache entry. The code in write_entry() needs to be refactored | |
247 | * to allow us to feed a <buffer, size, mode> instead of a cache | |
248 | * entry. Such a refactoring would help merge_recursive as well | |
249 | * (it also writes the merge result to the object database even | |
250 | * when it may contain conflicts). | |
0cf8581e | 251 | */ |
a09c985e | 252 | if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) |
e8a8a4d7 | 253 | die(_("Unable to add merge result for '%s'"), path); |
443a12f3 | 254 | free(result_buf.ptr); |
a849735b | 255 | ce = make_transient_cache_entry(mode, &oid, path, 2); |
048f2762 | 256 | if (!ce) |
e8a8a4d7 | 257 | die(_("make_cache_entry failed for path '%s'"), path); |
0f086e6d | 258 | status = checkout_entry(ce, state, NULL, nr_checkouts); |
a849735b | 259 | discard_cache_entry(ce); |
0cf8581e JH |
260 | return status; |
261 | } | |
8fdcf312 | 262 | |
091e04bc TG |
263 | static void mark_ce_for_checkout_overlay(struct cache_entry *ce, |
264 | char *ps_matched, | |
265 | const struct checkout_opts *opts) | |
b7033e73 TG |
266 | { |
267 | ce->ce_flags &= ~CE_MATCHED; | |
268 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
269 | return; | |
270 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
271 | /* | |
272 | * "git checkout tree-ish -- path", but this entry | |
273 | * is in the original index but is not in tree-ish | |
274 | * or does not match the pathspec; it will not be | |
275 | * checked out to the working tree. We will not do | |
276 | * anything to this entry at all. | |
277 | */ | |
278 | return; | |
279 | /* | |
280 | * Either this entry came from the tree-ish we are | |
281 | * checking the paths out of, or we are checking out | |
282 | * of the index. | |
283 | * | |
284 | * If it comes from the tree-ish, we already know it | |
285 | * matches the pathspec and could just stamp | |
286 | * CE_MATCHED to it from update_some(). But we still | |
287 | * need ps_matched and read_tree_recursive (and | |
288 | * eventually tree_entry_interesting) cannot fill | |
289 | * ps_matched yet. Once it can, we can avoid calling | |
290 | * match_pathspec() for _all_ entries when | |
291 | * opts->source_tree != NULL. | |
292 | */ | |
293 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) | |
294 | ce->ce_flags |= CE_MATCHED; | |
295 | } | |
296 | ||
091e04bc TG |
297 | static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce, |
298 | char *ps_matched, | |
299 | const struct checkout_opts *opts) | |
300 | { | |
301 | ce->ce_flags &= ~CE_MATCHED; | |
302 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
303 | return; | |
304 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) { | |
305 | ce->ce_flags |= CE_MATCHED; | |
306 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
307 | /* | |
308 | * In overlay mode, but the path is not in | |
309 | * tree-ish, which means we should remove it | |
310 | * from the index and the working tree. | |
311 | */ | |
312 | ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; | |
313 | } | |
314 | } | |
315 | ||
b6312c27 NTND |
316 | static int checkout_paths(const struct checkout_opts *opts, |
317 | const char *revision) | |
782c2d65 DB |
318 | { |
319 | int pos; | |
68e3d629 | 320 | struct checkout state = CHECKOUT_INIT; |
782c2d65 | 321 | static char *ps_matched; |
60af7691 | 322 | struct object_id rev; |
782c2d65 | 323 | struct commit *head; |
d2b3691b | 324 | int errs = 0; |
837e34eb | 325 | struct lock_file lock_file = LOCK_INIT; |
1d1f689b | 326 | int nr_checkouts = 0, nr_unmerged = 0; |
b6312c27 | 327 | |
e27dd8ae JH |
328 | trace2_cmd_mode(opts->patch_mode ? "patch" : "path"); |
329 | ||
b6312c27 NTND |
330 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) |
331 | die(_("'%s' cannot be used with updating paths"), "--track"); | |
332 | ||
333 | if (opts->new_branch_log) | |
334 | die(_("'%s' cannot be used with updating paths"), "-l"); | |
335 | ||
336 | if (opts->force && opts->patch_mode) | |
337 | die(_("'%s' cannot be used with updating paths"), "-f"); | |
338 | ||
339 | if (opts->force_detach) | |
340 | die(_("'%s' cannot be used with updating paths"), "--detach"); | |
341 | ||
342 | if (opts->merge && opts->patch_mode) | |
343 | die(_("'%s' cannot be used with %s"), "--merge", "--patch"); | |
344 | ||
345 | if (opts->force && opts->merge) | |
346 | die(_("'%s' cannot be used with %s"), "-f", "-m"); | |
347 | ||
348 | if (opts->new_branch) | |
349 | die(_("Cannot update paths and switch to branch '%s' at the same time."), | |
350 | opts->new_branch); | |
351 | ||
352 | if (opts->patch_mode) | |
353 | return run_add_interactive(revision, "--patch=checkout", | |
480ca644 | 354 | &opts->pathspec); |
b6312c27 | 355 | |
fb4a8464 | 356 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
5ab2a2da | 357 | if (read_cache_preload(&opts->pathspec) < 0) |
e923a8ab | 358 | return error(_("index file corrupt")); |
75336878 | 359 | |
e51e3057 | 360 | if (opts->source_tree) |
18e4f405 | 361 | read_tree_some(opts->source_tree, &opts->pathspec); |
75336878 | 362 | |
8b54c234 | 363 | ps_matched = xcalloc(opts->pathspec.nr, 1); |
782c2d65 | 364 | |
e721c154 NTND |
365 | /* |
366 | * Make sure all pathspecs participated in locating the paths | |
367 | * to be checked out. | |
368 | */ | |
b7033e73 | 369 | for (pos = 0; pos < active_nr; pos++) |
091e04bc TG |
370 | if (opts->overlay_mode) |
371 | mark_ce_for_checkout_overlay(active_cache[pos], | |
372 | ps_matched, | |
373 | opts); | |
374 | else | |
375 | mark_ce_for_checkout_no_overlay(active_cache[pos], | |
376 | ps_matched, | |
377 | opts); | |
782c2d65 | 378 | |
c5c33504 | 379 | if (report_path_error(ps_matched, &opts->pathspec)) { |
e721c154 | 380 | free(ps_matched); |
782c2d65 | 381 | return 1; |
e721c154 NTND |
382 | } |
383 | free(ps_matched); | |
782c2d65 | 384 | |
4421a823 JH |
385 | /* "checkout -m path" to recreate conflicted state */ |
386 | if (opts->merge) | |
e721c154 | 387 | unmerge_marked_index(&the_index); |
4421a823 | 388 | |
8fdcf312 JH |
389 | /* Any unmerged paths? */ |
390 | for (pos = 0; pos < active_nr; pos++) { | |
9c5e6c80 | 391 | const struct cache_entry *ce = active_cache[pos]; |
e721c154 | 392 | if (ce->ce_flags & CE_MATCHED) { |
8fdcf312 JH |
393 | if (!ce_stage(ce)) |
394 | continue; | |
db941099 | 395 | if (opts->force) { |
e8a8a4d7 | 396 | warning(_("path '%s' is unmerged"), ce->name); |
f9022075 | 397 | } else if (opts->writeout_stage) { |
091e04bc | 398 | errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode); |
0cf8581e | 399 | } else if (opts->merge) { |
fbbccd0a | 400 | errs |= check_stages((1<<2) | (1<<3), ce, pos); |
db941099 JH |
401 | } else { |
402 | errs = 1; | |
e8a8a4d7 | 403 | error(_("path '%s' is unmerged"), ce->name); |
db941099 | 404 | } |
8fdcf312 JH |
405 | pos = skip_same_name(ce, pos) - 1; |
406 | } | |
407 | } | |
408 | if (errs) | |
409 | return 1; | |
410 | ||
d2b3691b | 411 | /* Now we are committed to check them out */ |
782c2d65 DB |
412 | state.force = 1; |
413 | state.refresh_cache = 1; | |
d4a2024a | 414 | state.istate = &the_index; |
2841e8f8 LS |
415 | |
416 | enable_delayed_checkout(&state); | |
782c2d65 DB |
417 | for (pos = 0; pos < active_nr; pos++) { |
418 | struct cache_entry *ce = active_cache[pos]; | |
e721c154 | 419 | if (ce->ce_flags & CE_MATCHED) { |
8fdcf312 | 420 | if (!ce_stage(ce)) { |
0f086e6d NTND |
421 | errs |= checkout_entry(ce, &state, |
422 | NULL, &nr_checkouts); | |
8fdcf312 JH |
423 | continue; |
424 | } | |
f9022075 | 425 | if (opts->writeout_stage) |
0f086e6d NTND |
426 | errs |= checkout_stage(opts->writeout_stage, |
427 | ce, pos, | |
7d0c1f45 JH |
428 | &state, |
429 | &nr_checkouts, opts->overlay_mode); | |
f9022075 | 430 | else if (opts->merge) |
0f086e6d | 431 | errs |= checkout_merged(pos, &state, |
1d1f689b | 432 | &nr_unmerged); |
8fdcf312 | 433 | pos = skip_same_name(ce, pos) - 1; |
782c2d65 DB |
434 | } |
435 | } | |
091e04bc TG |
436 | remove_marked_cache_entries(&the_index, 1); |
437 | remove_scheduled_dirs(); | |
0f086e6d NTND |
438 | errs |= finish_delayed_checkout(&state, &nr_checkouts); |
439 | ||
440 | if (opts->count_checkout_paths) { | |
1d1f689b NTND |
441 | if (nr_unmerged) |
442 | fprintf_ln(stderr, Q_("Recreated %d merge conflict", | |
443 | "Recreated %d merge conflicts", | |
444 | nr_unmerged), | |
445 | nr_unmerged); | |
0f086e6d | 446 | if (opts->source_tree) |
3c5883b3 NTND |
447 | fprintf_ln(stderr, Q_("Updated %d path from %s", |
448 | "Updated %d paths from %s", | |
0f086e6d NTND |
449 | nr_checkouts), |
450 | nr_checkouts, | |
451 | find_unique_abbrev(&opts->source_tree->object.oid, | |
452 | DEFAULT_ABBREV)); | |
1d1f689b | 453 | else if (!nr_unmerged || nr_checkouts) |
3c5883b3 NTND |
454 | fprintf_ln(stderr, Q_("Updated %d path from the index", |
455 | "Updated %d paths from the index", | |
0f086e6d NTND |
456 | nr_checkouts), |
457 | nr_checkouts); | |
458 | } | |
782c2d65 | 459 | |
837e34eb | 460 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
e8a8a4d7 | 461 | die(_("unable to write new index file")); |
75336878 | 462 | |
34c290a6 | 463 | read_ref_full("HEAD", 0, &rev, NULL); |
21e1ee8f | 464 | head = lookup_commit_reference_gently(the_repository, &rev, 1); |
782c2d65 | 465 | |
d2b3691b JH |
466 | errs |= post_checkout_hook(head, head, 0); |
467 | return errs; | |
782c2d65 DB |
468 | } |
469 | ||
a2b4994c NTND |
470 | static void show_local_changes(struct object *head, |
471 | const struct diff_options *opts) | |
782c2d65 DB |
472 | { |
473 | struct rev_info rev; | |
474 | /* I think we want full paths, even if we're in a subdirectory. */ | |
2abf3503 | 475 | repo_init_revisions(the_repository, &rev, NULL); |
175f6e59 | 476 | rev.diffopt.flags = opts->flags; |
782c2d65 | 477 | rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; |
28452655 | 478 | diff_setup_done(&rev.diffopt); |
782c2d65 DB |
479 | add_pending_object(&rev, head, NULL); |
480 | run_diff_index(&rev, 0); | |
481 | } | |
482 | ||
b3c0494a | 483 | static void describe_detached_head(const char *msg, struct commit *commit) |
782c2d65 | 484 | { |
f285a2d7 | 485 | struct strbuf sb = STRBUF_INIT; |
ca69d4d5 | 486 | |
3c621839 JK |
487 | if (!parse_commit(commit)) |
488 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); | |
ca69d4d5 AR |
489 | if (print_sha1_ellipsis()) { |
490 | fprintf(stderr, "%s %s... %s\n", msg, | |
aab9583f | 491 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); |
ca69d4d5 AR |
492 | } else { |
493 | fprintf(stderr, "%s %s %s\n", msg, | |
aab9583f | 494 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); |
ca69d4d5 | 495 | } |
782c2d65 DB |
496 | strbuf_release(&sb); |
497 | } | |
498 | ||
a2b4994c NTND |
499 | static int reset_tree(struct tree *tree, const struct checkout_opts *o, |
500 | int worktree, int *writeout_error) | |
782c2d65 DB |
501 | { |
502 | struct unpack_trees_options opts; | |
503 | struct tree_desc tree_desc; | |
bc052d7f | 504 | |
782c2d65 DB |
505 | memset(&opts, 0, sizeof(opts)); |
506 | opts.head_idx = -1; | |
6286a08d JH |
507 | opts.update = worktree; |
508 | opts.skip_unmerged = !worktree; | |
782c2d65 DB |
509 | opts.reset = 1; |
510 | opts.merge = 1; | |
511 | opts.fn = oneway_merge; | |
870ebdb9 | 512 | opts.verbose_update = o->show_progress; |
34110cd4 LT |
513 | opts.src_index = &the_index; |
514 | opts.dst_index = &the_index; | |
782c2d65 DB |
515 | parse_tree(tree); |
516 | init_tree_desc(&tree_desc, tree->buffer, tree->size); | |
291d823e JH |
517 | switch (unpack_trees(1, &tree_desc, &opts)) { |
518 | case -2: | |
a2b4994c | 519 | *writeout_error = 1; |
291d823e JH |
520 | /* |
521 | * We return 0 nevertheless, as the index is all right | |
522 | * and more importantly we have made best efforts to | |
523 | * update paths in the work tree, and we cannot revert | |
524 | * them. | |
525 | */ | |
1cf01a34 | 526 | /* fallthrough */ |
291d823e JH |
527 | case 0: |
528 | return 0; | |
529 | default: | |
84a5750b | 530 | return 128; |
291d823e | 531 | } |
782c2d65 DB |
532 | } |
533 | ||
782c2d65 DB |
534 | struct branch_info { |
535 | const char *name; /* The short name used */ | |
536 | const char *path; /* The full name of a real branch */ | |
537 | struct commit *commit; /* The named commit */ | |
5883034c NTND |
538 | /* |
539 | * if not null the branch is detached because it's already | |
540 | * checked out in this checkout | |
541 | */ | |
542 | char *checkout; | |
782c2d65 DB |
543 | }; |
544 | ||
545 | static void setup_branch_path(struct branch_info *branch) | |
546 | { | |
f285a2d7 | 547 | struct strbuf buf = STRBUF_INIT; |
ae5a6c36 | 548 | |
fd4692ff | 549 | strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); |
a552de75 | 550 | if (strcmp(buf.buf, branch->name)) |
ae5a6c36 | 551 | branch->name = xstrdup(buf.buf); |
a552de75 | 552 | strbuf_splice(&buf, 0, 0, "refs/heads/", 11); |
782c2d65 DB |
553 | branch->path = strbuf_detach(&buf, NULL); |
554 | } | |
555 | ||
fa655d84 BP |
556 | /* |
557 | * Skip merging the trees, updating the index and working directory if and | |
558 | * only if we are creating a new branch via "git checkout -b <new_branch>." | |
559 | */ | |
560 | static int skip_merge_working_tree(const struct checkout_opts *opts, | |
561 | const struct branch_info *old_branch_info, | |
562 | const struct branch_info *new_branch_info) | |
563 | { | |
564 | /* | |
565 | * Do the merge if sparse checkout is on and the user has not opted in | |
566 | * to the optimized behavior | |
567 | */ | |
568 | if (core_apply_sparse_checkout && !checkout_optimize_new_branch) | |
569 | return 0; | |
570 | ||
571 | /* | |
572 | * We must do the merge if we are actually moving to a new commit. | |
573 | */ | |
574 | if (!old_branch_info->commit || !new_branch_info->commit || | |
e43d2dcc JK |
575 | !oideq(&old_branch_info->commit->object.oid, |
576 | &new_branch_info->commit->object.oid)) | |
fa655d84 BP |
577 | return 0; |
578 | ||
579 | /* | |
580 | * opts->patch_mode cannot be used with switching branches so is | |
581 | * not tested here | |
582 | */ | |
583 | ||
584 | /* | |
585 | * opts->quiet only impacts output so doesn't require a merge | |
586 | */ | |
587 | ||
588 | /* | |
589 | * Honor the explicit request for a three-way merge or to throw away | |
590 | * local changes | |
591 | */ | |
592 | if (opts->merge || opts->force) | |
593 | return 0; | |
594 | ||
595 | /* | |
596 | * --detach is documented as "updating the index and the files in the | |
597 | * working tree" but this optimization skips those steps so fall through | |
598 | * to the regular code path. | |
599 | */ | |
600 | if (opts->force_detach) | |
601 | return 0; | |
602 | ||
603 | /* | |
604 | * opts->writeout_stage cannot be used with switching branches so is | |
605 | * not tested here | |
606 | */ | |
607 | ||
608 | /* | |
609 | * Honor the explicit ignore requests | |
610 | */ | |
611 | if (!opts->overwrite_ignore || opts->ignore_skipworktree || | |
612 | opts->ignore_other_worktrees) | |
613 | return 0; | |
614 | ||
615 | /* | |
616 | * opts->show_progress only impacts output so doesn't require a merge | |
617 | */ | |
618 | ||
091e04bc TG |
619 | /* |
620 | * opts->overlay_mode cannot be used with switching branches so is | |
621 | * not tested here | |
622 | */ | |
623 | ||
fa655d84 BP |
624 | /* |
625 | * If we aren't creating a new branch any changes or updates will | |
626 | * happen in the existing branch. Since that could only be updating | |
627 | * the index and working directory, we don't want to skip those steps | |
628 | * or we've defeated any purpose in running the command. | |
629 | */ | |
630 | if (!opts->new_branch) | |
631 | return 0; | |
632 | ||
633 | /* | |
634 | * new_branch_force is defined to "create/reset and checkout a branch" | |
635 | * so needs to go through the merge to do the reset | |
636 | */ | |
637 | if (opts->new_branch_force) | |
638 | return 0; | |
639 | ||
640 | /* | |
641 | * A new orphaned branch requrires the index and the working tree to be | |
642 | * adjusted to <start_point> | |
643 | */ | |
644 | if (opts->new_orphan_branch) | |
645 | return 0; | |
646 | ||
647 | /* | |
648 | * Remaining variables are not checkout options but used to track state | |
649 | */ | |
650 | ||
8424bfd4 BP |
651 | /* |
652 | * Do the merge if this is the initial checkout. We cannot use | |
653 | * is_cache_unborn() here because the index hasn't been loaded yet | |
654 | * so cache_nr and timestamp.sec are always zero. | |
655 | */ | |
656 | if (!file_exists(get_index_file())) | |
657 | return 0; | |
658 | ||
fa655d84 BP |
659 | return 1; |
660 | } | |
661 | ||
a2b4994c | 662 | static int merge_working_tree(const struct checkout_opts *opts, |
c8a3ea1f BW |
663 | struct branch_info *old_branch_info, |
664 | struct branch_info *new_branch_info, | |
a2b4994c | 665 | int *writeout_error) |
782c2d65 DB |
666 | { |
667 | int ret; | |
837e34eb | 668 | struct lock_file lock_file = LOCK_INIT; |
b96524f8 | 669 | |
837e34eb | 670 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
53996fe5 | 671 | if (read_cache_preload(NULL) < 0) |
e923a8ab | 672 | return error(_("index file corrupt")); |
782c2d65 | 673 | |
cfc5789a | 674 | resolve_undo_clear(); |
782c2d65 | 675 | if (opts->force) { |
2e27bd77 DS |
676 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
677 | opts, 1, writeout_error); | |
782c2d65 DB |
678 | if (ret) |
679 | return ret; | |
680 | } else { | |
681 | struct tree_desc trees[2]; | |
682 | struct tree *tree; | |
683 | struct unpack_trees_options topts; | |
bc052d7f | 684 | |
782c2d65 DB |
685 | memset(&topts, 0, sizeof(topts)); |
686 | topts.head_idx = -1; | |
34110cd4 LT |
687 | topts.src_index = &the_index; |
688 | topts.dst_index = &the_index; | |
782c2d65 | 689 | |
e294030f | 690 | setup_unpack_trees_porcelain(&topts, "checkout"); |
8ccba008 | 691 | |
782c2d65 DB |
692 | refresh_cache(REFRESH_QUIET); |
693 | ||
694 | if (unmerged_cache()) { | |
e8a8a4d7 | 695 | error(_("you need to resolve your current index first")); |
04c9e11f | 696 | return 1; |
782c2d65 | 697 | } |
04c9e11f JH |
698 | |
699 | /* 2-way merge to the new branch */ | |
fa7b3c2f | 700 | topts.initial_checkout = is_cache_unborn(); |
04c9e11f JH |
701 | topts.update = 1; |
702 | topts.merge = 1; | |
b165fac8 | 703 | topts.quiet = opts->merge && old_branch_info->commit; |
870ebdb9 | 704 | topts.verbose_update = opts->show_progress; |
04c9e11f | 705 | topts.fn = twoway_merge; |
c1d7036b NTND |
706 | if (opts->overwrite_ignore) { |
707 | topts.dir = xcalloc(1, sizeof(*topts.dir)); | |
708 | topts.dir->flags |= DIR_SHOW_IGNORED; | |
709 | setup_standard_excludes(topts.dir); | |
710 | } | |
c8a3ea1f BW |
711 | tree = parse_tree_indirect(old_branch_info->commit ? |
712 | &old_branch_info->commit->object.oid : | |
eb0ccfd7 | 713 | the_hash_algo->empty_tree); |
04c9e11f | 714 | init_tree_desc(&trees[0], tree->buffer, tree->size); |
c8a3ea1f | 715 | tree = parse_tree_indirect(&new_branch_info->commit->object.oid); |
04c9e11f JH |
716 | init_tree_desc(&trees[1], tree->buffer, tree->size); |
717 | ||
291d823e | 718 | ret = unpack_trees(2, trees, &topts); |
1c41d280 | 719 | clear_unpack_trees_porcelain(&topts); |
49d833dc | 720 | if (ret == -1) { |
782c2d65 DB |
721 | /* |
722 | * Unpack couldn't do a trivial merge; either | |
723 | * give up or do a real merge, depending on | |
724 | * whether the merge flag was used. | |
725 | */ | |
726 | struct tree *result; | |
727 | struct tree *work; | |
6eff409e | 728 | struct tree *old_tree; |
8a2fce18 | 729 | struct merge_options o; |
a7256deb NTND |
730 | struct strbuf sb = STRBUF_INIT; |
731 | ||
782c2d65 DB |
732 | if (!opts->merge) |
733 | return 1; | |
64da3ae5 JH |
734 | |
735 | /* | |
c8a3ea1f | 736 | * Without old_branch_info->commit, the below is the same as |
64da3ae5 JH |
737 | * the two-tree unpack we already tried and failed. |
738 | */ | |
c8a3ea1f | 739 | if (!old_branch_info->commit) |
64da3ae5 | 740 | return 1; |
6eff409e NTND |
741 | old_tree = get_commit_tree(old_branch_info->commit); |
742 | ||
743 | if (repo_index_has_changes(the_repository, old_tree, &sb)) | |
744 | die(_("cannot continue with staged changes in " | |
745 | "the following files:\n%s"), sb.buf); | |
746 | strbuf_release(&sb); | |
782c2d65 | 747 | |
a7256deb NTND |
748 | if (repo_index_has_changes(the_repository, |
749 | get_commit_tree(old_branch_info->commit), | |
750 | &sb)) | |
751 | warning(_("staged changes in the following files may be lost: %s"), | |
752 | sb.buf); | |
753 | strbuf_release(&sb); | |
754 | ||
782c2d65 DB |
755 | /* Do more real merge */ |
756 | ||
757 | /* | |
758 | * We update the index fully, then write the | |
759 | * tree from the index, then merge the new | |
760 | * branch with the current tree, with the old | |
761 | * branch as the base. Then we reset the index | |
762 | * (but not the working tree) to the new | |
763 | * branch, leaving the working tree as the | |
764 | * merged version, but skipping unmerged | |
765 | * entries in the index. | |
766 | */ | |
767 | ||
610d55af | 768 | add_files_to_cache(NULL, NULL, 0); |
7610fa57 JN |
769 | /* |
770 | * NEEDSWORK: carrying over local changes | |
771 | * when branches have different end-of-line | |
772 | * normalization (or clean+smudge rules) is | |
773 | * a pain; plumb in an option to set | |
774 | * o.renormalize? | |
775 | */ | |
0d6caa2d | 776 | init_merge_options(&o, the_repository); |
8a2fce18 MV |
777 | o.verbosity = 0; |
778 | work = write_tree_from_memory(&o); | |
782c2d65 | 779 | |
2e27bd77 DS |
780 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
781 | opts, 1, | |
a2b4994c | 782 | writeout_error); |
782c2d65 DB |
783 | if (ret) |
784 | return ret; | |
c8a3ea1f BW |
785 | o.ancestor = old_branch_info->name; |
786 | o.branch1 = new_branch_info->name; | |
8a2fce18 | 787 | o.branch2 = "local"; |
2e27bd77 DS |
788 | ret = merge_trees(&o, |
789 | get_commit_tree(new_branch_info->commit), | |
790 | work, | |
6eff409e | 791 | old_tree, |
2e27bd77 | 792 | &result); |
f241ff0d JS |
793 | if (ret < 0) |
794 | exit(128); | |
2e27bd77 DS |
795 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
796 | opts, 0, | |
a2b4994c | 797 | writeout_error); |
548009c0 | 798 | strbuf_release(&o.obuf); |
84a5750b JH |
799 | if (ret) |
800 | return ret; | |
782c2d65 DB |
801 | } |
802 | } | |
803 | ||
aecf567c DT |
804 | if (!active_cache_tree) |
805 | active_cache_tree = cache_tree(); | |
806 | ||
807 | if (!cache_tree_fully_valid(active_cache_tree)) | |
3fd13cbc | 808 | cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); |
aecf567c | 809 | |
837e34eb | 810 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
e8a8a4d7 | 811 | die(_("unable to write new index file")); |
782c2d65 | 812 | |
7fe4a728 | 813 | if (!opts->force && !opts->quiet) |
c8a3ea1f | 814 | show_local_changes(&new_branch_info->commit->object, &opts->diff_options); |
782c2d65 DB |
815 | |
816 | return 0; | |
817 | } | |
818 | ||
c8a3ea1f | 819 | static void report_tracking(struct branch_info *new_branch_info) |
79a1e6b4 | 820 | { |
6d21bf96 | 821 | struct strbuf sb = STRBUF_INIT; |
c8a3ea1f | 822 | struct branch *branch = branch_get(new_branch_info->name); |
79a1e6b4 | 823 | |
f39a757d | 824 | if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL)) |
79a1e6b4 | 825 | return; |
6d21bf96 JH |
826 | fputs(sb.buf, stdout); |
827 | strbuf_release(&sb); | |
b0030db3 | 828 | } |
79a1e6b4 | 829 | |
a2b4994c | 830 | static void update_refs_for_switch(const struct checkout_opts *opts, |
c8a3ea1f BW |
831 | struct branch_info *old_branch_info, |
832 | struct branch_info *new_branch_info) | |
782c2d65 | 833 | { |
f285a2d7 | 834 | struct strbuf msg = STRBUF_INIT; |
3bed291a | 835 | const char *old_desc, *reflog_msg; |
782c2d65 | 836 | if (opts->new_branch) { |
3631bf77 | 837 | if (opts->new_orphan_branch) { |
341fb286 CW |
838 | char *refname; |
839 | ||
840 | refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch); | |
841 | if (opts->new_branch_log && | |
842 | !should_autocreate_reflog(refname)) { | |
1a83c240 | 843 | int ret; |
a4c653df | 844 | struct strbuf err = STRBUF_INIT; |
3631bf77 | 845 | |
abd0cd3a | 846 | ret = safe_create_reflog(refname, 1, &err); |
1a83c240 | 847 | if (ret) { |
a4c653df DT |
848 | fprintf(stderr, _("Can not do reflog for '%s': %s\n"), |
849 | opts->new_orphan_branch, err.buf); | |
850 | strbuf_release(&err); | |
341fb286 | 851 | free(refname); |
3631bf77 EM |
852 | return; |
853 | } | |
abd0cd3a | 854 | strbuf_release(&err); |
3631bf77 | 855 | } |
341fb286 | 856 | free(refname); |
3631bf77 EM |
857 | } |
858 | else | |
4edce172 NTND |
859 | create_branch(the_repository, |
860 | opts->new_branch, new_branch_info->name, | |
02ac9837 | 861 | opts->new_branch_force ? 1 : 0, |
39bd6f72 | 862 | opts->new_branch_force ? 1 : 0, |
e2bbd0cc | 863 | opts->new_branch_log, |
f9a482e6 | 864 | opts->quiet, |
39bd6f72 | 865 | opts->track); |
c8a3ea1f BW |
866 | new_branch_info->name = opts->new_branch; |
867 | setup_branch_path(new_branch_info); | |
782c2d65 DB |
868 | } |
869 | ||
c8a3ea1f BW |
870 | old_desc = old_branch_info->name; |
871 | if (!old_desc && old_branch_info->commit) | |
872 | old_desc = oid_to_hex(&old_branch_info->commit->object.oid); | |
3bed291a RR |
873 | |
874 | reflog_msg = getenv("GIT_REFLOG_ACTION"); | |
875 | if (!reflog_msg) | |
876 | strbuf_addf(&msg, "checkout: moving from %s to %s", | |
c8a3ea1f | 877 | old_desc ? old_desc : "(invalid)", new_branch_info->name); |
3bed291a RR |
878 | else |
879 | strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg)); | |
782c2d65 | 880 | |
c8a3ea1f | 881 | if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) { |
f8bd36a4 | 882 | /* Nothing to do. */ |
c8a3ea1f BW |
883 | } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */ |
884 | update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL, | |
91774afc | 885 | REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); |
f8bd36a4 | 886 | if (!opts->quiet) { |
c8a3ea1f | 887 | if (old_branch_info->path && |
779b88a9 | 888 | advice_detached_head && !opts->force_detach) |
c8a3ea1f BW |
889 | detach_advice(new_branch_info->name); |
890 | describe_detached_head(_("HEAD is now at"), new_branch_info->commit); | |
f8bd36a4 | 891 | } |
c8a3ea1f BW |
892 | } else if (new_branch_info->path) { /* Switch branches. */ |
893 | if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0) | |
4636f651 | 894 | die(_("unable to update HEAD")); |
782c2d65 | 895 | if (!opts->quiet) { |
c8a3ea1f | 896 | if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { |
39bd6f72 JN |
897 | if (opts->new_branch_force) |
898 | fprintf(stderr, _("Reset branch '%s'\n"), | |
c8a3ea1f | 899 | new_branch_info->name); |
39bd6f72 JN |
900 | else |
901 | fprintf(stderr, _("Already on '%s'\n"), | |
c8a3ea1f | 902 | new_branch_info->name); |
08eaa4be ÆAB |
903 | } else if (opts->new_branch) { |
904 | if (opts->branch_exists) | |
c8a3ea1f | 905 | fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name); |
08eaa4be | 906 | else |
c8a3ea1f | 907 | fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name); |
08eaa4be | 908 | } else { |
e8a8a4d7 | 909 | fprintf(stderr, _("Switched to branch '%s'\n"), |
c8a3ea1f | 910 | new_branch_info->name); |
08eaa4be | 911 | } |
782c2d65 | 912 | } |
c8a3ea1f BW |
913 | if (old_branch_info->path && old_branch_info->name) { |
914 | if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path)) | |
915 | delete_reflog(old_branch_info->path); | |
3631bf77 | 916 | } |
782c2d65 | 917 | } |
4edce172 | 918 | remove_branch_state(the_repository); |
782c2d65 | 919 | strbuf_release(&msg); |
32669671 | 920 | if (!opts->quiet && |
c8a3ea1f BW |
921 | (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD")))) |
922 | report_tracking(new_branch_info); | |
782c2d65 DB |
923 | } |
924 | ||
468224e5 | 925 | static int add_pending_uninteresting_ref(const char *refname, |
fcb615f5 | 926 | const struct object_id *oid, |
468224e5 | 927 | int flags, void *cb_data) |
8e2dc6ac | 928 | { |
a58a1b01 | 929 | add_pending_oid(cb_data, refname, oid, UNINTERESTING); |
5c08dc48 JK |
930 | return 0; |
931 | } | |
8e2dc6ac JH |
932 | |
933 | static void describe_one_orphan(struct strbuf *sb, struct commit *commit) | |
934 | { | |
0be240cc | 935 | strbuf_addstr(sb, " "); |
30e677e0 | 936 | strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); |
0be240cc | 937 | strbuf_addch(sb, ' '); |
3c621839 JK |
938 | if (!parse_commit(commit)) |
939 | pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); | |
8e2dc6ac JH |
940 | strbuf_addch(sb, '\n'); |
941 | } | |
942 | ||
943 | #define ORPHAN_CUTOFF 4 | |
944 | static void suggest_reattach(struct commit *commit, struct rev_info *revs) | |
945 | { | |
946 | struct commit *c, *last = NULL; | |
947 | struct strbuf sb = STRBUF_INIT; | |
948 | int lost = 0; | |
949 | while ((c = get_revision(revs)) != NULL) { | |
950 | if (lost < ORPHAN_CUTOFF) | |
951 | describe_one_orphan(&sb, c); | |
952 | last = c; | |
953 | lost++; | |
954 | } | |
955 | if (ORPHAN_CUTOFF < lost) { | |
956 | int more = lost - ORPHAN_CUTOFF; | |
957 | if (more == 1) | |
958 | describe_one_orphan(&sb, last); | |
959 | else | |
f06f08b7 | 960 | strbuf_addf(&sb, _(" ... and %d more.\n"), more); |
8e2dc6ac JH |
961 | } |
962 | ||
963 | fprintf(stderr, | |
f06f08b7 ÆAB |
964 | Q_( |
965 | /* The singular version */ | |
966 | "Warning: you are leaving %d commit behind, " | |
967 | "not connected to\n" | |
968 | "any of your branches:\n\n" | |
0faf2474 | 969 | "%s\n", |
f06f08b7 ÆAB |
970 | /* The plural version */ |
971 | "Warning: you are leaving %d commits behind, " | |
8e2dc6ac JH |
972 | "not connected to\n" |
973 | "any of your branches:\n\n" | |
f807b3dc | 974 | "%s\n", |
f06f08b7 ÆAB |
975 | /* Give ngettext() the count */ |
976 | lost), | |
977 | lost, | |
f807b3dc | 978 | sb.buf); |
8e2dc6ac | 979 | strbuf_release(&sb); |
f807b3dc JH |
980 | |
981 | if (advice_detached_head) | |
982 | fprintf(stderr, | |
fc792ca8 TS |
983 | Q_( |
984 | /* The singular version */ | |
985 | "If you want to keep it by creating a new branch, " | |
986 | "this may be a good time\nto do so with:\n\n" | |
987 | " git branch <new-branch-name> %s\n\n", | |
988 | /* The plural version */ | |
f807b3dc JH |
989 | "If you want to keep them by creating a new branch, " |
990 | "this may be a good time\nto do so with:\n\n" | |
fc792ca8 TS |
991 | " git branch <new-branch-name> %s\n\n", |
992 | /* Give ngettext() the count */ | |
993 | lost), | |
aab9583f | 994 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
8e2dc6ac JH |
995 | } |
996 | ||
997 | /* | |
998 | * We are about to leave commit that was at the tip of a detached | |
999 | * HEAD. If it is not reachable from any ref, this is the last chance | |
1000 | * for the user to do so without resorting to reflog. | |
1001 | */ | |
c8a3ea1f | 1002 | static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit) |
8e2dc6ac | 1003 | { |
8e2dc6ac | 1004 | struct rev_info revs; |
c8a3ea1f | 1005 | struct object *object = &old_commit->object; |
8e2dc6ac | 1006 | |
2abf3503 | 1007 | repo_init_revisions(the_repository, &revs, NULL); |
468224e5 RS |
1008 | setup_revisions(0, NULL, &revs, NULL); |
1009 | ||
1010 | object->flags &= ~UNINTERESTING; | |
f2fd0760 | 1011 | add_pending_object(&revs, object, oid_to_hex(&object->oid)); |
468224e5 | 1012 | |
fcb615f5 | 1013 | for_each_ref(add_pending_uninteresting_ref, &revs); |
c8a3ea1f | 1014 | add_pending_oid(&revs, "HEAD", &new_commit->object.oid, UNINTERESTING); |
468224e5 | 1015 | |
8e2dc6ac | 1016 | if (prepare_revision_walk(&revs)) |
6c80cd29 | 1017 | die(_("internal error in revision walk")); |
c8a3ea1f BW |
1018 | if (!(old_commit->object.flags & UNINTERESTING)) |
1019 | suggest_reattach(old_commit, &revs); | |
8e2dc6ac | 1020 | else |
c8a3ea1f | 1021 | describe_detached_head(_("Previous HEAD position was"), old_commit); |
5c08dc48 | 1022 | |
b2ccdf7f | 1023 | /* Clean up objects used, as they will be reused. */ |
a9a03fa0 | 1024 | clear_commit_marks_all(ALL_REV_FLAGS); |
8e2dc6ac JH |
1025 | } |
1026 | ||
e51e3057 | 1027 | static int switch_branches(const struct checkout_opts *opts, |
c8a3ea1f | 1028 | struct branch_info *new_branch_info) |
782c2d65 DB |
1029 | { |
1030 | int ret = 0; | |
c8a3ea1f | 1031 | struct branch_info old_branch_info; |
96ec7b1e | 1032 | void *path_to_free; |
60af7691 | 1033 | struct object_id rev; |
a2b4994c | 1034 | int flag, writeout_error = 0; |
e27dd8ae JH |
1035 | |
1036 | trace2_cmd_mode("branch"); | |
1037 | ||
c8a3ea1f BW |
1038 | memset(&old_branch_info, 0, sizeof(old_branch_info)); |
1039 | old_branch_info.path = path_to_free = resolve_refdup("HEAD", 0, &rev, &flag); | |
1040 | if (old_branch_info.path) | |
21e1ee8f | 1041 | old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1); |
96ec7b1e | 1042 | if (!(flag & REF_ISSYMREF)) |
c8a3ea1f | 1043 | old_branch_info.path = NULL; |
782c2d65 | 1044 | |
c8a3ea1f BW |
1045 | if (old_branch_info.path) |
1046 | skip_prefix(old_branch_info.path, "refs/heads/", &old_branch_info.name); | |
782c2d65 | 1047 | |
c8a3ea1f BW |
1048 | if (!new_branch_info->name) { |
1049 | new_branch_info->name = "HEAD"; | |
1050 | new_branch_info->commit = old_branch_info.commit; | |
1051 | if (!new_branch_info->commit) | |
e8a8a4d7 | 1052 | die(_("You are on a branch yet to be born")); |
c8a3ea1f | 1053 | parse_commit_or_die(new_branch_info->commit); |
782c2d65 DB |
1054 | } |
1055 | ||
fa655d84 BP |
1056 | /* optimize the "checkout -b <new_branch> path */ |
1057 | if (skip_merge_working_tree(opts, &old_branch_info, new_branch_info)) { | |
1058 | if (!checkout_optimize_new_branch && !opts->quiet) { | |
1059 | if (read_cache_preload(NULL) < 0) | |
1060 | return error(_("index file corrupt")); | |
1061 | show_local_changes(&new_branch_info->commit->object, &opts->diff_options); | |
1062 | } | |
1063 | } else { | |
1064 | ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); | |
1065 | if (ret) { | |
1066 | free(path_to_free); | |
1067 | return ret; | |
1068 | } | |
96ec7b1e | 1069 | } |
782c2d65 | 1070 | |
c8a3ea1f BW |
1071 | if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit) |
1072 | orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit); | |
77ebd56d | 1073 | |
c8a3ea1f | 1074 | update_refs_for_switch(opts, &old_branch_info, new_branch_info); |
782c2d65 | 1075 | |
c8a3ea1f | 1076 | ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1); |
96ec7b1e | 1077 | free(path_to_free); |
a2b4994c | 1078 | return ret || writeout_error; |
782c2d65 DB |
1079 | } |
1080 | ||
0cf8581e JH |
1081 | static int git_checkout_config(const char *var, const char *value, void *cb) |
1082 | { | |
fa655d84 BP |
1083 | if (!strcmp(var, "checkout.optimizenewbranch")) { |
1084 | checkout_optimize_new_branch = git_config_bool(var, value); | |
1085 | return 0; | |
1086 | } | |
1087 | ||
175f6e59 JS |
1088 | if (!strcmp(var, "diff.ignoresubmodules")) { |
1089 | struct checkout_opts *opts = cb; | |
1090 | handle_ignore_submodules_arg(&opts->diff_options, value); | |
1091 | return 0; | |
1092 | } | |
23b4c7bc | 1093 | |
59556548 | 1094 | if (starts_with(var, "submodule.")) |
7463e2ec | 1095 | return git_default_submodule_config(var, value, NULL); |
23b4c7bc | 1096 | |
175f6e59 | 1097 | return git_xmerge_config(var, value, NULL); |
0cf8581e JH |
1098 | } |
1099 | ||
09ebad6f JN |
1100 | static int parse_branchname_arg(int argc, const char **argv, |
1101 | int dwim_new_local_branch_ok, | |
c8a3ea1f | 1102 | struct branch_info *new_branch_info, |
10f102be | 1103 | struct checkout_opts *opts, |
3c87aa94 ÆAB |
1104 | struct object_id *rev, |
1105 | int *dwim_remotes_matched) | |
09ebad6f | 1106 | { |
10f102be NTND |
1107 | struct tree **source_tree = &opts->source_tree; |
1108 | const char **new_branch = &opts->new_branch; | |
09ebad6f | 1109 | int argcount = 0; |
60af7691 | 1110 | struct object_id branch_rev; |
09ebad6f | 1111 | const char *arg; |
bca39695 MM |
1112 | int dash_dash_pos; |
1113 | int has_dash_dash = 0; | |
1114 | int i; | |
09ebad6f JN |
1115 | |
1116 | /* | |
1117 | * case 1: git checkout <ref> -- [<paths>] | |
1118 | * | |
1119 | * <ref> must be a valid tree, everything after the '--' must be | |
1120 | * a path. | |
1121 | * | |
1122 | * case 2: git checkout -- [<paths>] | |
1123 | * | |
1124 | * everything after the '--' must be paths. | |
1125 | * | |
a047fafc | 1126 | * case 3: git checkout <something> [--] |
09ebad6f | 1127 | * |
a047fafc MM |
1128 | * (a) If <something> is a commit, that is to |
1129 | * switch to the branch or detach HEAD at it. As a special case, | |
1130 | * if <something> is A...B (missing A or B means HEAD but you can | |
1131 | * omit at most one side), and if there is a unique merge base | |
1132 | * between A and B, A...B names that merge base. | |
09ebad6f | 1133 | * |
a047fafc | 1134 | * (b) If <something> is _not_ a commit, either "--" is present |
01689909 | 1135 | * or <something> is not a path, no -t or -b was given, and |
a047fafc | 1136 | * and there is a tracking branch whose name is <something> |
8d7b558b ÆAB |
1137 | * in one and only one remote (or if the branch exists on the |
1138 | * remote named in checkout.defaultRemote), then this is a | |
1139 | * short-hand to fork local <something> from that | |
1140 | * remote-tracking branch. | |
09ebad6f | 1141 | * |
a047fafc MM |
1142 | * (c) Otherwise, if "--" is present, treat it like case (1). |
1143 | * | |
1144 | * (d) Otherwise : | |
1145 | * - if it's a reference, treat it like case (1) | |
1146 | * - else if it's a path, treat it like case (2) | |
1147 | * - else: fail. | |
1148 | * | |
1149 | * case 4: git checkout <something> <paths> | |
1150 | * | |
1151 | * The first argument must not be ambiguous. | |
09ebad6f JN |
1152 | * - If it's *only* a reference, treat it like case (1). |
1153 | * - If it's only a path, treat it like case (2). | |
1154 | * - else: fail. | |
1155 | * | |
1156 | */ | |
1157 | if (!argc) | |
1158 | return 0; | |
1159 | ||
09ebad6f | 1160 | arg = argv[0]; |
bca39695 MM |
1161 | dash_dash_pos = -1; |
1162 | for (i = 0; i < argc; i++) { | |
1163 | if (!strcmp(argv[i], "--")) { | |
1164 | dash_dash_pos = i; | |
1165 | break; | |
1166 | } | |
1167 | } | |
1168 | if (dash_dash_pos == 0) | |
1169 | return 1; /* case (2) */ | |
1170 | else if (dash_dash_pos == 1) | |
1171 | has_dash_dash = 1; /* case (3) or (1) */ | |
1172 | else if (dash_dash_pos >= 2) | |
1173 | die(_("only one reference expected, %d given."), dash_dash_pos); | |
0f086e6d | 1174 | opts->count_checkout_paths = !opts->quiet && !has_dash_dash; |
09ebad6f JN |
1175 | |
1176 | if (!strcmp(arg, "-")) | |
1177 | arg = "@{-1}"; | |
1178 | ||
151b2911 | 1179 | if (get_oid_mb(arg, rev)) { |
a047fafc MM |
1180 | /* |
1181 | * Either case (3) or (4), with <something> not being | |
1182 | * a commit, or an attempt to use case (1) with an | |
1183 | * invalid ref. | |
1184 | * | |
1185 | * It's likely an error, but we need to find out if | |
1186 | * we should auto-create the branch, case (3).(b). | |
1187 | */ | |
1188 | int recover_with_dwim = dwim_new_local_branch_ok; | |
1189 | ||
be4908f1 NTND |
1190 | int could_be_checkout_paths = !has_dash_dash && |
1191 | check_filename(opts->prefix, arg); | |
1192 | ||
1193 | if (!has_dash_dash && !no_wildcard(arg)) | |
a047fafc | 1194 | recover_with_dwim = 0; |
be4908f1 | 1195 | |
a047fafc MM |
1196 | /* |
1197 | * Accept "git checkout foo" and "git checkout foo --" | |
1198 | * as candidates for dwim. | |
1199 | */ | |
1200 | if (!(argc == 1 && !has_dash_dash) && | |
1201 | !(argc == 2 && has_dash_dash)) | |
1202 | recover_with_dwim = 0; | |
1203 | ||
1204 | if (recover_with_dwim) { | |
3c87aa94 ÆAB |
1205 | const char *remote = unique_tracking_name(arg, rev, |
1206 | dwim_remotes_matched); | |
a047fafc | 1207 | if (remote) { |
be4908f1 NTND |
1208 | if (could_be_checkout_paths) |
1209 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
1210 | "Please use -- (and optionally --no-guess) to disambiguate"), | |
1211 | arg); | |
a047fafc MM |
1212 | *new_branch = arg; |
1213 | arg = remote; | |
1214 | /* DWIMmed to create local branch, case (3).(b) */ | |
1215 | } else { | |
1216 | recover_with_dwim = 0; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | if (!recover_with_dwim) { | |
1221 | if (has_dash_dash) | |
1222 | die(_("invalid reference: %s"), arg); | |
09ebad6f JN |
1223 | return argcount; |
1224 | } | |
1225 | } | |
1226 | ||
1227 | /* we can't end up being in (2) anymore, eat the argument */ | |
1228 | argcount++; | |
1229 | argv++; | |
1230 | argc--; | |
1231 | ||
c8a3ea1f BW |
1232 | new_branch_info->name = arg; |
1233 | setup_branch_path(new_branch_info); | |
09ebad6f | 1234 | |
c8a3ea1f BW |
1235 | if (!check_refname_format(new_branch_info->path, 0) && |
1236 | !read_ref(new_branch_info->path, &branch_rev)) | |
60af7691 | 1237 | oidcpy(rev, &branch_rev); |
09ebad6f | 1238 | else |
c8a3ea1f | 1239 | new_branch_info->path = NULL; /* not an existing branch */ |
09ebad6f | 1240 | |
21e1ee8f | 1241 | new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1); |
c8a3ea1f | 1242 | if (!new_branch_info->commit) { |
09ebad6f | 1243 | /* not a commit */ |
a9dbc179 | 1244 | *source_tree = parse_tree_indirect(rev); |
09ebad6f | 1245 | } else { |
c8a3ea1f | 1246 | parse_commit_or_die(new_branch_info->commit); |
2e27bd77 | 1247 | *source_tree = get_commit_tree(new_branch_info->commit); |
09ebad6f JN |
1248 | } |
1249 | ||
1250 | if (!*source_tree) /* case (1): want a tree */ | |
6c80cd29 | 1251 | die(_("reference is not a tree: %s"), arg); |
7f82b24e | 1252 | if (!has_dash_dash) { /* case (3).(d) -> (1) */ |
09ebad6f JN |
1253 | /* |
1254 | * Do not complain the most common case | |
1255 | * git checkout branch | |
1256 | * even if there happen to be a file called 'branch'; | |
1257 | * it would be extremely annoying. | |
1258 | */ | |
1259 | if (argc) | |
b829b943 | 1260 | verify_non_filename(opts->prefix, arg); |
09ebad6f JN |
1261 | } else { |
1262 | argcount++; | |
1263 | argv++; | |
1264 | argc--; | |
1265 | } | |
1266 | ||
1267 | return argcount; | |
1268 | } | |
1269 | ||
a2b4994c | 1270 | static int switch_unborn_to_new_branch(const struct checkout_opts *opts) |
abe19980 JH |
1271 | { |
1272 | int status; | |
1273 | struct strbuf branch_ref = STRBUF_INIT; | |
1274 | ||
e27dd8ae JH |
1275 | trace2_cmd_mode("unborn"); |
1276 | ||
8ced1aa0 CW |
1277 | if (!opts->new_branch) |
1278 | die(_("You are on a branch yet to be born")); | |
abe19980 JH |
1279 | strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); |
1280 | status = create_symref("HEAD", branch_ref.buf, "checkout -b"); | |
1281 | strbuf_release(&branch_ref); | |
afa8c07a JK |
1282 | if (!opts->quiet) |
1283 | fprintf(stderr, _("Switched to a new branch '%s'\n"), | |
1284 | opts->new_branch); | |
abe19980 JH |
1285 | return status; |
1286 | } | |
1287 | ||
b6312c27 | 1288 | static int checkout_branch(struct checkout_opts *opts, |
c8a3ea1f | 1289 | struct branch_info *new_branch_info) |
b6312c27 | 1290 | { |
817b345a | 1291 | if (opts->pathspec.nr) |
b6312c27 NTND |
1292 | die(_("paths cannot be used with switching branches")); |
1293 | ||
1294 | if (opts->patch_mode) | |
1295 | die(_("'%s' cannot be used with switching branches"), | |
1296 | "--patch"); | |
1297 | ||
091e04bc TG |
1298 | if (!opts->overlay_mode) |
1299 | die(_("'%s' cannot be used with switching branches"), | |
1300 | "--no-overlay"); | |
1301 | ||
b6312c27 NTND |
1302 | if (opts->writeout_stage) |
1303 | die(_("'%s' cannot be used with switching branches"), | |
1304 | "--ours/--theirs"); | |
1305 | ||
1306 | if (opts->force && opts->merge) | |
1307 | die(_("'%s' cannot be used with '%s'"), "-f", "-m"); | |
1308 | ||
1309 | if (opts->force_detach && opts->new_branch) | |
1310 | die(_("'%s' cannot be used with '%s'"), | |
1311 | "--detach", "-b/-B/--orphan"); | |
1312 | ||
1313 | if (opts->new_orphan_branch) { | |
1314 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1315 | die(_("'%s' cannot be used with '%s'"), "--orphan", "-t"); | |
1316 | } else if (opts->force_detach) { | |
1317 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1318 | die(_("'%s' cannot be used with '%s'"), "--detach", "-t"); | |
1319 | } else if (opts->track == BRANCH_TRACK_UNSPECIFIED) | |
1320 | opts->track = git_branch_track; | |
1321 | ||
c8a3ea1f | 1322 | if (new_branch_info->name && !new_branch_info->commit) |
b6312c27 | 1323 | die(_("Cannot switch branch to a non-commit '%s'"), |
c8a3ea1f | 1324 | new_branch_info->name); |
b6312c27 | 1325 | |
c8a3ea1f | 1326 | if (new_branch_info->path && !opts->force_detach && !opts->new_branch && |
c265c533 | 1327 | !opts->ignore_other_worktrees) { |
e1c1ab9d | 1328 | int flag; |
efbd4fdf | 1329 | char *head_ref = resolve_refdup("HEAD", 0, NULL, &flag); |
e1c1ab9d | 1330 | if (head_ref && |
c8a3ea1f BW |
1331 | (!(flag & REF_ISSYMREF) || strcmp(head_ref, new_branch_info->path))) |
1332 | die_if_checked_out(new_branch_info->path, 1); | |
e1c1ab9d NTND |
1333 | free(head_ref); |
1334 | } | |
1335 | ||
c8a3ea1f | 1336 | if (!new_branch_info->commit && opts->new_branch) { |
60af7691 | 1337 | struct object_id rev; |
b6312c27 NTND |
1338 | int flag; |
1339 | ||
34c290a6 | 1340 | if (!read_ref_full("HEAD", 0, &rev, &flag) && |
60af7691 | 1341 | (flag & REF_ISSYMREF) && is_null_oid(&rev)) |
b6312c27 NTND |
1342 | return switch_unborn_to_new_branch(opts); |
1343 | } | |
c8a3ea1f | 1344 | return switch_branches(opts, new_branch_info); |
b6312c27 NTND |
1345 | } |
1346 | ||
782c2d65 DB |
1347 | int cmd_checkout(int argc, const char **argv, const char *prefix) |
1348 | { | |
1349 | struct checkout_opts opts; | |
c8a3ea1f | 1350 | struct branch_info new_branch_info; |
eac5a401 | 1351 | char *conflict_style = NULL; |
be4908f1 | 1352 | int dwim_new_local_branch, no_dwim_new_local_branch = 0; |
3c87aa94 | 1353 | int dwim_remotes_matched = 0; |
782c2d65 | 1354 | struct option options[] = { |
e05a1093 NTND |
1355 | OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), |
1356 | OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), | |
1357 | N_("create and checkout a new branch")), | |
1358 | OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), | |
1359 | N_("create/reset and checkout a branch")), | |
d5d09d47 | 1360 | OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), |
e923a8ab | 1361 | OPT_BOOL(0, "detach", &opts.force_detach, N_("detach HEAD at named commit")), |
e05a1093 | 1362 | OPT_SET_INT('t', "track", &opts.track, N_("set upstream info for new branch"), |
9ed36cfa | 1363 | BRANCH_TRACK_EXPLICIT), |
e703d711 | 1364 | OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new-branch"), N_("new unparented branch")), |
3fe735e7 NTND |
1365 | OPT_SET_INT_F('2', "ours", &opts.writeout_stage, |
1366 | N_("checkout our version for unmerged files"), | |
1367 | 2, PARSE_OPT_NONEG), | |
1368 | OPT_SET_INT_F('3', "theirs", &opts.writeout_stage, | |
1369 | N_("checkout their version for unmerged files"), | |
1370 | 3, PARSE_OPT_NONEG), | |
77afafb2 NTND |
1371 | OPT__FORCE(&opts.force, N_("force checkout (throw away local modifications)"), |
1372 | PARSE_OPT_NOCOMPLETE), | |
d5d09d47 | 1373 | OPT_BOOL('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")), |
77afafb2 NTND |
1374 | OPT_BOOL_F(0, "overwrite-ignore", &opts.overwrite_ignore, |
1375 | N_("update ignored files (default)"), | |
1376 | PARSE_OPT_NOCOMPLETE), | |
e05a1093 NTND |
1377 | OPT_STRING(0, "conflict", &conflict_style, N_("style"), |
1378 | N_("conflict style (merge or diff3)")), | |
d5d09d47 | 1379 | OPT_BOOL('p', "patch", &opts.patch_mode, N_("select hunks interactively")), |
08d595dc NTND |
1380 | OPT_BOOL(0, "ignore-skip-worktree-bits", &opts.ignore_skipworktree, |
1381 | N_("do not limit pathspecs to sparse entries only")), | |
be4908f1 NTND |
1382 | OPT_BOOL(0, "no-guess", &no_dwim_new_local_branch, |
1383 | N_("do not second guess 'git checkout <no-such-branch>'")), | |
1d0fa898 NTND |
1384 | OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees, |
1385 | N_("do not check if another worktree is holding the given ref")), | |
58b75bd6 | 1386 | { OPTION_CALLBACK, 0, "recurse-submodules", NULL, |
1fc458d9 | 1387 | "checkout", "control recursive updating of submodules", |
d7a3803f | 1388 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater }, |
870ebdb9 | 1389 | OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")), |
091e04bc | 1390 | OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), |
b249b552 | 1391 | OPT_END(), |
782c2d65 DB |
1392 | }; |
1393 | ||
1394 | memset(&opts, 0, sizeof(opts)); | |
c8a3ea1f | 1395 | memset(&new_branch_info, 0, sizeof(new_branch_info)); |
c1d7036b | 1396 | opts.overwrite_ignore = 1; |
e51e3057 | 1397 | opts.prefix = prefix; |
870ebdb9 | 1398 | opts.show_progress = -1; |
091e04bc | 1399 | opts.overlay_mode = -1; |
782c2d65 | 1400 | |
175f6e59 | 1401 | git_config(git_checkout_config, &opts); |
782c2d65 | 1402 | |
9188ed89 | 1403 | opts.track = BRANCH_TRACK_UNSPECIFIED; |
782c2d65 | 1404 | |
37782920 | 1405 | argc = parse_options(argc, argv, prefix, options, checkout_usage, |
f5242ebf | 1406 | PARSE_OPT_KEEP_DASHDASH); |
859fdaba | 1407 | |
be4908f1 | 1408 | dwim_new_local_branch = !no_dwim_new_local_branch; |
870ebdb9 ECA |
1409 | if (opts.show_progress < 0) { |
1410 | if (opts.quiet) | |
1411 | opts.show_progress = 0; | |
1412 | else | |
1413 | opts.show_progress = isatty(2); | |
1414 | } | |
1415 | ||
b6312c27 NTND |
1416 | if (conflict_style) { |
1417 | opts.merge = 1; /* implied */ | |
1418 | git_xmerge_config("merge.conflictstyle", conflict_style, NULL); | |
1419 | } | |
02ac9837 | 1420 | |
b6312c27 NTND |
1421 | if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1) |
1422 | die(_("-b, -B and --orphan are mutually exclusive")); | |
02ac9837 | 1423 | |
091e04bc TG |
1424 | if (opts.overlay_mode == 1 && opts.patch_mode) |
1425 | die(_("-p and --overlay are mutually exclusive")); | |
1426 | ||
b6312c27 NTND |
1427 | /* |
1428 | * From here on, new_branch will contain the branch to be checked out, | |
1429 | * and new_branch_force and new_orphan_branch will tell us which one of | |
1430 | * -b/-B/--orphan is being used. | |
1431 | */ | |
02ac9837 TRC |
1432 | if (opts.new_branch_force) |
1433 | opts.new_branch = opts.new_branch_force; | |
1434 | ||
b6312c27 NTND |
1435 | if (opts.new_orphan_branch) |
1436 | opts.new_branch = opts.new_orphan_branch; | |
32669671 | 1437 | |
b6312c27 NTND |
1438 | /* --track without -b/-B/--orphan should DWIM */ |
1439 | if (opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) { | |
9188ed89 AR |
1440 | const char *argv0 = argv[0]; |
1441 | if (!argc || !strcmp(argv0, "--")) | |
1a07e59c | 1442 | die(_("--track needs a branch name")); |
e3f1da98 RS |
1443 | skip_prefix(argv0, "refs/", &argv0); |
1444 | skip_prefix(argv0, "remotes/", &argv0); | |
9188ed89 AR |
1445 | argv0 = strchr(argv0, '/'); |
1446 | if (!argv0 || !argv0[1]) | |
1a07e59c | 1447 | die(_("missing branch name; try -b")); |
9188ed89 | 1448 | opts.new_branch = argv0 + 1; |
bb0ceb62 JS |
1449 | } |
1450 | ||
859fdaba | 1451 | /* |
09ebad6f JN |
1452 | * Extract branch name from command line arguments, so |
1453 | * all that is left is pathspecs. | |
859fdaba | 1454 | * |
09ebad6f | 1455 | * Handle |
70c9ac2f | 1456 | * |
09ebad6f JN |
1457 | * 1) git checkout <tree> -- [<paths>] |
1458 | * 2) git checkout -- [<paths>] | |
1459 | * 3) git checkout <something> [<paths>] | |
859fdaba | 1460 | * |
09ebad6f JN |
1461 | * including "last branch" syntax and DWIM-ery for names of |
1462 | * remote branches, erroring out for invalid or ambiguous cases. | |
859fdaba | 1463 | */ |
782c2d65 | 1464 | if (argc) { |
60af7691 | 1465 | struct object_id rev; |
09ebad6f | 1466 | int dwim_ok = |
e51e3057 | 1467 | !opts.patch_mode && |
09ebad6f JN |
1468 | dwim_new_local_branch && |
1469 | opts.track == BRANCH_TRACK_UNSPECIFIED && | |
1470 | !opts.new_branch; | |
f8bd36a4 | 1471 | int n = parse_branchname_arg(argc, argv, dwim_ok, |
3c87aa94 ÆAB |
1472 | &new_branch_info, &opts, &rev, |
1473 | &dwim_remotes_matched); | |
09ebad6f JN |
1474 | argv += n; |
1475 | argc -= n; | |
782c2d65 DB |
1476 | } |
1477 | ||
782c2d65 | 1478 | if (argc) { |
480ca644 NTND |
1479 | parse_pathspec(&opts.pathspec, 0, |
1480 | opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0, | |
1481 | prefix, argv); | |
301e42ed | 1482 | |
817b345a | 1483 | if (!opts.pathspec.nr) |
e8a8a4d7 | 1484 | die(_("invalid path specification")); |
301e42ed | 1485 | |
b6312c27 NTND |
1486 | /* |
1487 | * Try to give more helpful suggestion. | |
1488 | * new_branch && argc > 1 will be caught later. | |
1489 | */ | |
1490 | if (opts.new_branch && argc == 1) | |
6c486862 JNA |
1491 | die(_("'%s' is not a commit and a branch '%s' cannot be created from it"), |
1492 | argv[0], opts.new_branch); | |
782c2d65 | 1493 | |
32669671 | 1494 | if (opts.force_detach) |
b6312c27 NTND |
1495 | die(_("git checkout: --detach does not take a path argument '%s'"), |
1496 | argv[0]); | |
32669671 | 1497 | |
0cf8581e | 1498 | if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge) |
b6312c27 NTND |
1499 | die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" |
1500 | "checking out of the index.")); | |
782c2d65 DB |
1501 | } |
1502 | ||
352eadc4 | 1503 | if (opts.new_branch) { |
f285a2d7 | 1504 | struct strbuf buf = STRBUF_INIT; |
55c4a673 | 1505 | |
bc1c9c0e JH |
1506 | if (opts.new_branch_force) |
1507 | opts.branch_exists = validate_branchname(opts.new_branch, &buf); | |
1508 | else | |
1509 | opts.branch_exists = | |
1510 | validate_new_branchname(opts.new_branch, &buf, 0); | |
352eadc4 DB |
1511 | strbuf_release(&buf); |
1512 | } | |
1513 | ||
886e1084 | 1514 | UNLEAK(opts); |
1c550553 ÆAB |
1515 | if (opts.patch_mode || opts.pathspec.nr) { |
1516 | int ret = checkout_paths(&opts, new_branch_info.name); | |
ad8d5104 ÆAB |
1517 | if (ret && dwim_remotes_matched > 1 && |
1518 | advice_checkout_ambiguous_remote_branch_name) | |
1519 | advise(_("'%s' matched more than one remote tracking branch.\n" | |
1520 | "We found %d remotes with a reference that matched. So we fell back\n" | |
1521 | "on trying to resolve the argument as a path, but failed there too!\n" | |
1522 | "\n" | |
1523 | "If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
1524 | "you can do so by fully qualifying the name with the --track option:\n" | |
1525 | "\n" | |
8d7b558b ÆAB |
1526 | " git checkout --track origin/<name>\n" |
1527 | "\n" | |
1528 | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
1529 | "one remote, e.g. the 'origin' remote, consider setting\n" | |
1530 | "checkout.defaultRemote=origin in your config."), | |
ad8d5104 ÆAB |
1531 | argv[0], |
1532 | dwim_remotes_matched); | |
1c550553 ÆAB |
1533 | return ret; |
1534 | } else { | |
c8a3ea1f | 1535 | return checkout_branch(&opts, &new_branch_info); |
1c550553 | 1536 | } |
782c2d65 | 1537 | } |