1/*
2 * "git reset" builtin command
3 *
4 * Copyright (c) 2007 Carlos Rica
5 *
6 * Based on git-reset.sh, which is
7 *
8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
9 */
10#include "builtin.h"
11#include "tag.h"
12#include "object.h"
13#include "commit.h"
14#include "run-command.h"
15#include "refs.h"
16#include "diff.h"
17#include "diffcore.h"
18#include "tree.h"
19#include "branch.h"
20#include "parse-options.h"
21#include "unpack-trees.h"
22#include "cache-tree.h"
23
24static 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>...]"),
28 NULL
29};
30
31enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
32static const char *reset_type_names[] = {
33 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
34};
35
36static inline int is_merge(void)
37{
38 return !access(git_path("MERGE_HEAD"), F_OK);
39}
40
41static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
42{
43 int nr = 1;
44 int newfd;
45 struct tree_desc desc[2];
46 struct tree *tree;
47 struct unpack_trees_options opts;
48 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
49
50 memset(&opts, 0, sizeof(opts));
51 opts.head_idx = 1;
52 opts.src_index = &the_index;
53 opts.dst_index = &the_index;
54 opts.fn = oneway_merge;
55 opts.merge = 1;
56 if (!quiet)
57 opts.verbose_update = 1;
58 switch (reset_type) {
59 case KEEP:
60 case MERGE:
61 opts.update = 1;
62 break;
63 case HARD:
64 opts.update = 1;
65 /* fallthrough */
66 default:
67 opts.reset = 1;
68 }
69
70 newfd = hold_locked_index(lock, 1);
71
72 read_cache_unmerged();
73
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."));
80 nr++;
81 opts.fn = twoway_merge;
82 }
83
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))
87 return -1;
88
89 if (reset_type == MIXED || reset_type == HARD) {
90 tree = parse_tree_indirect(sha1);
91 prime_cache_tree(&active_cache_tree, tree);
92 }
93
94 if (write_cache(newfd, active_cache, active_nr) ||
95 commit_locked_index(lock))
96 return error(_("Could not write new index file."));
97
98 return 0;
99}
100
101static void print_new_head_line(struct commit *commit)
102{
103 const char *hex, *body;
104
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");
108 if (body) {
109 const char *eol;
110 size_t len;
111 body += 2;
112 eol = strchr(body, '\n');
113 len = eol ? eol - body : strlen(body);
114 printf(" %.*s\n", (int) len, body);
115 }
116 else
117 printf("\n");
118}
119
120static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
121{
122 if (!index_lock) {
123 index_lock = xcalloc(1, sizeof(struct lock_file));
124 fd = hold_locked_index(index_lock, 1);
125 }
126
127 refresh_index(&the_index, (flags), NULL, NULL,
128 _("Unstaged changes after reset:"));
129 if (write_cache(fd, active_cache, active_nr) ||
130 commit_locked_index(index_lock))
131 return error ("Could not refresh index");
132 return 0;
133}
134
135static void update_index_from_diff(struct diff_queue_struct *q,
136 struct diff_options *opt, void *data)
137{
138 int i;
139
140 for (i = 0; i < q->nr; i++) {
141 struct diff_filespec *one = q->queue[i]->one;
142 if (one->mode && !is_null_sha1(one->sha1)) {
143 struct cache_entry *ce;
144 ce = make_cache_entry(one->mode, one->sha1, one->path,
145 0, 0);
146 if (!ce)
147 die(_("make_cache_entry failed for path '%s'"),
148 one->path);
149 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
150 ADD_CACHE_OK_TO_REPLACE);
151 } else
152 remove_file_from_cache(one->path);
153 }
154}
155
156static int interactive_reset(const char *revision, const char **argv,
157 const char *prefix)
158{
159 const char **pathspec = NULL;
160
161 if (*argv)
162 pathspec = get_pathspec(prefix, argv);
163
164 return run_add_interactive(revision, "--patch=reset", pathspec);
165}
166
167static int read_from_tree(const char *prefix, const char **argv,
168 unsigned char *tree_sha1, int refresh_flags)
169{
170 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
171 int index_fd;
172 struct diff_options opt;
173
174 memset(&opt, 0, sizeof(opt));
175 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
176 opt.output_format = DIFF_FORMAT_CALLBACK;
177 opt.format_callback = update_index_from_diff;
178
179 index_fd = hold_locked_index(lock, 1);
180 read_cache();
181 if (do_diff_cache(tree_sha1, &opt))
182 return 1;
183 diffcore_std(&opt);
184 diff_flush(&opt);
185 diff_tree_release_paths(&opt);
186
187 return update_index_refresh(index_fd, lock, refresh_flags);
188}
189
190static void set_reflog_message(struct strbuf *sb, const char *action,
191 const char *rev)
192{
193 const char *rla = getenv("GIT_REFLOG_ACTION");
194
195 strbuf_reset(sb);
196 if (rla)
197 strbuf_addf(sb, "%s: %s", rla, action);
198 else if (rev)
199 strbuf_addf(sb, "reset: moving to %s", rev);
200 else
201 strbuf_addf(sb, "reset: %s", action);
202}
203
204static void die_if_unmerged_cache(int reset_type)
205{
206 if (is_merge() || read_cache() < 0 || unmerged_cache())
207 die(_("Cannot do a %s reset in the middle of a merge."),
208 _(reset_type_names[reset_type]));
209
210}
211
212int cmd_reset(int argc, const char **argv, const char *prefix)
213{
214 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
215 int patch_mode = 0;
216 const char *rev = "HEAD";
217 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
218 *old_orig = NULL, sha1_old_orig[20];
219 struct commit *commit;
220 struct strbuf msg = STRBUF_INIT;
221 const struct option options[] = {
222 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
223 OPT_SET_INT(0, "mixed", &reset_type,
224 N_("reset HEAD and index"), MIXED),
225 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
226 OPT_SET_INT(0, "hard", &reset_type,
227 N_("reset HEAD, index and working tree"), HARD),
228 OPT_SET_INT(0, "merge", &reset_type,
229 N_("reset HEAD, index and working tree"), MERGE),
230 OPT_SET_INT(0, "keep", &reset_type,
231 N_("reset HEAD but keep local changes"), KEEP),
232 OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
233 OPT_END()
234 };
235
236 git_config(git_default_config, NULL);
237
238 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
239 PARSE_OPT_KEEP_DASHDASH);
240
241 /*
242 * Possible arguments are:
243 *
244 * git reset [-opts] <rev> <paths>...
245 * git reset [-opts] <rev> -- <paths>...
246 * git reset [-opts] -- <paths>...
247 * git reset [-opts] <paths>...
248 *
249 * At this point, argv[i] points immediately after [-opts].
250 */
251
252 if (i < argc) {
253 if (!strcmp(argv[i], "--")) {
254 i++; /* reset to HEAD, possibly with paths */
255 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
256 rev = argv[i];
257 i += 2;
258 }
259 /*
260 * Otherwise, argv[i] could be either <rev> or <paths> and
261 * has to be unambiguous.
262 */
263 else if (!get_sha1_committish(argv[i], sha1)) {
264 /*
265 * Ok, argv[i] looks like a rev; it should not
266 * be a filename.
267 */
268 verify_non_filename(prefix, argv[i]);
269 rev = argv[i++];
270 } else {
271 /* Otherwise we treat this as a filename */
272 verify_filename(prefix, argv[i], 1);
273 }
274 }
275
276 if (get_sha1_committish(rev, sha1))
277 die(_("Failed to resolve '%s' as a valid ref."), rev);
278
279 /*
280 * NOTE: As "git reset $treeish -- $path" should be usable on
281 * any tree-ish, this is not strictly correct. We are not
282 * moving the HEAD to any commit; we are merely resetting the
283 * entries in the index to that of a treeish.
284 */
285 commit = lookup_commit_reference(sha1);
286 if (!commit)
287 die(_("Could not parse object '%s'."), rev);
288 hashcpy(sha1, commit->object.sha1);
289
290 if (patch_mode) {
291 if (reset_type != NONE)
292 die(_("--patch is incompatible with --{hard,mixed,soft}"));
293 return interactive_reset(rev, argv + i, prefix);
294 }
295
296 /* git reset tree [--] paths... can be used to
297 * load chosen paths from the tree into the index without
298 * affecting the working tree nor HEAD. */
299 if (i < argc) {
300 if (reset_type == MIXED)
301 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
302 else if (reset_type != NONE)
303 die(_("Cannot do %s reset with paths."),
304 _(reset_type_names[reset_type]));
305 return read_from_tree(prefix, argv + i, sha1,
306 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
307 }
308 if (reset_type == NONE)
309 reset_type = MIXED; /* by default */
310
311 if (reset_type != SOFT && reset_type != MIXED)
312 setup_work_tree();
313
314 if (reset_type == MIXED && is_bare_repository())
315 die(_("%s reset is not allowed in a bare repository"),
316 _(reset_type_names[reset_type]));
317
318 /* Soft reset does not touch the index file nor the working tree
319 * at all, but requires them in a good order. Other resets reset
320 * the index file to the tree object we are switching to. */
321 if (reset_type == SOFT)
322 die_if_unmerged_cache(reset_type);
323 else {
324 int err;
325 if (reset_type == KEEP)
326 die_if_unmerged_cache(reset_type);
327 err = reset_index_file(sha1, reset_type, quiet);
328 if (reset_type == KEEP)
329 err = err || reset_index_file(sha1, MIXED, quiet);
330 if (err)
331 die(_("Could not reset index file to revision '%s'."), rev);
332 }
333
334 /* Any resets update HEAD to the head being switched to,
335 * saving the previous head in ORIG_HEAD before. */
336 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
337 old_orig = sha1_old_orig;
338 if (!get_sha1("HEAD", sha1_orig)) {
339 orig = sha1_orig;
340 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
341 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
342 }
343 else if (old_orig)
344 delete_ref("ORIG_HEAD", old_orig, 0);
345 set_reflog_message(&msg, "updating HEAD", rev);
346 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
347
348 switch (reset_type) {
349 case HARD:
350 if (!update_ref_status && !quiet)
351 print_new_head_line(commit);
352 break;
353 case SOFT: /* Nothing else to do. */
354 break;
355 case MIXED: /* Report what has not been updated. */
356 update_index_refresh(0, NULL,
357 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
358 break;
359 }
360
361 remove_branch_state();
362
363 strbuf_release(&msg);
364
365 return update_ref_status;
366}