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