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(const unsigned char *sha1, int reset_type, int quiet)
44 struct tree_desc desc[2];
46 struct unpack_trees_options opts;
48 memset(&opts, 0, sizeof(opts));
50 opts.src_index = &the_index;
51 opts.dst_index = &the_index;
52 opts.fn = oneway_merge;
55 opts.verbose_update = 1;
68 read_cache_unmerged();
70 if (reset_type == KEEP) {
71 unsigned char head_sha1[20];
72 if (get_sha1("HEAD", head_sha1))
73 return error(_("You do not have a valid HEAD."));
74 if (!fill_tree_descriptor(desc, head_sha1))
75 return error(_("Failed to find tree of HEAD."));
77 opts.fn = twoway_merge;
80 if (!fill_tree_descriptor(desc + nr - 1, sha1))
81 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
82 if (unpack_trees(nr, desc, &opts))
85 if (reset_type == MIXED || reset_type == HARD) {
86 tree = parse_tree_indirect(sha1);
87 prime_cache_tree(&active_cache_tree, tree);
93 static void print_new_head_line(struct commit *commit)
95 const char *hex, *body;
97 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
98 printf(_("HEAD is now at %s"), hex);
99 body = strstr(commit->buffer, "\n\n");
104 eol = strchr(body, '\n');
105 len = eol ? eol - body : strlen(body);
106 printf(" %.*s\n", (int) len, body);
112 static void update_index_from_diff(struct diff_queue_struct *q,
113 struct diff_options *opt, void *data)
117 for (i = 0; i < q->nr; i++) {
118 struct diff_filespec *one = q->queue[i]->one;
119 if (one->mode && !is_null_sha1(one->sha1)) {
120 struct cache_entry *ce;
121 ce = make_cache_entry(one->mode, one->sha1, one->path,
124 die(_("make_cache_entry failed for path '%s'"),
126 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
127 ADD_CACHE_OK_TO_REPLACE);
129 remove_file_from_cache(one->path);
133 static int read_from_tree(const char **pathspec, unsigned char *tree_sha1)
135 struct diff_options opt;
137 memset(&opt, 0, sizeof(opt));
138 diff_tree_setup_paths(pathspec, &opt);
139 opt.output_format = DIFF_FORMAT_CALLBACK;
140 opt.format_callback = update_index_from_diff;
143 if (do_diff_cache(tree_sha1, &opt))
147 diff_tree_release_paths(&opt);
152 static void set_reflog_message(struct strbuf *sb, const char *action,
155 const char *rla = getenv("GIT_REFLOG_ACTION");
159 strbuf_addf(sb, "%s: %s", rla, action);
161 strbuf_addf(sb, "reset: moving to %s", rev);
163 strbuf_addf(sb, "reset: %s", action);
166 static void die_if_unmerged_cache(int reset_type)
168 if (is_merge() || read_cache() < 0 || unmerged_cache())
169 die(_("Cannot do a %s reset in the middle of a merge."),
170 _(reset_type_names[reset_type]));
174 static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret)
176 const char *rev = "HEAD";
177 unsigned char unused[20];
179 * Possible arguments are:
181 * git reset [-opts] <rev> <paths>...
182 * git reset [-opts] <rev> -- <paths>...
183 * git reset [-opts] -- <paths>...
184 * git reset [-opts] <paths>...
186 * At this point, argv points immediately after [-opts].
190 if (!strcmp(argv[0], "--")) {
191 argv++; /* reset to HEAD, possibly with paths */
192 } else if (argv[1] && !strcmp(argv[1], "--")) {
197 * Otherwise, argv[0] could be either <rev> or <paths> and
198 * has to be unambiguous.
200 else if (!get_sha1_committish(argv[0], unused)) {
202 * Ok, argv[0] looks like a rev; it should not
205 verify_non_filename(prefix, argv[0]);
208 /* Otherwise we treat this as a filename */
209 verify_filename(prefix, argv[0], 1);
213 return argv[0] ? get_pathspec(prefix, argv) : NULL;
216 static int update_refs(const char *rev, const unsigned char *sha1)
218 int update_ref_status;
219 struct strbuf msg = STRBUF_INIT;
220 unsigned char *orig = NULL, sha1_orig[20],
221 *old_orig = NULL, sha1_old_orig[20];
223 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
224 old_orig = sha1_old_orig;
225 if (!get_sha1("HEAD", sha1_orig)) {
227 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
228 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
230 delete_ref("ORIG_HEAD", old_orig, 0);
231 set_reflog_message(&msg, "updating HEAD", rev);
232 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
233 strbuf_release(&msg);
234 return update_ref_status;
237 int cmd_reset(int argc, const char **argv, const char *prefix)
239 int reset_type = NONE, update_ref_status = 0, quiet = 0;
242 unsigned char sha1[20];
243 const char **pathspec = NULL;
244 struct commit *commit;
245 const struct option options[] = {
246 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
247 OPT_SET_INT(0, "mixed", &reset_type,
248 N_("reset HEAD and index"), MIXED),
249 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
250 OPT_SET_INT(0, "hard", &reset_type,
251 N_("reset HEAD, index and working tree"), HARD),
252 OPT_SET_INT(0, "merge", &reset_type,
253 N_("reset HEAD, index and working tree"), MERGE),
254 OPT_SET_INT(0, "keep", &reset_type,
255 N_("reset HEAD but keep local changes"), KEEP),
256 OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
260 git_config(git_default_config, NULL);
262 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
263 PARSE_OPT_KEEP_DASHDASH);
264 pathspec = parse_args(argv, prefix, &rev);
266 if (get_sha1_committish(rev, sha1))
267 die(_("Failed to resolve '%s' as a valid ref."), rev);
270 * NOTE: As "git reset $treeish -- $path" should be usable on
271 * any tree-ish, this is not strictly correct. We are not
272 * moving the HEAD to any commit; we are merely resetting the
273 * entries in the index to that of a treeish.
275 commit = lookup_commit_reference(sha1);
277 die(_("Could not parse object '%s'."), rev);
278 hashcpy(sha1, commit->object.sha1);
281 if (reset_type != NONE)
282 die(_("--patch is incompatible with --{hard,mixed,soft}"));
283 return run_add_interactive(rev, "--patch=reset", pathspec);
286 /* git reset tree [--] paths... can be used to
287 * load chosen paths from the tree into the index without
288 * affecting the working tree nor HEAD. */
290 if (reset_type == MIXED)
291 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
292 else if (reset_type != NONE)
293 die(_("Cannot do %s reset with paths."),
294 _(reset_type_names[reset_type]));
296 if (reset_type == NONE)
297 reset_type = MIXED; /* by default */
299 if (reset_type != SOFT && reset_type != MIXED)
302 if (reset_type == MIXED && is_bare_repository())
303 die(_("%s reset is not allowed in a bare repository"),
304 _(reset_type_names[reset_type]));
306 /* Soft reset does not touch the index file nor the working tree
307 * at all, but requires them in a good order. Other resets reset
308 * the index file to the tree object we are switching to. */
309 if (reset_type == SOFT || reset_type == KEEP)
310 die_if_unmerged_cache(reset_type);
312 if (reset_type != SOFT) {
313 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
314 int newfd = hold_locked_index(lock, 1);
316 if (read_from_tree(pathspec, sha1))
319 int err = reset_index(sha1, reset_type, quiet);
320 if (reset_type == KEEP && !err)
321 err = reset_index(sha1, MIXED, quiet);
323 die(_("Could not reset index file to revision '%s'."), rev);
326 if (reset_type == MIXED) { /* Report what has not been updated. */
327 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
328 refresh_index(&the_index, flags, NULL, NULL,
329 _("Unstaged changes after reset:"));
332 if (write_cache(newfd, active_cache, active_nr) ||
333 commit_locked_index(lock))
334 die(_("Could not write new index file."));
338 /* Any resets without paths update HEAD to the head being
339 * switched to, saving the previous head in ORIG_HEAD before. */
340 update_ref_status = update_refs(rev, sha1);
342 if (reset_type == HARD && !update_ref_status && !quiet)
343 print_new_head_line(commit);
345 remove_branch_state();
348 return update_ref_status;