reset --hard: make use of the pretty machinery
[git.git] / builtin / reset.c
blob651d98303b6e0f3307dfb613a6042971757413ec
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 #include "builtin.h"
11 #include "lockfile.h"
12 #include "tag.h"
13 #include "object.h"
14 #include "commit.h"
15 #include "run-command.h"
16 #include "refs.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "tree.h"
20 #include "branch.h"
21 #include "parse-options.h"
22 #include "unpack-trees.h"
23 #include "cache-tree.h"
25 static const char * const git_reset_usage[] = {
26 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
27 N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
28 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
29 NULL
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 struct object_id *oid, int reset_type, int quiet)
44 int nr = 1;
45 struct tree_desc desc[2];
46 struct tree *tree;
47 struct unpack_trees_options opts;
49 memset(&opts, 0, sizeof(opts));
50 opts.head_idx = 1;
51 opts.src_index = &the_index;
52 opts.dst_index = &the_index;
53 opts.fn = oneway_merge;
54 opts.merge = 1;
55 if (!quiet)
56 opts.verbose_update = 1;
57 switch (reset_type) {
58 case KEEP:
59 case MERGE:
60 opts.update = 1;
61 break;
62 case HARD:
63 opts.update = 1;
64 /* fallthrough */
65 default:
66 opts.reset = 1;
69 read_cache_unmerged();
71 if (reset_type == KEEP) {
72 struct object_id head_oid;
73 if (get_oid("HEAD", &head_oid))
74 return error(_("You do not have a valid HEAD."));
75 if (!fill_tree_descriptor(desc, head_oid.hash))
76 return error(_("Failed to find tree of HEAD."));
77 nr++;
78 opts.fn = twoway_merge;
81 if (!fill_tree_descriptor(desc + nr - 1, oid->hash))
82 return error(_("Failed to find tree of %s."), oid_to_hex(oid));
83 if (unpack_trees(nr, desc, &opts))
84 return -1;
86 if (reset_type == MIXED || reset_type == HARD) {
87 tree = parse_tree_indirect(oid->hash);
88 prime_cache_tree(&the_index, tree);
91 return 0;
94 static void print_new_head_line(struct commit *commit)
96 struct strbuf buf = STRBUF_INIT;
98 printf(_("HEAD is now at %s"),
99 find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
101 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
102 if (buf.len > 0)
103 printf(" %s", buf.buf);
104 putchar('\n');
105 strbuf_release(&buf);
108 static void update_index_from_diff(struct diff_queue_struct *q,
109 struct diff_options *opt, void *data)
111 int i;
112 int intent_to_add = *(int *)data;
114 for (i = 0; i < q->nr; i++) {
115 struct diff_filespec *one = q->queue[i]->one;
116 int is_missing = !(one->mode && !is_null_oid(&one->oid));
117 struct cache_entry *ce;
119 if (is_missing && !intent_to_add) {
120 remove_file_from_cache(one->path);
121 continue;
124 ce = make_cache_entry(one->mode, one->oid.hash, one->path,
125 0, 0);
126 if (!ce)
127 die(_("make_cache_entry failed for path '%s'"),
128 one->path);
129 if (is_missing) {
130 ce->ce_flags |= CE_INTENT_TO_ADD;
131 set_object_name_for_intent_to_add_entry(ce);
133 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
137 static int read_from_tree(const struct pathspec *pathspec,
138 struct object_id *tree_oid,
139 int intent_to_add)
141 struct diff_options opt;
143 memset(&opt, 0, sizeof(opt));
144 copy_pathspec(&opt.pathspec, pathspec);
145 opt.output_format = DIFF_FORMAT_CALLBACK;
146 opt.format_callback = update_index_from_diff;
147 opt.format_callback_data = &intent_to_add;
149 if (do_diff_cache(tree_oid->hash, &opt))
150 return 1;
151 diffcore_std(&opt);
152 diff_flush(&opt);
153 clear_pathspec(&opt.pathspec);
155 return 0;
158 static void set_reflog_message(struct strbuf *sb, const char *action,
159 const char *rev)
161 const char *rla = getenv("GIT_REFLOG_ACTION");
163 strbuf_reset(sb);
164 if (rla)
165 strbuf_addf(sb, "%s: %s", rla, action);
166 else if (rev)
167 strbuf_addf(sb, "reset: moving to %s", rev);
168 else
169 strbuf_addf(sb, "reset: %s", action);
172 static void die_if_unmerged_cache(int reset_type)
174 if (is_merge() || unmerged_cache())
175 die(_("Cannot do a %s reset in the middle of a merge."),
176 _(reset_type_names[reset_type]));
180 static void parse_args(struct pathspec *pathspec,
181 const char **argv, const char *prefix,
182 int patch_mode,
183 const char **rev_ret)
185 const char *rev = "HEAD";
186 struct object_id unused;
188 * Possible arguments are:
190 * git reset [-opts] [<rev>]
191 * git reset [-opts] <tree> [<paths>...]
192 * git reset [-opts] <tree> -- [<paths>...]
193 * git reset [-opts] -- [<paths>...]
194 * git reset [-opts] <paths>...
196 * At this point, argv points immediately after [-opts].
199 if (argv[0]) {
200 if (!strcmp(argv[0], "--")) {
201 argv++; /* reset to HEAD, possibly with paths */
202 } else if (argv[1] && !strcmp(argv[1], "--")) {
203 rev = argv[0];
204 argv += 2;
207 * Otherwise, argv[0] could be either <rev> or <paths> and
208 * has to be unambiguous. If there is a single argument, it
209 * can not be a tree
211 else if ((!argv[1] && !get_sha1_committish(argv[0], unused.hash)) ||
212 (argv[1] && !get_sha1_treeish(argv[0], unused.hash))) {
214 * Ok, argv[0] looks like a commit/tree; it should not
215 * be a filename.
217 verify_non_filename(prefix, argv[0]);
218 rev = *argv++;
219 } else {
220 /* Otherwise we treat this as a filename */
221 verify_filename(prefix, argv[0], 1);
224 *rev_ret = rev;
226 if (read_cache() < 0)
227 die(_("index file corrupt"));
229 parse_pathspec(pathspec, 0,
230 PATHSPEC_PREFER_FULL |
231 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
232 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
233 prefix, argv);
236 static int reset_refs(const char *rev, const struct object_id *oid)
238 int update_ref_status;
239 struct strbuf msg = STRBUF_INIT;
240 struct object_id *orig = NULL, oid_orig,
241 *old_orig = NULL, oid_old_orig;
243 if (!get_oid("ORIG_HEAD", &oid_old_orig))
244 old_orig = &oid_old_orig;
245 if (!get_oid("HEAD", &oid_orig)) {
246 orig = &oid_orig;
247 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
248 update_ref_oid(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
249 UPDATE_REFS_MSG_ON_ERR);
250 } else if (old_orig)
251 delete_ref(NULL, "ORIG_HEAD", old_orig->hash, 0);
252 set_reflog_message(&msg, "updating HEAD", rev);
253 update_ref_status = update_ref_oid(msg.buf, "HEAD", oid, orig, 0,
254 UPDATE_REFS_MSG_ON_ERR);
255 strbuf_release(&msg);
256 return update_ref_status;
259 int cmd_reset(int argc, const char **argv, const char *prefix)
261 int reset_type = NONE, update_ref_status = 0, quiet = 0;
262 int patch_mode = 0, unborn;
263 const char *rev;
264 struct object_id oid;
265 struct pathspec pathspec;
266 int intent_to_add = 0;
267 const struct option options[] = {
268 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
269 OPT_SET_INT(0, "mixed", &reset_type,
270 N_("reset HEAD and index"), MIXED),
271 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
272 OPT_SET_INT(0, "hard", &reset_type,
273 N_("reset HEAD, index and working tree"), HARD),
274 OPT_SET_INT(0, "merge", &reset_type,
275 N_("reset HEAD, index and working tree"), MERGE),
276 OPT_SET_INT(0, "keep", &reset_type,
277 N_("reset HEAD but keep local changes"), KEEP),
278 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
279 OPT_BOOL('N', "intent-to-add", &intent_to_add,
280 N_("record only the fact that removed paths will be added later")),
281 OPT_END()
284 git_config(git_default_config, NULL);
286 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
287 PARSE_OPT_KEEP_DASHDASH);
288 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
290 unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", oid.hash);
291 if (unborn) {
292 /* reset on unborn branch: treat as reset to empty tree */
293 hashcpy(oid.hash, EMPTY_TREE_SHA1_BIN);
294 } else if (!pathspec.nr) {
295 struct commit *commit;
296 if (get_sha1_committish(rev, oid.hash))
297 die(_("Failed to resolve '%s' as a valid revision."), rev);
298 commit = lookup_commit_reference(oid.hash);
299 if (!commit)
300 die(_("Could not parse object '%s'."), rev);
301 oidcpy(&oid, &commit->object.oid);
302 } else {
303 struct tree *tree;
304 if (get_sha1_treeish(rev, oid.hash))
305 die(_("Failed to resolve '%s' as a valid tree."), rev);
306 tree = parse_tree_indirect(oid.hash);
307 if (!tree)
308 die(_("Could not parse object '%s'."), rev);
309 oidcpy(&oid, &tree->object.oid);
312 if (patch_mode) {
313 if (reset_type != NONE)
314 die(_("--patch is incompatible with --{hard,mixed,soft}"));
315 return run_add_interactive(rev, "--patch=reset", &pathspec);
318 /* git reset tree [--] paths... can be used to
319 * load chosen paths from the tree into the index without
320 * affecting the working tree nor HEAD. */
321 if (pathspec.nr) {
322 if (reset_type == MIXED)
323 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
324 else if (reset_type != NONE)
325 die(_("Cannot do %s reset with paths."),
326 _(reset_type_names[reset_type]));
328 if (reset_type == NONE)
329 reset_type = MIXED; /* by default */
331 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
332 setup_work_tree();
334 if (reset_type == MIXED && is_bare_repository())
335 die(_("%s reset is not allowed in a bare repository"),
336 _(reset_type_names[reset_type]));
338 if (intent_to_add && reset_type != MIXED)
339 die(_("-N can only be used with --mixed"));
341 /* Soft reset does not touch the index file nor the working tree
342 * at all, but requires them in a good order. Other resets reset
343 * the index file to the tree object we are switching to. */
344 if (reset_type == SOFT || reset_type == KEEP)
345 die_if_unmerged_cache(reset_type);
347 if (reset_type != SOFT) {
348 struct lock_file *lock = xcalloc(1, sizeof(*lock));
349 hold_locked_index(lock, LOCK_DIE_ON_ERROR);
350 if (reset_type == MIXED) {
351 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
352 if (read_from_tree(&pathspec, &oid, intent_to_add))
353 return 1;
354 if (get_git_work_tree())
355 refresh_index(&the_index, flags, NULL, NULL,
356 _("Unstaged changes after reset:"));
357 } else {
358 int err = reset_index(&oid, reset_type, quiet);
359 if (reset_type == KEEP && !err)
360 err = reset_index(&oid, MIXED, quiet);
361 if (err)
362 die(_("Could not reset index file to revision '%s'."), rev);
365 if (write_locked_index(&the_index, lock, COMMIT_LOCK))
366 die(_("Could not write new index file."));
369 if (!pathspec.nr && !unborn) {
370 /* Any resets without paths update HEAD to the head being
371 * switched to, saving the previous head in ORIG_HEAD before. */
372 update_ref_status = reset_refs(rev, &oid);
374 if (reset_type == HARD && !update_ref_status && !quiet)
375 print_new_head_line(lookup_commit_reference(oid.hash));
377 if (!pathspec.nr)
378 remove_branch_state();
380 return update_ref_status;