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 [--stage | --work | --keep] [-q] [<commit>]"),
27 N_("git reset [-q] <tree-ish> [--] <paths>..."),
28 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
32 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
33 static const char *reset_type_names[] = {
34 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
37 static inline int is_merge(void)
39 return !access(git_path("MERGE_HEAD"), F_OK);
42 static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
45 struct tree_desc desc[2];
47 struct unpack_trees_options opts;
49 memset(&opts, 0, sizeof(opts));
51 opts.src_index = &the_index;
52 opts.dst_index = &the_index;
53 opts.fn = oneway_merge;
56 opts.verbose_update = 1;
69 read_cache_unmerged();
71 if (reset_type == KEEP) {
72 unsigned char head_sha1[20];
73 if (get_sha1("HEAD", head_sha1))
74 return error(_("You do not have a valid HEAD."));
75 if (!fill_tree_descriptor(desc, head_sha1))
76 return error(_("Failed to find tree of HEAD."));
78 opts.fn = twoway_merge;
81 if (!fill_tree_descriptor(desc + nr - 1, sha1))
82 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
83 if (unpack_trees(nr, desc, &opts))
86 if (reset_type == MIXED || reset_type == HARD) {
87 tree = parse_tree_indirect(sha1);
88 prime_cache_tree(&active_cache_tree, tree);
94 static void print_new_head_line(struct commit *commit)
96 const char *hex, *body;
99 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
100 printf(_("HEAD is now at %s"), hex);
101 msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
102 body = strstr(msg, "\n\n");
107 eol = strchr(body, '\n');
108 len = eol ? eol - body : strlen(body);
109 printf(" %.*s\n", (int) len, body);
113 logmsg_free(msg, commit);
116 static void update_index_from_diff(struct diff_queue_struct *q,
117 struct diff_options *opt, void *data)
121 for (i = 0; i < q->nr; i++) {
122 struct diff_filespec *one = q->queue[i]->one;
123 if (one->mode && !is_null_sha1(one->sha1)) {
124 struct cache_entry *ce;
125 ce = make_cache_entry(one->mode, one->sha1, one->path,
128 die(_("make_cache_entry failed for path '%s'"),
130 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
131 ADD_CACHE_OK_TO_REPLACE);
133 remove_file_from_cache(one->path);
137 static int read_from_tree(const struct pathspec *pathspec,
138 unsigned char *tree_sha1)
140 struct diff_options opt;
142 memset(&opt, 0, sizeof(opt));
143 copy_pathspec(&opt.pathspec, pathspec);
144 opt.output_format = DIFF_FORMAT_CALLBACK;
145 opt.format_callback = update_index_from_diff;
147 if (do_diff_cache(tree_sha1, &opt))
151 free_pathspec(&opt.pathspec);
156 static void set_reflog_message(struct strbuf *sb, const char *action,
159 const char *rla = getenv("GIT_REFLOG_ACTION");
163 strbuf_addf(sb, "%s: %s", rla, action);
165 strbuf_addf(sb, "reset: moving to %s", rev);
167 strbuf_addf(sb, "reset: %s", action);
170 static void die_if_unmerged_cache(int reset_type)
172 if (is_merge() || unmerged_cache())
173 die(_("Cannot do a %s reset in the middle of a merge."),
174 _(reset_type_names[reset_type]));
178 static void parse_args(struct pathspec *pathspec,
179 const char **argv, const char *prefix,
181 const char **rev_ret)
183 const char *rev = "HEAD";
184 unsigned char unused[20];
186 * Possible arguments are:
188 * git reset [-opts] [<rev>]
189 * git reset [-opts] <tree> [<paths>...]
190 * git reset [-opts] <tree> -- [<paths>...]
191 * git reset [-opts] -- [<paths>...]
192 * git reset [-opts] <paths>...
194 * At this point, argv points immediately after [-opts].
198 if (!strcmp(argv[0], "--")) {
199 argv++; /* reset to HEAD, possibly with paths */
200 } else if (argv[1] && !strcmp(argv[1], "--")) {
205 * Otherwise, argv[0] could be either <rev> or <paths> and
206 * has to be unambiguous. If there is a single argument, it
209 else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) ||
210 (argv[1] && !get_sha1_treeish(argv[0], unused))) {
212 * Ok, argv[0] looks like a commit/tree; it should not
215 verify_non_filename(prefix, argv[0]);
218 /* Otherwise we treat this as a filename */
219 verify_filename(prefix, argv[0], 1);
224 if (read_cache() < 0)
225 die(_("index file corrupt"));
227 parse_pathspec(pathspec, 0,
228 PATHSPEC_PREFER_FULL |
229 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
230 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
234 static int reset_refs(const char *rev, const unsigned char *sha1)
236 int update_ref_status;
237 struct strbuf msg = STRBUF_INIT;
238 unsigned char *orig = NULL, sha1_orig[20],
239 *old_orig = NULL, sha1_old_orig[20];
241 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
242 old_orig = sha1_old_orig;
243 if (!get_sha1("HEAD", sha1_orig)) {
245 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
246 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
248 delete_ref("ORIG_HEAD", old_orig, 0);
249 set_reflog_message(&msg, "updating HEAD", rev);
250 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
251 strbuf_release(&msg);
252 return update_ref_status;
255 int cmd_reset(int argc, const char **argv, const char *prefix)
257 int reset_type = NONE, update_ref_status = 0, quiet = 0;
258 int stage = -1, working_tree = -1;
259 int patch_mode = 0, unborn;
261 unsigned char sha1[20];
262 struct pathspec pathspec;
263 const struct option options[] = {
264 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
265 OPT_SET_INT(0, "mixed", &reset_type,
266 N_("reset HEAD and index"), MIXED),
267 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
268 OPT_SET_INT(0, "hard", &reset_type,
269 N_("reset HEAD, index and working tree"), HARD),
270 OPT_SET_INT(0, "merge", &reset_type,
271 N_("reset HEAD, index and working tree"), MERGE),
272 OPT_SET_INT(0, "keep", &reset_type,
273 N_("reset HEAD but keep local changes"), KEEP),
274 OPT_BOOL(0, "stage", &stage, N_("reset index")),
275 OPT_BOOL(0, "work", &working_tree, N_("reset working tree")),
276 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
280 git_config(git_default_config, NULL);
282 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
283 PARSE_OPT_KEEP_DASHDASH);
284 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
286 unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1);
288 /* reset on unborn branch: treat as reset to empty tree */
289 hashcpy(sha1, EMPTY_TREE_SHA1_BIN);
290 } else if (!pathspec.nr) {
291 struct commit *commit;
292 if (get_sha1_committish(rev, sha1))
293 die(_("Failed to resolve '%s' as a valid revision."), rev);
294 commit = lookup_commit_reference(sha1);
296 die(_("Could not parse object '%s'."), rev);
297 hashcpy(sha1, commit->object.sha1);
300 if (get_sha1_treeish(rev, sha1))
301 die(_("Failed to resolve '%s' as a valid tree."), rev);
302 tree = parse_tree_indirect(sha1);
304 die(_("Could not parse object '%s'."), rev);
305 hashcpy(sha1, tree->object.sha1);
308 if (stage >= 0 || working_tree >= 0) {
311 if (reset_type == KEEP) {
312 if (working_tree == 1)
313 die(_("--keep is incompatible with --work"));
315 } else if (reset_type != NONE) {
316 die(_("--{stage,work} are incompatible with --{hard,mixed,soft,merge}"));
319 if (working_tree == 1) {
321 die(_("--no-stage doesn't make sense with --work"));
325 reset_type = keep ? KEEP : NONE;
332 if (reset_type != NONE)
333 die(_("--patch is incompatible with --{hard,mixed,soft}"));
334 return run_add_interactive(rev, "--patch=reset", &pathspec);
337 /* git reset tree [--] paths... can be used to
338 * load chosen paths from the tree into the index without
339 * affecting the working tree nor HEAD. */
341 if (reset_type == MIXED)
342 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
343 else if (reset_type != NONE)
344 die(_("Cannot do %s reset with paths."),
345 _(reset_type_names[reset_type]));
347 if (reset_type == NONE)
348 reset_type = MIXED; /* by default */
350 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
353 if (reset_type == MIXED && is_bare_repository())
354 die(_("%s reset is not allowed in a bare repository"),
355 _(reset_type_names[reset_type]));
357 /* Soft reset does not touch the index file nor the working tree
358 * at all, but requires them in a good order. Other resets reset
359 * the index file to the tree object we are switching to. */
360 if (reset_type == SOFT || reset_type == KEEP)
361 die_if_unmerged_cache(reset_type);
363 if (reset_type != SOFT) {
364 struct lock_file *lock = xcalloc(1, sizeof(*lock));
365 int newfd = hold_locked_index(lock, 1);
366 if (reset_type == MIXED) {
367 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
368 if (read_from_tree(&pathspec, sha1))
370 if (get_git_work_tree())
371 refresh_index(&the_index, flags, NULL, NULL,
372 _("Unstaged changes after reset:"));
374 int err = reset_index(sha1, reset_type, quiet);
375 if (reset_type == KEEP && !err)
376 err = reset_index(sha1, MIXED, quiet);
378 die(_("Could not reset index file to revision '%s'."), rev);
381 if (write_cache(newfd, active_cache, active_nr) ||
382 commit_locked_index(lock))
383 die(_("Could not write new index file."));
386 if (!pathspec.nr && !unborn) {
387 /* Any resets without paths update HEAD to the head being
388 * switched to, saving the previous head in ORIG_HEAD before. */
389 update_ref_status = reset_refs(rev, sha1);
391 if (reset_type == HARD && !update_ref_status && !quiet)
392 print_new_head_line(lookup_commit_reference(sha1));
395 remove_branch_state();
397 return update_ref_status;