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