]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/reset.c
cocci: add a index-compatibility.pending.cocci
[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 */
f8adbec9 10#define USE_THE_INDEX_COMPATIBILITY_MACROS
c2e86add 11#include "builtin.h"
b2141fc1 12#include "config.h"
697cc8ef 13#include "lockfile.h"
0e5a7faa
CR
14#include "tag.h"
15#include "object.h"
cf394719 16#include "pretty.h"
0e5a7faa
CR
17#include "run-command.h"
18#include "refs.h"
19#include "diff.h"
20#include "diffcore.h"
21#include "tree.h"
c369e7b8 22#include "branch.h"
5eee6b28 23#include "parse-options.h"
d0f379c2
SB
24#include "unpack-trees.h"
25#include "cache-tree.h"
35b96d1d
SB
26#include "submodule.h"
27#include "submodule-config.h"
71471b2a 28#include "dir.h"
35b96d1d 29
649bf3a4
BP
30#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
31
5eee6b28 32static const char * const git_reset_usage[] = {
c1e9c2a7 33 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
d137b507 34 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
64bac8df 35 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
d137b507 36 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
5eee6b28
CR
37 NULL
38};
0e5a7faa 39
9bc454df
CC
40enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
41static const char *reset_type_names[] = {
8b2a57b6 42 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
9bc454df 43};
9e8eceab 44
0e5a7faa
CR
45static inline int is_merge(void)
46{
102de880 47 return !access(git_path_merge_head(the_repository), F_OK);
0e5a7faa
CR
48}
49
4cf76f6b 50static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
0e5a7faa 51{
afbb8838 52 int i, nr = 0;
d0f379c2 53 struct tree_desc desc[2];
6c52ec8a 54 struct tree *tree;
d0f379c2 55 struct unpack_trees_options opts;
afbb8838 56 int ret = -1;
0e5a7faa 57
d0f379c2
SB
58 memset(&opts, 0, sizeof(opts));
59 opts.head_idx = 1;
60 opts.src_index = &the_index;
61 opts.dst_index = &the_index;
62 opts.fn = oneway_merge;
63 opts.merge = 1;
4cf76f6b 64 init_checkout_metadata(&opts.meta, ref, oid, NULL);
5aa965a0 65 if (!quiet)
d0f379c2 66 opts.verbose_update = 1;
9e8eceab 67 switch (reset_type) {
9bc454df 68 case KEEP:
9e8eceab 69 case MERGE:
d0f379c2 70 opts.update = 1;
1b5f3733 71 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
9e8eceab
LT
72 break;
73 case HARD:
d0f379c2 74 opts.update = 1;
480d3d6b 75 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
0e47bca0 76 opts.skip_cache_tree_update = 1;
480d3d6b
EN
77 break;
78 case MIXED:
79 opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
0e47bca0 80 opts.skip_cache_tree_update = 1;
480d3d6b
EN
81 /* but opts.update=0, so working tree not updated */
82 break;
9e8eceab 83 default:
480d3d6b 84 BUG("invalid reset_type passed to reset_index");
9e8eceab 85 }
0e5a7faa 86
fbc1ed62 87 repo_read_index_unmerged(the_repository);
d0f379c2 88
9bc454df 89 if (reset_type == KEEP) {
3a5d7c55 90 struct object_id head_oid;
91 if (get_oid("HEAD", &head_oid))
b50a64e8 92 return error(_("You do not have a valid HEAD."));
5e575807 93 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
b50a64e8 94 return error(_("Failed to find tree of HEAD."));
9bc454df
CC
95 nr++;
96 opts.fn = twoway_merge;
97 }
98
5e575807 99 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
afbb8838
JK
100 error(_("Failed to find tree of %s."), oid_to_hex(oid));
101 goto out;
102 }
e9ce897b
JK
103 nr++;
104
d0f379c2 105 if (unpack_trees(nr, desc, &opts))
afbb8838 106 goto out;
6c52ec8a
TR
107
108 if (reset_type == MIXED || reset_type == HARD) {
a9dbc179 109 tree = parse_tree_indirect(oid);
c207e9e1 110 prime_cache_tree(the_repository, the_repository->index, tree);
6c52ec8a
TR
111 }
112
afbb8838
JK
113 ret = 0;
114
115out:
116 for (i = 0; i < nr; i++)
117 free((void *)desc[i].buffer);
118 return ret;
0e5a7faa
CR
119}
120
121static void print_new_head_line(struct commit *commit)
122{
1cf823fb 123 struct strbuf buf = STRBUF_INIT;
0e5a7faa 124
1cf823fb 125 printf(_("HEAD is now at %s"),
aab9583f 126 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
1cf823fb
TG
127
128 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
129 if (buf.len > 0)
130 printf(" %s", buf.buf);
131 putchar('\n');
132 strbuf_release(&buf);
0e5a7faa
CR
133}
134
0e5a7faa
CR
135static void update_index_from_diff(struct diff_queue_struct *q,
136 struct diff_options *opt, void *data)
137{
138 int i;
b4b313f9 139 int intent_to_add = *(int *)data;
0e5a7faa
CR
140
141 for (i = 0; i < q->nr; i++) {
71471b2a 142 int pos;
0e5a7faa 143 struct diff_filespec *one = q->queue[i]->one;
1f86b7cb 144 int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
b4b313f9
NTND
145 struct cache_entry *ce;
146
1f86b7cb 147 if (!is_in_reset_tree && !intent_to_add) {
0e5a7faa 148 remove_file_from_cache(one->path);
b4b313f9
NTND
149 continue;
150 }
151
a849735b 152 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
b4b313f9 153 0, 0);
71471b2a
VD
154
155 /*
156 * If the file 1) corresponds to an existing index entry with
157 * skip-worktree set, or 2) does not exist in the index but is
158 * outside the sparse checkout definition, add a skip-worktree bit
4d1cfc13
VD
159 * to the new index entry. Note that a sparse index will be expanded
160 * if this entry is outside the sparse cone - this is necessary
161 * to properly construct the reset sparse directory.
71471b2a
VD
162 */
163 pos = cache_name_pos(one->path, strlen(one->path));
164 if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
165 (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
166 ce->ce_flags |= CE_SKIP_WORKTREE;
167
b4b313f9
NTND
168 if (!ce)
169 die(_("make_cache_entry failed for path '%s'"),
170 one->path);
1f86b7cb 171 if (!is_in_reset_tree) {
b4b313f9
NTND
172 ce->ce_flags |= CE_INTENT_TO_ADD;
173 set_object_name_for_intent_to_add_entry(ce);
174 }
175 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
0e5a7faa
CR
176 }
177}
178
bd1928df 179static int read_from_tree(const struct pathspec *pathspec,
3a5d7c55 180 struct object_id *tree_oid,
b4b313f9 181 int intent_to_add)
0e5a7faa 182{
0e5a7faa
CR
183 struct diff_options opt;
184
185 memset(&opt, 0, sizeof(opt));
bd1928df 186 copy_pathspec(&opt.pathspec, pathspec);
0e5a7faa
CR
187 opt.output_format = DIFF_FORMAT_CALLBACK;
188 opt.format_callback = update_index_from_diff;
b4b313f9 189 opt.format_callback_data = &intent_to_add;
0d1e0e78 190 opt.flags.override_submodule_config = 1;
4d1cfc13 191 opt.flags.recursive = 1;
b78ea5fc 192 opt.repo = the_repository;
4d1cfc13
VD
193 opt.change = diff_change;
194 opt.add_remove = diff_addremove;
195
b29ad383 196 if (pathspec->nr && pathspec_needs_expanded_index(&the_index, pathspec))
4d1cfc13 197 ensure_full_index(&the_index);
0e5a7faa 198
944cffbd 199 if (do_diff_cache(tree_oid, &opt))
0e5a7faa
CR
200 return 1;
201 diffcore_std(&opt);
202 diff_flush(&opt);
2e7a9785 203
bf883f30 204 return 0;
0e5a7faa
CR
205}
206
d04520e3
JK
207static void set_reflog_message(struct strbuf *sb, const char *action,
208 const char *rev)
0e5a7faa 209{
0e5a7faa 210 const char *rla = getenv("GIT_REFLOG_ACTION");
d04520e3
JK
211
212 strbuf_reset(sb);
213 if (rla)
214 strbuf_addf(sb, "%s: %s", rla, action);
215 else if (rev)
216 strbuf_addf(sb, "reset: moving to %s", rev);
217 else
218 strbuf_addf(sb, "reset: %s", action);
0e5a7faa
CR
219}
220
812d2a3d
CC
221static void die_if_unmerged_cache(int reset_type)
222{
fbc1ed62 223 if (is_merge() || unmerged_index(&the_index))
8b2a57b6
ÆAB
224 die(_("Cannot do a %s reset in the middle of a merge."),
225 _(reset_type_names[reset_type]));
812d2a3d
CC
226
227}
228
f8144c9f
NTND
229static void parse_args(struct pathspec *pathspec,
230 const char **argv, const char *prefix,
231 int patch_mode,
232 const char **rev_ret)
0e5a7faa 233{
0e5a7faa 234 const char *rev = "HEAD";
3a5d7c55 235 struct object_id unused;
dfc8f39e
JH
236 /*
237 * Possible arguments are:
238 *
2f328c3d
MZ
239 * git reset [-opts] [<rev>]
240 * git reset [-opts] <tree> [<paths>...]
241 * git reset [-opts] <tree> -- [<paths>...]
242 * git reset [-opts] -- [<paths>...]
dfc8f39e
JH
243 * git reset [-opts] <paths>...
244 *
dca48cf5 245 * At this point, argv points immediately after [-opts].
dfc8f39e
JH
246 */
247
dca48cf5
MZ
248 if (argv[0]) {
249 if (!strcmp(argv[0], "--")) {
250 argv++; /* reset to HEAD, possibly with paths */
251 } else if (argv[1] && !strcmp(argv[1], "--")) {
252 rev = argv[0];
253 argv += 2;
dfc8f39e
JH
254 }
255 /*
dca48cf5 256 * Otherwise, argv[0] could be either <rev> or <paths> and
2f328c3d
MZ
257 * has to be unambiguous. If there is a single argument, it
258 * can not be a tree
dfc8f39e 259 */
e82caf38 260 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
261 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
dfc8f39e 262 /*
2f328c3d 263 * Ok, argv[0] looks like a commit/tree; it should not
dfc8f39e
JH
264 * be a filename.
265 */
dca48cf5
MZ
266 verify_non_filename(prefix, argv[0]);
267 rev = *argv++;
dfc8f39e
JH
268 } else {
269 /* Otherwise we treat this as a filename */
dca48cf5 270 verify_filename(prefix, argv[0], 1);
dfc8f39e
JH
271 }
272 }
39ea722d 273 *rev_ret = rev;
2c63d6eb 274
480ca644
NTND
275 parse_pathspec(pathspec, 0,
276 PATHSPEC_PREFER_FULL |
277 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
f8144c9f 278 prefix, argv);
39ea722d
MZ
279}
280
3a5d7c55 281static int reset_refs(const char *rev, const struct object_id *oid)
7bca0e45
MZ
282{
283 int update_ref_status;
284 struct strbuf msg = STRBUF_INIT;
3a5d7c55 285 struct object_id *orig = NULL, oid_orig,
286 *old_orig = NULL, oid_old_orig;
7bca0e45 287
3a5d7c55 288 if (!get_oid("ORIG_HEAD", &oid_old_orig))
289 old_orig = &oid_old_orig;
290 if (!get_oid("HEAD", &oid_orig)) {
291 orig = &oid_orig;
7bca0e45 292 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
ae077771 293 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
f4124112 294 UPDATE_REFS_MSG_ON_ERR);
7bca0e45 295 } else if (old_orig)
2616a5e5 296 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
7bca0e45 297 set_reflog_message(&msg, "updating HEAD", rev);
ae077771 298 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
f4124112 299 UPDATE_REFS_MSG_ON_ERR);
7bca0e45
MZ
300 strbuf_release(&msg);
301 return update_ref_status;
302}
303
046b4823
SB
304static int git_reset_config(const char *var, const char *value, void *cb)
305{
306 if (!strcmp(var, "submodule.recurse"))
307 return git_default_submodule_config(var, value, cb);
308
309 return git_default_config(var, value, cb);
310}
311
39ea722d
MZ
312int cmd_reset(int argc, const char **argv, const char *prefix)
313{
314 int reset_type = NONE, update_ref_status = 0, quiet = 0;
5891c76c 315 int no_refresh = 0;
64bac8df
AM
316 int patch_mode = 0, pathspec_file_nul = 0, unborn;
317 const char *rev, *pathspec_from_file = NULL;
f2fd0760 318 struct object_id oid;
f8144c9f 319 struct pathspec pathspec;
b4b313f9 320 int intent_to_add = 0;
39ea722d
MZ
321 const struct option options[] = {
322 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
5891c76c 323 OPT_BOOL(0, "no-refresh", &no_refresh,
fd56fba9 324 N_("skip refreshing the index after reset")),
39ea722d
MZ
325 OPT_SET_INT(0, "mixed", &reset_type,
326 N_("reset HEAD and index"), MIXED),
327 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
328 OPT_SET_INT(0, "hard", &reset_type,
329 N_("reset HEAD, index and working tree"), HARD),
330 OPT_SET_INT(0, "merge", &reset_type,
331 N_("reset HEAD, index and working tree"), MERGE),
332 OPT_SET_INT(0, "keep", &reset_type,
333 N_("reset HEAD but keep local changes"), KEEP),
203c8533 334 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
35b96d1d 335 "reset", "control recursive updating of submodules",
203c8533 336 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
d5d09d47 337 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
b4b313f9
NTND
338 OPT_BOOL('N', "intent-to-add", &intent_to_add,
339 N_("record only the fact that removed paths will be added later")),
64bac8df
AM
340 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
341 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
39ea722d
MZ
342 OPT_END()
343 };
344
046b4823 345 git_config(git_reset_config, NULL);
39ea722d
MZ
346
347 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
348 PARSE_OPT_KEEP_DASHDASH);
f8144c9f 349 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
0e5a7faa 350
64bac8df
AM
351 if (pathspec_from_file) {
352 if (patch_mode)
12909b6b 353 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
64bac8df
AM
354
355 if (pathspec.nr)
246cac85 356 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
64bac8df
AM
357
358 parse_pathspec_file(&pathspec, 0,
359 PATHSPEC_PREFER_FULL,
360 prefix, pathspec_from_file, pathspec_file_nul);
361 } else if (pathspec_file_nul) {
6fa00ee8 362 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
64bac8df
AM
363 }
364
e82caf38 365 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
166ec2e9
MZ
366 if (unborn) {
367 /* reset on unborn branch: treat as reset to empty tree */
d8448522 368 oidcpy(&oid, the_hash_algo->empty_tree);
0a8e3036 369 } else if (!pathspec.nr && !patch_mode) {
2f328c3d 370 struct commit *commit;
e82caf38 371 if (get_oid_committish(rev, &oid))
2f328c3d 372 die(_("Failed to resolve '%s' as a valid revision."), rev);
2122f675 373 commit = lookup_commit_reference(the_repository, &oid);
2f328c3d
MZ
374 if (!commit)
375 die(_("Could not parse object '%s'."), rev);
f2fd0760 376 oidcpy(&oid, &commit->object.oid);
2f328c3d
MZ
377 } else {
378 struct tree *tree;
e82caf38 379 if (get_oid_treeish(rev, &oid))
2f328c3d 380 die(_("Failed to resolve '%s' as a valid tree."), rev);
a9dbc179 381 tree = parse_tree_indirect(&oid);
2f328c3d
MZ
382 if (!tree)
383 die(_("Could not parse object '%s'."), rev);
f2fd0760 384 oidcpy(&oid, &tree->object.oid);
2f328c3d 385 }
0e5a7faa 386
d002ef4d
TR
387 if (patch_mode) {
388 if (reset_type != NONE)
12909b6b 389 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
c18b6c1a 390 trace2_cmd_mode("patch-interactive");
b3e9ce13 391 return run_add_interactive(rev, "--patch=reset", &pathspec);
d002ef4d
TR
392 }
393
0e5a7faa
CR
394 /* git reset tree [--] paths... can be used to
395 * load chosen paths from the tree into the index without
396 * affecting the working tree nor HEAD. */
f8144c9f 397 if (pathspec.nr) {
0e5a7faa 398 if (reset_type == MIXED)
b50a64e8 399 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
0e5a7faa 400 else if (reset_type != NONE)
8b2a57b6
ÆAB
401 die(_("Cannot do %s reset with paths."),
402 _(reset_type_names[reset_type]));
0e5a7faa
CR
403 }
404 if (reset_type == NONE)
405 reset_type = MIXED; /* by default */
406
c18b6c1a
JH
407 if (pathspec.nr)
408 trace2_cmd_mode("path");
409 else
410 trace2_cmd_mode(reset_type_names[reset_type]);
411
b7756d41 412 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
cd0f0f68 413 setup_work_tree();
49b9362f 414
2b06b0a0 415 if (reset_type == MIXED && is_bare_repository())
8b2a57b6
ÆAB
416 die(_("%s reset is not allowed in a bare repository"),
417 _(reset_type_names[reset_type]));
2b06b0a0 418
b4b313f9 419 if (intent_to_add && reset_type != MIXED)
6fa00ee8 420 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
b4b313f9 421
c01b1cbd
VD
422 prepare_repo_settings(the_repository);
423 the_repository->settings.command_requires_full_index = 0;
424
425 if (read_cache() < 0)
426 die(_("index file corrupt"));
427
0e5a7faa
CR
428 /* Soft reset does not touch the index file nor the working tree
429 * at all, but requires them in a good order. Other resets reset
430 * the index file to the tree object we are switching to. */
352f58a5 431 if (reset_type == SOFT || reset_type == KEEP)
812d2a3d 432 die_if_unmerged_cache(reset_type);
352f58a5
MZ
433
434 if (reset_type != SOFT) {
bfffb48c
JK
435 struct lock_file lock = LOCK_INIT;
436 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
3fde386a 437 if (reset_type == MIXED) {
f38798f4 438 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
3a5d7c55 439 if (read_from_tree(&pathspec, &oid, intent_to_add))
3bbf2f20 440 return 1;
1956ecd0 441 the_index.updated_skipworktree = 1;
5891c76c 442 if (!no_refresh && get_git_work_tree()) {
649bf3a4
BP
443 uint64_t t_begin, t_delta_in_ms;
444
445 t_begin = getnanotime();
b7756d41
NTND
446 refresh_index(&the_index, flags, NULL, NULL,
447 _("Unstaged changes after reset:"));
649bf3a4 448 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
d492abb0 449 if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
9396251b 450 advise(_("It took %.2f seconds to refresh the index after reset. You can use\n"
7cff6765 451 "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0);
649bf3a4
BP
452 }
453 }
3bbf2f20 454 } else {
4cf76f6b 455 struct object_id dummy;
456 char *ref = NULL;
457 int err;
458
f24c30e0 459 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
4cf76f6b 460 if (ref && !starts_with(ref, "refs/"))
e901de68 461 FREE_AND_NULL(ref);
4cf76f6b 462
463 err = reset_index(ref, &oid, reset_type, quiet);
3bbf2f20 464 if (reset_type == KEEP && !err)
4cf76f6b 465 err = reset_index(ref, &oid, MIXED, quiet);
3bbf2f20
MZ
466 if (err)
467 die(_("Could not reset index file to revision '%s'."), rev);
4cf76f6b 468 free(ref);
3bbf2f20 469 }
bc41bf42 470
bfffb48c 471 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
1ca38f85 472 die(_("Could not write new index file."));
0e5a7faa 473 }
0e5a7faa 474
f8144c9f 475 if (!pathspec.nr && !unborn) {
3bbf2f20
MZ
476 /* Any resets without paths update HEAD to the head being
477 * switched to, saving the previous head in ORIG_HEAD before. */
3a5d7c55 478 update_ref_status = reset_refs(rev, &oid);
0e5a7faa 479
3bbf2f20 480 if (reset_type == HARD && !update_ref_status && !quiet)
2122f675 481 print_new_head_line(lookup_commit_reference(the_repository, &oid));
3bbf2f20 482 }
f8144c9f 483 if (!pathspec.nr)
f4a4b9ac 484 remove_branch_state(the_repository, 0);
0e5a7faa 485
0e5a7faa
CR
486 return update_ref_status;
487}