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