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