]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/reset.c
RelNotes: refer to the rebase -i "todo list", not "insn sheet"
[thirdparty/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"
697cc8ef 11#include "lockfile.h"
0e5a7faa
CR
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"
c369e7b8 20#include "branch.h"
5eee6b28 21#include "parse-options.h"
d0f379c2
SB
22#include "unpack-trees.h"
23#include "cache-tree.h"
0e5a7faa 24
5eee6b28 25static const char * const git_reset_usage[] = {
c1e9c2a7 26 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
bf44142f
MZ
27 N_("git reset [-q] <tree-ish> [--] <paths>..."),
28 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
5eee6b28
CR
29 NULL
30};
0e5a7faa 31
9bc454df
CC
32enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
33static const char *reset_type_names[] = {
8b2a57b6 34 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
9bc454df 35};
9e8eceab 36
0e5a7faa
CR
37static inline int is_merge(void)
38{
39 return !access(git_path("MERGE_HEAD"), F_OK);
40}
41
b7099a06 42static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
0e5a7faa 43{
d0f379c2 44 int nr = 1;
d0f379c2 45 struct tree_desc desc[2];
6c52ec8a 46 struct tree *tree;
d0f379c2 47 struct unpack_trees_options opts;
0e5a7faa 48
d0f379c2
SB
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;
5aa965a0 55 if (!quiet)
d0f379c2 56 opts.verbose_update = 1;
9e8eceab 57 switch (reset_type) {
9bc454df 58 case KEEP:
9e8eceab 59 case MERGE:
d0f379c2 60 opts.update = 1;
9e8eceab
LT
61 break;
62 case HARD:
d0f379c2 63 opts.update = 1;
9e8eceab
LT
64 /* fallthrough */
65 default:
d0f379c2 66 opts.reset = 1;
9e8eceab 67 }
0e5a7faa 68
d0f379c2
SB
69 read_cache_unmerged();
70
9bc454df
CC
71 if (reset_type == KEEP) {
72 unsigned char head_sha1[20];
73 if (get_sha1("HEAD", head_sha1))
b50a64e8 74 return error(_("You do not have a valid HEAD."));
9bc454df 75 if (!fill_tree_descriptor(desc, head_sha1))
b50a64e8 76 return error(_("Failed to find tree of HEAD."));
9bc454df
CC
77 nr++;
78 opts.fn = twoway_merge;
79 }
80
d0f379c2 81 if (!fill_tree_descriptor(desc + nr - 1, sha1))
b50a64e8 82 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
d0f379c2
SB
83 if (unpack_trees(nr, desc, &opts))
84 return -1;
6c52ec8a
TR
85
86 if (reset_type == MIXED || reset_type == HARD) {
87 tree = parse_tree_indirect(sha1);
e6c286e8 88 prime_cache_tree(&the_index, tree);
6c52ec8a
TR
89 }
90
d0f379c2 91 return 0;
0e5a7faa
CR
92}
93
94static void print_new_head_line(struct commit *commit)
95{
2efb3b06 96 const char *hex, *body;
b000c59b 97 const char *msg;
0e5a7faa
CR
98
99 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
b50a64e8 100 printf(_("HEAD is now at %s"), hex);
ecaee805
AS
101 msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
102 body = strstr(msg, "\n\n");
0e5a7faa
CR
103 if (body) {
104 const char *eol;
105 size_t len;
106 body += 2;
107 eol = strchr(body, '\n');
108 len = eol ? eol - body : strlen(body);
109 printf(" %.*s\n", (int) len, body);
110 }
111 else
112 printf("\n");
b66103c3 113 unuse_commit_buffer(commit, msg);
0e5a7faa
CR
114}
115
0e5a7faa
CR
116static void update_index_from_diff(struct diff_queue_struct *q,
117 struct diff_options *opt, void *data)
118{
119 int i;
b4b313f9 120 int intent_to_add = *(int *)data;
0e5a7faa
CR
121
122 for (i = 0; i < q->nr; i++) {
123 struct diff_filespec *one = q->queue[i]->one;
b4b313f9
NTND
124 int is_missing = !(one->mode && !is_null_sha1(one->sha1));
125 struct cache_entry *ce;
126
127 if (is_missing && !intent_to_add) {
0e5a7faa 128 remove_file_from_cache(one->path);
b4b313f9
NTND
129 continue;
130 }
131
132 ce = make_cache_entry(one->mode, one->sha1, one->path,
133 0, 0);
134 if (!ce)
135 die(_("make_cache_entry failed for path '%s'"),
136 one->path);
137 if (is_missing) {
138 ce->ce_flags |= CE_INTENT_TO_ADD;
139 set_object_name_for_intent_to_add_entry(ce);
140 }
141 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
0e5a7faa
CR
142 }
143}
144
bd1928df 145static int read_from_tree(const struct pathspec *pathspec,
b4b313f9
NTND
146 unsigned char *tree_sha1,
147 int intent_to_add)
0e5a7faa 148{
0e5a7faa
CR
149 struct diff_options opt;
150
151 memset(&opt, 0, sizeof(opt));
bd1928df 152 copy_pathspec(&opt.pathspec, pathspec);
0e5a7faa
CR
153 opt.output_format = DIFF_FORMAT_CALLBACK;
154 opt.format_callback = update_index_from_diff;
b4b313f9 155 opt.format_callback_data = &intent_to_add;
0e5a7faa 156
0e5a7faa
CR
157 if (do_diff_cache(tree_sha1, &opt))
158 return 1;
159 diffcore_std(&opt);
160 diff_flush(&opt);
bd1928df 161 free_pathspec(&opt.pathspec);
2e7a9785 162
bf883f30 163 return 0;
0e5a7faa
CR
164}
165
d04520e3
JK
166static void set_reflog_message(struct strbuf *sb, const char *action,
167 const char *rev)
0e5a7faa 168{
0e5a7faa 169 const char *rla = getenv("GIT_REFLOG_ACTION");
d04520e3
JK
170
171 strbuf_reset(sb);
172 if (rla)
173 strbuf_addf(sb, "%s: %s", rla, action);
174 else if (rev)
175 strbuf_addf(sb, "reset: moving to %s", rev);
176 else
177 strbuf_addf(sb, "reset: %s", action);
0e5a7faa
CR
178}
179
812d2a3d
CC
180static void die_if_unmerged_cache(int reset_type)
181{
2c63d6eb 182 if (is_merge() || unmerged_cache())
8b2a57b6
ÆAB
183 die(_("Cannot do a %s reset in the middle of a merge."),
184 _(reset_type_names[reset_type]));
812d2a3d
CC
185
186}
187
f8144c9f
NTND
188static void parse_args(struct pathspec *pathspec,
189 const char **argv, const char *prefix,
190 int patch_mode,
191 const char **rev_ret)
0e5a7faa 192{
0e5a7faa 193 const char *rev = "HEAD";
39ea722d 194 unsigned char unused[20];
dfc8f39e
JH
195 /*
196 * Possible arguments are:
197 *
2f328c3d
MZ
198 * git reset [-opts] [<rev>]
199 * git reset [-opts] <tree> [<paths>...]
200 * git reset [-opts] <tree> -- [<paths>...]
201 * git reset [-opts] -- [<paths>...]
dfc8f39e
JH
202 * git reset [-opts] <paths>...
203 *
dca48cf5 204 * At this point, argv points immediately after [-opts].
dfc8f39e
JH
205 */
206
dca48cf5
MZ
207 if (argv[0]) {
208 if (!strcmp(argv[0], "--")) {
209 argv++; /* reset to HEAD, possibly with paths */
210 } else if (argv[1] && !strcmp(argv[1], "--")) {
211 rev = argv[0];
212 argv += 2;
dfc8f39e
JH
213 }
214 /*
dca48cf5 215 * Otherwise, argv[0] could be either <rev> or <paths> and
2f328c3d
MZ
216 * has to be unambiguous. If there is a single argument, it
217 * can not be a tree
dfc8f39e 218 */
2f328c3d
MZ
219 else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) ||
220 (argv[1] && !get_sha1_treeish(argv[0], unused))) {
dfc8f39e 221 /*
2f328c3d 222 * Ok, argv[0] looks like a commit/tree; it should not
dfc8f39e
JH
223 * be a filename.
224 */
dca48cf5
MZ
225 verify_non_filename(prefix, argv[0]);
226 rev = *argv++;
dfc8f39e
JH
227 } else {
228 /* Otherwise we treat this as a filename */
dca48cf5 229 verify_filename(prefix, argv[0], 1);
dfc8f39e
JH
230 }
231 }
39ea722d 232 *rev_ret = rev;
2c63d6eb
JK
233
234 if (read_cache() < 0)
235 die(_("index file corrupt"));
236
480ca644
NTND
237 parse_pathspec(pathspec, 0,
238 PATHSPEC_PREFER_FULL |
2c63d6eb 239 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
480ca644 240 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
f8144c9f 241 prefix, argv);
39ea722d
MZ
242}
243
2be778a8 244static int reset_refs(const char *rev, const unsigned char *sha1)
7bca0e45
MZ
245{
246 int update_ref_status;
247 struct strbuf msg = STRBUF_INIT;
248 unsigned char *orig = NULL, sha1_orig[20],
249 *old_orig = NULL, sha1_old_orig[20];
250
251 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
252 old_orig = sha1_old_orig;
253 if (!get_sha1("HEAD", sha1_orig)) {
254 orig = sha1_orig;
255 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
f4124112
MH
256 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
257 UPDATE_REFS_MSG_ON_ERR);
7bca0e45
MZ
258 } else if (old_orig)
259 delete_ref("ORIG_HEAD", old_orig, 0);
260 set_reflog_message(&msg, "updating HEAD", rev);
f4124112
MH
261 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0,
262 UPDATE_REFS_MSG_ON_ERR);
7bca0e45
MZ
263 strbuf_release(&msg);
264 return update_ref_status;
265}
266
39ea722d
MZ
267int cmd_reset(int argc, const char **argv, const char *prefix)
268{
269 int reset_type = NONE, update_ref_status = 0, quiet = 0;
166ec2e9 270 int patch_mode = 0, unborn;
39ea722d 271 const char *rev;
7bca0e45 272 unsigned char sha1[20];
f8144c9f 273 struct pathspec pathspec;
b4b313f9 274 int intent_to_add = 0;
39ea722d
MZ
275 const struct option options[] = {
276 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
277 OPT_SET_INT(0, "mixed", &reset_type,
278 N_("reset HEAD and index"), MIXED),
279 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
280 OPT_SET_INT(0, "hard", &reset_type,
281 N_("reset HEAD, index and working tree"), HARD),
282 OPT_SET_INT(0, "merge", &reset_type,
283 N_("reset HEAD, index and working tree"), MERGE),
284 OPT_SET_INT(0, "keep", &reset_type,
285 N_("reset HEAD but keep local changes"), KEEP),
d5d09d47 286 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
b4b313f9
NTND
287 OPT_BOOL('N', "intent-to-add", &intent_to_add,
288 N_("record only the fact that removed paths will be added later")),
39ea722d
MZ
289 OPT_END()
290 };
291
292 git_config(git_default_config, NULL);
293
294 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
295 PARSE_OPT_KEEP_DASHDASH);
f8144c9f 296 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
0e5a7faa 297
166ec2e9
MZ
298 unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1);
299 if (unborn) {
300 /* reset on unborn branch: treat as reset to empty tree */
301 hashcpy(sha1, EMPTY_TREE_SHA1_BIN);
f8144c9f 302 } else if (!pathspec.nr) {
2f328c3d
MZ
303 struct commit *commit;
304 if (get_sha1_committish(rev, sha1))
305 die(_("Failed to resolve '%s' as a valid revision."), rev);
306 commit = lookup_commit_reference(sha1);
307 if (!commit)
308 die(_("Could not parse object '%s'."), rev);
309 hashcpy(sha1, commit->object.sha1);
310 } else {
311 struct tree *tree;
312 if (get_sha1_treeish(rev, sha1))
313 die(_("Failed to resolve '%s' as a valid tree."), rev);
314 tree = parse_tree_indirect(sha1);
315 if (!tree)
316 die(_("Could not parse object '%s'."), rev);
317 hashcpy(sha1, tree->object.sha1);
318 }
0e5a7faa 319
d002ef4d
TR
320 if (patch_mode) {
321 if (reset_type != NONE)
b50a64e8 322 die(_("--patch is incompatible with --{hard,mixed,soft}"));
b3e9ce13 323 return run_add_interactive(rev, "--patch=reset", &pathspec);
d002ef4d
TR
324 }
325
0e5a7faa
CR
326 /* git reset tree [--] paths... can be used to
327 * load chosen paths from the tree into the index without
328 * affecting the working tree nor HEAD. */
f8144c9f 329 if (pathspec.nr) {
0e5a7faa 330 if (reset_type == MIXED)
b50a64e8 331 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
0e5a7faa 332 else if (reset_type != NONE)
8b2a57b6
ÆAB
333 die(_("Cannot do %s reset with paths."),
334 _(reset_type_names[reset_type]));
0e5a7faa
CR
335 }
336 if (reset_type == NONE)
337 reset_type = MIXED; /* by default */
338
b7756d41 339 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
cd0f0f68 340 setup_work_tree();
49b9362f 341
2b06b0a0 342 if (reset_type == MIXED && is_bare_repository())
8b2a57b6
ÆAB
343 die(_("%s reset is not allowed in a bare repository"),
344 _(reset_type_names[reset_type]));
2b06b0a0 345
b4b313f9
NTND
346 if (intent_to_add && reset_type != MIXED)
347 die(_("-N can only be used with --mixed"));
348
0e5a7faa
CR
349 /* Soft reset does not touch the index file nor the working tree
350 * at all, but requires them in a good order. Other resets reset
351 * the index file to the tree object we are switching to. */
352f58a5 352 if (reset_type == SOFT || reset_type == KEEP)
812d2a3d 353 die_if_unmerged_cache(reset_type);
352f58a5
MZ
354
355 if (reset_type != SOFT) {
4e83ab3e 356 struct lock_file *lock = xcalloc(1, sizeof(*lock));
03b86647 357 hold_locked_index(lock, 1);
3fde386a 358 if (reset_type == MIXED) {
f38798f4 359 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
b4b313f9 360 if (read_from_tree(&pathspec, sha1, intent_to_add))
3bbf2f20 361 return 1;
b7756d41
NTND
362 if (get_git_work_tree())
363 refresh_index(&the_index, flags, NULL, NULL,
364 _("Unstaged changes after reset:"));
3bbf2f20
MZ
365 } else {
366 int err = reset_index(sha1, reset_type, quiet);
367 if (reset_type == KEEP && !err)
368 err = reset_index(sha1, MIXED, quiet);
369 if (err)
370 die(_("Could not reset index file to revision '%s'."), rev);
371 }
bc41bf42 372
03b86647 373 if (write_locked_index(&the_index, lock, COMMIT_LOCK))
1ca38f85 374 die(_("Could not write new index file."));
0e5a7faa 375 }
0e5a7faa 376
f8144c9f 377 if (!pathspec.nr && !unborn) {
3bbf2f20
MZ
378 /* Any resets without paths update HEAD to the head being
379 * switched to, saving the previous head in ORIG_HEAD before. */
2be778a8 380 update_ref_status = reset_refs(rev, sha1);
0e5a7faa 381
3bbf2f20 382 if (reset_type == HARD && !update_ref_status && !quiet)
2f328c3d 383 print_new_head_line(lookup_commit_reference(sha1));
3bbf2f20 384 }
f8144c9f 385 if (!pathspec.nr)
166ec2e9 386 remove_branch_state();
0e5a7faa 387
0e5a7faa
CR
388 return update_ref_status;
389}