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