]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "git reset" builtin command | |
3 | * | |
4 | * Copyright (c) 2007 Carlos Rica | |
5 | * | |
6 | * Based on git-reset.sh, which is | |
7 | * | |
8 | * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano | |
9 | */ | |
10 | #define USE_THE_INDEX_VARIABLE | |
11 | #include "builtin.h" | |
12 | #include "advice.h" | |
13 | #include "config.h" | |
14 | #include "environment.h" | |
15 | #include "gettext.h" | |
16 | #include "hex.h" | |
17 | #include "lockfile.h" | |
18 | #include "tag.h" | |
19 | #include "object.h" | |
20 | #include "pretty.h" | |
21 | #include "run-command.h" | |
22 | #include "refs.h" | |
23 | #include "diff.h" | |
24 | #include "diffcore.h" | |
25 | #include "tree.h" | |
26 | #include "branch.h" | |
27 | #include "parse-options.h" | |
28 | #include "unpack-trees.h" | |
29 | #include "cache-tree.h" | |
30 | #include "setup.h" | |
31 | #include "submodule.h" | |
32 | #include "submodule-config.h" | |
33 | #include "trace.h" | |
34 | #include "trace2.h" | |
35 | #include "dir.h" | |
36 | #include "add-interactive.h" | |
37 | ||
38 | #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000) | |
39 | ||
40 | static const char * const git_reset_usage[] = { | |
41 | N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"), | |
42 | N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."), | |
43 | N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"), | |
44 | N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"), | |
45 | NULL | |
46 | }; | |
47 | ||
48 | enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE }; | |
49 | static const char *reset_type_names[] = { | |
50 | N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL | |
51 | }; | |
52 | ||
53 | static inline int is_merge(void) | |
54 | { | |
55 | return !access(git_path_merge_head(the_repository), F_OK); | |
56 | } | |
57 | ||
58 | static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet) | |
59 | { | |
60 | int i, nr = 0; | |
61 | struct tree_desc desc[2]; | |
62 | struct tree *tree; | |
63 | struct unpack_trees_options opts; | |
64 | int ret = -1; | |
65 | ||
66 | memset(&opts, 0, sizeof(opts)); | |
67 | opts.head_idx = 1; | |
68 | opts.src_index = &the_index; | |
69 | opts.dst_index = &the_index; | |
70 | opts.fn = oneway_merge; | |
71 | opts.merge = 1; | |
72 | init_checkout_metadata(&opts.meta, ref, oid, NULL); | |
73 | if (!quiet) | |
74 | opts.verbose_update = 1; | |
75 | switch (reset_type) { | |
76 | case KEEP: | |
77 | case MERGE: | |
78 | opts.update = 1; | |
79 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ | |
80 | break; | |
81 | case HARD: | |
82 | opts.update = 1; | |
83 | opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED; | |
84 | opts.skip_cache_tree_update = 1; | |
85 | break; | |
86 | case MIXED: | |
87 | opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; | |
88 | opts.skip_cache_tree_update = 1; | |
89 | /* but opts.update=0, so working tree not updated */ | |
90 | break; | |
91 | default: | |
92 | BUG("invalid reset_type passed to reset_index"); | |
93 | } | |
94 | ||
95 | repo_read_index_unmerged(the_repository); | |
96 | ||
97 | if (reset_type == KEEP) { | |
98 | struct object_id head_oid; | |
99 | if (repo_get_oid(the_repository, "HEAD", &head_oid)) | |
100 | return error(_("You do not have a valid HEAD.")); | |
101 | if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid)) | |
102 | return error(_("Failed to find tree of HEAD.")); | |
103 | nr++; | |
104 | opts.fn = twoway_merge; | |
105 | } | |
106 | ||
107 | if (!fill_tree_descriptor(the_repository, desc + nr, oid)) { | |
108 | error(_("Failed to find tree of %s."), oid_to_hex(oid)); | |
109 | goto out; | |
110 | } | |
111 | nr++; | |
112 | ||
113 | if (unpack_trees(nr, desc, &opts)) | |
114 | goto out; | |
115 | ||
116 | if (reset_type == MIXED || reset_type == HARD) { | |
117 | tree = parse_tree_indirect(oid); | |
118 | prime_cache_tree(the_repository, the_repository->index, tree); | |
119 | } | |
120 | ||
121 | ret = 0; | |
122 | ||
123 | out: | |
124 | for (i = 0; i < nr; i++) | |
125 | free((void *)desc[i].buffer); | |
126 | return ret; | |
127 | } | |
128 | ||
129 | static void print_new_head_line(struct commit *commit) | |
130 | { | |
131 | struct strbuf buf = STRBUF_INIT; | |
132 | ||
133 | printf(_("HEAD is now at %s"), | |
134 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); | |
135 | ||
136 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); | |
137 | if (buf.len > 0) | |
138 | printf(" %s", buf.buf); | |
139 | putchar('\n'); | |
140 | strbuf_release(&buf); | |
141 | } | |
142 | ||
143 | static void update_index_from_diff(struct diff_queue_struct *q, | |
144 | struct diff_options *opt UNUSED, | |
145 | void *data) | |
146 | { | |
147 | int i; | |
148 | int intent_to_add = *(int *)data; | |
149 | ||
150 | for (i = 0; i < q->nr; i++) { | |
151 | int pos; | |
152 | struct diff_filespec *one = q->queue[i]->one; | |
153 | int is_in_reset_tree = one->mode && !is_null_oid(&one->oid); | |
154 | struct cache_entry *ce; | |
155 | ||
156 | if (!is_in_reset_tree && !intent_to_add) { | |
157 | remove_file_from_index(&the_index, one->path); | |
158 | continue; | |
159 | } | |
160 | ||
161 | ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path, | |
162 | 0, 0); | |
163 | ||
164 | /* | |
165 | * If the file 1) corresponds to an existing index entry with | |
166 | * skip-worktree set, or 2) does not exist in the index but is | |
167 | * outside the sparse checkout definition, add a skip-worktree bit | |
168 | * to the new index entry. Note that a sparse index will be expanded | |
169 | * if this entry is outside the sparse cone - this is necessary | |
170 | * to properly construct the reset sparse directory. | |
171 | */ | |
172 | pos = index_name_pos(&the_index, one->path, strlen(one->path)); | |
173 | if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) || | |
174 | (pos < 0 && !path_in_sparse_checkout(one->path, &the_index))) | |
175 | ce->ce_flags |= CE_SKIP_WORKTREE; | |
176 | ||
177 | if (!ce) | |
178 | die(_("make_cache_entry failed for path '%s'"), | |
179 | one->path); | |
180 | if (!is_in_reset_tree) { | |
181 | ce->ce_flags |= CE_INTENT_TO_ADD; | |
182 | set_object_name_for_intent_to_add_entry(ce); | |
183 | } | |
184 | add_index_entry(&the_index, ce, | |
185 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
186 | } | |
187 | } | |
188 | ||
189 | static int read_from_tree(const struct pathspec *pathspec, | |
190 | struct object_id *tree_oid, | |
191 | int intent_to_add) | |
192 | { | |
193 | struct diff_options opt; | |
194 | ||
195 | memset(&opt, 0, sizeof(opt)); | |
196 | copy_pathspec(&opt.pathspec, pathspec); | |
197 | opt.output_format = DIFF_FORMAT_CALLBACK; | |
198 | opt.format_callback = update_index_from_diff; | |
199 | opt.format_callback_data = &intent_to_add; | |
200 | opt.flags.override_submodule_config = 1; | |
201 | opt.flags.recursive = 1; | |
202 | opt.repo = the_repository; | |
203 | opt.change = diff_change; | |
204 | opt.add_remove = diff_addremove; | |
205 | ||
206 | if (pathspec->nr && pathspec_needs_expanded_index(&the_index, pathspec)) | |
207 | ensure_full_index(&the_index); | |
208 | ||
209 | if (do_diff_cache(tree_oid, &opt)) | |
210 | return 1; | |
211 | diffcore_std(&opt); | |
212 | diff_flush(&opt); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | static void set_reflog_message(struct strbuf *sb, const char *action, | |
218 | const char *rev) | |
219 | { | |
220 | const char *rla = getenv("GIT_REFLOG_ACTION"); | |
221 | ||
222 | strbuf_reset(sb); | |
223 | if (rla) | |
224 | strbuf_addf(sb, "%s: %s", rla, action); | |
225 | else if (rev) | |
226 | strbuf_addf(sb, "reset: moving to %s", rev); | |
227 | else | |
228 | strbuf_addf(sb, "reset: %s", action); | |
229 | } | |
230 | ||
231 | static void die_if_unmerged_cache(int reset_type) | |
232 | { | |
233 | if (is_merge() || unmerged_index(&the_index)) | |
234 | die(_("Cannot do a %s reset in the middle of a merge."), | |
235 | _(reset_type_names[reset_type])); | |
236 | ||
237 | } | |
238 | ||
239 | static void parse_args(struct pathspec *pathspec, | |
240 | const char **argv, const char *prefix, | |
241 | int patch_mode, | |
242 | const char **rev_ret) | |
243 | { | |
244 | const char *rev = "HEAD"; | |
245 | struct object_id unused; | |
246 | /* | |
247 | * Possible arguments are: | |
248 | * | |
249 | * git reset [-opts] [<rev>] | |
250 | * git reset [-opts] <tree> [<paths>...] | |
251 | * git reset [-opts] <tree> -- [<paths>...] | |
252 | * git reset [-opts] -- [<paths>...] | |
253 | * git reset [-opts] <paths>... | |
254 | * | |
255 | * At this point, argv points immediately after [-opts]. | |
256 | */ | |
257 | ||
258 | if (argv[0]) { | |
259 | if (!strcmp(argv[0], "--")) { | |
260 | argv++; /* reset to HEAD, possibly with paths */ | |
261 | } else if (argv[1] && !strcmp(argv[1], "--")) { | |
262 | rev = argv[0]; | |
263 | argv += 2; | |
264 | } | |
265 | /* | |
266 | * Otherwise, argv[0] could be either <rev> or <paths> and | |
267 | * has to be unambiguous. If there is a single argument, it | |
268 | * can not be a tree | |
269 | */ | |
270 | else if ((!argv[1] && !repo_get_oid_committish(the_repository, argv[0], &unused)) || | |
271 | (argv[1] && !repo_get_oid_treeish(the_repository, argv[0], &unused))) { | |
272 | /* | |
273 | * Ok, argv[0] looks like a commit/tree; it should not | |
274 | * be a filename. | |
275 | */ | |
276 | verify_non_filename(prefix, argv[0]); | |
277 | rev = *argv++; | |
278 | } else { | |
279 | /* Otherwise we treat this as a filename */ | |
280 | verify_filename(prefix, argv[0], 1); | |
281 | } | |
282 | } | |
283 | *rev_ret = rev; | |
284 | ||
285 | parse_pathspec(pathspec, 0, | |
286 | PATHSPEC_PREFER_FULL | | |
287 | (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0), | |
288 | prefix, argv); | |
289 | } | |
290 | ||
291 | static int reset_refs(const char *rev, const struct object_id *oid) | |
292 | { | |
293 | int update_ref_status; | |
294 | struct strbuf msg = STRBUF_INIT; | |
295 | struct object_id *orig = NULL, oid_orig, | |
296 | *old_orig = NULL, oid_old_orig; | |
297 | ||
298 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) | |
299 | old_orig = &oid_old_orig; | |
300 | if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) { | |
301 | orig = &oid_orig; | |
302 | set_reflog_message(&msg, "updating ORIG_HEAD", NULL); | |
303 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, | |
304 | UPDATE_REFS_MSG_ON_ERR); | |
305 | } else if (old_orig) | |
306 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
307 | set_reflog_message(&msg, "updating HEAD", rev); | |
308 | update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0, | |
309 | UPDATE_REFS_MSG_ON_ERR); | |
310 | strbuf_release(&msg); | |
311 | return update_ref_status; | |
312 | } | |
313 | ||
314 | static int git_reset_config(const char *var, const char *value, void *cb) | |
315 | { | |
316 | if (!strcmp(var, "submodule.recurse")) | |
317 | return git_default_submodule_config(var, value, cb); | |
318 | ||
319 | return git_default_config(var, value, cb); | |
320 | } | |
321 | ||
322 | int cmd_reset(int argc, const char **argv, const char *prefix) | |
323 | { | |
324 | int reset_type = NONE, update_ref_status = 0, quiet = 0; | |
325 | int no_refresh = 0; | |
326 | int patch_mode = 0, pathspec_file_nul = 0, unborn; | |
327 | const char *rev; | |
328 | char *pathspec_from_file = NULL; | |
329 | struct object_id oid; | |
330 | struct pathspec pathspec; | |
331 | int intent_to_add = 0; | |
332 | const struct option options[] = { | |
333 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
334 | OPT_BOOL(0, "no-refresh", &no_refresh, | |
335 | N_("skip refreshing the index after reset")), | |
336 | OPT_SET_INT(0, "mixed", &reset_type, | |
337 | N_("reset HEAD and index"), MIXED), | |
338 | OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT), | |
339 | OPT_SET_INT(0, "hard", &reset_type, | |
340 | N_("reset HEAD, index and working tree"), HARD), | |
341 | OPT_SET_INT(0, "merge", &reset_type, | |
342 | N_("reset HEAD, index and working tree"), MERGE), | |
343 | OPT_SET_INT(0, "keep", &reset_type, | |
344 | N_("reset HEAD but keep local changes"), KEEP), | |
345 | OPT_CALLBACK_F(0, "recurse-submodules", NULL, | |
346 | "reset", "control recursive updating of submodules", | |
347 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater), | |
348 | OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")), | |
349 | OPT_BOOL('N', "intent-to-add", &intent_to_add, | |
350 | N_("record only the fact that removed paths will be added later")), | |
351 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), | |
352 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
353 | OPT_END() | |
354 | }; | |
355 | ||
356 | git_config(git_reset_config, NULL); | |
357 | ||
358 | argc = parse_options(argc, argv, prefix, options, git_reset_usage, | |
359 | PARSE_OPT_KEEP_DASHDASH); | |
360 | parse_args(&pathspec, argv, prefix, patch_mode, &rev); | |
361 | ||
362 | if (pathspec_from_file) { | |
363 | if (patch_mode) | |
364 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); | |
365 | ||
366 | if (pathspec.nr) | |
367 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); | |
368 | ||
369 | parse_pathspec_file(&pathspec, 0, | |
370 | PATHSPEC_PREFER_FULL, | |
371 | prefix, pathspec_from_file, pathspec_file_nul); | |
372 | } else if (pathspec_file_nul) { | |
373 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); | |
374 | } | |
375 | ||
376 | unborn = !strcmp(rev, "HEAD") && repo_get_oid(the_repository, "HEAD", | |
377 | &oid); | |
378 | if (unborn) { | |
379 | /* reset on unborn branch: treat as reset to empty tree */ | |
380 | oidcpy(&oid, the_hash_algo->empty_tree); | |
381 | } else if (!pathspec.nr && !patch_mode) { | |
382 | struct commit *commit; | |
383 | if (repo_get_oid_committish(the_repository, rev, &oid)) | |
384 | die(_("Failed to resolve '%s' as a valid revision."), rev); | |
385 | commit = lookup_commit_reference(the_repository, &oid); | |
386 | if (!commit) | |
387 | die(_("Could not parse object '%s'."), rev); | |
388 | oidcpy(&oid, &commit->object.oid); | |
389 | } else { | |
390 | struct tree *tree; | |
391 | if (repo_get_oid_treeish(the_repository, rev, &oid)) | |
392 | die(_("Failed to resolve '%s' as a valid tree."), rev); | |
393 | tree = parse_tree_indirect(&oid); | |
394 | if (!tree) | |
395 | die(_("Could not parse object '%s'."), rev); | |
396 | oidcpy(&oid, &tree->object.oid); | |
397 | } | |
398 | ||
399 | if (patch_mode) { | |
400 | if (reset_type != NONE) | |
401 | die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}"); | |
402 | trace2_cmd_mode("patch-interactive"); | |
403 | update_ref_status = !!run_add_p(the_repository, ADD_P_RESET, rev, | |
404 | &pathspec); | |
405 | goto cleanup; | |
406 | } | |
407 | ||
408 | /* git reset tree [--] paths... can be used to | |
409 | * load chosen paths from the tree into the index without | |
410 | * affecting the working tree nor HEAD. */ | |
411 | if (pathspec.nr) { | |
412 | if (reset_type == MIXED) | |
413 | warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); | |
414 | else if (reset_type != NONE) | |
415 | die(_("Cannot do %s reset with paths."), | |
416 | _(reset_type_names[reset_type])); | |
417 | } | |
418 | if (reset_type == NONE) | |
419 | reset_type = MIXED; /* by default */ | |
420 | ||
421 | if (pathspec.nr) | |
422 | trace2_cmd_mode("path"); | |
423 | else | |
424 | trace2_cmd_mode(reset_type_names[reset_type]); | |
425 | ||
426 | if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree())) | |
427 | setup_work_tree(); | |
428 | ||
429 | if (reset_type == MIXED && is_bare_repository()) | |
430 | die(_("%s reset is not allowed in a bare repository"), | |
431 | _(reset_type_names[reset_type])); | |
432 | ||
433 | if (intent_to_add && reset_type != MIXED) | |
434 | die(_("the option '%s' requires '%s'"), "-N", "--mixed"); | |
435 | ||
436 | prepare_repo_settings(the_repository); | |
437 | the_repository->settings.command_requires_full_index = 0; | |
438 | ||
439 | if (repo_read_index(the_repository) < 0) | |
440 | die(_("index file corrupt")); | |
441 | ||
442 | /* Soft reset does not touch the index file nor the working tree | |
443 | * at all, but requires them in a good order. Other resets reset | |
444 | * the index file to the tree object we are switching to. */ | |
445 | if (reset_type == SOFT || reset_type == KEEP) | |
446 | die_if_unmerged_cache(reset_type); | |
447 | ||
448 | if (reset_type != SOFT) { | |
449 | struct lock_file lock = LOCK_INIT; | |
450 | repo_hold_locked_index(the_repository, &lock, | |
451 | LOCK_DIE_ON_ERROR); | |
452 | if (reset_type == MIXED) { | |
453 | int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN; | |
454 | if (read_from_tree(&pathspec, &oid, intent_to_add)) { | |
455 | update_ref_status = 1; | |
456 | goto cleanup; | |
457 | } | |
458 | the_index.updated_skipworktree = 1; | |
459 | if (!no_refresh && get_git_work_tree()) { | |
460 | uint64_t t_begin, t_delta_in_ms; | |
461 | ||
462 | t_begin = getnanotime(); | |
463 | refresh_index(&the_index, flags, NULL, NULL, | |
464 | _("Unstaged changes after reset:")); | |
465 | t_delta_in_ms = (getnanotime() - t_begin) / 1000000; | |
466 | if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) { | |
467 | advise(_("It took %.2f seconds to refresh the index after reset. You can use\n" | |
468 | "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0); | |
469 | } | |
470 | } | |
471 | } else { | |
472 | struct object_id dummy; | |
473 | char *ref = NULL; | |
474 | int err; | |
475 | ||
476 | repo_dwim_ref(the_repository, rev, strlen(rev), | |
477 | &dummy, &ref, 0); | |
478 | if (ref && !starts_with(ref, "refs/")) | |
479 | FREE_AND_NULL(ref); | |
480 | ||
481 | err = reset_index(ref, &oid, reset_type, quiet); | |
482 | if (reset_type == KEEP && !err) | |
483 | err = reset_index(ref, &oid, MIXED, quiet); | |
484 | if (err) | |
485 | die(_("Could not reset index file to revision '%s'."), rev); | |
486 | free(ref); | |
487 | } | |
488 | ||
489 | if (write_locked_index(&the_index, &lock, COMMIT_LOCK)) | |
490 | die(_("Could not write new index file.")); | |
491 | } | |
492 | ||
493 | if (!pathspec.nr && !unborn) { | |
494 | /* Any resets without paths update HEAD to the head being | |
495 | * switched to, saving the previous head in ORIG_HEAD before. */ | |
496 | update_ref_status = reset_refs(rev, &oid); | |
497 | ||
498 | if (reset_type == HARD && !update_ref_status && !quiet) | |
499 | print_new_head_line(lookup_commit_reference(the_repository, &oid)); | |
500 | } | |
501 | if (!pathspec.nr) | |
502 | remove_branch_state(the_repository, 0); | |
503 | ||
504 | discard_index(&the_index); | |
505 | ||
506 | cleanup: | |
507 | clear_pathspec(&pathspec); | |
508 | free(pathspec_from_file); | |
509 | return update_ref_status; | |
510 | } |