unpack-trees: introduce preserve_ignored to unpack_trees_options
[git/debian.git] / builtin / reset.c
blob7f38656f018efb37795148d1406c0c55315fb331
1 /*
2 * "git reset" builtin command
4 * Copyright (c) 2007 Carlos Rica
6 * Based on git-reset.sh, which is
8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
9 */
10 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "builtin.h"
12 #include "config.h"
13 #include "lockfile.h"
14 #include "tag.h"
15 #include "object.h"
16 #include "pretty.h"
17 #include "run-command.h"
18 #include "refs.h"
19 #include "diff.h"
20 #include "diffcore.h"
21 #include "tree.h"
22 #include "branch.h"
23 #include "parse-options.h"
24 #include "unpack-trees.h"
25 #include "cache-tree.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
29 #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
31 static const char * const git_reset_usage[] = {
32 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
33 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
34 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
35 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
36 NULL
39 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
40 static const char *reset_type_names[] = {
41 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
44 static inline int is_merge(void)
46 return !access(git_path_merge_head(the_repository), F_OK);
49 static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
51 int i, nr = 0;
52 struct tree_desc desc[2];
53 struct tree *tree;
54 struct unpack_trees_options opts;
55 int ret = -1;
57 memset(&opts, 0, sizeof(opts));
58 opts.head_idx = 1;
59 opts.src_index = &the_index;
60 opts.dst_index = &the_index;
61 opts.fn = oneway_merge;
62 opts.merge = 1;
63 init_checkout_metadata(&opts.meta, ref, oid, NULL);
64 if (!quiet)
65 opts.verbose_update = 1;
66 switch (reset_type) {
67 case KEEP:
68 case MERGE:
69 opts.update = 1;
70 /* FIXME: Default should be to remove ignored files */
71 opts.preserve_ignored = 1;
72 break;
73 case HARD:
74 opts.update = 1;
75 /* fallthrough */
76 default:
77 opts.reset = 1;
80 read_cache_unmerged();
82 if (reset_type == KEEP) {
83 struct object_id head_oid;
84 if (get_oid("HEAD", &head_oid))
85 return error(_("You do not have a valid HEAD."));
86 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
87 return error(_("Failed to find tree of HEAD."));
88 nr++;
89 opts.fn = twoway_merge;
92 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
93 error(_("Failed to find tree of %s."), oid_to_hex(oid));
94 goto out;
96 nr++;
98 if (unpack_trees(nr, desc, &opts))
99 goto out;
101 if (reset_type == MIXED || reset_type == HARD) {
102 tree = parse_tree_indirect(oid);
103 prime_cache_tree(the_repository, the_repository->index, tree);
106 ret = 0;
108 out:
109 for (i = 0; i < nr; i++)
110 free((void *)desc[i].buffer);
111 return ret;
114 static void print_new_head_line(struct commit *commit)
116 struct strbuf buf = STRBUF_INIT;
118 printf(_("HEAD is now at %s"),
119 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
121 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
122 if (buf.len > 0)
123 printf(" %s", buf.buf);
124 putchar('\n');
125 strbuf_release(&buf);
128 static void update_index_from_diff(struct diff_queue_struct *q,
129 struct diff_options *opt, void *data)
131 int i;
132 int intent_to_add = *(int *)data;
134 for (i = 0; i < q->nr; i++) {
135 struct diff_filespec *one = q->queue[i]->one;
136 int is_missing = !(one->mode && !is_null_oid(&one->oid));
137 struct cache_entry *ce;
139 if (is_missing && !intent_to_add) {
140 remove_file_from_cache(one->path);
141 continue;
144 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
145 0, 0);
146 if (!ce)
147 die(_("make_cache_entry failed for path '%s'"),
148 one->path);
149 if (is_missing) {
150 ce->ce_flags |= CE_INTENT_TO_ADD;
151 set_object_name_for_intent_to_add_entry(ce);
153 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
157 static int read_from_tree(const struct pathspec *pathspec,
158 struct object_id *tree_oid,
159 int intent_to_add)
161 struct diff_options opt;
163 memset(&opt, 0, sizeof(opt));
164 copy_pathspec(&opt.pathspec, pathspec);
165 opt.output_format = DIFF_FORMAT_CALLBACK;
166 opt.format_callback = update_index_from_diff;
167 opt.format_callback_data = &intent_to_add;
168 opt.flags.override_submodule_config = 1;
169 opt.repo = the_repository;
171 if (do_diff_cache(tree_oid, &opt))
172 return 1;
173 diffcore_std(&opt);
174 diff_flush(&opt);
175 clear_pathspec(&opt.pathspec);
177 return 0;
180 static void set_reflog_message(struct strbuf *sb, const char *action,
181 const char *rev)
183 const char *rla = getenv("GIT_REFLOG_ACTION");
185 strbuf_reset(sb);
186 if (rla)
187 strbuf_addf(sb, "%s: %s", rla, action);
188 else if (rev)
189 strbuf_addf(sb, "reset: moving to %s", rev);
190 else
191 strbuf_addf(sb, "reset: %s", action);
194 static void die_if_unmerged_cache(int reset_type)
196 if (is_merge() || unmerged_cache())
197 die(_("Cannot do a %s reset in the middle of a merge."),
198 _(reset_type_names[reset_type]));
202 static void parse_args(struct pathspec *pathspec,
203 const char **argv, const char *prefix,
204 int patch_mode,
205 const char **rev_ret)
207 const char *rev = "HEAD";
208 struct object_id unused;
210 * Possible arguments are:
212 * git reset [-opts] [<rev>]
213 * git reset [-opts] <tree> [<paths>...]
214 * git reset [-opts] <tree> -- [<paths>...]
215 * git reset [-opts] -- [<paths>...]
216 * git reset [-opts] <paths>...
218 * At this point, argv points immediately after [-opts].
221 if (argv[0]) {
222 if (!strcmp(argv[0], "--")) {
223 argv++; /* reset to HEAD, possibly with paths */
224 } else if (argv[1] && !strcmp(argv[1], "--")) {
225 rev = argv[0];
226 argv += 2;
229 * Otherwise, argv[0] could be either <rev> or <paths> and
230 * has to be unambiguous. If there is a single argument, it
231 * can not be a tree
233 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
234 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
236 * Ok, argv[0] looks like a commit/tree; it should not
237 * be a filename.
239 verify_non_filename(prefix, argv[0]);
240 rev = *argv++;
241 } else {
242 /* Otherwise we treat this as a filename */
243 verify_filename(prefix, argv[0], 1);
246 *rev_ret = rev;
248 if (read_cache() < 0)
249 die(_("index file corrupt"));
251 parse_pathspec(pathspec, 0,
252 PATHSPEC_PREFER_FULL |
253 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
254 prefix, argv);
257 static int reset_refs(const char *rev, const struct object_id *oid)
259 int update_ref_status;
260 struct strbuf msg = STRBUF_INIT;
261 struct object_id *orig = NULL, oid_orig,
262 *old_orig = NULL, oid_old_orig;
264 if (!get_oid("ORIG_HEAD", &oid_old_orig))
265 old_orig = &oid_old_orig;
266 if (!get_oid("HEAD", &oid_orig)) {
267 orig = &oid_orig;
268 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
269 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
270 UPDATE_REFS_MSG_ON_ERR);
271 } else if (old_orig)
272 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
273 set_reflog_message(&msg, "updating HEAD", rev);
274 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
275 UPDATE_REFS_MSG_ON_ERR);
276 strbuf_release(&msg);
277 return update_ref_status;
280 static int git_reset_config(const char *var, const char *value, void *cb)
282 if (!strcmp(var, "submodule.recurse"))
283 return git_default_submodule_config(var, value, cb);
285 return git_default_config(var, value, cb);
288 int cmd_reset(int argc, const char **argv, const char *prefix)
290 int reset_type = NONE, update_ref_status = 0, quiet = 0;
291 int patch_mode = 0, pathspec_file_nul = 0, unborn;
292 const char *rev, *pathspec_from_file = NULL;
293 struct object_id oid;
294 struct pathspec pathspec;
295 int intent_to_add = 0;
296 const struct option options[] = {
297 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
298 OPT_SET_INT(0, "mixed", &reset_type,
299 N_("reset HEAD and index"), MIXED),
300 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
301 OPT_SET_INT(0, "hard", &reset_type,
302 N_("reset HEAD, index and working tree"), HARD),
303 OPT_SET_INT(0, "merge", &reset_type,
304 N_("reset HEAD, index and working tree"), MERGE),
305 OPT_SET_INT(0, "keep", &reset_type,
306 N_("reset HEAD but keep local changes"), KEEP),
307 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
308 "reset", "control recursive updating of submodules",
309 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
310 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
311 OPT_BOOL('N', "intent-to-add", &intent_to_add,
312 N_("record only the fact that removed paths will be added later")),
313 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
314 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
315 OPT_END()
318 git_config(git_reset_config, NULL);
319 git_config_get_bool("reset.quiet", &quiet);
321 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
322 PARSE_OPT_KEEP_DASHDASH);
323 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
325 if (pathspec_from_file) {
326 if (patch_mode)
327 die(_("--pathspec-from-file is incompatible with --patch"));
329 if (pathspec.nr)
330 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
332 parse_pathspec_file(&pathspec, 0,
333 PATHSPEC_PREFER_FULL,
334 prefix, pathspec_from_file, pathspec_file_nul);
335 } else if (pathspec_file_nul) {
336 die(_("--pathspec-file-nul requires --pathspec-from-file"));
339 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
340 if (unborn) {
341 /* reset on unborn branch: treat as reset to empty tree */
342 oidcpy(&oid, the_hash_algo->empty_tree);
343 } else if (!pathspec.nr && !patch_mode) {
344 struct commit *commit;
345 if (get_oid_committish(rev, &oid))
346 die(_("Failed to resolve '%s' as a valid revision."), rev);
347 commit = lookup_commit_reference(the_repository, &oid);
348 if (!commit)
349 die(_("Could not parse object '%s'."), rev);
350 oidcpy(&oid, &commit->object.oid);
351 } else {
352 struct tree *tree;
353 if (get_oid_treeish(rev, &oid))
354 die(_("Failed to resolve '%s' as a valid tree."), rev);
355 tree = parse_tree_indirect(&oid);
356 if (!tree)
357 die(_("Could not parse object '%s'."), rev);
358 oidcpy(&oid, &tree->object.oid);
361 if (patch_mode) {
362 if (reset_type != NONE)
363 die(_("--patch is incompatible with --{hard,mixed,soft}"));
364 trace2_cmd_mode("patch-interactive");
365 return run_add_interactive(rev, "--patch=reset", &pathspec);
368 /* git reset tree [--] paths... can be used to
369 * load chosen paths from the tree into the index without
370 * affecting the working tree nor HEAD. */
371 if (pathspec.nr) {
372 if (reset_type == MIXED)
373 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
374 else if (reset_type != NONE)
375 die(_("Cannot do %s reset with paths."),
376 _(reset_type_names[reset_type]));
378 if (reset_type == NONE)
379 reset_type = MIXED; /* by default */
381 if (pathspec.nr)
382 trace2_cmd_mode("path");
383 else
384 trace2_cmd_mode(reset_type_names[reset_type]);
386 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
387 setup_work_tree();
389 if (reset_type == MIXED && is_bare_repository())
390 die(_("%s reset is not allowed in a bare repository"),
391 _(reset_type_names[reset_type]));
393 if (intent_to_add && reset_type != MIXED)
394 die(_("-N can only be used with --mixed"));
396 /* Soft reset does not touch the index file nor the working tree
397 * at all, but requires them in a good order. Other resets reset
398 * the index file to the tree object we are switching to. */
399 if (reset_type == SOFT || reset_type == KEEP)
400 die_if_unmerged_cache(reset_type);
402 if (reset_type != SOFT) {
403 struct lock_file lock = LOCK_INIT;
404 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
405 if (reset_type == MIXED) {
406 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
407 if (read_from_tree(&pathspec, &oid, intent_to_add))
408 return 1;
409 the_index.updated_skipworktree = 1;
410 if (!quiet && get_git_work_tree()) {
411 uint64_t t_begin, t_delta_in_ms;
413 t_begin = getnanotime();
414 refresh_index(&the_index, flags, NULL, NULL,
415 _("Unstaged changes after reset:"));
416 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
417 if (advice_enabled(ADVICE_RESET_QUIET_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
418 printf(_("\nIt took %.2f seconds to enumerate unstaged changes after reset. You can\n"
419 "use '--quiet' to avoid this. Set the config setting reset.quiet to true\n"
420 "to make this the default.\n"), t_delta_in_ms / 1000.0);
423 } else {
424 struct object_id dummy;
425 char *ref = NULL;
426 int err;
428 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
429 if (ref && !starts_with(ref, "refs/"))
430 FREE_AND_NULL(ref);
432 err = reset_index(ref, &oid, reset_type, quiet);
433 if (reset_type == KEEP && !err)
434 err = reset_index(ref, &oid, MIXED, quiet);
435 if (err)
436 die(_("Could not reset index file to revision '%s'."), rev);
437 free(ref);
440 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
441 die(_("Could not write new index file."));
444 if (!pathspec.nr && !unborn) {
445 /* Any resets without paths update HEAD to the head being
446 * switched to, saving the previous head in ORIG_HEAD before. */
447 update_ref_status = reset_refs(rev, &oid);
449 if (reset_type == HARD && !update_ref_status && !quiet)
450 print_new_head_line(lookup_commit_reference(the_repository, &oid));
452 if (!pathspec.nr)
453 remove_branch_state(the_repository, 0);
455 return update_ref_status;