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
16 #include "run-command.h"
22 #include "parse-options.h"
23 #include "unpack-trees.h"
24 #include "cache-tree.h"
25 #include "submodule.h"
26 #include "submodule-config.h"
28 #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
30 static const char * const git_reset_usage[] = {
31 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
32 N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
33 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
37 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
38 static const char *reset_type_names[] = {
39 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
42 static inline int is_merge(void)
44 return !access(git_path_merge_head(the_repository), F_OK);
47 static int reset_index(const struct object_id *oid, int reset_type, int quiet)
50 struct tree_desc desc[2];
52 struct unpack_trees_options opts;
55 memset(&opts, 0, sizeof(opts));
57 opts.src_index = &the_index;
58 opts.dst_index = &the_index;
59 opts.fn = oneway_merge;
62 opts.verbose_update = 1;
75 read_cache_unmerged();
77 if (reset_type == KEEP) {
78 struct object_id head_oid;
79 if (get_oid("HEAD", &head_oid))
80 return error(_("You do not have a valid HEAD."));
81 if (!fill_tree_descriptor(desc + nr, &head_oid))
82 return error(_("Failed to find tree of HEAD."));
84 opts.fn = twoway_merge;
87 if (!fill_tree_descriptor(desc + nr, oid)) {
88 error(_("Failed to find tree of %s."), oid_to_hex(oid));
93 if (unpack_trees(nr, desc, &opts))
96 if (reset_type == MIXED || reset_type == HARD) {
97 tree = parse_tree_indirect(oid);
98 prime_cache_tree(the_repository, the_repository->index, tree);
104 for (i = 0; i < nr; i++)
105 free((void *)desc[i].buffer);
109 static void print_new_head_line(struct commit *commit)
111 struct strbuf buf = STRBUF_INIT;
113 printf(_("HEAD is now at %s"),
114 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
116 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
118 printf(" %s", buf.buf);
120 strbuf_release(&buf);
123 static void update_index_from_diff(struct diff_queue_struct *q,
124 struct diff_options *opt, void *data)
127 int intent_to_add = *(int *)data;
129 for (i = 0; i < q->nr; i++) {
130 struct diff_filespec *one = q->queue[i]->one;
131 int is_missing = !(one->mode && !is_null_oid(&one->oid));
132 struct cache_entry *ce;
134 if (is_missing && !intent_to_add) {
135 remove_file_from_cache(one->path);
139 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
142 die(_("make_cache_entry failed for path '%s'"),
145 ce->ce_flags |= CE_INTENT_TO_ADD;
146 set_object_name_for_intent_to_add_entry(ce);
148 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
152 static int read_from_tree(const struct pathspec *pathspec,
153 struct object_id *tree_oid,
156 struct diff_options opt;
158 memset(&opt, 0, sizeof(opt));
159 copy_pathspec(&opt.pathspec, pathspec);
160 opt.output_format = DIFF_FORMAT_CALLBACK;
161 opt.format_callback = update_index_from_diff;
162 opt.format_callback_data = &intent_to_add;
163 opt.flags.override_submodule_config = 1;
164 opt.repo = the_repository;
166 if (do_diff_cache(tree_oid, &opt))
170 clear_pathspec(&opt.pathspec);
175 static void set_reflog_message(struct strbuf *sb, const char *action,
178 const char *rla = getenv("GIT_REFLOG_ACTION");
182 strbuf_addf(sb, "%s: %s", rla, action);
184 strbuf_addf(sb, "reset: moving to %s", rev);
186 strbuf_addf(sb, "reset: %s", action);
189 static void die_if_unmerged_cache(int reset_type)
191 if (is_merge() || unmerged_cache())
192 die(_("Cannot do a %s reset in the middle of a merge."),
193 _(reset_type_names[reset_type]));
197 static void parse_args(struct pathspec *pathspec,
198 const char **argv, const char *prefix,
200 const char **rev_ret)
202 const char *rev = "HEAD";
203 struct object_id unused;
205 * Possible arguments are:
207 * git reset [-opts] [<rev>]
208 * git reset [-opts] <tree> [<paths>...]
209 * git reset [-opts] <tree> -- [<paths>...]
210 * git reset [-opts] -- [<paths>...]
211 * git reset [-opts] <paths>...
213 * At this point, argv points immediately after [-opts].
217 if (!strcmp(argv[0], "--")) {
218 argv++; /* reset to HEAD, possibly with paths */
219 } else if (argv[1] && !strcmp(argv[1], "--")) {
224 * Otherwise, argv[0] could be either <rev> or <paths> and
225 * has to be unambiguous. If there is a single argument, it
228 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
229 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
231 * Ok, argv[0] looks like a commit/tree; it should not
234 verify_non_filename(prefix, argv[0]);
237 /* Otherwise we treat this as a filename */
238 verify_filename(prefix, argv[0], 1);
243 if (read_cache() < 0)
244 die(_("index file corrupt"));
246 parse_pathspec(pathspec, 0,
247 PATHSPEC_PREFER_FULL |
248 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
252 static int reset_refs(const char *rev, const struct object_id *oid)
254 int update_ref_status;
255 struct strbuf msg = STRBUF_INIT;
256 struct object_id *orig = NULL, oid_orig,
257 *old_orig = NULL, oid_old_orig;
259 if (!get_oid("ORIG_HEAD", &oid_old_orig))
260 old_orig = &oid_old_orig;
261 if (!get_oid("HEAD", &oid_orig)) {
263 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
264 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
265 UPDATE_REFS_MSG_ON_ERR);
267 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
268 set_reflog_message(&msg, "updating HEAD", rev);
269 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
270 UPDATE_REFS_MSG_ON_ERR);
271 strbuf_release(&msg);
272 return update_ref_status;
275 static int git_reset_config(const char *var, const char *value, void *cb)
277 if (!strcmp(var, "submodule.recurse"))
278 return git_default_submodule_config(var, value, cb);
280 return git_default_config(var, value, cb);
283 int cmd_reset(int argc, const char **argv, const char *prefix)
285 int reset_type = NONE, update_ref_status = 0, quiet = 0;
286 int patch_mode = 0, unborn;
288 struct object_id oid;
289 struct pathspec pathspec;
290 int intent_to_add = 0;
291 const struct option options[] = {
292 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
293 OPT_SET_INT(0, "mixed", &reset_type,
294 N_("reset HEAD and index"), MIXED),
295 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
296 OPT_SET_INT(0, "hard", &reset_type,
297 N_("reset HEAD, index and working tree"), HARD),
298 OPT_SET_INT(0, "merge", &reset_type,
299 N_("reset HEAD, index and working tree"), MERGE),
300 OPT_SET_INT(0, "keep", &reset_type,
301 N_("reset HEAD but keep local changes"), KEEP),
302 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
303 "reset", "control recursive updating of submodules",
304 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
305 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
306 OPT_BOOL('N', "intent-to-add", &intent_to_add,
307 N_("record only the fact that removed paths will be added later")),
311 git_config(git_reset_config, NULL);
312 git_config_get_bool("reset.quiet", &quiet);
314 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
315 PARSE_OPT_KEEP_DASHDASH);
316 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
318 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
320 /* reset on unborn branch: treat as reset to empty tree */
321 oidcpy(&oid, the_hash_algo->empty_tree);
322 } else if (!pathspec.nr) {
323 struct commit *commit;
324 if (get_oid_committish(rev, &oid))
325 die(_("Failed to resolve '%s' as a valid revision."), rev);
326 commit = lookup_commit_reference(the_repository, &oid);
328 die(_("Could not parse object '%s'."), rev);
329 oidcpy(&oid, &commit->object.oid);
332 if (get_oid_treeish(rev, &oid))
333 die(_("Failed to resolve '%s' as a valid tree."), rev);
334 tree = parse_tree_indirect(&oid);
336 die(_("Could not parse object '%s'."), rev);
337 oidcpy(&oid, &tree->object.oid);
341 if (reset_type != NONE)
342 die(_("--patch is incompatible with --{hard,mixed,soft}"));
343 return run_add_interactive(rev, "--patch=reset", &pathspec);
346 /* git reset tree [--] paths... can be used to
347 * load chosen paths from the tree into the index without
348 * affecting the working tree nor HEAD. */
350 if (reset_type == MIXED)
351 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
352 else if (reset_type != NONE)
353 die(_("Cannot do %s reset with paths."),
354 _(reset_type_names[reset_type]));
356 if (reset_type == NONE)
357 reset_type = MIXED; /* by default */
359 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
362 if (reset_type == MIXED && is_bare_repository())
363 die(_("%s reset is not allowed in a bare repository"),
364 _(reset_type_names[reset_type]));
366 if (intent_to_add && reset_type != MIXED)
367 die(_("-N can only be used with --mixed"));
369 /* Soft reset does not touch the index file nor the working tree
370 * at all, but requires them in a good order. Other resets reset
371 * the index file to the tree object we are switching to. */
372 if (reset_type == SOFT || reset_type == KEEP)
373 die_if_unmerged_cache(reset_type);
375 if (reset_type != SOFT) {
376 struct lock_file lock = LOCK_INIT;
377 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
378 if (reset_type == MIXED) {
379 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
380 if (read_from_tree(&pathspec, &oid, intent_to_add))
382 if (!quiet && get_git_work_tree()) {
383 uint64_t t_begin, t_delta_in_ms;
385 t_begin = getnanotime();
386 refresh_index(&the_index, flags, NULL, NULL,
387 _("Unstaged changes after reset:"));
388 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
389 if (advice_reset_quiet_warning && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
390 printf(_("\nIt took %.2f seconds to enumerate unstaged changes after reset. You can\n"
391 "use '--quiet' to avoid this. Set the config setting reset.quiet to true\n"
392 "to make this the default.\n"), t_delta_in_ms / 1000.0);
396 int err = reset_index(&oid, reset_type, quiet);
397 if (reset_type == KEEP && !err)
398 err = reset_index(&oid, MIXED, quiet);
400 die(_("Could not reset index file to revision '%s'."), rev);
403 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
404 die(_("Could not write new index file."));
407 if (!pathspec.nr && !unborn) {
408 /* Any resets without paths update HEAD to the head being
409 * switched to, saving the previous head in ORIG_HEAD before. */
410 update_ref_status = reset_refs(rev, &oid);
412 if (reset_type == HARD && !update_ref_status && !quiet)
413 print_new_head_line(lookup_commit_reference(the_repository, &oid));
416 remove_branch_state(the_repository);
418 return update_ref_status;