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