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