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