trace2: disable tr2_dst before warning on write errors
[git.git] / builtin / reset.c
blob43e855cb887650b87390e6f5e96294b4c967d2af
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 break;
71 case HARD:
72 opts.update = 1;
73 /* fallthrough */
74 default:
75 opts.reset = 1;
78 read_cache_unmerged();
80 if (reset_type == KEEP) {
81 struct object_id head_oid;
82 if (get_oid("HEAD", &head_oid))
83 return error(_("You do not have a valid HEAD."));
84 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
85 return error(_("Failed to find tree of HEAD."));
86 nr++;
87 opts.fn = twoway_merge;
90 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
91 error(_("Failed to find tree of %s."), oid_to_hex(oid));
92 goto out;
94 nr++;
96 if (unpack_trees(nr, desc, &opts))
97 goto out;
99 if (reset_type == MIXED || reset_type == HARD) {
100 tree = parse_tree_indirect(oid);
101 prime_cache_tree(the_repository, the_repository->index, tree);
104 ret = 0;
106 out:
107 for (i = 0; i < nr; i++)
108 free((void *)desc[i].buffer);
109 return ret;
112 static void print_new_head_line(struct commit *commit)
114 struct strbuf buf = STRBUF_INIT;
116 printf(_("HEAD is now at %s"),
117 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
119 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
120 if (buf.len > 0)
121 printf(" %s", buf.buf);
122 putchar('\n');
123 strbuf_release(&buf);
126 static void update_index_from_diff(struct diff_queue_struct *q,
127 struct diff_options *opt, void *data)
129 int i;
130 int intent_to_add = *(int *)data;
132 for (i = 0; i < q->nr; i++) {
133 struct diff_filespec *one = q->queue[i]->one;
134 int is_missing = !(one->mode && !is_null_oid(&one->oid));
135 struct cache_entry *ce;
137 if (is_missing && !intent_to_add) {
138 remove_file_from_cache(one->path);
139 continue;
142 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
143 0, 0);
144 if (!ce)
145 die(_("make_cache_entry failed for path '%s'"),
146 one->path);
147 if (is_missing) {
148 ce->ce_flags |= CE_INTENT_TO_ADD;
149 set_object_name_for_intent_to_add_entry(ce);
151 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
155 static int read_from_tree(const struct pathspec *pathspec,
156 struct object_id *tree_oid,
157 int intent_to_add)
159 struct diff_options opt;
161 memset(&opt, 0, sizeof(opt));
162 copy_pathspec(&opt.pathspec, pathspec);
163 opt.output_format = DIFF_FORMAT_CALLBACK;
164 opt.format_callback = update_index_from_diff;
165 opt.format_callback_data = &intent_to_add;
166 opt.flags.override_submodule_config = 1;
167 opt.repo = the_repository;
169 if (do_diff_cache(tree_oid, &opt))
170 return 1;
171 diffcore_std(&opt);
172 diff_flush(&opt);
173 clear_pathspec(&opt.pathspec);
175 return 0;
178 static void set_reflog_message(struct strbuf *sb, const char *action,
179 const char *rev)
181 const char *rla = getenv("GIT_REFLOG_ACTION");
183 strbuf_reset(sb);
184 if (rla)
185 strbuf_addf(sb, "%s: %s", rla, action);
186 else if (rev)
187 strbuf_addf(sb, "reset: moving to %s", rev);
188 else
189 strbuf_addf(sb, "reset: %s", action);
192 static void die_if_unmerged_cache(int reset_type)
194 if (is_merge() || unmerged_cache())
195 die(_("Cannot do a %s reset in the middle of a merge."),
196 _(reset_type_names[reset_type]));
200 static void parse_args(struct pathspec *pathspec,
201 const char **argv, const char *prefix,
202 int patch_mode,
203 const char **rev_ret)
205 const char *rev = "HEAD";
206 struct object_id unused;
208 * Possible arguments are:
210 * git reset [-opts] [<rev>]
211 * git reset [-opts] <tree> [<paths>...]
212 * git reset [-opts] <tree> -- [<paths>...]
213 * git reset [-opts] -- [<paths>...]
214 * git reset [-opts] <paths>...
216 * At this point, argv points immediately after [-opts].
219 if (argv[0]) {
220 if (!strcmp(argv[0], "--")) {
221 argv++; /* reset to HEAD, possibly with paths */
222 } else if (argv[1] && !strcmp(argv[1], "--")) {
223 rev = argv[0];
224 argv += 2;
227 * Otherwise, argv[0] could be either <rev> or <paths> and
228 * has to be unambiguous. If there is a single argument, it
229 * can not be a tree
231 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
232 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
234 * Ok, argv[0] looks like a commit/tree; it should not
235 * be a filename.
237 verify_non_filename(prefix, argv[0]);
238 rev = *argv++;
239 } else {
240 /* Otherwise we treat this as a filename */
241 verify_filename(prefix, argv[0], 1);
244 *rev_ret = rev;
246 if (read_cache() < 0)
247 die(_("index file corrupt"));
249 parse_pathspec(pathspec, 0,
250 PATHSPEC_PREFER_FULL |
251 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
252 prefix, argv);
255 static int reset_refs(const char *rev, const struct object_id *oid)
257 int update_ref_status;
258 struct strbuf msg = STRBUF_INIT;
259 struct object_id *orig = NULL, oid_orig,
260 *old_orig = NULL, oid_old_orig;
262 if (!get_oid("ORIG_HEAD", &oid_old_orig))
263 old_orig = &oid_old_orig;
264 if (!get_oid("HEAD", &oid_orig)) {
265 orig = &oid_orig;
266 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
267 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
268 UPDATE_REFS_MSG_ON_ERR);
269 } else if (old_orig)
270 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
271 set_reflog_message(&msg, "updating HEAD", rev);
272 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
273 UPDATE_REFS_MSG_ON_ERR);
274 strbuf_release(&msg);
275 return update_ref_status;
278 static int git_reset_config(const char *var, const char *value, void *cb)
280 if (!strcmp(var, "submodule.recurse"))
281 return git_default_submodule_config(var, value, cb);
283 return git_default_config(var, value, cb);
286 int cmd_reset(int argc, const char **argv, const char *prefix)
288 int reset_type = NONE, update_ref_status = 0, quiet = 0;
289 int patch_mode = 0, pathspec_file_nul = 0, unborn;
290 const char *rev, *pathspec_from_file = NULL;
291 struct object_id oid;
292 struct pathspec pathspec;
293 int intent_to_add = 0;
294 const struct option options[] = {
295 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
296 OPT_SET_INT(0, "mixed", &reset_type,
297 N_("reset HEAD and index"), MIXED),
298 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
299 OPT_SET_INT(0, "hard", &reset_type,
300 N_("reset HEAD, index and working tree"), HARD),
301 OPT_SET_INT(0, "merge", &reset_type,
302 N_("reset HEAD, index and working tree"), MERGE),
303 OPT_SET_INT(0, "keep", &reset_type,
304 N_("reset HEAD but keep local changes"), KEEP),
305 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
306 "reset", "control recursive updating of submodules",
307 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
308 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
309 OPT_BOOL('N', "intent-to-add", &intent_to_add,
310 N_("record only the fact that removed paths will be added later")),
311 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
312 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
313 OPT_END()
316 git_config(git_reset_config, NULL);
317 git_config_get_bool("reset.quiet", &quiet);
319 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
320 PARSE_OPT_KEEP_DASHDASH);
321 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
323 if (pathspec_from_file) {
324 if (patch_mode)
325 die(_("--pathspec-from-file is incompatible with --patch"));
327 if (pathspec.nr)
328 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
330 parse_pathspec_file(&pathspec, 0,
331 PATHSPEC_PREFER_FULL,
332 prefix, pathspec_from_file, pathspec_file_nul);
333 } else if (pathspec_file_nul) {
334 die(_("--pathspec-file-nul requires --pathspec-from-file"));
337 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
338 if (unborn) {
339 /* reset on unborn branch: treat as reset to empty tree */
340 oidcpy(&oid, the_hash_algo->empty_tree);
341 } else if (!pathspec.nr && !patch_mode) {
342 struct commit *commit;
343 if (get_oid_committish(rev, &oid))
344 die(_("Failed to resolve '%s' as a valid revision."), rev);
345 commit = lookup_commit_reference(the_repository, &oid);
346 if (!commit)
347 die(_("Could not parse object '%s'."), rev);
348 oidcpy(&oid, &commit->object.oid);
349 } else {
350 struct tree *tree;
351 if (get_oid_treeish(rev, &oid))
352 die(_("Failed to resolve '%s' as a valid tree."), rev);
353 tree = parse_tree_indirect(&oid);
354 if (!tree)
355 die(_("Could not parse object '%s'."), rev);
356 oidcpy(&oid, &tree->object.oid);
359 if (patch_mode) {
360 if (reset_type != NONE)
361 die(_("--patch is incompatible with --{hard,mixed,soft}"));
362 trace2_cmd_mode("patch-interactive");
363 return run_add_interactive(rev, "--patch=reset", &pathspec);
366 /* git reset tree [--] paths... can be used to
367 * load chosen paths from the tree into the index without
368 * affecting the working tree nor HEAD. */
369 if (pathspec.nr) {
370 if (reset_type == MIXED)
371 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
372 else if (reset_type != NONE)
373 die(_("Cannot do %s reset with paths."),
374 _(reset_type_names[reset_type]));
376 if (reset_type == NONE)
377 reset_type = MIXED; /* by default */
379 if (pathspec.nr)
380 trace2_cmd_mode("path");
381 else
382 trace2_cmd_mode(reset_type_names[reset_type]);
384 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
385 setup_work_tree();
387 if (reset_type == MIXED && is_bare_repository())
388 die(_("%s reset is not allowed in a bare repository"),
389 _(reset_type_names[reset_type]));
391 if (intent_to_add && reset_type != MIXED)
392 die(_("-N can only be used with --mixed"));
394 /* Soft reset does not touch the index file nor the working tree
395 * at all, but requires them in a good order. Other resets reset
396 * the index file to the tree object we are switching to. */
397 if (reset_type == SOFT || reset_type == KEEP)
398 die_if_unmerged_cache(reset_type);
400 if (reset_type != SOFT) {
401 struct lock_file lock = LOCK_INIT;
402 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
403 if (reset_type == MIXED) {
404 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
405 if (read_from_tree(&pathspec, &oid, intent_to_add))
406 return 1;
407 the_index.updated_skipworktree = 1;
408 if (!quiet && get_git_work_tree()) {
409 uint64_t t_begin, t_delta_in_ms;
411 t_begin = getnanotime();
412 refresh_index(&the_index, flags, NULL, NULL,
413 _("Unstaged changes after reset:"));
414 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
415 if (advice_reset_quiet_warning && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
416 printf(_("\nIt took %.2f seconds to enumerate unstaged changes after reset. You can\n"
417 "use '--quiet' to avoid this. Set the config setting reset.quiet to true\n"
418 "to make this the default.\n"), t_delta_in_ms / 1000.0);
421 } else {
422 struct object_id dummy;
423 char *ref = NULL;
424 int err;
426 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
427 if (ref && !starts_with(ref, "refs/"))
428 FREE_AND_NULL(ref);
430 err = reset_index(ref, &oid, reset_type, quiet);
431 if (reset_type == KEEP && !err)
432 err = reset_index(ref, &oid, MIXED, quiet);
433 if (err)
434 die(_("Could not reset index file to revision '%s'."), rev);
435 free(ref);
438 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
439 die(_("Could not write new index file."));
442 if (!pathspec.nr && !unborn) {
443 /* Any resets without paths update HEAD to the head being
444 * switched to, saving the previous head in ORIG_HEAD before. */
445 update_ref_status = reset_refs(rev, &oid);
447 if (reset_type == HARD && !update_ref_status && !quiet)
448 print_new_head_line(lookup_commit_reference(the_repository, &oid));
450 if (!pathspec.nr)
451 remove_branch_state(the_repository, 0);
453 return update_ref_status;