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