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