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