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
14 #include "run-command.h"
20 #include "parse-options.h"
21 #include "unpack-trees.h"
22 #include "cache-tree.h"
24 static const char * const git_reset_usage[] = {
25 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
26 N_("git reset [-q] <commit> [--] <paths>..."),
27 N_("git reset --patch [<commit>] [--] [<paths>...]"),
31 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
32 static const char *reset_type_names[] = {
33 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
36 static inline int is_merge(void)
38 return !access(git_path("MERGE_HEAD"), F_OK);
41 static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
45 struct tree_desc desc[2];
47 struct unpack_trees_options opts;
48 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
50 memset(&opts, 0, sizeof(opts));
52 opts.src_index = &the_index;
53 opts.dst_index = &the_index;
54 opts.fn = oneway_merge;
57 opts.verbose_update = 1;
70 newfd = hold_locked_index(lock, 1);
72 read_cache_unmerged();
74 if (reset_type == KEEP) {
75 unsigned char head_sha1[20];
76 if (get_sha1("HEAD", head_sha1))
77 return error(_("You do not have a valid HEAD."));
78 if (!fill_tree_descriptor(desc, head_sha1))
79 return error(_("Failed to find tree of HEAD."));
81 opts.fn = twoway_merge;
84 if (!fill_tree_descriptor(desc + nr - 1, sha1))
85 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
86 if (unpack_trees(nr, desc, &opts))
89 if (reset_type == MIXED || reset_type == HARD) {
90 tree = parse_tree_indirect(sha1);
91 prime_cache_tree(&active_cache_tree, tree);
94 if (write_cache(newfd, active_cache, active_nr) ||
95 commit_locked_index(lock))
96 return error(_("Could not write new index file."));
101 static void print_new_head_line(struct commit *commit)
103 const char *hex, *body;
105 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
106 printf(_("HEAD is now at %s"), hex);
107 body = strstr(commit->buffer, "\n\n");
112 eol = strchr(body, '\n');
113 len = eol ? eol - body : strlen(body);
114 printf(" %.*s\n", (int) len, body);
120 static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
125 index_lock = xcalloc(1, sizeof(struct lock_file));
126 fd = hold_locked_index(index_lock, 1);
129 if (read_cache() < 0)
130 return error(_("Could not read index"));
132 result = refresh_index(&the_index, (flags), NULL, NULL,
133 _("Unstaged changes after reset:")) ? 1 : 0;
134 if (write_cache(fd, active_cache, active_nr) ||
135 commit_locked_index(index_lock))
136 return error ("Could not refresh index");
140 static void update_index_from_diff(struct diff_queue_struct *q,
141 struct diff_options *opt, void *data)
144 int *discard_flag = data;
146 /* do_diff_cache() mangled the index */
151 for (i = 0; i < q->nr; i++) {
152 struct diff_filespec *one = q->queue[i]->one;
153 if (one->mode && !is_null_sha1(one->sha1)) {
154 struct cache_entry *ce;
155 ce = make_cache_entry(one->mode, one->sha1, one->path,
158 die(_("make_cache_entry failed for path '%s'"),
160 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
161 ADD_CACHE_OK_TO_REPLACE);
163 remove_file_from_cache(one->path);
167 static int interactive_reset(const char *revision, const char **argv,
170 const char **pathspec = NULL;
173 pathspec = get_pathspec(prefix, argv);
175 return run_add_interactive(revision, "--patch=reset", pathspec);
178 static int read_from_tree(const char *prefix, const char **argv,
179 unsigned char *tree_sha1, int refresh_flags)
181 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
182 int index_fd, index_was_discarded = 0;
183 struct diff_options opt;
185 memset(&opt, 0, sizeof(opt));
186 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
187 opt.output_format = DIFF_FORMAT_CALLBACK;
188 opt.format_callback = update_index_from_diff;
189 opt.format_callback_data = &index_was_discarded;
191 index_fd = hold_locked_index(lock, 1);
192 index_was_discarded = 0;
194 if (do_diff_cache(tree_sha1, &opt))
198 diff_tree_release_paths(&opt);
200 if (!index_was_discarded)
201 /* The index is still clobbered from do_diff_cache() */
203 return update_index_refresh(index_fd, lock, refresh_flags);
206 static void set_reflog_message(struct strbuf *sb, const char *action,
209 const char *rla = getenv("GIT_REFLOG_ACTION");
213 strbuf_addf(sb, "%s: %s", rla, action);
215 strbuf_addf(sb, "reset: moving to %s", rev);
217 strbuf_addf(sb, "reset: %s", action);
220 static void die_if_unmerged_cache(int reset_type)
222 if (is_merge() || read_cache() < 0 || unmerged_cache())
223 die(_("Cannot do a %s reset in the middle of a merge."),
224 _(reset_type_names[reset_type]));
228 int cmd_reset(int argc, const char **argv, const char *prefix)
230 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
232 const char *rev = "HEAD";
233 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
234 *old_orig = NULL, sha1_old_orig[20];
235 struct commit *commit;
236 struct strbuf msg = STRBUF_INIT;
237 const struct option options[] = {
238 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
239 OPT_SET_INT(0, "mixed", &reset_type,
240 N_("reset HEAD and index"), MIXED),
241 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
242 OPT_SET_INT(0, "hard", &reset_type,
243 N_("reset HEAD, index and working tree"), HARD),
244 OPT_SET_INT(0, "merge", &reset_type,
245 N_("reset HEAD, index and working tree"), MERGE),
246 OPT_SET_INT(0, "keep", &reset_type,
247 N_("reset HEAD but keep local changes"), KEEP),
248 OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
252 git_config(git_default_config, NULL);
254 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
255 PARSE_OPT_KEEP_DASHDASH);
258 * Possible arguments are:
260 * git reset [-opts] <rev> <paths>...
261 * git reset [-opts] <rev> -- <paths>...
262 * git reset [-opts] -- <paths>...
263 * git reset [-opts] <paths>...
265 * At this point, argv[i] points immediately after [-opts].
269 if (!strcmp(argv[i], "--")) {
270 i++; /* reset to HEAD, possibly with paths */
271 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
276 * Otherwise, argv[i] could be either <rev> or <paths> and
277 * has to be unambiguous.
279 else if (!get_sha1_committish(argv[i], sha1)) {
281 * Ok, argv[i] looks like a rev; it should not
284 verify_non_filename(prefix, argv[i]);
287 /* Otherwise we treat this as a filename */
288 verify_filename(prefix, argv[i], 1);
292 if (get_sha1_committish(rev, sha1))
293 die(_("Failed to resolve '%s' as a valid ref."), rev);
296 * NOTE: As "git reset $treeish -- $path" should be usable on
297 * any tree-ish, this is not strictly correct. We are not
298 * moving the HEAD to any commit; we are merely resetting the
299 * entries in the index to that of a treeish.
301 commit = lookup_commit_reference(sha1);
303 die(_("Could not parse object '%s'."), rev);
304 hashcpy(sha1, commit->object.sha1);
307 if (reset_type != NONE)
308 die(_("--patch is incompatible with --{hard,mixed,soft}"));
309 return interactive_reset(rev, argv + i, prefix);
312 /* git reset tree [--] paths... can be used to
313 * load chosen paths from the tree into the index without
314 * affecting the working tree nor HEAD. */
316 if (reset_type == MIXED)
317 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
318 else if (reset_type != NONE)
319 die(_("Cannot do %s reset with paths."),
320 _(reset_type_names[reset_type]));
321 return read_from_tree(prefix, argv + i, sha1,
322 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
324 if (reset_type == NONE)
325 reset_type = MIXED; /* by default */
327 if (reset_type != SOFT && reset_type != MIXED)
330 if (reset_type == MIXED && is_bare_repository())
331 die(_("%s reset is not allowed in a bare repository"),
332 _(reset_type_names[reset_type]));
334 /* Soft reset does not touch the index file nor the working tree
335 * at all, but requires them in a good order. Other resets reset
336 * the index file to the tree object we are switching to. */
337 if (reset_type == SOFT)
338 die_if_unmerged_cache(reset_type);
341 if (reset_type == KEEP)
342 die_if_unmerged_cache(reset_type);
343 err = reset_index_file(sha1, reset_type, quiet);
344 if (reset_type == KEEP)
345 err = err || reset_index_file(sha1, MIXED, quiet);
347 die(_("Could not reset index file to revision '%s'."), rev);
350 /* Any resets update HEAD to the head being switched to,
351 * saving the previous head in ORIG_HEAD before. */
352 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
353 old_orig = sha1_old_orig;
354 if (!get_sha1("HEAD", sha1_orig)) {
356 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
357 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
360 delete_ref("ORIG_HEAD", old_orig, 0);
361 set_reflog_message(&msg, "updating HEAD", rev);
362 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
364 switch (reset_type) {
366 if (!update_ref_status && !quiet)
367 print_new_head_line(commit);
369 case SOFT: /* Nothing else to do. */
371 case MIXED: /* Report what has not been updated. */
372 update_index_refresh(0, NULL,
373 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
377 remove_branch_state();
379 strbuf_release(&msg);
381 return update_ref_status;