]>
Commit | Line | Data |
---|---|---|
f8adbec9 | 1 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
782c2d65 | 2 | #include "builtin.h" |
0dabeffc NTND |
3 | #include "advice.h" |
4 | #include "blob.h" | |
5 | #include "branch.h" | |
6 | #include "cache-tree.h" | |
7c85a87c | 7 | #include "checkout.h" |
0dabeffc NTND |
8 | #include "commit.h" |
9 | #include "config.h" | |
10 | #include "diff.h" | |
11 | #include "dir.h" | |
12 | #include "ll-merge.h" | |
697cc8ef | 13 | #include "lockfile.h" |
0dabeffc NTND |
14 | #include "merge-recursive.h" |
15 | #include "object-store.h" | |
782c2d65 DB |
16 | #include "parse-options.h" |
17 | #include "refs.h" | |
0dabeffc NTND |
18 | #include "remote.h" |
19 | #include "resolve-undo.h" | |
20 | #include "revision.h" | |
21 | #include "run-command.h" | |
22 | #include "submodule.h" | |
23 | #include "submodule-config.h" | |
782c2d65 DB |
24 | #include "tree.h" |
25 | #include "tree-walk.h" | |
26 | #include "unpack-trees.h" | |
c45f0f52 | 27 | #include "wt-status.h" |
0cf8581e | 28 | #include "xdiff-interface.h" |
782c2d65 DB |
29 | |
30 | static const char * const checkout_usage[] = { | |
9c9b4f2f AH |
31 | N_("git checkout [<options>] <branch>"), |
32 | N_("git checkout [<options>] [<branch>] -- <file>..."), | |
782c2d65 DB |
33 | NULL, |
34 | }; | |
35 | ||
d787d311 NTND |
36 | static const char * const switch_branch_usage[] = { |
37 | N_("git switch [<options>] [<branch>]"), | |
38 | NULL, | |
39 | }; | |
40 | ||
db941099 | 41 | struct checkout_opts { |
e51e3057 | 42 | int patch_mode; |
db941099 JH |
43 | int quiet; |
44 | int merge; | |
45 | int force; | |
32669671 | 46 | int force_detach; |
7968bef0 | 47 | int implicit_detach; |
38901a48 | 48 | int writeout_stage; |
c1d7036b | 49 | int overwrite_ignore; |
08d595dc | 50 | int ignore_skipworktree; |
1d0fa898 | 51 | int ignore_other_worktrees; |
870ebdb9 | 52 | int show_progress; |
0f086e6d | 53 | int count_checkout_paths; |
091e04bc | 54 | int overlay_mode; |
ccb111b3 | 55 | int dwim_new_local_branch; |
3ec37ad1 | 56 | int discard_changes; |
5c06e269 | 57 | int accept_pathspec; |
e342c63a | 58 | int switch_branch_doing_nothing_is_ok; |
65f099b3 | 59 | int only_merge_on_switching_branches; |
c45f0f52 | 60 | int can_switch_when_in_progress; |
db941099 JH |
61 | |
62 | const char *new_branch; | |
02ac9837 | 63 | const char *new_branch_force; |
9db5ebf4 | 64 | const char *new_orphan_branch; |
db941099 JH |
65 | int new_branch_log; |
66 | enum branch_track track; | |
175f6e59 | 67 | struct diff_options diff_options; |
55cf704a | 68 | char *conflict_style; |
e51e3057 NTND |
69 | |
70 | int branch_exists; | |
71 | const char *prefix; | |
817b345a | 72 | struct pathspec pathspec; |
e51e3057 | 73 | struct tree *source_tree; |
db941099 JH |
74 | }; |
75 | ||
c8a3ea1f | 76 | static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, |
782c2d65 DB |
77 | int changed) |
78 | { | |
15048f8a | 79 | return run_hook_le(NULL, "post-checkout", |
c8a3ea1f BW |
80 | oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid), |
81 | oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid), | |
15048f8a | 82 | changed ? "1" : "0", NULL); |
c8a3ea1f | 83 | /* "new_commit" can be NULL when checking out from the index before |
2292ce47 | 84 | a commit exists. */ |
ae98a008 | 85 | |
782c2d65 DB |
86 | } |
87 | ||
df46d77e | 88 | static int update_some(const struct object_id *oid, struct strbuf *base, |
671f0707 | 89 | const char *pathname, unsigned mode, int stage, void *context) |
782c2d65 DB |
90 | { |
91 | int len; | |
92 | struct cache_entry *ce; | |
c5326bd6 | 93 | int pos; |
782c2d65 | 94 | |
782c2d65 DB |
95 | if (S_ISDIR(mode)) |
96 | return READ_TREE_RECURSIVE; | |
97 | ||
6a0b0b6d | 98 | len = base->len + strlen(pathname); |
a849735b | 99 | ce = make_empty_cache_entry(&the_index, len); |
df46d77e | 100 | oidcpy(&ce->oid, oid); |
6a0b0b6d NTND |
101 | memcpy(ce->name, base->buf, base->len); |
102 | memcpy(ce->name + base->len, pathname, len - base->len); | |
b60e188c TG |
103 | ce->ce_flags = create_ce_flags(0) | CE_UPDATE; |
104 | ce->ce_namelen = len; | |
782c2d65 | 105 | ce->ce_mode = create_ce_mode(mode); |
c5326bd6 JK |
106 | |
107 | /* | |
108 | * If the entry is the same as the current index, we can leave the old | |
109 | * entry in place. Whether it is UPTODATE or not, checkout_entry will | |
110 | * do the right thing. | |
111 | */ | |
112 | pos = cache_name_pos(ce->name, ce->ce_namelen); | |
113 | if (pos >= 0) { | |
114 | struct cache_entry *old = active_cache[pos]; | |
115 | if (ce->ce_mode == old->ce_mode && | |
4a7e27e9 | 116 | oideq(&ce->oid, &old->oid)) { |
c5326bd6 | 117 | old->ce_flags |= CE_UPDATE; |
a849735b | 118 | discard_cache_entry(ce); |
c5326bd6 JK |
119 | return 0; |
120 | } | |
121 | } | |
122 | ||
782c2d65 DB |
123 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
124 | return 0; | |
125 | } | |
126 | ||
18e4f405 | 127 | static int read_tree_some(struct tree *tree, const struct pathspec *pathspec) |
782c2d65 | 128 | { |
e092073d NTND |
129 | read_tree_recursive(the_repository, tree, "", 0, 0, |
130 | pathspec, update_some, NULL); | |
782c2d65 | 131 | |
782c2d65 DB |
132 | /* update the index with the given tree's info |
133 | * for all args, expanding wildcards, and exit | |
134 | * with any non-zero return code. | |
135 | */ | |
136 | return 0; | |
137 | } | |
138 | ||
9c5e6c80 | 139 | static int skip_same_name(const struct cache_entry *ce, int pos) |
8fdcf312 JH |
140 | { |
141 | while (++pos < active_nr && | |
142 | !strcmp(active_cache[pos]->name, ce->name)) | |
143 | ; /* skip */ | |
144 | return pos; | |
145 | } | |
146 | ||
091e04bc TG |
147 | static int check_stage(int stage, const struct cache_entry *ce, int pos, |
148 | int overlay_mode) | |
38901a48 JH |
149 | { |
150 | while (pos < active_nr && | |
151 | !strcmp(active_cache[pos]->name, ce->name)) { | |
152 | if (ce_stage(active_cache[pos]) == stage) | |
153 | return 0; | |
154 | pos++; | |
155 | } | |
091e04bc TG |
156 | if (!overlay_mode) |
157 | return 0; | |
9f97ab08 ÆAB |
158 | if (stage == 2) |
159 | return error(_("path '%s' does not have our version"), ce->name); | |
160 | else | |
161 | return error(_("path '%s' does not have their version"), ce->name); | |
38901a48 JH |
162 | } |
163 | ||
9c5e6c80 | 164 | static int check_stages(unsigned stages, const struct cache_entry *ce, int pos) |
0cf8581e | 165 | { |
fbbccd0a JH |
166 | unsigned seen = 0; |
167 | const char *name = ce->name; | |
168 | ||
169 | while (pos < active_nr) { | |
170 | ce = active_cache[pos]; | |
171 | if (strcmp(name, ce->name)) | |
172 | break; | |
173 | seen |= (1 << ce_stage(ce)); | |
174 | pos++; | |
175 | } | |
176 | if ((stages & seen) != stages) | |
177 | return error(_("path '%s' does not have all necessary versions"), | |
178 | name); | |
0cf8581e JH |
179 | return 0; |
180 | } | |
181 | ||
ce25e4c7 | 182 | static int checkout_stage(int stage, const struct cache_entry *ce, int pos, |
7d0c1f45 JH |
183 | const struct checkout *state, int *nr_checkouts, |
184 | int overlay_mode) | |
38901a48 JH |
185 | { |
186 | while (pos < active_nr && | |
187 | !strcmp(active_cache[pos]->name, ce->name)) { | |
188 | if (ce_stage(active_cache[pos]) == stage) | |
0f086e6d NTND |
189 | return checkout_entry(active_cache[pos], state, |
190 | NULL, nr_checkouts); | |
38901a48 JH |
191 | pos++; |
192 | } | |
091e04bc TG |
193 | if (!overlay_mode) { |
194 | unlink_entry(ce); | |
195 | return 0; | |
196 | } | |
9f97ab08 ÆAB |
197 | if (stage == 2) |
198 | return error(_("path '%s' does not have our version"), ce->name); | |
199 | else | |
200 | return error(_("path '%s' does not have their version"), ce->name); | |
38901a48 | 201 | } |
8fdcf312 | 202 | |
0f086e6d | 203 | static int checkout_merged(int pos, const struct checkout *state, int *nr_checkouts) |
0cf8581e JH |
204 | { |
205 | struct cache_entry *ce = active_cache[pos]; | |
206 | const char *path = ce->name; | |
207 | mmfile_t ancestor, ours, theirs; | |
208 | int status; | |
60af7691 | 209 | struct object_id oid; |
0cf8581e | 210 | mmbuffer_t result_buf; |
60af7691 | 211 | struct object_id threeway[3]; |
335c6e40 | 212 | unsigned mode = 0; |
0cf8581e | 213 | |
fbbccd0a JH |
214 | memset(threeway, 0, sizeof(threeway)); |
215 | while (pos < active_nr) { | |
216 | int stage; | |
217 | stage = ce_stage(ce); | |
218 | if (!stage || strcmp(path, ce->name)) | |
219 | break; | |
60af7691 | 220 | oidcpy(&threeway[stage - 1], &ce->oid); |
fbbccd0a JH |
221 | if (stage == 2) |
222 | mode = create_ce_mode(ce->ce_mode); | |
223 | pos++; | |
224 | ce = active_cache[pos]; | |
225 | } | |
60af7691 | 226 | if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2])) |
fbbccd0a | 227 | return error(_("path '%s' does not have necessary versions"), path); |
0cf8581e | 228 | |
d449347d | 229 | read_mmblob(&ancestor, &threeway[0]); |
230 | read_mmblob(&ours, &threeway[1]); | |
231 | read_mmblob(&theirs, &threeway[2]); | |
0cf8581e | 232 | |
18b037a5 JN |
233 | /* |
234 | * NEEDSWORK: re-create conflicts from merges with | |
235 | * merge.renormalize set, too | |
236 | */ | |
f0531a29 | 237 | status = ll_merge(&result_buf, path, &ancestor, "base", |
32eaa468 NTND |
238 | &ours, "ours", &theirs, "theirs", |
239 | state->istate, NULL); | |
0cf8581e JH |
240 | free(ancestor.ptr); |
241 | free(ours.ptr); | |
242 | free(theirs.ptr); | |
243 | if (status < 0 || !result_buf.ptr) { | |
244 | free(result_buf.ptr); | |
e8a8a4d7 | 245 | return error(_("path '%s': cannot merge"), path); |
0cf8581e JH |
246 | } |
247 | ||
248 | /* | |
249 | * NEEDSWORK: | |
250 | * There is absolutely no reason to write this as a blob object | |
514e8039 JS |
251 | * and create a phony cache entry. This hack is primarily to get |
252 | * to the write_entry() machinery that massages the contents to | |
253 | * work-tree format and writes out which only allows it for a | |
254 | * cache entry. The code in write_entry() needs to be refactored | |
255 | * to allow us to feed a <buffer, size, mode> instead of a cache | |
256 | * entry. Such a refactoring would help merge_recursive as well | |
257 | * (it also writes the merge result to the object database even | |
258 | * when it may contain conflicts). | |
0cf8581e | 259 | */ |
a09c985e | 260 | if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) |
e8a8a4d7 | 261 | die(_("Unable to add merge result for '%s'"), path); |
443a12f3 | 262 | free(result_buf.ptr); |
a849735b | 263 | ce = make_transient_cache_entry(mode, &oid, path, 2); |
048f2762 | 264 | if (!ce) |
e8a8a4d7 | 265 | die(_("make_cache_entry failed for path '%s'"), path); |
0f086e6d | 266 | status = checkout_entry(ce, state, NULL, nr_checkouts); |
a849735b | 267 | discard_cache_entry(ce); |
0cf8581e JH |
268 | return status; |
269 | } | |
8fdcf312 | 270 | |
091e04bc TG |
271 | static void mark_ce_for_checkout_overlay(struct cache_entry *ce, |
272 | char *ps_matched, | |
273 | const struct checkout_opts *opts) | |
b7033e73 TG |
274 | { |
275 | ce->ce_flags &= ~CE_MATCHED; | |
276 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
277 | return; | |
278 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
279 | /* | |
280 | * "git checkout tree-ish -- path", but this entry | |
281 | * is in the original index but is not in tree-ish | |
282 | * or does not match the pathspec; it will not be | |
283 | * checked out to the working tree. We will not do | |
284 | * anything to this entry at all. | |
285 | */ | |
286 | return; | |
287 | /* | |
288 | * Either this entry came from the tree-ish we are | |
289 | * checking the paths out of, or we are checking out | |
290 | * of the index. | |
291 | * | |
292 | * If it comes from the tree-ish, we already know it | |
293 | * matches the pathspec and could just stamp | |
294 | * CE_MATCHED to it from update_some(). But we still | |
295 | * need ps_matched and read_tree_recursive (and | |
296 | * eventually tree_entry_interesting) cannot fill | |
297 | * ps_matched yet. Once it can, we can avoid calling | |
298 | * match_pathspec() for _all_ entries when | |
299 | * opts->source_tree != NULL. | |
300 | */ | |
301 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) | |
302 | ce->ce_flags |= CE_MATCHED; | |
303 | } | |
304 | ||
091e04bc TG |
305 | static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce, |
306 | char *ps_matched, | |
307 | const struct checkout_opts *opts) | |
308 | { | |
309 | ce->ce_flags &= ~CE_MATCHED; | |
310 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) | |
311 | return; | |
312 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) { | |
313 | ce->ce_flags |= CE_MATCHED; | |
314 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) | |
315 | /* | |
316 | * In overlay mode, but the path is not in | |
317 | * tree-ish, which means we should remove it | |
318 | * from the index and the working tree. | |
319 | */ | |
320 | ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; | |
321 | } | |
322 | } | |
323 | ||
b6312c27 NTND |
324 | static int checkout_paths(const struct checkout_opts *opts, |
325 | const char *revision) | |
782c2d65 DB |
326 | { |
327 | int pos; | |
68e3d629 | 328 | struct checkout state = CHECKOUT_INIT; |
782c2d65 | 329 | static char *ps_matched; |
60af7691 | 330 | struct object_id rev; |
782c2d65 | 331 | struct commit *head; |
d2b3691b | 332 | int errs = 0; |
837e34eb | 333 | struct lock_file lock_file = LOCK_INIT; |
1d1f689b | 334 | int nr_checkouts = 0, nr_unmerged = 0; |
b6312c27 | 335 | |
e27dd8ae JH |
336 | trace2_cmd_mode(opts->patch_mode ? "patch" : "path"); |
337 | ||
b6312c27 NTND |
338 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) |
339 | die(_("'%s' cannot be used with updating paths"), "--track"); | |
340 | ||
341 | if (opts->new_branch_log) | |
342 | die(_("'%s' cannot be used with updating paths"), "-l"); | |
343 | ||
344 | if (opts->force && opts->patch_mode) | |
345 | die(_("'%s' cannot be used with updating paths"), "-f"); | |
346 | ||
347 | if (opts->force_detach) | |
348 | die(_("'%s' cannot be used with updating paths"), "--detach"); | |
349 | ||
350 | if (opts->merge && opts->patch_mode) | |
351 | die(_("'%s' cannot be used with %s"), "--merge", "--patch"); | |
352 | ||
353 | if (opts->force && opts->merge) | |
354 | die(_("'%s' cannot be used with %s"), "-f", "-m"); | |
355 | ||
356 | if (opts->new_branch) | |
357 | die(_("Cannot update paths and switch to branch '%s' at the same time."), | |
358 | opts->new_branch); | |
359 | ||
360 | if (opts->patch_mode) | |
361 | return run_add_interactive(revision, "--patch=checkout", | |
480ca644 | 362 | &opts->pathspec); |
b6312c27 | 363 | |
fb4a8464 | 364 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
5ab2a2da | 365 | if (read_cache_preload(&opts->pathspec) < 0) |
e923a8ab | 366 | return error(_("index file corrupt")); |
75336878 | 367 | |
e51e3057 | 368 | if (opts->source_tree) |
18e4f405 | 369 | read_tree_some(opts->source_tree, &opts->pathspec); |
75336878 | 370 | |
8b54c234 | 371 | ps_matched = xcalloc(opts->pathspec.nr, 1); |
782c2d65 | 372 | |
e721c154 NTND |
373 | /* |
374 | * Make sure all pathspecs participated in locating the paths | |
375 | * to be checked out. | |
376 | */ | |
b7033e73 | 377 | for (pos = 0; pos < active_nr; pos++) |
091e04bc TG |
378 | if (opts->overlay_mode) |
379 | mark_ce_for_checkout_overlay(active_cache[pos], | |
380 | ps_matched, | |
381 | opts); | |
382 | else | |
383 | mark_ce_for_checkout_no_overlay(active_cache[pos], | |
384 | ps_matched, | |
385 | opts); | |
782c2d65 | 386 | |
17ddc66e | 387 | if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) { |
e721c154 | 388 | free(ps_matched); |
782c2d65 | 389 | return 1; |
e721c154 NTND |
390 | } |
391 | free(ps_matched); | |
782c2d65 | 392 | |
4421a823 JH |
393 | /* "checkout -m path" to recreate conflicted state */ |
394 | if (opts->merge) | |
e721c154 | 395 | unmerge_marked_index(&the_index); |
4421a823 | 396 | |
8fdcf312 JH |
397 | /* Any unmerged paths? */ |
398 | for (pos = 0; pos < active_nr; pos++) { | |
9c5e6c80 | 399 | const struct cache_entry *ce = active_cache[pos]; |
e721c154 | 400 | if (ce->ce_flags & CE_MATCHED) { |
8fdcf312 JH |
401 | if (!ce_stage(ce)) |
402 | continue; | |
db941099 | 403 | if (opts->force) { |
e8a8a4d7 | 404 | warning(_("path '%s' is unmerged"), ce->name); |
f9022075 | 405 | } else if (opts->writeout_stage) { |
091e04bc | 406 | errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode); |
0cf8581e | 407 | } else if (opts->merge) { |
fbbccd0a | 408 | errs |= check_stages((1<<2) | (1<<3), ce, pos); |
db941099 JH |
409 | } else { |
410 | errs = 1; | |
e8a8a4d7 | 411 | error(_("path '%s' is unmerged"), ce->name); |
db941099 | 412 | } |
8fdcf312 JH |
413 | pos = skip_same_name(ce, pos) - 1; |
414 | } | |
415 | } | |
416 | if (errs) | |
417 | return 1; | |
418 | ||
d2b3691b | 419 | /* Now we are committed to check them out */ |
782c2d65 DB |
420 | state.force = 1; |
421 | state.refresh_cache = 1; | |
d4a2024a | 422 | state.istate = &the_index; |
2841e8f8 LS |
423 | |
424 | enable_delayed_checkout(&state); | |
782c2d65 DB |
425 | for (pos = 0; pos < active_nr; pos++) { |
426 | struct cache_entry *ce = active_cache[pos]; | |
e721c154 | 427 | if (ce->ce_flags & CE_MATCHED) { |
8fdcf312 | 428 | if (!ce_stage(ce)) { |
0f086e6d NTND |
429 | errs |= checkout_entry(ce, &state, |
430 | NULL, &nr_checkouts); | |
8fdcf312 JH |
431 | continue; |
432 | } | |
f9022075 | 433 | if (opts->writeout_stage) |
0f086e6d NTND |
434 | errs |= checkout_stage(opts->writeout_stage, |
435 | ce, pos, | |
7d0c1f45 JH |
436 | &state, |
437 | &nr_checkouts, opts->overlay_mode); | |
f9022075 | 438 | else if (opts->merge) |
0f086e6d | 439 | errs |= checkout_merged(pos, &state, |
1d1f689b | 440 | &nr_unmerged); |
8fdcf312 | 441 | pos = skip_same_name(ce, pos) - 1; |
782c2d65 DB |
442 | } |
443 | } | |
091e04bc TG |
444 | remove_marked_cache_entries(&the_index, 1); |
445 | remove_scheduled_dirs(); | |
0f086e6d NTND |
446 | errs |= finish_delayed_checkout(&state, &nr_checkouts); |
447 | ||
448 | if (opts->count_checkout_paths) { | |
1d1f689b NTND |
449 | if (nr_unmerged) |
450 | fprintf_ln(stderr, Q_("Recreated %d merge conflict", | |
451 | "Recreated %d merge conflicts", | |
452 | nr_unmerged), | |
453 | nr_unmerged); | |
0f086e6d | 454 | if (opts->source_tree) |
3c5883b3 NTND |
455 | fprintf_ln(stderr, Q_("Updated %d path from %s", |
456 | "Updated %d paths from %s", | |
0f086e6d NTND |
457 | nr_checkouts), |
458 | nr_checkouts, | |
459 | find_unique_abbrev(&opts->source_tree->object.oid, | |
460 | DEFAULT_ABBREV)); | |
1d1f689b | 461 | else if (!nr_unmerged || nr_checkouts) |
3c5883b3 NTND |
462 | fprintf_ln(stderr, Q_("Updated %d path from the index", |
463 | "Updated %d paths from the index", | |
0f086e6d NTND |
464 | nr_checkouts), |
465 | nr_checkouts); | |
466 | } | |
782c2d65 | 467 | |
837e34eb | 468 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
e8a8a4d7 | 469 | die(_("unable to write new index file")); |
75336878 | 470 | |
34c290a6 | 471 | read_ref_full("HEAD", 0, &rev, NULL); |
21e1ee8f | 472 | head = lookup_commit_reference_gently(the_repository, &rev, 1); |
782c2d65 | 473 | |
d2b3691b JH |
474 | errs |= post_checkout_hook(head, head, 0); |
475 | return errs; | |
782c2d65 DB |
476 | } |
477 | ||
a2b4994c NTND |
478 | static void show_local_changes(struct object *head, |
479 | const struct diff_options *opts) | |
782c2d65 DB |
480 | { |
481 | struct rev_info rev; | |
482 | /* I think we want full paths, even if we're in a subdirectory. */ | |
2abf3503 | 483 | repo_init_revisions(the_repository, &rev, NULL); |
175f6e59 | 484 | rev.diffopt.flags = opts->flags; |
782c2d65 | 485 | rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; |
28452655 | 486 | diff_setup_done(&rev.diffopt); |
782c2d65 DB |
487 | add_pending_object(&rev, head, NULL); |
488 | run_diff_index(&rev, 0); | |
489 | } | |
490 | ||
b3c0494a | 491 | static void describe_detached_head(const char *msg, struct commit *commit) |
782c2d65 | 492 | { |
f285a2d7 | 493 | struct strbuf sb = STRBUF_INIT; |
ca69d4d5 | 494 | |
3c621839 JK |
495 | if (!parse_commit(commit)) |
496 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); | |
ca69d4d5 AR |
497 | if (print_sha1_ellipsis()) { |
498 | fprintf(stderr, "%s %s... %s\n", msg, | |
aab9583f | 499 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); |
ca69d4d5 AR |
500 | } else { |
501 | fprintf(stderr, "%s %s %s\n", msg, | |
aab9583f | 502 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); |
ca69d4d5 | 503 | } |
782c2d65 DB |
504 | strbuf_release(&sb); |
505 | } | |
506 | ||
a2b4994c NTND |
507 | static int reset_tree(struct tree *tree, const struct checkout_opts *o, |
508 | int worktree, int *writeout_error) | |
782c2d65 DB |
509 | { |
510 | struct unpack_trees_options opts; | |
511 | struct tree_desc tree_desc; | |
bc052d7f | 512 | |
782c2d65 DB |
513 | memset(&opts, 0, sizeof(opts)); |
514 | opts.head_idx = -1; | |
6286a08d JH |
515 | opts.update = worktree; |
516 | opts.skip_unmerged = !worktree; | |
782c2d65 DB |
517 | opts.reset = 1; |
518 | opts.merge = 1; | |
519 | opts.fn = oneway_merge; | |
870ebdb9 | 520 | opts.verbose_update = o->show_progress; |
34110cd4 LT |
521 | opts.src_index = &the_index; |
522 | opts.dst_index = &the_index; | |
782c2d65 DB |
523 | parse_tree(tree); |
524 | init_tree_desc(&tree_desc, tree->buffer, tree->size); | |
291d823e JH |
525 | switch (unpack_trees(1, &tree_desc, &opts)) { |
526 | case -2: | |
a2b4994c | 527 | *writeout_error = 1; |
291d823e JH |
528 | /* |
529 | * We return 0 nevertheless, as the index is all right | |
530 | * and more importantly we have made best efforts to | |
531 | * update paths in the work tree, and we cannot revert | |
532 | * them. | |
533 | */ | |
1cf01a34 | 534 | /* fallthrough */ |
291d823e JH |
535 | case 0: |
536 | return 0; | |
537 | default: | |
84a5750b | 538 | return 128; |
291d823e | 539 | } |
782c2d65 DB |
540 | } |
541 | ||
782c2d65 DB |
542 | struct branch_info { |
543 | const char *name; /* The short name used */ | |
544 | const char *path; /* The full name of a real branch */ | |
545 | struct commit *commit; /* The named commit */ | |
5883034c NTND |
546 | /* |
547 | * if not null the branch is detached because it's already | |
548 | * checked out in this checkout | |
549 | */ | |
550 | char *checkout; | |
782c2d65 DB |
551 | }; |
552 | ||
553 | static void setup_branch_path(struct branch_info *branch) | |
554 | { | |
f285a2d7 | 555 | struct strbuf buf = STRBUF_INIT; |
ae5a6c36 | 556 | |
fd4692ff | 557 | strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); |
a552de75 | 558 | if (strcmp(buf.buf, branch->name)) |
ae5a6c36 | 559 | branch->name = xstrdup(buf.buf); |
a552de75 | 560 | strbuf_splice(&buf, 0, 0, "refs/heads/", 11); |
782c2d65 DB |
561 | branch->path = strbuf_detach(&buf, NULL); |
562 | } | |
563 | ||
a2b4994c | 564 | static int merge_working_tree(const struct checkout_opts *opts, |
c8a3ea1f BW |
565 | struct branch_info *old_branch_info, |
566 | struct branch_info *new_branch_info, | |
a2b4994c | 567 | int *writeout_error) |
782c2d65 DB |
568 | { |
569 | int ret; | |
837e34eb | 570 | struct lock_file lock_file = LOCK_INIT; |
b96524f8 | 571 | |
837e34eb | 572 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
53996fe5 | 573 | if (read_cache_preload(NULL) < 0) |
e923a8ab | 574 | return error(_("index file corrupt")); |
782c2d65 | 575 | |
cfc5789a | 576 | resolve_undo_clear(); |
3ec37ad1 | 577 | if (opts->discard_changes) { |
2e27bd77 DS |
578 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
579 | opts, 1, writeout_error); | |
782c2d65 DB |
580 | if (ret) |
581 | return ret; | |
582 | } else { | |
583 | struct tree_desc trees[2]; | |
584 | struct tree *tree; | |
585 | struct unpack_trees_options topts; | |
bc052d7f | 586 | |
782c2d65 DB |
587 | memset(&topts, 0, sizeof(topts)); |
588 | topts.head_idx = -1; | |
34110cd4 LT |
589 | topts.src_index = &the_index; |
590 | topts.dst_index = &the_index; | |
782c2d65 | 591 | |
e294030f | 592 | setup_unpack_trees_porcelain(&topts, "checkout"); |
8ccba008 | 593 | |
782c2d65 DB |
594 | refresh_cache(REFRESH_QUIET); |
595 | ||
596 | if (unmerged_cache()) { | |
e8a8a4d7 | 597 | error(_("you need to resolve your current index first")); |
04c9e11f | 598 | return 1; |
782c2d65 | 599 | } |
04c9e11f JH |
600 | |
601 | /* 2-way merge to the new branch */ | |
fa7b3c2f | 602 | topts.initial_checkout = is_cache_unborn(); |
04c9e11f JH |
603 | topts.update = 1; |
604 | topts.merge = 1; | |
c8a3ea1f | 605 | topts.gently = opts->merge && old_branch_info->commit; |
870ebdb9 | 606 | topts.verbose_update = opts->show_progress; |
04c9e11f | 607 | topts.fn = twoway_merge; |
c1d7036b NTND |
608 | if (opts->overwrite_ignore) { |
609 | topts.dir = xcalloc(1, sizeof(*topts.dir)); | |
610 | topts.dir->flags |= DIR_SHOW_IGNORED; | |
611 | setup_standard_excludes(topts.dir); | |
612 | } | |
c8a3ea1f BW |
613 | tree = parse_tree_indirect(old_branch_info->commit ? |
614 | &old_branch_info->commit->object.oid : | |
eb0ccfd7 | 615 | the_hash_algo->empty_tree); |
04c9e11f | 616 | init_tree_desc(&trees[0], tree->buffer, tree->size); |
c8a3ea1f | 617 | tree = parse_tree_indirect(&new_branch_info->commit->object.oid); |
04c9e11f JH |
618 | init_tree_desc(&trees[1], tree->buffer, tree->size); |
619 | ||
291d823e | 620 | ret = unpack_trees(2, trees, &topts); |
1c41d280 | 621 | clear_unpack_trees_porcelain(&topts); |
49d833dc | 622 | if (ret == -1) { |
782c2d65 DB |
623 | /* |
624 | * Unpack couldn't do a trivial merge; either | |
625 | * give up or do a real merge, depending on | |
626 | * whether the merge flag was used. | |
627 | */ | |
628 | struct tree *result; | |
629 | struct tree *work; | |
8a2fce18 | 630 | struct merge_options o; |
782c2d65 DB |
631 | if (!opts->merge) |
632 | return 1; | |
64da3ae5 JH |
633 | |
634 | /* | |
c8a3ea1f | 635 | * Without old_branch_info->commit, the below is the same as |
64da3ae5 JH |
636 | * the two-tree unpack we already tried and failed. |
637 | */ | |
c8a3ea1f | 638 | if (!old_branch_info->commit) |
64da3ae5 | 639 | return 1; |
782c2d65 DB |
640 | |
641 | /* Do more real merge */ | |
642 | ||
643 | /* | |
644 | * We update the index fully, then write the | |
645 | * tree from the index, then merge the new | |
646 | * branch with the current tree, with the old | |
647 | * branch as the base. Then we reset the index | |
648 | * (but not the working tree) to the new | |
649 | * branch, leaving the working tree as the | |
650 | * merged version, but skipping unmerged | |
651 | * entries in the index. | |
652 | */ | |
653 | ||
610d55af | 654 | add_files_to_cache(NULL, NULL, 0); |
7610fa57 JN |
655 | /* |
656 | * NEEDSWORK: carrying over local changes | |
657 | * when branches have different end-of-line | |
658 | * normalization (or clean+smudge rules) is | |
659 | * a pain; plumb in an option to set | |
660 | * o.renormalize? | |
661 | */ | |
0d6caa2d | 662 | init_merge_options(&o, the_repository); |
8a2fce18 MV |
663 | o.verbosity = 0; |
664 | work = write_tree_from_memory(&o); | |
782c2d65 | 665 | |
2e27bd77 DS |
666 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
667 | opts, 1, | |
a2b4994c | 668 | writeout_error); |
782c2d65 DB |
669 | if (ret) |
670 | return ret; | |
c8a3ea1f BW |
671 | o.ancestor = old_branch_info->name; |
672 | o.branch1 = new_branch_info->name; | |
8a2fce18 | 673 | o.branch2 = "local"; |
2e27bd77 DS |
674 | ret = merge_trees(&o, |
675 | get_commit_tree(new_branch_info->commit), | |
676 | work, | |
677 | get_commit_tree(old_branch_info->commit), | |
678 | &result); | |
f241ff0d JS |
679 | if (ret < 0) |
680 | exit(128); | |
2e27bd77 DS |
681 | ret = reset_tree(get_commit_tree(new_branch_info->commit), |
682 | opts, 0, | |
a2b4994c | 683 | writeout_error); |
548009c0 | 684 | strbuf_release(&o.obuf); |
84a5750b JH |
685 | if (ret) |
686 | return ret; | |
782c2d65 DB |
687 | } |
688 | } | |
689 | ||
aecf567c DT |
690 | if (!active_cache_tree) |
691 | active_cache_tree = cache_tree(); | |
692 | ||
693 | if (!cache_tree_fully_valid(active_cache_tree)) | |
3fd13cbc | 694 | cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); |
aecf567c | 695 | |
837e34eb | 696 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
e8a8a4d7 | 697 | die(_("unable to write new index file")); |
782c2d65 | 698 | |
3ec37ad1 | 699 | if (!opts->discard_changes && !opts->quiet) |
c8a3ea1f | 700 | show_local_changes(&new_branch_info->commit->object, &opts->diff_options); |
782c2d65 DB |
701 | |
702 | return 0; | |
703 | } | |
704 | ||
c8a3ea1f | 705 | static void report_tracking(struct branch_info *new_branch_info) |
79a1e6b4 | 706 | { |
6d21bf96 | 707 | struct strbuf sb = STRBUF_INIT; |
c8a3ea1f | 708 | struct branch *branch = branch_get(new_branch_info->name); |
79a1e6b4 | 709 | |
f39a757d | 710 | if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL)) |
79a1e6b4 | 711 | return; |
6d21bf96 JH |
712 | fputs(sb.buf, stdout); |
713 | strbuf_release(&sb); | |
b0030db3 | 714 | } |
79a1e6b4 | 715 | |
a2b4994c | 716 | static void update_refs_for_switch(const struct checkout_opts *opts, |
c8a3ea1f BW |
717 | struct branch_info *old_branch_info, |
718 | struct branch_info *new_branch_info) | |
782c2d65 | 719 | { |
f285a2d7 | 720 | struct strbuf msg = STRBUF_INIT; |
3bed291a | 721 | const char *old_desc, *reflog_msg; |
782c2d65 | 722 | if (opts->new_branch) { |
3631bf77 | 723 | if (opts->new_orphan_branch) { |
341fb286 CW |
724 | char *refname; |
725 | ||
726 | refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch); | |
727 | if (opts->new_branch_log && | |
728 | !should_autocreate_reflog(refname)) { | |
1a83c240 | 729 | int ret; |
a4c653df | 730 | struct strbuf err = STRBUF_INIT; |
3631bf77 | 731 | |
abd0cd3a | 732 | ret = safe_create_reflog(refname, 1, &err); |
1a83c240 | 733 | if (ret) { |
a4c653df DT |
734 | fprintf(stderr, _("Can not do reflog for '%s': %s\n"), |
735 | opts->new_orphan_branch, err.buf); | |
736 | strbuf_release(&err); | |
341fb286 | 737 | free(refname); |
3631bf77 EM |
738 | return; |
739 | } | |
abd0cd3a | 740 | strbuf_release(&err); |
3631bf77 | 741 | } |
341fb286 | 742 | free(refname); |
3631bf77 EM |
743 | } |
744 | else | |
4edce172 NTND |
745 | create_branch(the_repository, |
746 | opts->new_branch, new_branch_info->name, | |
02ac9837 | 747 | opts->new_branch_force ? 1 : 0, |
39bd6f72 | 748 | opts->new_branch_force ? 1 : 0, |
e2bbd0cc | 749 | opts->new_branch_log, |
f9a482e6 | 750 | opts->quiet, |
39bd6f72 | 751 | opts->track); |
c8a3ea1f BW |
752 | new_branch_info->name = opts->new_branch; |
753 | setup_branch_path(new_branch_info); | |
782c2d65 DB |
754 | } |
755 | ||
c8a3ea1f BW |
756 | old_desc = old_branch_info->name; |
757 | if (!old_desc && old_branch_info->commit) | |
758 | old_desc = oid_to_hex(&old_branch_info->commit->object.oid); | |
3bed291a RR |
759 | |
760 | reflog_msg = getenv("GIT_REFLOG_ACTION"); | |
761 | if (!reflog_msg) | |
762 | strbuf_addf(&msg, "checkout: moving from %s to %s", | |
c8a3ea1f | 763 | old_desc ? old_desc : "(invalid)", new_branch_info->name); |
3bed291a RR |
764 | else |
765 | strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg)); | |
782c2d65 | 766 | |
c8a3ea1f | 767 | if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) { |
f8bd36a4 | 768 | /* Nothing to do. */ |
c8a3ea1f BW |
769 | } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */ |
770 | update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL, | |
91774afc | 771 | REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); |
f8bd36a4 | 772 | if (!opts->quiet) { |
c8a3ea1f | 773 | if (old_branch_info->path && |
779b88a9 | 774 | advice_detached_head && !opts->force_detach) |
c8a3ea1f BW |
775 | detach_advice(new_branch_info->name); |
776 | describe_detached_head(_("HEAD is now at"), new_branch_info->commit); | |
f8bd36a4 | 777 | } |
c8a3ea1f BW |
778 | } else if (new_branch_info->path) { /* Switch branches. */ |
779 | if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0) | |
4636f651 | 780 | die(_("unable to update HEAD")); |
782c2d65 | 781 | if (!opts->quiet) { |
c8a3ea1f | 782 | if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { |
39bd6f72 JN |
783 | if (opts->new_branch_force) |
784 | fprintf(stderr, _("Reset branch '%s'\n"), | |
c8a3ea1f | 785 | new_branch_info->name); |
39bd6f72 JN |
786 | else |
787 | fprintf(stderr, _("Already on '%s'\n"), | |
c8a3ea1f | 788 | new_branch_info->name); |
08eaa4be ÆAB |
789 | } else if (opts->new_branch) { |
790 | if (opts->branch_exists) | |
c8a3ea1f | 791 | fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name); |
08eaa4be | 792 | else |
c8a3ea1f | 793 | fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name); |
08eaa4be | 794 | } else { |
e8a8a4d7 | 795 | fprintf(stderr, _("Switched to branch '%s'\n"), |
c8a3ea1f | 796 | new_branch_info->name); |
08eaa4be | 797 | } |
782c2d65 | 798 | } |
c8a3ea1f BW |
799 | if (old_branch_info->path && old_branch_info->name) { |
800 | if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path)) | |
801 | delete_reflog(old_branch_info->path); | |
3631bf77 | 802 | } |
782c2d65 | 803 | } |
f4a4b9ac | 804 | remove_branch_state(the_repository, !opts->quiet); |
782c2d65 | 805 | strbuf_release(&msg); |
32669671 | 806 | if (!opts->quiet && |
c8a3ea1f BW |
807 | (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD")))) |
808 | report_tracking(new_branch_info); | |
782c2d65 DB |
809 | } |
810 | ||
468224e5 | 811 | static int add_pending_uninteresting_ref(const char *refname, |
fcb615f5 | 812 | const struct object_id *oid, |
468224e5 | 813 | int flags, void *cb_data) |
8e2dc6ac | 814 | { |
a58a1b01 | 815 | add_pending_oid(cb_data, refname, oid, UNINTERESTING); |
5c08dc48 JK |
816 | return 0; |
817 | } | |
8e2dc6ac JH |
818 | |
819 | static void describe_one_orphan(struct strbuf *sb, struct commit *commit) | |
820 | { | |
0be240cc | 821 | strbuf_addstr(sb, " "); |
30e677e0 | 822 | strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); |
0be240cc | 823 | strbuf_addch(sb, ' '); |
3c621839 JK |
824 | if (!parse_commit(commit)) |
825 | pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); | |
8e2dc6ac JH |
826 | strbuf_addch(sb, '\n'); |
827 | } | |
828 | ||
829 | #define ORPHAN_CUTOFF 4 | |
830 | static void suggest_reattach(struct commit *commit, struct rev_info *revs) | |
831 | { | |
832 | struct commit *c, *last = NULL; | |
833 | struct strbuf sb = STRBUF_INIT; | |
834 | int lost = 0; | |
835 | while ((c = get_revision(revs)) != NULL) { | |
836 | if (lost < ORPHAN_CUTOFF) | |
837 | describe_one_orphan(&sb, c); | |
838 | last = c; | |
839 | lost++; | |
840 | } | |
841 | if (ORPHAN_CUTOFF < lost) { | |
842 | int more = lost - ORPHAN_CUTOFF; | |
843 | if (more == 1) | |
844 | describe_one_orphan(&sb, last); | |
845 | else | |
f06f08b7 | 846 | strbuf_addf(&sb, _(" ... and %d more.\n"), more); |
8e2dc6ac JH |
847 | } |
848 | ||
849 | fprintf(stderr, | |
f06f08b7 ÆAB |
850 | Q_( |
851 | /* The singular version */ | |
852 | "Warning: you are leaving %d commit behind, " | |
853 | "not connected to\n" | |
854 | "any of your branches:\n\n" | |
0faf2474 | 855 | "%s\n", |
f06f08b7 ÆAB |
856 | /* The plural version */ |
857 | "Warning: you are leaving %d commits behind, " | |
8e2dc6ac JH |
858 | "not connected to\n" |
859 | "any of your branches:\n\n" | |
f807b3dc | 860 | "%s\n", |
f06f08b7 ÆAB |
861 | /* Give ngettext() the count */ |
862 | lost), | |
863 | lost, | |
f807b3dc | 864 | sb.buf); |
8e2dc6ac | 865 | strbuf_release(&sb); |
f807b3dc JH |
866 | |
867 | if (advice_detached_head) | |
868 | fprintf(stderr, | |
fc792ca8 TS |
869 | Q_( |
870 | /* The singular version */ | |
871 | "If you want to keep it by creating a new branch, " | |
872 | "this may be a good time\nto do so with:\n\n" | |
873 | " git branch <new-branch-name> %s\n\n", | |
874 | /* The plural version */ | |
f807b3dc JH |
875 | "If you want to keep them by creating a new branch, " |
876 | "this may be a good time\nto do so with:\n\n" | |
fc792ca8 TS |
877 | " git branch <new-branch-name> %s\n\n", |
878 | /* Give ngettext() the count */ | |
879 | lost), | |
aab9583f | 880 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
8e2dc6ac JH |
881 | } |
882 | ||
883 | /* | |
884 | * We are about to leave commit that was at the tip of a detached | |
885 | * HEAD. If it is not reachable from any ref, this is the last chance | |
886 | * for the user to do so without resorting to reflog. | |
887 | */ | |
c8a3ea1f | 888 | static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit) |
8e2dc6ac | 889 | { |
8e2dc6ac | 890 | struct rev_info revs; |
c8a3ea1f | 891 | struct object *object = &old_commit->object; |
8e2dc6ac | 892 | |
2abf3503 | 893 | repo_init_revisions(the_repository, &revs, NULL); |
468224e5 RS |
894 | setup_revisions(0, NULL, &revs, NULL); |
895 | ||
896 | object->flags &= ~UNINTERESTING; | |
f2fd0760 | 897 | add_pending_object(&revs, object, oid_to_hex(&object->oid)); |
468224e5 | 898 | |
fcb615f5 | 899 | for_each_ref(add_pending_uninteresting_ref, &revs); |
c8a3ea1f | 900 | add_pending_oid(&revs, "HEAD", &new_commit->object.oid, UNINTERESTING); |
468224e5 | 901 | |
8e2dc6ac | 902 | if (prepare_revision_walk(&revs)) |
6c80cd29 | 903 | die(_("internal error in revision walk")); |
c8a3ea1f BW |
904 | if (!(old_commit->object.flags & UNINTERESTING)) |
905 | suggest_reattach(old_commit, &revs); | |
8e2dc6ac | 906 | else |
c8a3ea1f | 907 | describe_detached_head(_("Previous HEAD position was"), old_commit); |
5c08dc48 | 908 | |
b2ccdf7f | 909 | /* Clean up objects used, as they will be reused. */ |
a9a03fa0 | 910 | clear_commit_marks_all(ALL_REV_FLAGS); |
8e2dc6ac JH |
911 | } |
912 | ||
e51e3057 | 913 | static int switch_branches(const struct checkout_opts *opts, |
c8a3ea1f | 914 | struct branch_info *new_branch_info) |
782c2d65 DB |
915 | { |
916 | int ret = 0; | |
c8a3ea1f | 917 | struct branch_info old_branch_info; |
96ec7b1e | 918 | void *path_to_free; |
60af7691 | 919 | struct object_id rev; |
a2b4994c | 920 | int flag, writeout_error = 0; |
65f099b3 | 921 | int do_merge = 1; |
e27dd8ae JH |
922 | |
923 | trace2_cmd_mode("branch"); | |
924 | ||
c8a3ea1f BW |
925 | memset(&old_branch_info, 0, sizeof(old_branch_info)); |
926 | old_branch_info.path = path_to_free = resolve_refdup("HEAD", 0, &rev, &flag); | |
927 | if (old_branch_info.path) | |
21e1ee8f | 928 | old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1); |
96ec7b1e | 929 | if (!(flag & REF_ISSYMREF)) |
c8a3ea1f | 930 | old_branch_info.path = NULL; |
782c2d65 | 931 | |
c8a3ea1f BW |
932 | if (old_branch_info.path) |
933 | skip_prefix(old_branch_info.path, "refs/heads/", &old_branch_info.name); | |
782c2d65 | 934 | |
c8a3ea1f BW |
935 | if (!new_branch_info->name) { |
936 | new_branch_info->name = "HEAD"; | |
937 | new_branch_info->commit = old_branch_info.commit; | |
938 | if (!new_branch_info->commit) | |
e8a8a4d7 | 939 | die(_("You are on a branch yet to be born")); |
c8a3ea1f | 940 | parse_commit_or_die(new_branch_info->commit); |
65f099b3 NTND |
941 | |
942 | if (opts->only_merge_on_switching_branches) | |
943 | do_merge = 0; | |
782c2d65 DB |
944 | } |
945 | ||
65f099b3 | 946 | if (do_merge) { |
fa655d84 BP |
947 | ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); |
948 | if (ret) { | |
949 | free(path_to_free); | |
950 | return ret; | |
951 | } | |
96ec7b1e | 952 | } |
782c2d65 | 953 | |
c8a3ea1f BW |
954 | if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit) |
955 | orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit); | |
77ebd56d | 956 | |
c8a3ea1f | 957 | update_refs_for_switch(opts, &old_branch_info, new_branch_info); |
782c2d65 | 958 | |
c8a3ea1f | 959 | ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1); |
96ec7b1e | 960 | free(path_to_free); |
a2b4994c | 961 | return ret || writeout_error; |
782c2d65 DB |
962 | } |
963 | ||
0cf8581e JH |
964 | static int git_checkout_config(const char *var, const char *value, void *cb) |
965 | { | |
175f6e59 JS |
966 | if (!strcmp(var, "diff.ignoresubmodules")) { |
967 | struct checkout_opts *opts = cb; | |
968 | handle_ignore_submodules_arg(&opts->diff_options, value); | |
969 | return 0; | |
970 | } | |
23b4c7bc | 971 | |
59556548 | 972 | if (starts_with(var, "submodule.")) |
7463e2ec | 973 | return git_default_submodule_config(var, value, NULL); |
23b4c7bc | 974 | |
175f6e59 | 975 | return git_xmerge_config(var, value, NULL); |
0cf8581e JH |
976 | } |
977 | ||
7ab4ad00 NTND |
978 | static void setup_new_branch_info_and_source_tree( |
979 | struct branch_info *new_branch_info, | |
980 | struct checkout_opts *opts, | |
981 | struct object_id *rev, | |
982 | const char *arg) | |
983 | { | |
984 | struct tree **source_tree = &opts->source_tree; | |
985 | struct object_id branch_rev; | |
986 | ||
987 | new_branch_info->name = arg; | |
988 | setup_branch_path(new_branch_info); | |
989 | ||
990 | if (!check_refname_format(new_branch_info->path, 0) && | |
991 | !read_ref(new_branch_info->path, &branch_rev)) | |
992 | oidcpy(rev, &branch_rev); | |
993 | else | |
994 | new_branch_info->path = NULL; /* not an existing branch */ | |
995 | ||
996 | new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1); | |
997 | if (!new_branch_info->commit) { | |
998 | /* not a commit */ | |
999 | *source_tree = parse_tree_indirect(rev); | |
1000 | } else { | |
1001 | parse_commit_or_die(new_branch_info->commit); | |
1002 | *source_tree = get_commit_tree(new_branch_info->commit); | |
1003 | } | |
1004 | } | |
1005 | ||
09ebad6f JN |
1006 | static int parse_branchname_arg(int argc, const char **argv, |
1007 | int dwim_new_local_branch_ok, | |
c8a3ea1f | 1008 | struct branch_info *new_branch_info, |
10f102be | 1009 | struct checkout_opts *opts, |
3c87aa94 ÆAB |
1010 | struct object_id *rev, |
1011 | int *dwim_remotes_matched) | |
09ebad6f | 1012 | { |
10f102be | 1013 | const char **new_branch = &opts->new_branch; |
09ebad6f | 1014 | int argcount = 0; |
09ebad6f | 1015 | const char *arg; |
bca39695 MM |
1016 | int dash_dash_pos; |
1017 | int has_dash_dash = 0; | |
1018 | int i; | |
09ebad6f JN |
1019 | |
1020 | /* | |
1021 | * case 1: git checkout <ref> -- [<paths>] | |
1022 | * | |
1023 | * <ref> must be a valid tree, everything after the '--' must be | |
1024 | * a path. | |
1025 | * | |
1026 | * case 2: git checkout -- [<paths>] | |
1027 | * | |
1028 | * everything after the '--' must be paths. | |
1029 | * | |
a047fafc | 1030 | * case 3: git checkout <something> [--] |
09ebad6f | 1031 | * |
a047fafc MM |
1032 | * (a) If <something> is a commit, that is to |
1033 | * switch to the branch or detach HEAD at it. As a special case, | |
1034 | * if <something> is A...B (missing A or B means HEAD but you can | |
1035 | * omit at most one side), and if there is a unique merge base | |
1036 | * between A and B, A...B names that merge base. | |
09ebad6f | 1037 | * |
a047fafc | 1038 | * (b) If <something> is _not_ a commit, either "--" is present |
01689909 | 1039 | * or <something> is not a path, no -t or -b was given, and |
a047fafc | 1040 | * and there is a tracking branch whose name is <something> |
8d7b558b ÆAB |
1041 | * in one and only one remote (or if the branch exists on the |
1042 | * remote named in checkout.defaultRemote), then this is a | |
1043 | * short-hand to fork local <something> from that | |
1044 | * remote-tracking branch. | |
09ebad6f | 1045 | * |
a047fafc MM |
1046 | * (c) Otherwise, if "--" is present, treat it like case (1). |
1047 | * | |
1048 | * (d) Otherwise : | |
1049 | * - if it's a reference, treat it like case (1) | |
1050 | * - else if it's a path, treat it like case (2) | |
1051 | * - else: fail. | |
1052 | * | |
1053 | * case 4: git checkout <something> <paths> | |
1054 | * | |
1055 | * The first argument must not be ambiguous. | |
09ebad6f JN |
1056 | * - If it's *only* a reference, treat it like case (1). |
1057 | * - If it's only a path, treat it like case (2). | |
1058 | * - else: fail. | |
1059 | * | |
1060 | */ | |
1061 | if (!argc) | |
1062 | return 0; | |
1063 | ||
5c06e269 NTND |
1064 | if (!opts->accept_pathspec) { |
1065 | if (argc > 1) | |
1066 | die(_("only one reference expected")); | |
1067 | has_dash_dash = 1; /* helps disambiguate */ | |
1068 | } | |
1069 | ||
09ebad6f | 1070 | arg = argv[0]; |
bca39695 MM |
1071 | dash_dash_pos = -1; |
1072 | for (i = 0; i < argc; i++) { | |
5c06e269 | 1073 | if (opts->accept_pathspec && !strcmp(argv[i], "--")) { |
bca39695 MM |
1074 | dash_dash_pos = i; |
1075 | break; | |
1076 | } | |
1077 | } | |
1078 | if (dash_dash_pos == 0) | |
1079 | return 1; /* case (2) */ | |
1080 | else if (dash_dash_pos == 1) | |
1081 | has_dash_dash = 1; /* case (3) or (1) */ | |
1082 | else if (dash_dash_pos >= 2) | |
1083 | die(_("only one reference expected, %d given."), dash_dash_pos); | |
0f086e6d | 1084 | opts->count_checkout_paths = !opts->quiet && !has_dash_dash; |
09ebad6f JN |
1085 | |
1086 | if (!strcmp(arg, "-")) | |
1087 | arg = "@{-1}"; | |
1088 | ||
151b2911 | 1089 | if (get_oid_mb(arg, rev)) { |
a047fafc MM |
1090 | /* |
1091 | * Either case (3) or (4), with <something> not being | |
1092 | * a commit, or an attempt to use case (1) with an | |
1093 | * invalid ref. | |
1094 | * | |
1095 | * It's likely an error, but we need to find out if | |
1096 | * we should auto-create the branch, case (3).(b). | |
1097 | */ | |
1098 | int recover_with_dwim = dwim_new_local_branch_ok; | |
1099 | ||
be4908f1 NTND |
1100 | int could_be_checkout_paths = !has_dash_dash && |
1101 | check_filename(opts->prefix, arg); | |
1102 | ||
1103 | if (!has_dash_dash && !no_wildcard(arg)) | |
a047fafc | 1104 | recover_with_dwim = 0; |
be4908f1 | 1105 | |
a047fafc | 1106 | /* |
5c06e269 NTND |
1107 | * Accept "git checkout foo", "git checkout foo --" |
1108 | * and "git switch foo" as candidates for dwim. | |
a047fafc MM |
1109 | */ |
1110 | if (!(argc == 1 && !has_dash_dash) && | |
5c06e269 NTND |
1111 | !(argc == 2 && has_dash_dash) && |
1112 | opts->accept_pathspec) | |
a047fafc MM |
1113 | recover_with_dwim = 0; |
1114 | ||
1115 | if (recover_with_dwim) { | |
3c87aa94 ÆAB |
1116 | const char *remote = unique_tracking_name(arg, rev, |
1117 | dwim_remotes_matched); | |
a047fafc | 1118 | if (remote) { |
be4908f1 NTND |
1119 | if (could_be_checkout_paths) |
1120 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
1121 | "Please use -- (and optionally --no-guess) to disambiguate"), | |
1122 | arg); | |
a047fafc MM |
1123 | *new_branch = arg; |
1124 | arg = remote; | |
1125 | /* DWIMmed to create local branch, case (3).(b) */ | |
1126 | } else { | |
1127 | recover_with_dwim = 0; | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | if (!recover_with_dwim) { | |
1132 | if (has_dash_dash) | |
1133 | die(_("invalid reference: %s"), arg); | |
09ebad6f JN |
1134 | return argcount; |
1135 | } | |
1136 | } | |
1137 | ||
1138 | /* we can't end up being in (2) anymore, eat the argument */ | |
1139 | argcount++; | |
1140 | argv++; | |
1141 | argc--; | |
1142 | ||
7ab4ad00 | 1143 | setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg); |
09ebad6f | 1144 | |
7ab4ad00 | 1145 | if (!opts->source_tree) /* case (1): want a tree */ |
6c80cd29 | 1146 | die(_("reference is not a tree: %s"), arg); |
7ab4ad00 | 1147 | |
7f82b24e | 1148 | if (!has_dash_dash) { /* case (3).(d) -> (1) */ |
09ebad6f JN |
1149 | /* |
1150 | * Do not complain the most common case | |
1151 | * git checkout branch | |
1152 | * even if there happen to be a file called 'branch'; | |
1153 | * it would be extremely annoying. | |
1154 | */ | |
1155 | if (argc) | |
b829b943 | 1156 | verify_non_filename(opts->prefix, arg); |
5c06e269 | 1157 | } else if (opts->accept_pathspec) { |
09ebad6f JN |
1158 | argcount++; |
1159 | argv++; | |
1160 | argc--; | |
1161 | } | |
1162 | ||
1163 | return argcount; | |
1164 | } | |
1165 | ||
a2b4994c | 1166 | static int switch_unborn_to_new_branch(const struct checkout_opts *opts) |
abe19980 JH |
1167 | { |
1168 | int status; | |
1169 | struct strbuf branch_ref = STRBUF_INIT; | |
1170 | ||
e27dd8ae JH |
1171 | trace2_cmd_mode("unborn"); |
1172 | ||
8ced1aa0 CW |
1173 | if (!opts->new_branch) |
1174 | die(_("You are on a branch yet to be born")); | |
abe19980 JH |
1175 | strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); |
1176 | status = create_symref("HEAD", branch_ref.buf, "checkout -b"); | |
1177 | strbuf_release(&branch_ref); | |
afa8c07a JK |
1178 | if (!opts->quiet) |
1179 | fprintf(stderr, _("Switched to a new branch '%s'\n"), | |
1180 | opts->new_branch); | |
abe19980 JH |
1181 | return status; |
1182 | } | |
1183 | ||
7968bef0 NTND |
1184 | static void die_expecting_a_branch(const struct branch_info *branch_info) |
1185 | { | |
1186 | struct object_id oid; | |
1187 | char *to_free; | |
1188 | ||
1189 | if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free) == 1) { | |
1190 | const char *ref = to_free; | |
1191 | ||
1192 | if (skip_prefix(ref, "refs/tags/", &ref)) | |
1193 | die(_("a branch is expected, got tag '%s'"), ref); | |
1194 | if (skip_prefix(ref, "refs/remotes/", &ref)) | |
1195 | die(_("a branch is expected, got remote branch '%s'"), ref); | |
1196 | die(_("a branch is expected, got '%s'"), ref); | |
1197 | } | |
1198 | if (branch_info->commit) | |
1199 | die(_("a branch is expected, got commit '%s'"), branch_info->name); | |
1200 | /* | |
1201 | * This case should never happen because we already die() on | |
1202 | * non-commit, but just in case. | |
1203 | */ | |
1204 | die(_("a branch is expected, got '%s'"), branch_info->name); | |
1205 | } | |
1206 | ||
c45f0f52 NTND |
1207 | static void die_if_some_operation_in_progress(void) |
1208 | { | |
1209 | struct wt_status_state state; | |
1210 | ||
1211 | memset(&state, 0, sizeof(state)); | |
1212 | wt_status_get_state(the_repository, &state, 0); | |
1213 | ||
1214 | if (state.merge_in_progress) | |
1215 | die(_("cannot switch branch while merging\n" | |
1216 | "Consider \"git merge --quit\" " | |
1217 | "or \"git worktree add\".")); | |
1218 | if (state.am_in_progress) | |
1219 | die(_("cannot switch branch in the middle of an am session\n" | |
1220 | "Consider \"git am --quit\" " | |
1221 | "or \"git worktree add\".")); | |
1222 | if (state.rebase_interactive_in_progress || state.rebase_in_progress) | |
1223 | die(_("cannot switch branch while rebasing\n" | |
1224 | "Consider \"git rebase --quit\" " | |
1225 | "or \"git worktree add\".")); | |
1226 | if (state.cherry_pick_in_progress) | |
1227 | die(_("cannot switch branch while cherry-picking\n" | |
1228 | "Consider \"git cherry-pick --quit\" " | |
1229 | "or \"git worktree add\".")); | |
1230 | if (state.revert_in_progress) | |
1231 | die(_("cannot switch branch while reverting\n" | |
1232 | "Consider \"git revert --quit\" " | |
1233 | "or \"git worktree add\".")); | |
1234 | if (state.bisect_in_progress) | |
1235 | die(_("cannot switch branch while bisecting\n" | |
1236 | "Consider \"git bisect reset HEAD\" " | |
1237 | "or \"git worktree add\".")); | |
1238 | } | |
1239 | ||
b6312c27 | 1240 | static int checkout_branch(struct checkout_opts *opts, |
c8a3ea1f | 1241 | struct branch_info *new_branch_info) |
b6312c27 | 1242 | { |
817b345a | 1243 | if (opts->pathspec.nr) |
b6312c27 NTND |
1244 | die(_("paths cannot be used with switching branches")); |
1245 | ||
1246 | if (opts->patch_mode) | |
1247 | die(_("'%s' cannot be used with switching branches"), | |
1248 | "--patch"); | |
1249 | ||
091e04bc TG |
1250 | if (!opts->overlay_mode) |
1251 | die(_("'%s' cannot be used with switching branches"), | |
1252 | "--no-overlay"); | |
1253 | ||
b6312c27 NTND |
1254 | if (opts->writeout_stage) |
1255 | die(_("'%s' cannot be used with switching branches"), | |
1256 | "--ours/--theirs"); | |
1257 | ||
1258 | if (opts->force && opts->merge) | |
1259 | die(_("'%s' cannot be used with '%s'"), "-f", "-m"); | |
1260 | ||
3ec37ad1 NTND |
1261 | if (opts->discard_changes && opts->merge) |
1262 | die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge"); | |
1263 | ||
b6312c27 NTND |
1264 | if (opts->force_detach && opts->new_branch) |
1265 | die(_("'%s' cannot be used with '%s'"), | |
1266 | "--detach", "-b/-B/--orphan"); | |
1267 | ||
1268 | if (opts->new_orphan_branch) { | |
1269 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1270 | die(_("'%s' cannot be used with '%s'"), "--orphan", "-t"); | |
1271 | } else if (opts->force_detach) { | |
1272 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) | |
1273 | die(_("'%s' cannot be used with '%s'"), "--detach", "-t"); | |
1274 | } else if (opts->track == BRANCH_TRACK_UNSPECIFIED) | |
1275 | opts->track = git_branch_track; | |
1276 | ||
c8a3ea1f | 1277 | if (new_branch_info->name && !new_branch_info->commit) |
b6312c27 | 1278 | die(_("Cannot switch branch to a non-commit '%s'"), |
c8a3ea1f | 1279 | new_branch_info->name); |
b6312c27 | 1280 | |
e342c63a NTND |
1281 | if (!opts->switch_branch_doing_nothing_is_ok && |
1282 | !new_branch_info->name && | |
1283 | !opts->new_branch && | |
1284 | !opts->force_detach) | |
1285 | die(_("missing branch or commit argument")); | |
1286 | ||
7968bef0 NTND |
1287 | if (!opts->implicit_detach && |
1288 | !opts->force_detach && | |
1289 | !opts->new_branch && | |
1290 | !opts->new_branch_force && | |
1291 | new_branch_info->name && | |
1292 | !new_branch_info->path) | |
1293 | die_expecting_a_branch(new_branch_info); | |
1294 | ||
c45f0f52 NTND |
1295 | if (!opts->can_switch_when_in_progress) |
1296 | die_if_some_operation_in_progress(); | |
1297 | ||
c8a3ea1f | 1298 | if (new_branch_info->path && !opts->force_detach && !opts->new_branch && |
c265c533 | 1299 | !opts->ignore_other_worktrees) { |
e1c1ab9d | 1300 | int flag; |
efbd4fdf | 1301 | char *head_ref = resolve_refdup("HEAD", 0, NULL, &flag); |
e1c1ab9d | 1302 | if (head_ref && |
c8a3ea1f BW |
1303 | (!(flag & REF_ISSYMREF) || strcmp(head_ref, new_branch_info->path))) |
1304 | die_if_checked_out(new_branch_info->path, 1); | |
e1c1ab9d NTND |
1305 | free(head_ref); |
1306 | } | |
1307 | ||
c8a3ea1f | 1308 | if (!new_branch_info->commit && opts->new_branch) { |
60af7691 | 1309 | struct object_id rev; |
b6312c27 NTND |
1310 | int flag; |
1311 | ||
34c290a6 | 1312 | if (!read_ref_full("HEAD", 0, &rev, &flag) && |
60af7691 | 1313 | (flag & REF_ISSYMREF) && is_null_oid(&rev)) |
b6312c27 NTND |
1314 | return switch_unborn_to_new_branch(opts); |
1315 | } | |
c8a3ea1f | 1316 | return switch_branches(opts, new_branch_info); |
b6312c27 NTND |
1317 | } |
1318 | ||
20871822 NTND |
1319 | static struct option *add_common_options(struct checkout_opts *opts, |
1320 | struct option *prevopts) | |
782c2d65 | 1321 | { |
782c2d65 | 1322 | struct option options[] = { |
b3edccb9 | 1323 | OPT__QUIET(&opts->quiet, N_("suppress progress reporting")), |
20871822 NTND |
1324 | { OPTION_CALLBACK, 0, "recurse-submodules", NULL, |
1325 | "checkout", "control recursive updating of submodules", | |
1326 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater }, | |
1327 | OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")), | |
1328 | OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), | |
1329 | PARSE_OPT_NOCOMPLETE), | |
1330 | OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")), | |
1331 | OPT_STRING(0, "conflict", &opts->conflict_style, N_("style"), | |
1332 | N_("conflict style (merge or diff3)")), | |
1333 | OPT_END() | |
1334 | }; | |
1335 | struct option *newopts = parse_options_concat(prevopts, options); | |
1336 | free(prevopts); | |
1337 | return newopts; | |
1338 | } | |
1339 | ||
b7b5fce2 NTND |
1340 | static struct option *add_common_switch_branch_options( |
1341 | struct checkout_opts *opts, struct option *prevopts) | |
20871822 NTND |
1342 | { |
1343 | struct option options[] = { | |
163e3b29 | 1344 | OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), |
b3edccb9 | 1345 | OPT_SET_INT('t', "track", &opts->track, N_("set upstream info for new branch"), |
9ed36cfa | 1346 | BRANCH_TRACK_EXPLICIT), |
b3edccb9 | 1347 | OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")), |
20871822 NTND |
1348 | OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore, |
1349 | N_("update ignored files (default)"), | |
1350 | PARSE_OPT_NOCOMPLETE), | |
20871822 NTND |
1351 | OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees, |
1352 | N_("do not check if another worktree is holding the given ref")), | |
1353 | OPT_END() | |
1354 | }; | |
1355 | struct option *newopts = parse_options_concat(prevopts, options); | |
1356 | free(prevopts); | |
1357 | return newopts; | |
1358 | } | |
1359 | ||
1360 | static struct option *add_checkout_path_options(struct checkout_opts *opts, | |
1361 | struct option *prevopts) | |
1362 | { | |
1363 | struct option options[] = { | |
b3edccb9 | 1364 | OPT_SET_INT_F('2', "ours", &opts->writeout_stage, |
3fe735e7 NTND |
1365 | N_("checkout our version for unmerged files"), |
1366 | 2, PARSE_OPT_NONEG), | |
b3edccb9 | 1367 | OPT_SET_INT_F('3', "theirs", &opts->writeout_stage, |
3fe735e7 NTND |
1368 | N_("checkout their version for unmerged files"), |
1369 | 3, PARSE_OPT_NONEG), | |
b3edccb9 NTND |
1370 | OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")), |
1371 | OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree, | |
08d595dc | 1372 | N_("do not limit pathspecs to sparse entries only")), |
b3edccb9 | 1373 | OPT_BOOL(0, "overlay", &opts->overlay_mode, N_("use overlay mode (default)")), |
20871822 | 1374 | OPT_END() |
782c2d65 | 1375 | }; |
20871822 NTND |
1376 | struct option *newopts = parse_options_concat(prevopts, options); |
1377 | free(prevopts); | |
1378 | return newopts; | |
1379 | } | |
1380 | ||
d787d311 NTND |
1381 | static int checkout_main(int argc, const char **argv, const char *prefix, |
1382 | struct checkout_opts *opts, struct option *options, | |
1383 | const char * const usagestr[]) | |
20871822 | 1384 | { |
20871822 NTND |
1385 | struct branch_info new_branch_info; |
1386 | int dwim_remotes_matched = 0; | |
782c2d65 | 1387 | |
c8a3ea1f | 1388 | memset(&new_branch_info, 0, sizeof(new_branch_info)); |
b3edccb9 NTND |
1389 | opts->overwrite_ignore = 1; |
1390 | opts->prefix = prefix; | |
1391 | opts->show_progress = -1; | |
1392 | opts->overlay_mode = -1; | |
782c2d65 | 1393 | |
b3edccb9 | 1394 | git_config(git_checkout_config, opts); |
782c2d65 | 1395 | |
b3edccb9 | 1396 | opts->track = BRANCH_TRACK_UNSPECIFIED; |
782c2d65 | 1397 | |
d787d311 | 1398 | argc = parse_options(argc, argv, prefix, options, usagestr, |
f5242ebf | 1399 | PARSE_OPT_KEEP_DASHDASH); |
859fdaba | 1400 | |
b3edccb9 NTND |
1401 | if (opts->show_progress < 0) { |
1402 | if (opts->quiet) | |
1403 | opts->show_progress = 0; | |
870ebdb9 | 1404 | else |
b3edccb9 | 1405 | opts->show_progress = isatty(2); |
870ebdb9 ECA |
1406 | } |
1407 | ||
55cf704a | 1408 | if (opts->conflict_style) { |
b3edccb9 | 1409 | opts->merge = 1; /* implied */ |
55cf704a | 1410 | git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL); |
b6312c27 | 1411 | } |
3ec37ad1 NTND |
1412 | if (opts->force) |
1413 | opts->discard_changes = 1; | |
02ac9837 | 1414 | |
b3edccb9 | 1415 | if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1) |
b6312c27 | 1416 | die(_("-b, -B and --orphan are mutually exclusive")); |
02ac9837 | 1417 | |
b3edccb9 | 1418 | if (opts->overlay_mode == 1 && opts->patch_mode) |
091e04bc TG |
1419 | die(_("-p and --overlay are mutually exclusive")); |
1420 | ||
b6312c27 NTND |
1421 | /* |
1422 | * From here on, new_branch will contain the branch to be checked out, | |
1423 | * and new_branch_force and new_orphan_branch will tell us which one of | |
1424 | * -b/-B/--orphan is being used. | |
1425 | */ | |
b3edccb9 NTND |
1426 | if (opts->new_branch_force) |
1427 | opts->new_branch = opts->new_branch_force; | |
02ac9837 | 1428 | |
b3edccb9 NTND |
1429 | if (opts->new_orphan_branch) |
1430 | opts->new_branch = opts->new_orphan_branch; | |
32669671 | 1431 | |
b6312c27 | 1432 | /* --track without -b/-B/--orphan should DWIM */ |
b3edccb9 | 1433 | if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { |
9188ed89 AR |
1434 | const char *argv0 = argv[0]; |
1435 | if (!argc || !strcmp(argv0, "--")) | |
1a07e59c | 1436 | die(_("--track needs a branch name")); |
e3f1da98 RS |
1437 | skip_prefix(argv0, "refs/", &argv0); |
1438 | skip_prefix(argv0, "remotes/", &argv0); | |
9188ed89 AR |
1439 | argv0 = strchr(argv0, '/'); |
1440 | if (!argv0 || !argv0[1]) | |
1a07e59c | 1441 | die(_("missing branch name; try -b")); |
b3edccb9 | 1442 | opts->new_branch = argv0 + 1; |
bb0ceb62 JS |
1443 | } |
1444 | ||
859fdaba | 1445 | /* |
09ebad6f JN |
1446 | * Extract branch name from command line arguments, so |
1447 | * all that is left is pathspecs. | |
859fdaba | 1448 | * |
09ebad6f | 1449 | * Handle |
70c9ac2f | 1450 | * |
09ebad6f JN |
1451 | * 1) git checkout <tree> -- [<paths>] |
1452 | * 2) git checkout -- [<paths>] | |
1453 | * 3) git checkout <something> [<paths>] | |
859fdaba | 1454 | * |
09ebad6f JN |
1455 | * including "last branch" syntax and DWIM-ery for names of |
1456 | * remote branches, erroring out for invalid or ambiguous cases. | |
859fdaba | 1457 | */ |
782c2d65 | 1458 | if (argc) { |
60af7691 | 1459 | struct object_id rev; |
09ebad6f | 1460 | int dwim_ok = |
b3edccb9 | 1461 | !opts->patch_mode && |
ccb111b3 | 1462 | opts->dwim_new_local_branch && |
b3edccb9 NTND |
1463 | opts->track == BRANCH_TRACK_UNSPECIFIED && |
1464 | !opts->new_branch; | |
f8bd36a4 | 1465 | int n = parse_branchname_arg(argc, argv, dwim_ok, |
b3edccb9 | 1466 | &new_branch_info, opts, &rev, |
3c87aa94 | 1467 | &dwim_remotes_matched); |
09ebad6f JN |
1468 | argv += n; |
1469 | argc -= n; | |
782c2d65 DB |
1470 | } |
1471 | ||
782c2d65 | 1472 | if (argc) { |
b3edccb9 NTND |
1473 | parse_pathspec(&opts->pathspec, 0, |
1474 | opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0, | |
480ca644 | 1475 | prefix, argv); |
301e42ed | 1476 | |
b3edccb9 | 1477 | if (!opts->pathspec.nr) |
e8a8a4d7 | 1478 | die(_("invalid path specification")); |
301e42ed | 1479 | |
b6312c27 NTND |
1480 | /* |
1481 | * Try to give more helpful suggestion. | |
1482 | * new_branch && argc > 1 will be caught later. | |
1483 | */ | |
b3edccb9 | 1484 | if (opts->new_branch && argc == 1) |
6c486862 | 1485 | die(_("'%s' is not a commit and a branch '%s' cannot be created from it"), |
b3edccb9 | 1486 | argv[0], opts->new_branch); |
782c2d65 | 1487 | |
b3edccb9 | 1488 | if (opts->force_detach) |
b6312c27 NTND |
1489 | die(_("git checkout: --detach does not take a path argument '%s'"), |
1490 | argv[0]); | |
32669671 | 1491 | |
b3edccb9 | 1492 | if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) |
b6312c27 NTND |
1493 | die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" |
1494 | "checking out of the index.")); | |
782c2d65 DB |
1495 | } |
1496 | ||
b3edccb9 | 1497 | if (opts->new_branch) { |
f285a2d7 | 1498 | struct strbuf buf = STRBUF_INIT; |
55c4a673 | 1499 | |
b3edccb9 NTND |
1500 | if (opts->new_branch_force) |
1501 | opts->branch_exists = validate_branchname(opts->new_branch, &buf); | |
bc1c9c0e | 1502 | else |
b3edccb9 NTND |
1503 | opts->branch_exists = |
1504 | validate_new_branchname(opts->new_branch, &buf, 0); | |
352eadc4 DB |
1505 | strbuf_release(&buf); |
1506 | } | |
1507 | ||
886e1084 | 1508 | UNLEAK(opts); |
b3edccb9 NTND |
1509 | if (opts->patch_mode || opts->pathspec.nr) { |
1510 | int ret = checkout_paths(opts, new_branch_info.name); | |
ad8d5104 ÆAB |
1511 | if (ret && dwim_remotes_matched > 1 && |
1512 | advice_checkout_ambiguous_remote_branch_name) | |
1513 | advise(_("'%s' matched more than one remote tracking branch.\n" | |
1514 | "We found %d remotes with a reference that matched. So we fell back\n" | |
1515 | "on trying to resolve the argument as a path, but failed there too!\n" | |
1516 | "\n" | |
1517 | "If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
1518 | "you can do so by fully qualifying the name with the --track option:\n" | |
1519 | "\n" | |
8d7b558b ÆAB |
1520 | " git checkout --track origin/<name>\n" |
1521 | "\n" | |
1522 | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
1523 | "one remote, e.g. the 'origin' remote, consider setting\n" | |
1524 | "checkout.defaultRemote=origin in your config."), | |
ad8d5104 ÆAB |
1525 | argv[0], |
1526 | dwim_remotes_matched); | |
1c550553 ÆAB |
1527 | return ret; |
1528 | } else { | |
b3edccb9 | 1529 | return checkout_branch(opts, &new_branch_info); |
1c550553 | 1530 | } |
782c2d65 | 1531 | } |
d787d311 NTND |
1532 | |
1533 | int cmd_checkout(int argc, const char **argv, const char *prefix) | |
1534 | { | |
1535 | struct checkout_opts opts; | |
b7b5fce2 NTND |
1536 | struct option *options; |
1537 | struct option checkout_options[] = { | |
1538 | OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), | |
1539 | N_("create and checkout a new branch")), | |
1540 | OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), | |
1541 | N_("create/reset and checkout a branch")), | |
492ededc | 1542 | OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), |
ccb111b3 NTND |
1543 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, |
1544 | N_("second guess 'git checkout <no-such-branch>' (default)")), | |
b7b5fce2 NTND |
1545 | OPT_END() |
1546 | }; | |
d787d311 NTND |
1547 | int ret; |
1548 | ||
1549 | memset(&opts, 0, sizeof(opts)); | |
ccb111b3 | 1550 | opts.dwim_new_local_branch = 1; |
e342c63a | 1551 | opts.switch_branch_doing_nothing_is_ok = 1; |
65f099b3 | 1552 | opts.only_merge_on_switching_branches = 0; |
5c06e269 | 1553 | opts.accept_pathspec = 1; |
7968bef0 | 1554 | opts.implicit_detach = 1; |
c45f0f52 | 1555 | opts.can_switch_when_in_progress = 1; |
d787d311 | 1556 | |
b7b5fce2 | 1557 | options = parse_options_dup(checkout_options); |
d787d311 | 1558 | options = add_common_options(&opts, options); |
b7b5fce2 | 1559 | options = add_common_switch_branch_options(&opts, options); |
d787d311 NTND |
1560 | options = add_checkout_path_options(&opts, options); |
1561 | ||
1562 | ret = checkout_main(argc, argv, prefix, &opts, | |
1563 | options, checkout_usage); | |
1564 | FREE_AND_NULL(options); | |
1565 | return ret; | |
1566 | } | |
1567 | ||
1568 | int cmd_switch(int argc, const char **argv, const char *prefix) | |
1569 | { | |
1570 | struct checkout_opts opts; | |
1571 | struct option *options = NULL; | |
b7b5fce2 NTND |
1572 | struct option switch_options[] = { |
1573 | OPT_STRING('c', "create", &opts.new_branch, N_("branch"), | |
1574 | N_("create and switch to a new branch")), | |
1575 | OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"), | |
1576 | N_("create/reset and switch to a branch")), | |
ccb111b3 NTND |
1577 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, |
1578 | N_("second guess 'git switch <no-such-branch>'")), | |
3ec37ad1 NTND |
1579 | OPT_BOOL(0, "discard-changes", &opts.discard_changes, |
1580 | N_("throw away local modifications")), | |
b7b5fce2 NTND |
1581 | OPT_END() |
1582 | }; | |
d787d311 NTND |
1583 | int ret; |
1584 | ||
1585 | memset(&opts, 0, sizeof(opts)); | |
ccb111b3 | 1586 | opts.dwim_new_local_branch = 1; |
5c06e269 | 1587 | opts.accept_pathspec = 0; |
e342c63a | 1588 | opts.switch_branch_doing_nothing_is_ok = 0; |
65f099b3 | 1589 | opts.only_merge_on_switching_branches = 1; |
7968bef0 | 1590 | opts.implicit_detach = 0; |
c45f0f52 | 1591 | opts.can_switch_when_in_progress = 0; |
d787d311 | 1592 | |
b7b5fce2 | 1593 | options = parse_options_dup(switch_options); |
d787d311 | 1594 | options = add_common_options(&opts, options); |
b7b5fce2 | 1595 | options = add_common_switch_branch_options(&opts, options); |
d787d311 NTND |
1596 | |
1597 | ret = checkout_main(argc, argv, prefix, &opts, | |
1598 | options, switch_branch_usage); | |
1599 | FREE_AND_NULL(options); | |
1600 | return ret; | |
1601 | } |