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