]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
3 | ||
4 | #include "builtin.h" | |
5 | #include "advice.h" | |
6 | #include "branch.h" | |
7 | #include "cache-tree.h" | |
8 | #include "checkout.h" | |
9 | #include "commit.h" | |
10 | #include "config.h" | |
11 | #include "diff.h" | |
12 | #include "dir.h" | |
13 | #include "environment.h" | |
14 | #include "gettext.h" | |
15 | #include "hex.h" | |
16 | #include "hook.h" | |
17 | #include "merge-ll.h" | |
18 | #include "lockfile.h" | |
19 | #include "mem-pool.h" | |
20 | #include "merge-ort-wrappers.h" | |
21 | #include "object-file.h" | |
22 | #include "object-name.h" | |
23 | #include "object-store.h" | |
24 | #include "parse-options.h" | |
25 | #include "path.h" | |
26 | #include "preload-index.h" | |
27 | #include "read-cache.h" | |
28 | #include "refs.h" | |
29 | #include "remote.h" | |
30 | #include "repo-settings.h" | |
31 | #include "resolve-undo.h" | |
32 | #include "revision.h" | |
33 | #include "setup.h" | |
34 | #include "submodule.h" | |
35 | #include "symlinks.h" | |
36 | #include "trace2.h" | |
37 | #include "tree.h" | |
38 | #include "tree-walk.h" | |
39 | #include "unpack-trees.h" | |
40 | #include "wt-status.h" | |
41 | #include "xdiff-interface.h" | |
42 | #include "entry.h" | |
43 | #include "parallel-checkout.h" | |
44 | #include "add-interactive.h" | |
45 | ||
46 | static const char * const checkout_usage[] = { | |
47 | N_("git checkout [<options>] <branch>"), | |
48 | N_("git checkout [<options>] [<branch>] -- <file>..."), | |
49 | NULL, | |
50 | }; | |
51 | ||
52 | static const char * const switch_branch_usage[] = { | |
53 | N_("git switch [<options>] [<branch>]"), | |
54 | NULL, | |
55 | }; | |
56 | ||
57 | static const char * const restore_usage[] = { | |
58 | N_("git restore [<options>] [--source=<branch>] <file>..."), | |
59 | NULL, | |
60 | }; | |
61 | ||
62 | struct checkout_opts { | |
63 | int patch_mode; | |
64 | int quiet; | |
65 | int merge; | |
66 | int force; | |
67 | int force_detach; | |
68 | int implicit_detach; | |
69 | int writeout_stage; | |
70 | int overwrite_ignore; | |
71 | int ignore_skipworktree; | |
72 | int ignore_other_worktrees; | |
73 | int show_progress; | |
74 | int count_checkout_paths; | |
75 | int overlay_mode; | |
76 | int dwim_new_local_branch; | |
77 | int discard_changes; | |
78 | int accept_ref; | |
79 | int accept_pathspec; | |
80 | int switch_branch_doing_nothing_is_ok; | |
81 | int only_merge_on_switching_branches; | |
82 | int can_switch_when_in_progress; | |
83 | int orphan_from_empty_tree; | |
84 | int empty_pathspec_ok; | |
85 | int checkout_index; | |
86 | int checkout_worktree; | |
87 | const char *ignore_unmerged_opt; | |
88 | int ignore_unmerged; | |
89 | int pathspec_file_nul; | |
90 | char *pathspec_from_file; | |
91 | ||
92 | const char *new_branch; | |
93 | const char *new_branch_force; | |
94 | const char *new_orphan_branch; | |
95 | int new_branch_log; | |
96 | enum branch_track track; | |
97 | struct diff_options diff_options; | |
98 | int conflict_style; | |
99 | ||
100 | int branch_exists; | |
101 | const char *prefix; | |
102 | struct pathspec pathspec; | |
103 | const char *from_treeish; | |
104 | struct tree *source_tree; | |
105 | }; | |
106 | ||
107 | #define CHECKOUT_OPTS_INIT { .conflict_style = -1, .merge = -1 } | |
108 | ||
109 | struct branch_info { | |
110 | char *name; /* The short name used */ | |
111 | char *path; /* The full name of a real branch */ | |
112 | struct commit *commit; /* The named commit */ | |
113 | char *refname; /* The full name of the ref being checked out. */ | |
114 | struct object_id oid; /* The object ID of the commit being checked out. */ | |
115 | /* | |
116 | * if not null the branch is detached because it's already | |
117 | * checked out in this checkout | |
118 | */ | |
119 | char *checkout; | |
120 | }; | |
121 | ||
122 | static void branch_info_release(struct branch_info *info) | |
123 | { | |
124 | free(info->name); | |
125 | free(info->path); | |
126 | free(info->refname); | |
127 | free(info->checkout); | |
128 | } | |
129 | ||
130 | static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, | |
131 | int changed) | |
132 | { | |
133 | return run_hooks_l(the_repository, "post-checkout", | |
134 | oid_to_hex(old_commit ? &old_commit->object.oid : null_oid(the_hash_algo)), | |
135 | oid_to_hex(new_commit ? &new_commit->object.oid : null_oid(the_hash_algo)), | |
136 | changed ? "1" : "0", NULL); | |
137 | /* "new_commit" can be NULL when checking out from the index before | |
138 | a commit exists. */ | |
139 | ||
140 | } | |
141 | ||
142 | static int update_some(const struct object_id *oid, struct strbuf *base, | |
143 | const char *pathname, unsigned mode, void *context UNUSED) | |
144 | { | |
145 | int len; | |
146 | struct cache_entry *ce; | |
147 | int pos; | |
148 | ||
149 | if (S_ISDIR(mode)) | |
150 | return READ_TREE_RECURSIVE; | |
151 | ||
152 | len = base->len + strlen(pathname); | |
153 | ce = make_empty_cache_entry(the_repository->index, len); | |
154 | oidcpy(&ce->oid, oid); | |
155 | memcpy(ce->name, base->buf, base->len); | |
156 | memcpy(ce->name + base->len, pathname, len - base->len); | |
157 | ce->ce_flags = create_ce_flags(0) | CE_UPDATE; | |
158 | ce->ce_namelen = len; | |
159 | ce->ce_mode = create_ce_mode(mode); | |
160 | ||
161 | /* | |
162 | * If the entry is the same as the current index, we can leave the old | |
163 | * entry in place. Whether it is UPTODATE or not, checkout_entry will | |
164 | * do the right thing. | |
165 | */ | |
166 | pos = index_name_pos(the_repository->index, ce->name, ce->ce_namelen); | |
167 | if (pos >= 0) { | |
168 | struct cache_entry *old = the_repository->index->cache[pos]; | |
169 | if (ce->ce_mode == old->ce_mode && | |
170 | !ce_intent_to_add(old) && | |
171 | oideq(&ce->oid, &old->oid)) { | |
172 | old->ce_flags |= CE_UPDATE; | |
173 | discard_cache_entry(ce); | |
174 | return 0; | |
175 | } | |
176 | } | |
177 | ||
178 | add_index_entry(the_repository->index, ce, | |
179 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int read_tree_some(struct tree *tree, const struct pathspec *pathspec) | |
184 | { | |
185 | read_tree(the_repository, tree, | |
186 | pathspec, update_some, NULL); | |
187 | ||
188 | /* update the index with the given tree's info | |
189 | * for all args, expanding wildcards, and exit | |
190 | * with any non-zero return code. | |
191 | */ | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static int skip_same_name(const struct cache_entry *ce, int pos) | |
196 | { | |
197 | while (++pos < the_repository->index->cache_nr && | |
198 | !strcmp(the_repository->index->cache[pos]->name, ce->name)) | |
199 | ; /* skip */ | |
200 | return pos; | |
201 | } | |
202 | ||
203 | static int check_stage(int stage, const struct cache_entry *ce, int pos, | |
204 | int overlay_mode) | |
205 | { | |
206 | while (pos < the_repository->index->cache_nr && | |
207 | !strcmp(the_repository->index->cache[pos]->name, ce->name)) { | |
208 | if (ce_stage(the_repository->index->cache[pos]) == stage) | |
209 | return 0; | |
210 | pos++; | |
211 | } | |
212 | if (!overlay_mode) | |
213 | return 0; | |
214 | if (stage == 2) | |
215 | return error(_("path '%s' does not have our version"), ce->name); | |
216 | else | |
217 | return error(_("path '%s' does not have their version"), ce->name); | |
218 | } | |
219 | ||
220 | static int check_stages(unsigned stages, const struct cache_entry *ce, int pos) | |
221 | { | |
222 | unsigned seen = 0; | |
223 | const char *name = ce->name; | |
224 | ||
225 | while (pos < the_repository->index->cache_nr) { | |
226 | ce = the_repository->index->cache[pos]; | |
227 | if (strcmp(name, ce->name)) | |
228 | break; | |
229 | seen |= (1 << ce_stage(ce)); | |
230 | pos++; | |
231 | } | |
232 | if ((stages & seen) != stages) | |
233 | return error(_("path '%s' does not have all necessary versions"), | |
234 | name); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | static int checkout_stage(int stage, const struct cache_entry *ce, int pos, | |
239 | const struct checkout *state, int *nr_checkouts, | |
240 | int overlay_mode) | |
241 | { | |
242 | while (pos < the_repository->index->cache_nr && | |
243 | !strcmp(the_repository->index->cache[pos]->name, ce->name)) { | |
244 | if (ce_stage(the_repository->index->cache[pos]) == stage) | |
245 | return checkout_entry(the_repository->index->cache[pos], state, | |
246 | NULL, nr_checkouts); | |
247 | pos++; | |
248 | } | |
249 | if (!overlay_mode) { | |
250 | unlink_entry(ce, NULL); | |
251 | return 0; | |
252 | } | |
253 | if (stage == 2) | |
254 | return error(_("path '%s' does not have our version"), ce->name); | |
255 | else | |
256 | return error(_("path '%s' does not have their version"), ce->name); | |
257 | } | |
258 | ||
259 | static int checkout_merged(int pos, const struct checkout *state, | |
260 | int *nr_checkouts, struct mem_pool *ce_mem_pool, | |
261 | int conflict_style) | |
262 | { | |
263 | struct cache_entry *ce = the_repository->index->cache[pos]; | |
264 | const char *path = ce->name; | |
265 | mmfile_t ancestor, ours, theirs; | |
266 | enum ll_merge_result merge_status; | |
267 | int status; | |
268 | struct object_id oid; | |
269 | mmbuffer_t result_buf; | |
270 | struct object_id threeway[3]; | |
271 | unsigned mode = 0; | |
272 | struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT; | |
273 | int renormalize = 0; | |
274 | ||
275 | memset(threeway, 0, sizeof(threeway)); | |
276 | while (pos < the_repository->index->cache_nr) { | |
277 | int stage; | |
278 | stage = ce_stage(ce); | |
279 | if (!stage || strcmp(path, ce->name)) | |
280 | break; | |
281 | oidcpy(&threeway[stage - 1], &ce->oid); | |
282 | if (stage == 2) | |
283 | mode = create_ce_mode(ce->ce_mode); | |
284 | pos++; | |
285 | ce = the_repository->index->cache[pos]; | |
286 | } | |
287 | if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2])) | |
288 | return error(_("path '%s' does not have necessary versions"), path); | |
289 | ||
290 | read_mmblob(&ancestor, &threeway[0]); | |
291 | read_mmblob(&ours, &threeway[1]); | |
292 | read_mmblob(&theirs, &threeway[2]); | |
293 | ||
294 | git_config_get_bool("merge.renormalize", &renormalize); | |
295 | ll_opts.renormalize = renormalize; | |
296 | ll_opts.conflict_style = conflict_style; | |
297 | merge_status = ll_merge(&result_buf, path, &ancestor, "base", | |
298 | &ours, "ours", &theirs, "theirs", | |
299 | state->istate, &ll_opts); | |
300 | free(ancestor.ptr); | |
301 | free(ours.ptr); | |
302 | free(theirs.ptr); | |
303 | if (merge_status == LL_MERGE_BINARY_CONFLICT) | |
304 | warning("Cannot merge binary files: %s (%s vs. %s)", | |
305 | path, "ours", "theirs"); | |
306 | if (merge_status < 0 || !result_buf.ptr) { | |
307 | free(result_buf.ptr); | |
308 | return error(_("path '%s': cannot merge"), path); | |
309 | } | |
310 | ||
311 | /* | |
312 | * NEEDSWORK: | |
313 | * There is absolutely no reason to write this as a blob object | |
314 | * and create a phony cache entry. This hack is primarily to get | |
315 | * to the write_entry() machinery that massages the contents to | |
316 | * work-tree format and writes out which only allows it for a | |
317 | * cache entry. The code in write_entry() needs to be refactored | |
318 | * to allow us to feed a <buffer, size, mode> instead of a cache | |
319 | * entry. Such a refactoring would help merge_recursive as well | |
320 | * (it also writes the merge result to the object database even | |
321 | * when it may contain conflicts). | |
322 | */ | |
323 | if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid)) | |
324 | die(_("Unable to add merge result for '%s'"), path); | |
325 | free(result_buf.ptr); | |
326 | ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool); | |
327 | if (!ce) | |
328 | die(_("make_cache_entry failed for path '%s'"), path); | |
329 | status = checkout_entry(ce, state, NULL, nr_checkouts); | |
330 | return status; | |
331 | } | |
332 | ||
333 | static void mark_ce_for_checkout_overlay(struct cache_entry *ce, | |
334 | char *ps_matched, | |
335 | const struct checkout_opts *opts) | |
336 | { | |
337 | ce->ce_flags &= ~CE_MATCHED; | |
338 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
339 | return; | |
340 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
341 | /* | |
342 | * "git checkout tree-ish -- path", but this entry | |
343 | * is in the original index but is not in tree-ish | |
344 | * or does not match the pathspec; it will not be | |
345 | * checked out to the working tree. We will not do | |
346 | * anything to this entry at all. | |
347 | */ | |
348 | return; | |
349 | /* | |
350 | * Either this entry came from the tree-ish we are | |
351 | * checking the paths out of, or we are checking out | |
352 | * of the index. | |
353 | * | |
354 | * If it comes from the tree-ish, we already know it | |
355 | * matches the pathspec and could just stamp | |
356 | * CE_MATCHED to it from update_some(). But we still | |
357 | * need ps_matched and read_tree (and | |
358 | * eventually tree_entry_interesting) cannot fill | |
359 | * ps_matched yet. Once it can, we can avoid calling | |
360 | * match_pathspec() for _all_ entries when | |
361 | * opts->source_tree != NULL. | |
362 | */ | |
363 | if (ce_path_match(the_repository->index, ce, &opts->pathspec, ps_matched)) | |
364 | ce->ce_flags |= CE_MATCHED; | |
365 | } | |
366 | ||
367 | static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce, | |
368 | char *ps_matched, | |
369 | const struct checkout_opts *opts) | |
370 | { | |
371 | ce->ce_flags &= ~CE_MATCHED; | |
372 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
373 | return; | |
374 | if (ce_path_match(the_repository->index, ce, &opts->pathspec, ps_matched)) { | |
375 | ce->ce_flags |= CE_MATCHED; | |
376 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
377 | /* | |
378 | * In overlay mode, but the path is not in | |
379 | * tree-ish, which means we should remove it | |
380 | * from the index and the working tree. | |
381 | */ | |
382 | ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; | |
383 | } | |
384 | } | |
385 | ||
386 | static int checkout_worktree(const struct checkout_opts *opts, | |
387 | const struct branch_info *info) | |
388 | { | |
389 | struct checkout state = CHECKOUT_INIT; | |
390 | int nr_checkouts = 0, nr_unmerged = 0; | |
391 | int errs = 0; | |
392 | int pos; | |
393 | int pc_workers, pc_threshold; | |
394 | struct mem_pool ce_mem_pool; | |
395 | ||
396 | state.force = 1; | |
397 | state.refresh_cache = 1; | |
398 | state.istate = the_repository->index; | |
399 | ||
400 | mem_pool_init(&ce_mem_pool, 0); | |
401 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); | |
402 | init_checkout_metadata(&state.meta, info->refname, | |
403 | info->commit ? &info->commit->object.oid : &info->oid, | |
404 | NULL); | |
405 | ||
406 | enable_delayed_checkout(&state); | |
407 | ||
408 | if (pc_workers > 1) | |
409 | init_parallel_checkout(); | |
410 | ||
411 | for (pos = 0; pos < the_repository->index->cache_nr; pos++) { | |
412 | struct cache_entry *ce = the_repository->index->cache[pos]; | |
413 | if (ce->ce_flags & CE_MATCHED) { | |
414 | if (!ce_stage(ce)) { | |
415 | errs |= checkout_entry(ce, &state, | |
416 | NULL, &nr_checkouts); | |
417 | continue; | |
418 | } | |
419 | if (opts->writeout_stage) | |
420 | errs |= checkout_stage(opts->writeout_stage, | |
421 | ce, pos, | |
422 | &state, | |
423 | &nr_checkouts, opts->overlay_mode); | |
424 | else if (opts->merge) | |
425 | errs |= checkout_merged(pos, &state, | |
426 | &nr_unmerged, | |
427 | &ce_mem_pool, | |
428 | opts->conflict_style); | |
429 | pos = skip_same_name(ce, pos) - 1; | |
430 | } | |
431 | } | |
432 | if (pc_workers > 1) | |
433 | errs |= run_parallel_checkout(&state, pc_workers, pc_threshold, | |
434 | NULL, NULL); | |
435 | mem_pool_discard(&ce_mem_pool, should_validate_cache_entries()); | |
436 | remove_marked_cache_entries(the_repository->index, 1); | |
437 | remove_scheduled_dirs(); | |
438 | errs |= finish_delayed_checkout(&state, opts->show_progress); | |
439 | ||
440 | if (opts->count_checkout_paths) { | |
441 | if (nr_unmerged) | |
442 | fprintf_ln(stderr, Q_("Recreated %d merge conflict", | |
443 | "Recreated %d merge conflicts", | |
444 | nr_unmerged), | |
445 | nr_unmerged); | |
446 | if (opts->source_tree) | |
447 | fprintf_ln(stderr, Q_("Updated %d path from %s", | |
448 | "Updated %d paths from %s", | |
449 | nr_checkouts), | |
450 | nr_checkouts, | |
451 | repo_find_unique_abbrev(the_repository, &opts->source_tree->object.oid, | |
452 | DEFAULT_ABBREV)); | |
453 | else if (!nr_unmerged || nr_checkouts) | |
454 | fprintf_ln(stderr, Q_("Updated %d path from the index", | |
455 | "Updated %d paths from the index", | |
456 | nr_checkouts), | |
457 | nr_checkouts); | |
458 | } | |
459 | ||
460 | return errs; | |
461 | } | |
462 | ||
463 | static int checkout_paths(const struct checkout_opts *opts, | |
464 | const struct branch_info *new_branch_info) | |
465 | { | |
466 | int pos; | |
467 | static char *ps_matched; | |
468 | struct object_id rev; | |
469 | struct commit *head; | |
470 | int errs = 0; | |
471 | struct lock_file lock_file = LOCK_INIT; | |
472 | int checkout_index; | |
473 | ||
474 | trace2_cmd_mode(opts->patch_mode ? "patch" : "path"); | |
475 | ||
476 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
477 | die(_("'%s' cannot be used with updating paths"), "--track"); | |
478 | ||
479 | if (opts->new_branch_log) | |
480 | die(_("'%s' cannot be used with updating paths"), "-l"); | |
481 | ||
482 | if (opts->ignore_unmerged && opts->patch_mode) | |
483 | die(_("'%s' cannot be used with updating paths"), | |
484 | opts->ignore_unmerged_opt); | |
485 | ||
486 | if (opts->force_detach) | |
487 | die(_("'%s' cannot be used with updating paths"), "--detach"); | |
488 | ||
489 | if (opts->merge && opts->patch_mode) | |
490 | die(_("options '%s' and '%s' cannot be used together"), "--merge", "--patch"); | |
491 | ||
492 | if (opts->ignore_unmerged && opts->merge) | |
493 | die(_("options '%s' and '%s' cannot be used together"), | |
494 | opts->ignore_unmerged_opt, "-m"); | |
495 | ||
496 | if (opts->new_branch) | |
497 | die(_("Cannot update paths and switch to branch '%s' at the same time."), | |
498 | opts->new_branch); | |
499 | ||
500 | if (!opts->checkout_worktree && !opts->checkout_index) | |
501 | die(_("neither '%s' or '%s' is specified"), | |
502 | "--staged", "--worktree"); | |
503 | ||
504 | if (!opts->checkout_worktree && !opts->from_treeish) | |
505 | die(_("'%s' must be used when '%s' is not specified"), | |
506 | "--worktree", "--source"); | |
507 | ||
508 | /* | |
509 | * Reject --staged option to the restore command when combined with | |
510 | * merge-related options. Use the accept_ref flag to distinguish it | |
511 | * from the checkout command, which does not accept --staged anyway. | |
512 | * | |
513 | * `restore --ours|--theirs --worktree --staged` could mean resolving | |
514 | * conflicted paths to one side in both the worktree and the index, | |
515 | * but does not currently. | |
516 | * | |
517 | * `restore --merge|--conflict=<style>` already recreates conflicts | |
518 | * in both the worktree and the index, so adding --staged would be | |
519 | * meaningless. | |
520 | */ | |
521 | if (!opts->accept_ref && opts->checkout_index) { | |
522 | if (opts->writeout_stage) | |
523 | die(_("'%s' or '%s' cannot be used with %s"), | |
524 | "--ours", "--theirs", "--staged"); | |
525 | ||
526 | if (opts->merge) | |
527 | die(_("'%s' or '%s' cannot be used with %s"), | |
528 | "--merge", "--conflict", "--staged"); | |
529 | } | |
530 | ||
531 | /* | |
532 | * recreating unmerged index entries and writing out data from | |
533 | * unmerged index entries would make no sense when checking out | |
534 | * of a tree-ish. | |
535 | */ | |
536 | if ((opts->merge || opts->writeout_stage) && opts->source_tree) | |
537 | die(_("'%s', '%s', or '%s' cannot be used when checking out of a tree"), | |
538 | "--merge", "--ours", "--theirs"); | |
539 | ||
540 | if (opts->patch_mode) { | |
541 | enum add_p_mode patch_mode; | |
542 | const char *rev = new_branch_info->name; | |
543 | char rev_oid[GIT_MAX_HEXSZ + 1]; | |
544 | ||
545 | /* | |
546 | * Since rev can be in the form of `<a>...<b>` (which is not | |
547 | * recognized by diff-index), we will always replace the name | |
548 | * with the hex of the commit (whether it's in `...` form or | |
549 | * not) for the run_add_interactive() machinery to work | |
550 | * properly. However, there is special logic for the HEAD case | |
551 | * so we mustn't replace that. Also, when we were given a | |
552 | * tree-object, new_branch_info->commit would be NULL, but we | |
553 | * do not have to do any replacement, either. | |
554 | */ | |
555 | if (rev && new_branch_info->commit && strcmp(rev, "HEAD")) | |
556 | rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid); | |
557 | ||
558 | if (opts->checkout_index && opts->checkout_worktree) | |
559 | patch_mode = ADD_P_CHECKOUT; | |
560 | else if (opts->checkout_index && !opts->checkout_worktree) | |
561 | patch_mode = ADD_P_RESET; | |
562 | else if (!opts->checkout_index && opts->checkout_worktree) | |
563 | patch_mode = ADD_P_WORKTREE; | |
564 | else | |
565 | BUG("either flag must have been set, worktree=%d, index=%d", | |
566 | opts->checkout_worktree, opts->checkout_index); | |
567 | return !!run_add_p(the_repository, patch_mode, rev, | |
568 | &opts->pathspec); | |
569 | } | |
570 | ||
571 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); | |
572 | if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0) | |
573 | return error(_("index file corrupt")); | |
574 | ||
575 | if (opts->source_tree) | |
576 | read_tree_some(opts->source_tree, &opts->pathspec); | |
577 | if (opts->merge) | |
578 | unmerge_index(the_repository->index, &opts->pathspec, CE_MATCHED); | |
579 | ||
580 | ps_matched = xcalloc(opts->pathspec.nr, 1); | |
581 | ||
582 | /* | |
583 | * Make sure all pathspecs participated in locating the paths | |
584 | * to be checked out. | |
585 | */ | |
586 | for (pos = 0; pos < the_repository->index->cache_nr; pos++) | |
587 | if (opts->overlay_mode) | |
588 | mark_ce_for_checkout_overlay(the_repository->index->cache[pos], | |
589 | ps_matched, | |
590 | opts); | |
591 | else | |
592 | mark_ce_for_checkout_no_overlay(the_repository->index->cache[pos], | |
593 | ps_matched, | |
594 | opts); | |
595 | ||
596 | if (report_path_error(ps_matched, &opts->pathspec)) { | |
597 | free(ps_matched); | |
598 | return 1; | |
599 | } | |
600 | free(ps_matched); | |
601 | ||
602 | /* Any unmerged paths? */ | |
603 | for (pos = 0; pos < the_repository->index->cache_nr; pos++) { | |
604 | const struct cache_entry *ce = the_repository->index->cache[pos]; | |
605 | if (ce->ce_flags & CE_MATCHED) { | |
606 | if (!ce_stage(ce)) | |
607 | continue; | |
608 | if (opts->ignore_unmerged) { | |
609 | if (!opts->quiet) | |
610 | warning(_("path '%s' is unmerged"), ce->name); | |
611 | } else if (opts->writeout_stage) { | |
612 | errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode); | |
613 | } else if (opts->merge) { | |
614 | errs |= check_stages((1<<2) | (1<<3), ce, pos); | |
615 | } else { | |
616 | errs = 1; | |
617 | error(_("path '%s' is unmerged"), ce->name); | |
618 | } | |
619 | pos = skip_same_name(ce, pos) - 1; | |
620 | } | |
621 | } | |
622 | if (errs) | |
623 | return 1; | |
624 | ||
625 | /* Now we are committed to check them out */ | |
626 | if (opts->checkout_worktree) | |
627 | errs |= checkout_worktree(opts, new_branch_info); | |
628 | else | |
629 | remove_marked_cache_entries(the_repository->index, 1); | |
630 | ||
631 | /* | |
632 | * Allow updating the index when checking out from the index. | |
633 | * This is to save new stat info. | |
634 | */ | |
635 | if (opts->checkout_worktree && !opts->checkout_index && !opts->source_tree) | |
636 | checkout_index = 1; | |
637 | else | |
638 | checkout_index = opts->checkout_index; | |
639 | ||
640 | if (checkout_index) { | |
641 | if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK)) | |
642 | die(_("unable to write new index file")); | |
643 | } else { | |
644 | /* | |
645 | * NEEDSWORK: if --worktree is not specified, we | |
646 | * should save stat info of checked out files in the | |
647 | * index to avoid the next (potentially costly) | |
648 | * refresh. But it's a bit tricker to do... | |
649 | */ | |
650 | rollback_lock_file(&lock_file); | |
651 | } | |
652 | ||
653 | refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, | |
654 | &rev, NULL); | |
655 | head = lookup_commit_reference_gently(the_repository, &rev, 1); | |
656 | ||
657 | errs |= post_checkout_hook(head, head, 0); | |
658 | return errs; | |
659 | } | |
660 | ||
661 | static void show_local_changes(struct object *head, | |
662 | const struct diff_options *opts) | |
663 | { | |
664 | struct rev_info rev; | |
665 | /* I think we want full paths, even if we're in a subdirectory. */ | |
666 | repo_init_revisions(the_repository, &rev, NULL); | |
667 | rev.diffopt.flags = opts->flags; | |
668 | rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; | |
669 | rev.diffopt.flags.recursive = 1; | |
670 | diff_setup_done(&rev.diffopt); | |
671 | add_pending_object(&rev, head, NULL); | |
672 | run_diff_index(&rev, 0); | |
673 | release_revisions(&rev); | |
674 | } | |
675 | ||
676 | static void describe_detached_head(const char *msg, struct commit *commit) | |
677 | { | |
678 | struct strbuf sb = STRBUF_INIT; | |
679 | ||
680 | if (!repo_parse_commit(the_repository, commit)) | |
681 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); | |
682 | if (print_sha1_ellipsis()) { | |
683 | fprintf(stderr, "%s %s... %s\n", msg, | |
684 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV), | |
685 | sb.buf); | |
686 | } else { | |
687 | fprintf(stderr, "%s %s %s\n", msg, | |
688 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV), | |
689 | sb.buf); | |
690 | } | |
691 | strbuf_release(&sb); | |
692 | } | |
693 | ||
694 | static int reset_tree(struct tree *tree, const struct checkout_opts *o, | |
695 | int worktree, int *writeout_error, | |
696 | struct branch_info *info) | |
697 | { | |
698 | struct unpack_trees_options opts; | |
699 | struct tree_desc tree_desc; | |
700 | ||
701 | memset(&opts, 0, sizeof(opts)); | |
702 | opts.head_idx = -1; | |
703 | opts.update = worktree; | |
704 | opts.skip_unmerged = !worktree; | |
705 | opts.reset = o->force ? UNPACK_RESET_OVERWRITE_UNTRACKED : | |
706 | UNPACK_RESET_PROTECT_UNTRACKED; | |
707 | opts.preserve_ignored = (!o->force && !o->overwrite_ignore); | |
708 | opts.merge = 1; | |
709 | opts.fn = oneway_merge; | |
710 | opts.verbose_update = o->show_progress; | |
711 | opts.src_index = the_repository->index; | |
712 | opts.dst_index = the_repository->index; | |
713 | init_checkout_metadata(&opts.meta, info->refname, | |
714 | info->commit ? &info->commit->object.oid : null_oid(the_hash_algo), | |
715 | NULL); | |
716 | if (parse_tree(tree) < 0) | |
717 | return 128; | |
718 | init_tree_desc(&tree_desc, &tree->object.oid, tree->buffer, tree->size); | |
719 | switch (unpack_trees(1, &tree_desc, &opts)) { | |
720 | case -2: | |
721 | *writeout_error = 1; | |
722 | /* | |
723 | * We return 0 nevertheless, as the index is all right | |
724 | * and more importantly we have made best efforts to | |
725 | * update paths in the work tree, and we cannot revert | |
726 | * them. | |
727 | */ | |
728 | /* fallthrough */ | |
729 | case 0: | |
730 | return 0; | |
731 | default: | |
732 | return 128; | |
733 | } | |
734 | } | |
735 | ||
736 | static void setup_branch_path(struct branch_info *branch) | |
737 | { | |
738 | struct strbuf buf = STRBUF_INIT; | |
739 | ||
740 | /* | |
741 | * If this is a ref, resolve it; otherwise, look up the OID for our | |
742 | * expression. Failure here is okay. | |
743 | */ | |
744 | if (!repo_dwim_ref(the_repository, branch->name, strlen(branch->name), | |
745 | &branch->oid, &branch->refname, 0)) | |
746 | repo_get_oid_committish(the_repository, branch->name, &branch->oid); | |
747 | ||
748 | copy_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); | |
749 | if (strcmp(buf.buf, branch->name)) { | |
750 | free(branch->name); | |
751 | branch->name = xstrdup(buf.buf); | |
752 | } | |
753 | strbuf_splice(&buf, 0, 0, "refs/heads/", 11); | |
754 | free(branch->path); | |
755 | branch->path = strbuf_detach(&buf, NULL); | |
756 | } | |
757 | ||
758 | static void init_topts(struct unpack_trees_options *topts, int merge, | |
759 | int show_progress, int overwrite_ignore, | |
760 | struct commit *old_commit) | |
761 | { | |
762 | memset(topts, 0, sizeof(*topts)); | |
763 | topts->head_idx = -1; | |
764 | topts->src_index = the_repository->index; | |
765 | topts->dst_index = the_repository->index; | |
766 | ||
767 | setup_unpack_trees_porcelain(topts, "checkout"); | |
768 | ||
769 | topts->initial_checkout = is_index_unborn(the_repository->index); | |
770 | topts->update = 1; | |
771 | topts->merge = 1; | |
772 | topts->quiet = merge && old_commit; | |
773 | topts->verbose_update = show_progress; | |
774 | topts->fn = twoway_merge; | |
775 | topts->preserve_ignored = !overwrite_ignore; | |
776 | } | |
777 | ||
778 | static int merge_working_tree(const struct checkout_opts *opts, | |
779 | struct branch_info *old_branch_info, | |
780 | struct branch_info *new_branch_info, | |
781 | int *writeout_error) | |
782 | { | |
783 | int ret; | |
784 | struct lock_file lock_file = LOCK_INIT; | |
785 | struct tree *new_tree; | |
786 | ||
787 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); | |
788 | if (repo_read_index_preload(the_repository, NULL, 0) < 0) | |
789 | return error(_("index file corrupt")); | |
790 | ||
791 | resolve_undo_clear_index(the_repository->index); | |
792 | if (opts->new_orphan_branch && opts->orphan_from_empty_tree) { | |
793 | if (new_branch_info->commit) | |
794 | BUG("'switch --orphan' should never accept a commit as starting point"); | |
795 | new_tree = parse_tree_indirect(the_hash_algo->empty_tree); | |
796 | if (!new_tree) | |
797 | BUG("unable to read empty tree"); | |
798 | } else { | |
799 | new_tree = repo_get_commit_tree(the_repository, | |
800 | new_branch_info->commit); | |
801 | if (!new_tree) | |
802 | return error(_("unable to read tree (%s)"), | |
803 | oid_to_hex(&new_branch_info->commit->object.oid)); | |
804 | } | |
805 | if (opts->discard_changes) { | |
806 | ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info); | |
807 | if (ret) | |
808 | return ret; | |
809 | } else { | |
810 | struct tree_desc trees[2]; | |
811 | struct tree *tree; | |
812 | struct unpack_trees_options topts; | |
813 | const struct object_id *old_commit_oid; | |
814 | ||
815 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); | |
816 | ||
817 | if (unmerged_index(the_repository->index)) { | |
818 | error(_("you need to resolve your current index first")); | |
819 | return 1; | |
820 | } | |
821 | ||
822 | /* 2-way merge to the new branch */ | |
823 | init_topts(&topts, opts->merge, opts->show_progress, | |
824 | opts->overwrite_ignore, old_branch_info->commit); | |
825 | init_checkout_metadata(&topts.meta, new_branch_info->refname, | |
826 | new_branch_info->commit ? | |
827 | &new_branch_info->commit->object.oid : | |
828 | &new_branch_info->oid, NULL); | |
829 | ||
830 | old_commit_oid = old_branch_info->commit ? | |
831 | &old_branch_info->commit->object.oid : | |
832 | the_hash_algo->empty_tree; | |
833 | tree = parse_tree_indirect(old_commit_oid); | |
834 | if (!tree) | |
835 | die(_("unable to parse commit %s"), | |
836 | oid_to_hex(old_commit_oid)); | |
837 | ||
838 | init_tree_desc(&trees[0], &tree->object.oid, | |
839 | tree->buffer, tree->size); | |
840 | if (parse_tree(new_tree) < 0) | |
841 | die(NULL); | |
842 | tree = new_tree; | |
843 | init_tree_desc(&trees[1], &tree->object.oid, | |
844 | tree->buffer, tree->size); | |
845 | ||
846 | ret = unpack_trees(2, trees, &topts); | |
847 | clear_unpack_trees_porcelain(&topts); | |
848 | if (ret == -1) { | |
849 | /* | |
850 | * Unpack couldn't do a trivial merge; either | |
851 | * give up or do a real merge, depending on | |
852 | * whether the merge flag was used. | |
853 | */ | |
854 | struct tree *work; | |
855 | struct tree *old_tree; | |
856 | struct merge_options o; | |
857 | struct strbuf sb = STRBUF_INIT; | |
858 | struct strbuf old_commit_shortname = STRBUF_INIT; | |
859 | ||
860 | if (!opts->merge) | |
861 | return 1; | |
862 | ||
863 | /* | |
864 | * Without old_branch_info->commit, the below is the same as | |
865 | * the two-tree unpack we already tried and failed. | |
866 | */ | |
867 | if (!old_branch_info->commit) | |
868 | return 1; | |
869 | old_tree = repo_get_commit_tree(the_repository, | |
870 | old_branch_info->commit); | |
871 | ||
872 | if (repo_index_has_changes(the_repository, old_tree, &sb)) | |
873 | die(_("cannot continue with staged changes in " | |
874 | "the following files:\n%s"), sb.buf); | |
875 | strbuf_release(&sb); | |
876 | ||
877 | /* Do more real merge */ | |
878 | ||
879 | /* | |
880 | * We update the index fully, then write the | |
881 | * tree from the index, then merge the new | |
882 | * branch with the current tree, with the old | |
883 | * branch as the base. Then we reset the index | |
884 | * (but not the working tree) to the new | |
885 | * branch, leaving the working tree as the | |
886 | * merged version, but skipping unmerged | |
887 | * entries in the index. | |
888 | */ | |
889 | ||
890 | add_files_to_cache(the_repository, NULL, NULL, NULL, 0, | |
891 | 0); | |
892 | init_ui_merge_options(&o, the_repository); | |
893 | o.verbosity = 0; | |
894 | work = write_in_core_index_as_tree(the_repository); | |
895 | ||
896 | ret = reset_tree(new_tree, | |
897 | opts, 1, | |
898 | writeout_error, new_branch_info); | |
899 | if (ret) | |
900 | return ret; | |
901 | o.ancestor = old_branch_info->name; | |
902 | if (!old_branch_info->name) { | |
903 | strbuf_add_unique_abbrev(&old_commit_shortname, | |
904 | &old_branch_info->commit->object.oid, | |
905 | DEFAULT_ABBREV); | |
906 | o.ancestor = old_commit_shortname.buf; | |
907 | } | |
908 | o.branch1 = new_branch_info->name; | |
909 | o.branch2 = "local"; | |
910 | o.conflict_style = opts->conflict_style; | |
911 | ret = merge_ort_nonrecursive(&o, | |
912 | new_tree, | |
913 | work, | |
914 | old_tree); | |
915 | if (ret < 0) | |
916 | die(NULL); | |
917 | ret = reset_tree(new_tree, | |
918 | opts, 0, | |
919 | writeout_error, new_branch_info); | |
920 | strbuf_release(&o.obuf); | |
921 | strbuf_release(&old_commit_shortname); | |
922 | if (ret) | |
923 | return ret; | |
924 | } | |
925 | } | |
926 | ||
927 | if (!cache_tree_fully_valid(the_repository->index->cache_tree)) | |
928 | cache_tree_update(the_repository->index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); | |
929 | ||
930 | if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK)) | |
931 | die(_("unable to write new index file")); | |
932 | ||
933 | if (!opts->discard_changes && !opts->quiet && new_branch_info->commit) | |
934 | show_local_changes(&new_branch_info->commit->object, &opts->diff_options); | |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
939 | static void report_tracking(struct branch_info *new_branch_info) | |
940 | { | |
941 | struct strbuf sb = STRBUF_INIT; | |
942 | struct branch *branch = branch_get(new_branch_info->name); | |
943 | ||
944 | if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL, 1)) | |
945 | return; | |
946 | fputs(sb.buf, stdout); | |
947 | strbuf_release(&sb); | |
948 | } | |
949 | ||
950 | static void update_refs_for_switch(const struct checkout_opts *opts, | |
951 | struct branch_info *old_branch_info, | |
952 | struct branch_info *new_branch_info) | |
953 | { | |
954 | struct strbuf msg = STRBUF_INIT; | |
955 | const char *old_desc, *reflog_msg; | |
956 | if (opts->new_branch) { | |
957 | if (opts->new_orphan_branch) { | |
958 | enum log_refs_config log_all_ref_updates = | |
959 | repo_settings_get_log_all_ref_updates(the_repository); | |
960 | char *refname; | |
961 | ||
962 | refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch); | |
963 | if (opts->new_branch_log && | |
964 | !should_autocreate_reflog(log_all_ref_updates, refname)) { | |
965 | int ret; | |
966 | struct strbuf err = STRBUF_INIT; | |
967 | ||
968 | ret = refs_create_reflog(get_main_ref_store(the_repository), | |
969 | refname, &err); | |
970 | if (ret) { | |
971 | fprintf(stderr, _("Can not do reflog for '%s': %s\n"), | |
972 | opts->new_orphan_branch, err.buf); | |
973 | strbuf_release(&err); | |
974 | free(refname); | |
975 | return; | |
976 | } | |
977 | strbuf_release(&err); | |
978 | } | |
979 | free(refname); | |
980 | } | |
981 | else | |
982 | create_branch(the_repository, | |
983 | opts->new_branch, new_branch_info->name, | |
984 | opts->new_branch_force ? 1 : 0, | |
985 | opts->new_branch_force ? 1 : 0, | |
986 | opts->new_branch_log, | |
987 | opts->quiet, | |
988 | opts->track, | |
989 | 0); | |
990 | free(new_branch_info->name); | |
991 | free(new_branch_info->refname); | |
992 | new_branch_info->name = xstrdup(opts->new_branch); | |
993 | setup_branch_path(new_branch_info); | |
994 | } | |
995 | ||
996 | old_desc = old_branch_info->name; | |
997 | if (!old_desc && old_branch_info->commit) | |
998 | old_desc = oid_to_hex(&old_branch_info->commit->object.oid); | |
999 | ||
1000 | reflog_msg = getenv("GIT_REFLOG_ACTION"); | |
1001 | if (!reflog_msg) | |
1002 | strbuf_addf(&msg, "checkout: moving from %s to %s", | |
1003 | old_desc ? old_desc : "(invalid)", new_branch_info->name); | |
1004 | else | |
1005 | strbuf_insertstr(&msg, 0, reflog_msg); | |
1006 | ||
1007 | if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) { | |
1008 | /* Nothing to do. */ | |
1009 | } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */ | |
1010 | refs_update_ref(get_main_ref_store(the_repository), msg.buf, | |
1011 | "HEAD", &new_branch_info->commit->object.oid, | |
1012 | NULL, | |
1013 | REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); | |
1014 | if (!opts->quiet) { | |
1015 | if (old_branch_info->path && | |
1016 | advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach) | |
1017 | detach_advice(new_branch_info->name); | |
1018 | describe_detached_head(_("HEAD is now at"), new_branch_info->commit); | |
1019 | } | |
1020 | } else if (new_branch_info->path) { /* Switch branches. */ | |
1021 | if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", new_branch_info->path, msg.buf) < 0) | |
1022 | die(_("unable to update HEAD")); | |
1023 | if (!opts->quiet) { | |
1024 | if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { | |
1025 | if (opts->new_branch_force) | |
1026 | fprintf(stderr, _("Reset branch '%s'\n"), | |
1027 | new_branch_info->name); | |
1028 | else | |
1029 | fprintf(stderr, _("Already on '%s'\n"), | |
1030 | new_branch_info->name); | |
1031 | } else if (opts->new_branch) { | |
1032 | if (opts->branch_exists) | |
1033 | fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name); | |
1034 | else | |
1035 | fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name); | |
1036 | } else { | |
1037 | fprintf(stderr, _("Switched to branch '%s'\n"), | |
1038 | new_branch_info->name); | |
1039 | } | |
1040 | } | |
1041 | if (old_branch_info->path && old_branch_info->name) { | |
1042 | if (!refs_ref_exists(get_main_ref_store(the_repository), old_branch_info->path) && refs_reflog_exists(get_main_ref_store(the_repository), old_branch_info->path)) | |
1043 | refs_delete_reflog(get_main_ref_store(the_repository), | |
1044 | old_branch_info->path); | |
1045 | } | |
1046 | } | |
1047 | remove_branch_state(the_repository, !opts->quiet); | |
1048 | strbuf_release(&msg); | |
1049 | if (!opts->quiet && | |
1050 | !opts->force_detach && | |
1051 | (new_branch_info->path || !strcmp(new_branch_info->name, "HEAD"))) | |
1052 | report_tracking(new_branch_info); | |
1053 | } | |
1054 | ||
1055 | static int add_pending_uninteresting_ref(const char *refname, const char *referent UNUSED, | |
1056 | const struct object_id *oid, | |
1057 | int flags UNUSED, void *cb_data) | |
1058 | { | |
1059 | add_pending_oid(cb_data, refname, oid, UNINTERESTING); | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static void describe_one_orphan(struct strbuf *sb, struct commit *commit) | |
1064 | { | |
1065 | strbuf_addstr(sb, " "); | |
1066 | strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); | |
1067 | strbuf_addch(sb, ' '); | |
1068 | if (!repo_parse_commit(the_repository, commit)) | |
1069 | pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); | |
1070 | strbuf_addch(sb, '\n'); | |
1071 | } | |
1072 | ||
1073 | #define ORPHAN_CUTOFF 4 | |
1074 | static void suggest_reattach(struct commit *commit, struct rev_info *revs) | |
1075 | { | |
1076 | struct commit *c, *last = NULL; | |
1077 | struct strbuf sb = STRBUF_INIT; | |
1078 | int lost = 0; | |
1079 | while ((c = get_revision(revs)) != NULL) { | |
1080 | if (lost < ORPHAN_CUTOFF) | |
1081 | describe_one_orphan(&sb, c); | |
1082 | last = c; | |
1083 | lost++; | |
1084 | } | |
1085 | if (ORPHAN_CUTOFF < lost) { | |
1086 | int more = lost - ORPHAN_CUTOFF; | |
1087 | if (more == 1) | |
1088 | describe_one_orphan(&sb, last); | |
1089 | else | |
1090 | strbuf_addf(&sb, _(" ... and %d more.\n"), more); | |
1091 | } | |
1092 | ||
1093 | fprintf(stderr, | |
1094 | Q_( | |
1095 | /* The singular version */ | |
1096 | "Warning: you are leaving %d commit behind, " | |
1097 | "not connected to\n" | |
1098 | "any of your branches:\n\n" | |
1099 | "%s\n", | |
1100 | /* The plural version */ | |
1101 | "Warning: you are leaving %d commits behind, " | |
1102 | "not connected to\n" | |
1103 | "any of your branches:\n\n" | |
1104 | "%s\n", | |
1105 | /* Give ngettext() the count */ | |
1106 | lost), | |
1107 | lost, | |
1108 | sb.buf); | |
1109 | strbuf_release(&sb); | |
1110 | ||
1111 | if (advice_enabled(ADVICE_DETACHED_HEAD)) | |
1112 | fprintf(stderr, | |
1113 | Q_( | |
1114 | /* The singular version */ | |
1115 | "If you want to keep it by creating a new branch, " | |
1116 | "this may be a good time\nto do so with:\n\n" | |
1117 | " git branch <new-branch-name> %s\n\n", | |
1118 | /* The plural version */ | |
1119 | "If you want to keep them by creating a new branch, " | |
1120 | "this may be a good time\nto do so with:\n\n" | |
1121 | " git branch <new-branch-name> %s\n\n", | |
1122 | /* Give ngettext() the count */ | |
1123 | lost), | |
1124 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * We are about to leave commit that was at the tip of a detached | |
1129 | * HEAD. If it is not reachable from any ref, this is the last chance | |
1130 | * for the user to do so without resorting to reflog. | |
1131 | */ | |
1132 | static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit) | |
1133 | { | |
1134 | struct rev_info revs; | |
1135 | struct object *object = &old_commit->object; | |
1136 | ||
1137 | repo_init_revisions(the_repository, &revs, NULL); | |
1138 | setup_revisions(0, NULL, &revs, NULL); | |
1139 | ||
1140 | object->flags &= ~UNINTERESTING; | |
1141 | add_pending_object(&revs, object, oid_to_hex(&object->oid)); | |
1142 | ||
1143 | refs_for_each_ref(get_main_ref_store(the_repository), | |
1144 | add_pending_uninteresting_ref, &revs); | |
1145 | if (new_commit) | |
1146 | add_pending_oid(&revs, "HEAD", | |
1147 | &new_commit->object.oid, | |
1148 | UNINTERESTING); | |
1149 | ||
1150 | if (prepare_revision_walk(&revs)) | |
1151 | die(_("internal error in revision walk")); | |
1152 | if (!(old_commit->object.flags & UNINTERESTING)) | |
1153 | suggest_reattach(old_commit, &revs); | |
1154 | else | |
1155 | describe_detached_head(_("Previous HEAD position was"), old_commit); | |
1156 | ||
1157 | /* Clean up objects used, as they will be reused. */ | |
1158 | repo_clear_commit_marks(the_repository, ALL_REV_FLAGS); | |
1159 | release_revisions(&revs); | |
1160 | } | |
1161 | ||
1162 | static int switch_branches(const struct checkout_opts *opts, | |
1163 | struct branch_info *new_branch_info) | |
1164 | { | |
1165 | int ret = 0; | |
1166 | struct branch_info old_branch_info = { 0 }; | |
1167 | struct object_id rev; | |
1168 | int flag, writeout_error = 0; | |
1169 | int do_merge = 1; | |
1170 | ||
1171 | trace2_cmd_mode("branch"); | |
1172 | ||
1173 | memset(&old_branch_info, 0, sizeof(old_branch_info)); | |
1174 | old_branch_info.path = refs_resolve_refdup(get_main_ref_store(the_repository), | |
1175 | "HEAD", 0, &rev, &flag); | |
1176 | if (old_branch_info.path) | |
1177 | old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1); | |
1178 | if (!(flag & REF_ISSYMREF)) | |
1179 | FREE_AND_NULL(old_branch_info.path); | |
1180 | ||
1181 | if (old_branch_info.path) { | |
1182 | const char *const prefix = "refs/heads/"; | |
1183 | const char *p; | |
1184 | if (skip_prefix(old_branch_info.path, prefix, &p)) | |
1185 | old_branch_info.name = xstrdup(p); | |
1186 | } | |
1187 | ||
1188 | if (opts->new_orphan_branch && opts->orphan_from_empty_tree) { | |
1189 | if (new_branch_info->name) | |
1190 | BUG("'switch --orphan' should never accept a commit as starting point"); | |
1191 | new_branch_info->commit = NULL; | |
1192 | new_branch_info->name = xstrdup("(empty)"); | |
1193 | do_merge = 1; | |
1194 | } | |
1195 | ||
1196 | if (!new_branch_info->name) { | |
1197 | new_branch_info->name = xstrdup("HEAD"); | |
1198 | new_branch_info->commit = old_branch_info.commit; | |
1199 | if (!new_branch_info->commit) | |
1200 | die(_("You are on a branch yet to be born")); | |
1201 | parse_commit_or_die(new_branch_info->commit); | |
1202 | ||
1203 | if (opts->only_merge_on_switching_branches) | |
1204 | do_merge = 0; | |
1205 | } | |
1206 | ||
1207 | if (do_merge) { | |
1208 | ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); | |
1209 | if (ret) { | |
1210 | branch_info_release(&old_branch_info); | |
1211 | return ret; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit) | |
1216 | orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit); | |
1217 | ||
1218 | update_refs_for_switch(opts, &old_branch_info, new_branch_info); | |
1219 | ||
1220 | ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1); | |
1221 | branch_info_release(&old_branch_info); | |
1222 | ||
1223 | return ret || writeout_error; | |
1224 | } | |
1225 | ||
1226 | static int git_checkout_config(const char *var, const char *value, | |
1227 | const struct config_context *ctx, void *cb) | |
1228 | { | |
1229 | struct checkout_opts *opts = cb; | |
1230 | ||
1231 | if (!strcmp(var, "diff.ignoresubmodules")) { | |
1232 | if (!value) | |
1233 | return config_error_nonbool(var); | |
1234 | handle_ignore_submodules_arg(&opts->diff_options, value); | |
1235 | return 0; | |
1236 | } | |
1237 | if (!strcmp(var, "checkout.guess")) { | |
1238 | opts->dwim_new_local_branch = git_config_bool(var, value); | |
1239 | return 0; | |
1240 | } | |
1241 | ||
1242 | if (starts_with(var, "submodule.")) | |
1243 | return git_default_submodule_config(var, value, NULL); | |
1244 | ||
1245 | return git_xmerge_config(var, value, ctx, NULL); | |
1246 | } | |
1247 | ||
1248 | static void setup_new_branch_info_and_source_tree( | |
1249 | struct branch_info *new_branch_info, | |
1250 | struct checkout_opts *opts, | |
1251 | struct object_id *rev, | |
1252 | const char *arg) | |
1253 | { | |
1254 | struct tree **source_tree = &opts->source_tree; | |
1255 | struct object_id branch_rev; | |
1256 | ||
1257 | /* treat '@' as a shortcut for 'HEAD' */ | |
1258 | new_branch_info->name = !strcmp(arg, "@") ? xstrdup("HEAD") : | |
1259 | xstrdup(arg); | |
1260 | setup_branch_path(new_branch_info); | |
1261 | ||
1262 | if (!check_refname_format(new_branch_info->path, 0) && | |
1263 | !refs_read_ref(get_main_ref_store(the_repository), new_branch_info->path, &branch_rev)) | |
1264 | oidcpy(rev, &branch_rev); | |
1265 | else | |
1266 | /* not an existing branch */ | |
1267 | FREE_AND_NULL(new_branch_info->path); | |
1268 | ||
1269 | new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1); | |
1270 | if (!new_branch_info->commit) { | |
1271 | /* not a commit */ | |
1272 | *source_tree = parse_tree_indirect(rev); | |
1273 | if (!*source_tree) | |
1274 | die(_("unable to read tree (%s)"), oid_to_hex(rev)); | |
1275 | } else { | |
1276 | parse_commit_or_die(new_branch_info->commit); | |
1277 | *source_tree = repo_get_commit_tree(the_repository, | |
1278 | new_branch_info->commit); | |
1279 | if (!*source_tree) | |
1280 | die(_("unable to read tree (%s)"), | |
1281 | oid_to_hex(&new_branch_info->commit->object.oid)); | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | static char *parse_remote_branch(const char *arg, | |
1286 | struct object_id *rev, | |
1287 | int could_be_checkout_paths) | |
1288 | { | |
1289 | int num_matches = 0; | |
1290 | char *remote = unique_tracking_name(arg, rev, &num_matches); | |
1291 | ||
1292 | if (remote && could_be_checkout_paths) { | |
1293 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
1294 | "Please use -- (and optionally --no-guess) to disambiguate"), | |
1295 | arg); | |
1296 | } | |
1297 | ||
1298 | if (!remote && num_matches > 1) { | |
1299 | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
1300 | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
1301 | "you can do so by fully qualifying the name with the --track option:\n" | |
1302 | "\n" | |
1303 | " git checkout --track origin/<name>\n" | |
1304 | "\n" | |
1305 | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
1306 | "one remote, e.g. the 'origin' remote, consider setting\n" | |
1307 | "checkout.defaultRemote=origin in your config.")); | |
1308 | } | |
1309 | ||
1310 | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
1311 | arg, num_matches); | |
1312 | } | |
1313 | ||
1314 | return remote; | |
1315 | } | |
1316 | ||
1317 | static int parse_branchname_arg(int argc, const char **argv, | |
1318 | int dwim_new_local_branch_ok, | |
1319 | struct branch_info *new_branch_info, | |
1320 | struct checkout_opts *opts, | |
1321 | struct object_id *rev) | |
1322 | { | |
1323 | const char **new_branch = &opts->new_branch; | |
1324 | int argcount = 0; | |
1325 | const char *arg; | |
1326 | char *remote = NULL; | |
1327 | int dash_dash_pos; | |
1328 | int has_dash_dash = 0; | |
1329 | int i; | |
1330 | ||
1331 | /* | |
1332 | * case 1: git checkout <ref> -- [<paths>] | |
1333 | * | |
1334 | * <ref> must be a valid tree, everything after the '--' must be | |
1335 | * a path. | |
1336 | * | |
1337 | * case 2: git checkout -- [<paths>] | |
1338 | * | |
1339 | * everything after the '--' must be paths. | |
1340 | * | |
1341 | * case 3: git checkout <something> [--] | |
1342 | * | |
1343 | * (a) If <something> is a commit, that is to | |
1344 | * switch to the branch or detach HEAD at it. As a special case, | |
1345 | * if <something> is A...B (missing A or B means HEAD but you can | |
1346 | * omit at most one side), and if there is a unique merge base | |
1347 | * between A and B, A...B names that merge base. | |
1348 | * | |
1349 | * (b) If <something> is _not_ a commit, either "--" is present | |
1350 | * or <something> is not a path, no -t or -b was given, | |
1351 | * and there is a tracking branch whose name is <something> | |
1352 | * in one and only one remote (or if the branch exists on the | |
1353 | * remote named in checkout.defaultRemote), then this is a | |
1354 | * short-hand to fork local <something> from that | |
1355 | * remote-tracking branch. | |
1356 | * | |
1357 | * (c) Otherwise, if "--" is present, treat it like case (1). | |
1358 | * | |
1359 | * (d) Otherwise : | |
1360 | * - if it's a reference, treat it like case (1) | |
1361 | * - else if it's a path, treat it like case (2) | |
1362 | * - else: fail. | |
1363 | * | |
1364 | * case 4: git checkout <something> <paths> | |
1365 | * | |
1366 | * The first argument must not be ambiguous. | |
1367 | * - If it's *only* a reference, treat it like case (1). | |
1368 | * - If it's only a path, treat it like case (2). | |
1369 | * - else: fail. | |
1370 | * | |
1371 | */ | |
1372 | if (!argc) | |
1373 | return 0; | |
1374 | ||
1375 | if (!opts->accept_pathspec) { | |
1376 | if (argc > 1) | |
1377 | die(_("only one reference expected")); | |
1378 | has_dash_dash = 1; /* helps disambiguate */ | |
1379 | } | |
1380 | ||
1381 | arg = argv[0]; | |
1382 | dash_dash_pos = -1; | |
1383 | for (i = 0; i < argc; i++) { | |
1384 | if (opts->accept_pathspec && !strcmp(argv[i], "--")) { | |
1385 | dash_dash_pos = i; | |
1386 | break; | |
1387 | } | |
1388 | } | |
1389 | if (dash_dash_pos == 0) | |
1390 | return 1; /* case (2) */ | |
1391 | else if (dash_dash_pos == 1) | |
1392 | has_dash_dash = 1; /* case (3) or (1) */ | |
1393 | else if (dash_dash_pos >= 2) | |
1394 | die(_("only one reference expected, %d given."), dash_dash_pos); | |
1395 | opts->count_checkout_paths = !opts->quiet && !has_dash_dash; | |
1396 | ||
1397 | if (!strcmp(arg, "-")) | |
1398 | arg = "@{-1}"; | |
1399 | ||
1400 | if (repo_get_oid_mb(the_repository, arg, rev)) { | |
1401 | /* | |
1402 | * Either case (3) or (4), with <something> not being | |
1403 | * a commit, or an attempt to use case (1) with an | |
1404 | * invalid ref. | |
1405 | * | |
1406 | * It's likely an error, but we need to find out if | |
1407 | * we should auto-create the branch, case (3).(b). | |
1408 | */ | |
1409 | int recover_with_dwim = dwim_new_local_branch_ok; | |
1410 | ||
1411 | int could_be_checkout_paths = !has_dash_dash && | |
1412 | check_filename(opts->prefix, arg); | |
1413 | ||
1414 | if (!has_dash_dash && !no_wildcard(arg)) | |
1415 | recover_with_dwim = 0; | |
1416 | ||
1417 | /* | |
1418 | * Accept "git checkout foo", "git checkout foo --" | |
1419 | * and "git switch foo" as candidates for dwim. | |
1420 | */ | |
1421 | if (!(argc == 1 && !has_dash_dash) && | |
1422 | !(argc == 2 && has_dash_dash) && | |
1423 | opts->accept_pathspec) | |
1424 | recover_with_dwim = 0; | |
1425 | ||
1426 | if (recover_with_dwim) { | |
1427 | remote = parse_remote_branch(arg, rev, | |
1428 | could_be_checkout_paths); | |
1429 | if (remote) { | |
1430 | *new_branch = arg; | |
1431 | arg = remote; | |
1432 | /* DWIMmed to create local branch, case (3).(b) */ | |
1433 | } else { | |
1434 | recover_with_dwim = 0; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | if (!recover_with_dwim) { | |
1439 | if (has_dash_dash) | |
1440 | die(_("invalid reference: %s"), arg); | |
1441 | return argcount; | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | /* we can't end up being in (2) anymore, eat the argument */ | |
1446 | argcount++; | |
1447 | argv++; | |
1448 | argc--; | |
1449 | ||
1450 | setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg); | |
1451 | ||
1452 | if (!opts->source_tree) /* case (1): want a tree */ | |
1453 | die(_("reference is not a tree: %s"), arg); | |
1454 | ||
1455 | if (!has_dash_dash) { /* case (3).(d) -> (1) */ | |
1456 | /* | |
1457 | * Do not complain the most common case | |
1458 | * git checkout branch | |
1459 | * even if there happen to be a file called 'branch'; | |
1460 | * it would be extremely annoying. | |
1461 | */ | |
1462 | if (argc) | |
1463 | verify_non_filename(opts->prefix, arg); | |
1464 | } else if (opts->accept_pathspec) { | |
1465 | argcount++; | |
1466 | argv++; | |
1467 | argc--; | |
1468 | } | |
1469 | ||
1470 | free(remote); | |
1471 | return argcount; | |
1472 | } | |
1473 | ||
1474 | static int switch_unborn_to_new_branch(const struct checkout_opts *opts) | |
1475 | { | |
1476 | int status; | |
1477 | struct strbuf branch_ref = STRBUF_INIT; | |
1478 | ||
1479 | trace2_cmd_mode("unborn"); | |
1480 | ||
1481 | if (!opts->new_branch) | |
1482 | die(_("You are on a branch yet to be born")); | |
1483 | strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); | |
1484 | status = refs_update_symref(get_main_ref_store(the_repository), | |
1485 | "HEAD", branch_ref.buf, "checkout -b"); | |
1486 | strbuf_release(&branch_ref); | |
1487 | if (!opts->quiet) | |
1488 | fprintf(stderr, _("Switched to a new branch '%s'\n"), | |
1489 | opts->new_branch); | |
1490 | return status; | |
1491 | } | |
1492 | ||
1493 | static void die_expecting_a_branch(const struct branch_info *branch_info) | |
1494 | { | |
1495 | struct object_id oid; | |
1496 | char *to_free; | |
1497 | int code; | |
1498 | ||
1499 | if (repo_dwim_ref(the_repository, branch_info->name, | |
1500 | strlen(branch_info->name), &oid, &to_free, 0) == 1) { | |
1501 | const char *ref = to_free; | |
1502 | ||
1503 | if (skip_prefix(ref, "refs/tags/", &ref)) | |
1504 | code = die_message(_("a branch is expected, got tag '%s'"), ref); | |
1505 | else if (skip_prefix(ref, "refs/remotes/", &ref)) | |
1506 | code = die_message(_("a branch is expected, got remote branch '%s'"), ref); | |
1507 | else | |
1508 | code = die_message(_("a branch is expected, got '%s'"), ref); | |
1509 | } | |
1510 | else if (branch_info->commit) | |
1511 | code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name); | |
1512 | else | |
1513 | /* | |
1514 | * This case should never happen because we already die() on | |
1515 | * non-commit, but just in case. | |
1516 | */ | |
1517 | code = die_message(_("a branch is expected, got '%s'"), branch_info->name); | |
1518 | ||
1519 | if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD)) | |
1520 | advise(_("If you want to detach HEAD at the commit, try again with the --detach option.")); | |
1521 | ||
1522 | exit(code); | |
1523 | } | |
1524 | ||
1525 | static void die_if_some_operation_in_progress(void) | |
1526 | { | |
1527 | struct wt_status_state state; | |
1528 | ||
1529 | memset(&state, 0, sizeof(state)); | |
1530 | wt_status_get_state(the_repository, &state, 0); | |
1531 | ||
1532 | if (state.merge_in_progress) | |
1533 | die(_("cannot switch branch while merging\n" | |
1534 | "Consider \"git merge --quit\" " | |
1535 | "or \"git worktree add\".")); | |
1536 | if (state.am_in_progress) | |
1537 | die(_("cannot switch branch in the middle of an am session\n" | |
1538 | "Consider \"git am --quit\" " | |
1539 | "or \"git worktree add\".")); | |
1540 | if (state.rebase_interactive_in_progress || state.rebase_in_progress) | |
1541 | die(_("cannot switch branch while rebasing\n" | |
1542 | "Consider \"git rebase --quit\" " | |
1543 | "or \"git worktree add\".")); | |
1544 | if (state.cherry_pick_in_progress) | |
1545 | die(_("cannot switch branch while cherry-picking\n" | |
1546 | "Consider \"git cherry-pick --quit\" " | |
1547 | "or \"git worktree add\".")); | |
1548 | if (state.revert_in_progress) | |
1549 | die(_("cannot switch branch while reverting\n" | |
1550 | "Consider \"git revert --quit\" " | |
1551 | "or \"git worktree add\".")); | |
1552 | if (state.bisect_in_progress) | |
1553 | warning(_("you are switching branch while bisecting")); | |
1554 | ||
1555 | wt_status_state_free_buffers(&state); | |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * die if attempting to checkout an existing branch that is in use | |
1560 | * in another worktree, unless ignore-other-wortrees option is given. | |
1561 | * The check is bypassed when the branch is already the current one, | |
1562 | * as it will not make things any worse. | |
1563 | */ | |
1564 | static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts, | |
1565 | const char *full_ref) | |
1566 | { | |
1567 | int flags; | |
1568 | char *head_ref; | |
1569 | ||
1570 | if (opts->ignore_other_worktrees) | |
1571 | return; | |
1572 | head_ref = refs_resolve_refdup(get_main_ref_store(the_repository), | |
1573 | "HEAD", 0, NULL, &flags); | |
1574 | if (head_ref && (!(flags & REF_ISSYMREF) || strcmp(head_ref, full_ref))) | |
1575 | die_if_checked_out(full_ref, 1); | |
1576 | free(head_ref); | |
1577 | } | |
1578 | ||
1579 | static int checkout_branch(struct checkout_opts *opts, | |
1580 | struct branch_info *new_branch_info) | |
1581 | { | |
1582 | int noop_switch = (!new_branch_info->name && | |
1583 | !opts->new_branch && | |
1584 | !opts->force_detach); | |
1585 | ||
1586 | if (opts->pathspec.nr) | |
1587 | die(_("paths cannot be used with switching branches")); | |
1588 | ||
1589 | if (opts->patch_mode) | |
1590 | die(_("'%s' cannot be used with switching branches"), | |
1591 | "--patch"); | |
1592 | ||
1593 | if (opts->overlay_mode != -1) | |
1594 | die(_("'%s' cannot be used with switching branches"), | |
1595 | "--[no]-overlay"); | |
1596 | ||
1597 | if (opts->writeout_stage) { | |
1598 | const char *msg; | |
1599 | if (noop_switch) | |
1600 | msg = _("'%s' needs the paths to check out"); | |
1601 | else | |
1602 | msg = _("'%s' cannot be used with switching branches"); | |
1603 | die(msg, "--ours/--theirs"); | |
1604 | } | |
1605 | ||
1606 | if (opts->force && opts->merge) | |
1607 | die(_("'%s' cannot be used with '%s'"), "-f", "-m"); | |
1608 | ||
1609 | if (opts->discard_changes && opts->merge) | |
1610 | die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge"); | |
1611 | ||
1612 | if (opts->force_detach && opts->new_branch) | |
1613 | die(_("'%s' cannot be used with '%s'"), | |
1614 | "--detach", "-b/-B/--orphan"); | |
1615 | ||
1616 | if (opts->new_orphan_branch) { | |
1617 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1618 | die(_("'%s' cannot be used with '%s'"), "--orphan", "-t"); | |
1619 | if (opts->orphan_from_empty_tree && new_branch_info->name) | |
1620 | die(_("'%s' cannot take <start-point>"), "--orphan"); | |
1621 | } else if (opts->force_detach) { | |
1622 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1623 | die(_("'%s' cannot be used with '%s'"), "--detach", "-t"); | |
1624 | } else if (opts->track == BRANCH_TRACK_UNSPECIFIED) | |
1625 | opts->track = git_branch_track; | |
1626 | ||
1627 | if (new_branch_info->name && !new_branch_info->commit) | |
1628 | die(_("Cannot switch branch to a non-commit '%s'"), | |
1629 | new_branch_info->name); | |
1630 | ||
1631 | if (noop_switch && | |
1632 | !opts->switch_branch_doing_nothing_is_ok) | |
1633 | die(_("missing branch or commit argument")); | |
1634 | ||
1635 | if (!opts->implicit_detach && | |
1636 | !opts->force_detach && | |
1637 | !opts->new_branch && | |
1638 | !opts->new_branch_force && | |
1639 | new_branch_info->name && | |
1640 | !new_branch_info->path) | |
1641 | die_expecting_a_branch(new_branch_info); | |
1642 | ||
1643 | if (!opts->can_switch_when_in_progress) | |
1644 | die_if_some_operation_in_progress(); | |
1645 | ||
1646 | /* "git checkout <branch>" */ | |
1647 | if (new_branch_info->path && !opts->force_detach && !opts->new_branch) | |
1648 | die_if_switching_to_a_branch_in_use(opts, new_branch_info->path); | |
1649 | ||
1650 | /* "git checkout -B <branch>" */ | |
1651 | if (opts->new_branch_force) { | |
1652 | char *full_ref = xstrfmt("refs/heads/%s", opts->new_branch); | |
1653 | die_if_switching_to_a_branch_in_use(opts, full_ref); | |
1654 | free(full_ref); | |
1655 | } | |
1656 | ||
1657 | if (!new_branch_info->commit && opts->new_branch) { | |
1658 | struct object_id rev; | |
1659 | int flag; | |
1660 | ||
1661 | if (!refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &rev, &flag) && | |
1662 | (flag & REF_ISSYMREF) && is_null_oid(&rev)) | |
1663 | return switch_unborn_to_new_branch(opts); | |
1664 | } | |
1665 | return switch_branches(opts, new_branch_info); | |
1666 | } | |
1667 | ||
1668 | static int parse_opt_conflict(const struct option *o, const char *arg, int unset) | |
1669 | { | |
1670 | struct checkout_opts *opts = o->value; | |
1671 | ||
1672 | if (unset) { | |
1673 | opts->conflict_style = -1; | |
1674 | return 0; | |
1675 | } | |
1676 | opts->conflict_style = parse_conflict_style_name(arg); | |
1677 | if (opts->conflict_style < 0) | |
1678 | return error(_("unknown conflict style '%s'"), arg); | |
1679 | /* --conflict overrides a previous --no-merge */ | |
1680 | if (!opts->merge) | |
1681 | opts->merge = -1; | |
1682 | ||
1683 | return 0; | |
1684 | } | |
1685 | ||
1686 | static struct option *add_common_options(struct checkout_opts *opts, | |
1687 | struct option *prevopts) | |
1688 | { | |
1689 | struct option options[] = { | |
1690 | OPT__QUIET(&opts->quiet, N_("suppress progress reporting")), | |
1691 | OPT_CALLBACK_F(0, "recurse-submodules", NULL, | |
1692 | "checkout", "control recursive updating of submodules", | |
1693 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater), | |
1694 | OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")), | |
1695 | OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")), | |
1696 | OPT_CALLBACK(0, "conflict", opts, N_("style"), | |
1697 | N_("conflict style (merge, diff3, or zdiff3)"), | |
1698 | parse_opt_conflict), | |
1699 | OPT_END() | |
1700 | }; | |
1701 | struct option *newopts = parse_options_concat(prevopts, options); | |
1702 | free(prevopts); | |
1703 | return newopts; | |
1704 | } | |
1705 | ||
1706 | static struct option *add_common_switch_branch_options( | |
1707 | struct checkout_opts *opts, struct option *prevopts) | |
1708 | { | |
1709 | struct option options[] = { | |
1710 | OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), | |
1711 | OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", | |
1712 | N_("set branch tracking configuration"), | |
1713 | PARSE_OPT_OPTARG, | |
1714 | parse_opt_tracking_mode), | |
1715 | OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), | |
1716 | PARSE_OPT_NOCOMPLETE), | |
1717 | OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), | |
1718 | OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore, | |
1719 | N_("update ignored files (default)"), | |
1720 | PARSE_OPT_NOCOMPLETE), | |
1721 | OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees, | |
1722 | N_("do not check if another worktree is using this branch")), | |
1723 | OPT_END() | |
1724 | }; | |
1725 | struct option *newopts = parse_options_concat(prevopts, options); | |
1726 | free(prevopts); | |
1727 | return newopts; | |
1728 | } | |
1729 | ||
1730 | static struct option *add_checkout_path_options(struct checkout_opts *opts, | |
1731 | struct option *prevopts) | |
1732 | { | |
1733 | struct option options[] = { | |
1734 | OPT_SET_INT_F('2', "ours", &opts->writeout_stage, | |
1735 | N_("checkout our version for unmerged files"), | |
1736 | 2, PARSE_OPT_NONEG), | |
1737 | OPT_SET_INT_F('3', "theirs", &opts->writeout_stage, | |
1738 | N_("checkout their version for unmerged files"), | |
1739 | 3, PARSE_OPT_NONEG), | |
1740 | OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")), | |
1741 | OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree, | |
1742 | N_("do not limit pathspecs to sparse entries only")), | |
1743 | OPT_PATHSPEC_FROM_FILE(&opts->pathspec_from_file), | |
1744 | OPT_PATHSPEC_FILE_NUL(&opts->pathspec_file_nul), | |
1745 | OPT_END() | |
1746 | }; | |
1747 | struct option *newopts = parse_options_concat(prevopts, options); | |
1748 | free(prevopts); | |
1749 | return newopts; | |
1750 | } | |
1751 | ||
1752 | /* create-branch option (either b or c) */ | |
1753 | static char cb_option = 'b'; | |
1754 | ||
1755 | static int checkout_main(int argc, const char **argv, const char *prefix, | |
1756 | struct checkout_opts *opts, struct option *options, | |
1757 | const char * const usagestr[]) | |
1758 | { | |
1759 | int parseopt_flags = 0; | |
1760 | struct branch_info new_branch_info = { 0 }; | |
1761 | int ret; | |
1762 | ||
1763 | opts->overwrite_ignore = 1; | |
1764 | opts->prefix = prefix; | |
1765 | opts->show_progress = -1; | |
1766 | ||
1767 | git_config(git_checkout_config, opts); | |
1768 | if (the_repository->gitdir) { | |
1769 | prepare_repo_settings(the_repository); | |
1770 | the_repository->settings.command_requires_full_index = 0; | |
1771 | } | |
1772 | ||
1773 | opts->track = BRANCH_TRACK_UNSPECIFIED; | |
1774 | ||
1775 | if (!opts->accept_pathspec && !opts->accept_ref) | |
1776 | BUG("make up your mind, you need to take _something_"); | |
1777 | if (opts->accept_pathspec && opts->accept_ref) | |
1778 | parseopt_flags = PARSE_OPT_KEEP_DASHDASH; | |
1779 | ||
1780 | argc = parse_options(argc, argv, prefix, options, | |
1781 | usagestr, parseopt_flags); | |
1782 | ||
1783 | if (opts->show_progress < 0) { | |
1784 | if (opts->quiet) | |
1785 | opts->show_progress = 0; | |
1786 | else | |
1787 | opts->show_progress = isatty(2); | |
1788 | } | |
1789 | ||
1790 | /* --conflicts implies --merge */ | |
1791 | if (opts->merge == -1) | |
1792 | opts->merge = opts->conflict_style >= 0; | |
1793 | ||
1794 | if (opts->force) { | |
1795 | opts->discard_changes = 1; | |
1796 | opts->ignore_unmerged_opt = "--force"; | |
1797 | opts->ignore_unmerged = 1; | |
1798 | } | |
1799 | ||
1800 | if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1) | |
1801 | die(_("options '-%c', '-%c', and '%s' cannot be used together"), | |
1802 | cb_option, toupper(cb_option), "--orphan"); | |
1803 | ||
1804 | if (opts->overlay_mode == 1 && opts->patch_mode) | |
1805 | die(_("options '%s' and '%s' cannot be used together"), "-p", "--overlay"); | |
1806 | ||
1807 | if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) { | |
1808 | if (opts->checkout_index < 0) | |
1809 | opts->checkout_index = 0; | |
1810 | if (opts->checkout_worktree < 0) | |
1811 | opts->checkout_worktree = 0; | |
1812 | } else { | |
1813 | if (opts->checkout_index < 0) | |
1814 | opts->checkout_index = -opts->checkout_index - 1; | |
1815 | if (opts->checkout_worktree < 0) | |
1816 | opts->checkout_worktree = -opts->checkout_worktree - 1; | |
1817 | } | |
1818 | if (opts->checkout_index < 0 || opts->checkout_worktree < 0) | |
1819 | BUG("these flags should be non-negative by now"); | |
1820 | /* | |
1821 | * convenient shortcut: "git restore --staged [--worktree]" equals | |
1822 | * "git restore --staged [--worktree] --source HEAD" | |
1823 | */ | |
1824 | if (!opts->from_treeish && opts->checkout_index) | |
1825 | opts->from_treeish = "HEAD"; | |
1826 | ||
1827 | /* | |
1828 | * From here on, new_branch will contain the branch to be checked out, | |
1829 | * and new_branch_force and new_orphan_branch will tell us which one of | |
1830 | * -b/-B/-c/-C/--orphan is being used. | |
1831 | */ | |
1832 | if (opts->new_branch_force) | |
1833 | opts->new_branch = opts->new_branch_force; | |
1834 | ||
1835 | if (opts->new_orphan_branch) | |
1836 | opts->new_branch = opts->new_orphan_branch; | |
1837 | ||
1838 | /* --track without -c/-C/-b/-B/--orphan should DWIM */ | |
1839 | if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { | |
1840 | const char *argv0 = argv[0]; | |
1841 | if (!argc || !strcmp(argv0, "--")) | |
1842 | die(_("--track needs a branch name")); | |
1843 | skip_prefix(argv0, "refs/", &argv0); | |
1844 | skip_prefix(argv0, "remotes/", &argv0); | |
1845 | argv0 = strchr(argv0, '/'); | |
1846 | if (!argv0 || !argv0[1]) | |
1847 | die(_("missing branch name; try -%c"), cb_option); | |
1848 | opts->new_branch = argv0 + 1; | |
1849 | } | |
1850 | ||
1851 | /* | |
1852 | * Extract branch name from command line arguments, so | |
1853 | * all that is left is pathspecs. | |
1854 | * | |
1855 | * Handle | |
1856 | * | |
1857 | * 1) git checkout <tree> -- [<paths>] | |
1858 | * 2) git checkout -- [<paths>] | |
1859 | * 3) git checkout <something> [<paths>] | |
1860 | * | |
1861 | * including "last branch" syntax and DWIM-ery for names of | |
1862 | * remote branches, erroring out for invalid or ambiguous cases. | |
1863 | */ | |
1864 | if (argc && opts->accept_ref) { | |
1865 | struct object_id rev; | |
1866 | int dwim_ok = | |
1867 | !opts->patch_mode && | |
1868 | opts->dwim_new_local_branch && | |
1869 | opts->track == BRANCH_TRACK_UNSPECIFIED && | |
1870 | !opts->new_branch; | |
1871 | int n = parse_branchname_arg(argc, argv, dwim_ok, | |
1872 | &new_branch_info, opts, &rev); | |
1873 | argv += n; | |
1874 | argc -= n; | |
1875 | } else if (!opts->accept_ref && opts->from_treeish) { | |
1876 | struct object_id rev; | |
1877 | ||
1878 | if (repo_get_oid_mb(the_repository, opts->from_treeish, &rev)) | |
1879 | die(_("could not resolve %s"), opts->from_treeish); | |
1880 | ||
1881 | setup_new_branch_info_and_source_tree(&new_branch_info, | |
1882 | opts, &rev, | |
1883 | opts->from_treeish); | |
1884 | ||
1885 | if (!opts->source_tree) | |
1886 | die(_("reference is not a tree: %s"), opts->from_treeish); | |
1887 | } | |
1888 | ||
1889 | if (argc) { | |
1890 | parse_pathspec(&opts->pathspec, 0, | |
1891 | opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0, | |
1892 | prefix, argv); | |
1893 | ||
1894 | if (!opts->pathspec.nr) | |
1895 | die(_("invalid path specification")); | |
1896 | ||
1897 | /* | |
1898 | * Try to give more helpful suggestion. | |
1899 | * new_branch && argc > 1 will be caught later. | |
1900 | */ | |
1901 | if (opts->new_branch && argc == 1 && !new_branch_info.commit) | |
1902 | die(_("'%s' is not a commit and a branch '%s' cannot be created from it"), | |
1903 | argv[0], opts->new_branch); | |
1904 | ||
1905 | if (opts->force_detach) | |
1906 | die(_("git checkout: --detach does not take a path argument '%s'"), | |
1907 | argv[0]); | |
1908 | } | |
1909 | ||
1910 | if (opts->pathspec_from_file) { | |
1911 | if (opts->pathspec.nr) | |
1912 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); | |
1913 | ||
1914 | if (opts->force_detach) | |
1915 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--detach"); | |
1916 | ||
1917 | if (opts->patch_mode) | |
1918 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); | |
1919 | ||
1920 | parse_pathspec_file(&opts->pathspec, 0, | |
1921 | 0, | |
1922 | prefix, opts->pathspec_from_file, opts->pathspec_file_nul); | |
1923 | } else if (opts->pathspec_file_nul) { | |
1924 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); | |
1925 | } | |
1926 | ||
1927 | opts->pathspec.recursive = 1; | |
1928 | ||
1929 | if (opts->pathspec.nr) { | |
1930 | if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) | |
1931 | die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" | |
1932 | "checking out of the index.")); | |
1933 | } else { | |
1934 | if (opts->accept_pathspec && !opts->empty_pathspec_ok && | |
1935 | !opts->patch_mode) /* patch mode is special */ | |
1936 | die(_("you must specify path(s) to restore")); | |
1937 | } | |
1938 | ||
1939 | if (opts->new_branch) { | |
1940 | struct strbuf buf = STRBUF_INIT; | |
1941 | ||
1942 | if (opts->new_branch_force) | |
1943 | opts->branch_exists = validate_branchname(opts->new_branch, &buf); | |
1944 | else | |
1945 | opts->branch_exists = | |
1946 | validate_new_branchname(opts->new_branch, &buf, 0); | |
1947 | strbuf_release(&buf); | |
1948 | } | |
1949 | ||
1950 | if (opts->patch_mode || opts->pathspec.nr) | |
1951 | ret = checkout_paths(opts, &new_branch_info); | |
1952 | else | |
1953 | ret = checkout_branch(opts, &new_branch_info); | |
1954 | ||
1955 | branch_info_release(&new_branch_info); | |
1956 | clear_pathspec(&opts->pathspec); | |
1957 | free(opts->pathspec_from_file); | |
1958 | free(options); | |
1959 | ||
1960 | return ret; | |
1961 | } | |
1962 | ||
1963 | int cmd_checkout(int argc, | |
1964 | const char **argv, | |
1965 | const char *prefix, | |
1966 | struct repository *repo UNUSED) | |
1967 | { | |
1968 | struct checkout_opts opts = CHECKOUT_OPTS_INIT; | |
1969 | struct option *options; | |
1970 | struct option checkout_options[] = { | |
1971 | OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), | |
1972 | N_("create and checkout a new branch")), | |
1973 | OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), | |
1974 | N_("create/reset and checkout a branch")), | |
1975 | OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), | |
1976 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, | |
1977 | N_("second guess 'git checkout <no-such-branch>' (default)")), | |
1978 | OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), | |
1979 | OPT_END() | |
1980 | }; | |
1981 | ||
1982 | opts.dwim_new_local_branch = 1; | |
1983 | opts.switch_branch_doing_nothing_is_ok = 1; | |
1984 | opts.only_merge_on_switching_branches = 0; | |
1985 | opts.accept_ref = 1; | |
1986 | opts.accept_pathspec = 1; | |
1987 | opts.implicit_detach = 1; | |
1988 | opts.can_switch_when_in_progress = 1; | |
1989 | opts.orphan_from_empty_tree = 0; | |
1990 | opts.empty_pathspec_ok = 1; | |
1991 | opts.overlay_mode = -1; | |
1992 | opts.checkout_index = -2; /* default on */ | |
1993 | opts.checkout_worktree = -2; /* default on */ | |
1994 | ||
1995 | if (argc == 3 && !strcmp(argv[1], "-b")) { | |
1996 | /* | |
1997 | * User ran 'git checkout -b <branch>' and expects | |
1998 | * the same behavior as 'git switch -c <branch>'. | |
1999 | */ | |
2000 | opts.switch_branch_doing_nothing_is_ok = 0; | |
2001 | opts.only_merge_on_switching_branches = 1; | |
2002 | } | |
2003 | ||
2004 | options = parse_options_dup(checkout_options); | |
2005 | options = add_common_options(&opts, options); | |
2006 | options = add_common_switch_branch_options(&opts, options); | |
2007 | options = add_checkout_path_options(&opts, options); | |
2008 | ||
2009 | return checkout_main(argc, argv, prefix, &opts, options, | |
2010 | checkout_usage); | |
2011 | } | |
2012 | ||
2013 | int cmd_switch(int argc, | |
2014 | const char **argv, | |
2015 | const char *prefix, | |
2016 | struct repository *repo UNUSED) | |
2017 | { | |
2018 | struct checkout_opts opts = CHECKOUT_OPTS_INIT; | |
2019 | struct option *options = NULL; | |
2020 | struct option switch_options[] = { | |
2021 | OPT_STRING('c', "create", &opts.new_branch, N_("branch"), | |
2022 | N_("create and switch to a new branch")), | |
2023 | OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"), | |
2024 | N_("create/reset and switch to a branch")), | |
2025 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, | |
2026 | N_("second guess 'git switch <no-such-branch>'")), | |
2027 | OPT_BOOL(0, "discard-changes", &opts.discard_changes, | |
2028 | N_("throw away local modifications")), | |
2029 | OPT_END() | |
2030 | }; | |
2031 | ||
2032 | opts.dwim_new_local_branch = 1; | |
2033 | opts.accept_ref = 1; | |
2034 | opts.accept_pathspec = 0; | |
2035 | opts.switch_branch_doing_nothing_is_ok = 0; | |
2036 | opts.only_merge_on_switching_branches = 1; | |
2037 | opts.implicit_detach = 0; | |
2038 | opts.can_switch_when_in_progress = 0; | |
2039 | opts.orphan_from_empty_tree = 1; | |
2040 | opts.overlay_mode = -1; | |
2041 | ||
2042 | options = parse_options_dup(switch_options); | |
2043 | options = add_common_options(&opts, options); | |
2044 | options = add_common_switch_branch_options(&opts, options); | |
2045 | ||
2046 | cb_option = 'c'; | |
2047 | ||
2048 | return checkout_main(argc, argv, prefix, &opts, options, | |
2049 | switch_branch_usage); | |
2050 | } | |
2051 | ||
2052 | int cmd_restore(int argc, | |
2053 | const char **argv, | |
2054 | const char *prefix, | |
2055 | struct repository *repo UNUSED) | |
2056 | { | |
2057 | struct checkout_opts opts = CHECKOUT_OPTS_INIT; | |
2058 | struct option *options; | |
2059 | struct option restore_options[] = { | |
2060 | OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>", | |
2061 | N_("which tree-ish to checkout from")), | |
2062 | OPT_BOOL('S', "staged", &opts.checkout_index, | |
2063 | N_("restore the index")), | |
2064 | OPT_BOOL('W', "worktree", &opts.checkout_worktree, | |
2065 | N_("restore the working tree (default)")), | |
2066 | OPT_BOOL(0, "ignore-unmerged", &opts.ignore_unmerged, | |
2067 | N_("ignore unmerged entries")), | |
2068 | OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode")), | |
2069 | OPT_END() | |
2070 | }; | |
2071 | ||
2072 | opts.accept_ref = 0; | |
2073 | opts.accept_pathspec = 1; | |
2074 | opts.empty_pathspec_ok = 0; | |
2075 | opts.overlay_mode = 0; | |
2076 | opts.checkout_index = -1; /* default off */ | |
2077 | opts.checkout_worktree = -2; /* default on */ | |
2078 | opts.ignore_unmerged_opt = "--ignore-unmerged"; | |
2079 | ||
2080 | options = parse_options_dup(restore_options); | |
2081 | options = add_common_options(&opts, options); | |
2082 | options = add_checkout_path_options(&opts, options); | |
2083 | ||
2084 | return checkout_main(argc, argv, prefix, &opts, options, | |
2085 | restore_usage); | |
2086 | } |