blob: 5f941fb3a29cce568608f7a07b6f60a683479748 [file] [log] [blame]
Carlos Rica0e5a7fa2007-09-11 05:19:34 +02001/*
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 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +020010
Stephen Boydc2e86ad2011-03-22 00:51:05 -070011#include "builtin.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +000012#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070013#include "config.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000014#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000015#include "gettext.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000016#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000017#include "hex.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020018#include "lockfile.h"
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020019#include "object.h"
Olga Telezhnayacf394712017-12-12 08:55:35 +000020#include "pretty.h"
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020021#include "refs.h"
22#include "diff.h"
23#include "diffcore.h"
24#include "tree.h"
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050025#include "branch.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070026#include "object-name.h"
Carlos Rica5eee6b22008-03-04 23:11:34 +010027#include "parse-options.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000028#include "path.h"
Stephan Beyerd0f379c2009-12-30 06:54:47 +010029#include "unpack-trees.h"
30#include "cache-tree.h"
Elijah Newrene38da482023-03-21 06:26:05 +000031#include "setup.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000032#include "sparse-index.h"
Stefan Beller35b96d12017-04-21 10:39:53 -070033#include "submodule.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000034#include "trace.h"
35#include "trace2.h"
Victoria Dye71471b22021-10-27 14:39:17 +000036#include "dir.h"
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +010037#include "add-interactive.h"
Stefan Beller35b96d12017-04-21 10:39:53 -070038
Ben Peart649bf3a2018-10-23 15:04:23 -040039#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
40
Carlos Rica5eee6b22008-03-04 23:11:34 +010041static const char * const git_reset_usage[] = {
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +070042 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
Alexandr Miloslavskiyd137b502019-11-19 16:48:52 +000043 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +000044 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
Alexandr Miloslavskiyd137b502019-11-19 16:48:52 +000045 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
Carlos Rica5eee6b22008-03-04 23:11:34 +010046 NULL
47};
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020048
Christian Couder9bc454d2010-01-19 05:25:57 +010049enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
50static const char *reset_type_names[] = {
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +000051 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
Christian Couder9bc454d2010-01-19 05:25:57 +010052};
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080053
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020054static inline int is_merge(void)
55{
Stefan Beller102de882018-05-17 15:51:51 -070056 return !access(git_path_merge_head(the_repository), F_OK);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020057}
58
brian m. carlson4cf76f62020-03-16 18:05:07 +000059static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020060{
Jeff Kingafbb8832017-09-05 09:04:51 -040061 int i, nr = 0;
Stephan Beyerd0f379c2009-12-30 06:54:47 +010062 struct tree_desc desc[2];
Thomas Rast6c52ec82011-12-06 18:43:39 +010063 struct tree *tree;
Stephan Beyerd0f379c2009-12-30 06:54:47 +010064 struct unpack_trees_options opts;
Jeff Kingafbb8832017-09-05 09:04:51 -040065 int ret = -1;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020066
Stephan Beyerd0f379c2009-12-30 06:54:47 +010067 memset(&opts, 0, sizeof(opts));
68 opts.head_idx = 1;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +020069 opts.src_index = the_repository->index;
70 opts.dst_index = the_repository->index;
Stephan Beyerd0f379c2009-12-30 06:54:47 +010071 opts.fn = oneway_merge;
72 opts.merge = 1;
brian m. carlson4cf76f62020-03-16 18:05:07 +000073 init_checkout_metadata(&opts.meta, ref, oid, NULL);
Jamis Buck5aa965a2008-05-31 18:10:58 -070074 if (!quiet)
Stephan Beyerd0f379c2009-12-30 06:54:47 +010075 opts.verbose_update = 1;
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080076 switch (reset_type) {
Christian Couder9bc454d2010-01-19 05:25:57 +010077 case KEEP:
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080078 case MERGE:
Stephan Beyerd0f379c2009-12-30 06:54:47 +010079 opts.update = 1;
Elijah Newren1b5f3732021-09-27 16:33:43 +000080 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080081 break;
82 case HARD:
Stephan Beyerd0f379c2009-12-30 06:54:47 +010083 opts.update = 1;
Elijah Newren480d3d62021-09-27 16:33:44 +000084 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
Victoria Dye0e47bca2022-11-10 19:06:03 +000085 opts.skip_cache_tree_update = 1;
Elijah Newren480d3d62021-09-27 16:33:44 +000086 break;
87 case MIXED:
88 opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
Victoria Dye0e47bca2022-11-10 19:06:03 +000089 opts.skip_cache_tree_update = 1;
Elijah Newren480d3d62021-09-27 16:33:44 +000090 /* but opts.update=0, so working tree not updated */
91 break;
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080092 default:
Elijah Newren480d3d62021-09-27 16:33:44 +000093 BUG("invalid reset_type passed to reset_index");
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080094 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020095
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +010096 repo_read_index_unmerged(the_repository);
Stephan Beyerd0f379c2009-12-30 06:54:47 +010097
Christian Couder9bc454d2010-01-19 05:25:57 +010098 if (reset_type == KEEP) {
brian m. carlson3a5d7c52016-09-05 20:08:11 +000099 struct object_id head_oid;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200100 if (repo_get_oid(the_repository, "HEAD", &head_oid))
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +0000101 return error(_("You do not have a valid HEAD."));
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700102 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +0000103 return error(_("Failed to find tree of HEAD."));
Christian Couder9bc454d2010-01-19 05:25:57 +0100104 nr++;
105 opts.fn = twoway_merge;
106 }
107
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700108 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
Jeff Kingafbb8832017-09-05 09:04:51 -0400109 error(_("Failed to find tree of %s."), oid_to_hex(oid));
110 goto out;
111 }
Jeff Kinge9ce8972017-09-05 09:04:28 -0400112 nr++;
113
Stephan Beyerd0f379c2009-12-30 06:54:47 +0100114 if (unpack_trees(nr, desc, &opts))
Jeff Kingafbb8832017-09-05 09:04:51 -0400115 goto out;
Thomas Rast6c52ec82011-12-06 18:43:39 +0100116
117 if (reset_type == MIXED || reset_type == HARD) {
brian m. carlsona9dbc172017-05-06 22:10:37 +0000118 tree = parse_tree_indirect(oid);
Johannes Schindelinaa9f6182024-02-23 08:34:23 +0000119 if (!tree) {
120 error(_("unable to read tree (%s)"), oid_to_hex(oid));
121 goto out;
122 }
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100123 prime_cache_tree(the_repository, the_repository->index, tree);
Thomas Rast6c52ec82011-12-06 18:43:39 +0100124 }
125
Jeff Kingafbb8832017-09-05 09:04:51 -0400126 ret = 0;
127
128out:
129 for (i = 0; i < nr; i++)
130 free((void *)desc[i].buffer);
131 return ret;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200132}
133
134static void print_new_head_line(struct commit *commit)
135{
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000136 struct strbuf buf = STRBUF_INIT;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200137
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000138 printf(_("HEAD is now at %s"),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200139 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000140
141 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
142 if (buf.len > 0)
143 printf(" %s", buf.buf);
144 putchar('\n');
145 strbuf_release(&buf);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200146}
147
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200148static void update_index_from_diff(struct diff_queue_struct *q,
Jeff King61bdc7c2022-12-13 06:13:48 -0500149 struct diff_options *opt UNUSED,
150 void *data)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200151{
152 int i;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700153 int intent_to_add = *(int *)data;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200154
155 for (i = 0; i < q->nr; i++) {
Victoria Dye71471b22021-10-27 14:39:17 +0000156 int pos;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200157 struct diff_filespec *one = q->queue[i]->one;
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000158 int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700159 struct cache_entry *ce;
160
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000161 if (!is_in_reset_tree && !intent_to_add) {
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200162 remove_file_from_index(the_repository->index, one->path);
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700163 continue;
164 }
165
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200166 ce = make_cache_entry(the_repository->index, one->mode, &one->oid, one->path,
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700167 0, 0);
Victoria Dye71471b22021-10-27 14:39:17 +0000168
169 /*
170 * If the file 1) corresponds to an existing index entry with
171 * skip-worktree set, or 2) does not exist in the index but is
172 * outside the sparse checkout definition, add a skip-worktree bit
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000173 * to the new index entry. Note that a sparse index will be expanded
174 * if this entry is outside the sparse cone - this is necessary
175 * to properly construct the reset sparse directory.
Victoria Dye71471b22021-10-27 14:39:17 +0000176 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200177 pos = index_name_pos(the_repository->index, one->path, strlen(one->path));
178 if ((pos >= 0 && ce_skip_worktree(the_repository->index->cache[pos])) ||
179 (pos < 0 && !path_in_sparse_checkout(one->path, the_repository->index)))
Victoria Dye71471b22021-10-27 14:39:17 +0000180 ce->ce_flags |= CE_SKIP_WORKTREE;
181
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700182 if (!ce)
183 die(_("make_cache_entry failed for path '%s'"),
184 one->path);
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000185 if (!is_in_reset_tree) {
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700186 ce->ce_flags |= CE_INTENT_TO_ADD;
187 set_object_name_for_intent_to_add_entry(ce);
188 }
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200189 add_index_entry(the_repository->index, ce,
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100190 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200191 }
192}
193
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700194static int read_from_tree(const struct pathspec *pathspec,
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000195 struct object_id *tree_oid,
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700196 int intent_to_add)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200197{
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200198 struct diff_options opt;
199
200 memset(&opt, 0, sizeof(opt));
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700201 copy_pathspec(&opt.pathspec, pathspec);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200202 opt.output_format = DIFF_FORMAT_CALLBACK;
203 opt.format_callback = update_index_from_diff;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700204 opt.format_callback_data = &intent_to_add;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700205 opt.flags.override_submodule_config = 1;
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000206 opt.flags.recursive = 1;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200207 opt.repo = the_repository;
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000208 opt.change = diff_change;
209 opt.add_remove = diff_addremove;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200210
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200211 if (pathspec->nr && pathspec_needs_expanded_index(the_repository->index, pathspec))
212 ensure_full_index(the_repository->index);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200213
brian m. carlson944cffb2017-05-06 22:10:35 +0000214 if (do_diff_cache(tree_oid, &opt))
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200215 return 1;
216 diffcore_std(&opt);
217 diff_flush(&opt);
Johannes Schindelin2e7a9782007-11-03 13:12:17 +0000218
Martin von Zweigbergkbf883f32013-01-14 21:47:44 -0800219 return 0;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200220}
221
Jeff Kingd04520e2011-07-22 10:12:23 -0600222static void set_reflog_message(struct strbuf *sb, const char *action,
223 const char *rev)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200224{
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200225 const char *rla = getenv("GIT_REFLOG_ACTION");
Jeff Kingd04520e2011-07-22 10:12:23 -0600226
227 strbuf_reset(sb);
228 if (rla)
229 strbuf_addf(sb, "%s: %s", rla, action);
230 else if (rev)
231 strbuf_addf(sb, "reset: moving to %s", rev);
232 else
233 strbuf_addf(sb, "reset: %s", action);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200234}
235
Christian Couder812d2a32010-01-19 05:26:01 +0100236static void die_if_unmerged_cache(int reset_type)
237{
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200238 if (is_merge() || unmerged_index(the_repository->index))
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000239 die(_("Cannot do a %s reset in the middle of a merge."),
240 _(reset_type_names[reset_type]));
Christian Couder812d2a32010-01-19 05:26:01 +0100241
242}
243
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700244static void parse_args(struct pathspec *pathspec,
245 const char **argv, const char *prefix,
246 int patch_mode,
247 const char **rev_ret)
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800248{
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800249 const char *rev = "HEAD";
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000250 struct object_id unused;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800251 /*
252 * Possible arguments are:
253 *
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800254 * git reset [-opts] [<rev>]
255 * git reset [-opts] <tree> [<paths>...]
256 * git reset [-opts] <tree> -- [<paths>...]
257 * git reset [-opts] -- [<paths>...]
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800258 * git reset [-opts] <paths>...
259 *
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800260 * At this point, argv points immediately after [-opts].
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800261 */
262
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800263 if (argv[0]) {
264 if (!strcmp(argv[0], "--")) {
265 argv++; /* reset to HEAD, possibly with paths */
266 } else if (argv[1] && !strcmp(argv[1], "--")) {
267 rev = argv[0];
268 argv += 2;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800269 }
270 /*
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800271 * Otherwise, argv[0] could be either <rev> or <paths> and
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800272 * has to be unambiguous. If there is a single argument, it
273 * can not be a tree
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800274 */
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200275 else if ((!argv[1] && !repo_get_oid_committish(the_repository, argv[0], &unused)) ||
276 (argv[1] && !repo_get_oid_treeish(the_repository, argv[0], &unused))) {
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800277 /*
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800278 * Ok, argv[0] looks like a commit/tree; it should not
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800279 * be a filename.
280 */
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800281 verify_non_filename(prefix, argv[0]);
282 rev = *argv++;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800283 } else {
284 /* Otherwise we treat this as a filename */
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800285 verify_filename(prefix, argv[0], 1);
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800286 }
287 }
Ghanshyam Thakkar5a8ed3f2024-02-13 05:35:29 +0530288
289 /* treat '@' as a shortcut for 'HEAD' */
290 *rev_ret = !strcmp("@", rev) ? "HEAD" : rev;
John Keeping2c63d6e2013-09-12 20:25:01 +0100291
Nguyễn Thái Ngọc Duy480ca642013-07-14 15:35:50 +0700292 parse_pathspec(pathspec, 0,
293 PATHSPEC_PREFER_FULL |
294 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700295 prefix, argv);
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800296}
297
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000298static int reset_refs(const char *rev, const struct object_id *oid)
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800299{
300 int update_ref_status;
301 struct strbuf msg = STRBUF_INIT;
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000302 struct object_id *orig = NULL, oid_orig,
303 *old_orig = NULL, oid_old_orig;
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800304
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200305 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000306 old_orig = &oid_old_orig;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200307 if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) {
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000308 orig = &oid_orig;
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800309 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200310 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
311 "ORIG_HEAD", orig, old_orig, 0,
312 UPDATE_REFS_MSG_ON_ERR);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800313 } else if (old_orig)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200314 refs_delete_ref(get_main_ref_store(the_repository), NULL,
315 "ORIG_HEAD", old_orig, 0);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800316 set_reflog_message(&msg, "updating HEAD", rev);
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200317 update_ref_status = refs_update_ref(get_main_ref_store(the_repository),
318 msg.buf, "HEAD", oid, orig, 0,
319 UPDATE_REFS_MSG_ON_ERR);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800320 strbuf_release(&msg);
321 return update_ref_status;
322}
323
Glen Chooa4e7e312023-06-28 19:26:22 +0000324static int git_reset_config(const char *var, const char *value,
325 const struct config_context *ctx, void *cb)
Stefan Beller046b4822017-05-31 17:30:47 -0700326{
327 if (!strcmp(var, "submodule.recurse"))
328 return git_default_submodule_config(var, value, cb);
329
Glen Chooa4e7e312023-06-28 19:26:22 +0000330 return git_default_config(var, value, ctx, cb);
Stefan Beller046b4822017-05-31 17:30:47 -0700331}
332
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200333int cmd_reset(int argc, const char **argv, const char *prefix)
334{
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800335 int reset_type = NONE, update_ref_status = 0, quiet = 0;
Junio C Hamano5891c762022-03-24 10:33:10 -0700336 int no_refresh = 0;
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000337 int patch_mode = 0, pathspec_file_nul = 0, unborn;
Jeff King7ce40882023-03-04 05:31:22 -0500338 const char *rev;
339 char *pathspec_from_file = NULL;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000340 struct object_id oid;
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700341 struct pathspec pathspec;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700342 int intent_to_add = 0;
Carlos Rica5eee6b22008-03-04 23:11:34 +0100343 const struct option options[] = {
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700344 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
Junio C Hamano5891c762022-03-24 10:33:10 -0700345 OPT_BOOL(0, "no-refresh", &no_refresh,
Victoria Dyefd56fba2022-03-15 01:49:39 +0000346 N_("skip refreshing the index after reset")),
Junio C Hamano3821eb62023-07-19 06:37:39 -0700347 OPT_SET_INT_F(0, "mixed", &reset_type,
348 N_("reset HEAD and index"),
349 MIXED, PARSE_OPT_NONEG),
350 OPT_SET_INT_F(0, "soft", &reset_type,
351 N_("reset only HEAD"),
352 SOFT, PARSE_OPT_NONEG),
353 OPT_SET_INT_F(0, "hard", &reset_type,
354 N_("reset HEAD, index and working tree"),
355 HARD, PARSE_OPT_NONEG),
356 OPT_SET_INT_F(0, "merge", &reset_type,
357 N_("reset HEAD, index and working tree"),
358 MERGE, PARSE_OPT_NONEG),
359 OPT_SET_INT_F(0, "keep", &reset_type,
360 N_("reset HEAD but keep local changes"),
361 KEEP, PARSE_OPT_NONEG),
Denton Liu203c8532020-04-28 04:36:28 -0400362 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
Junio C Hamano3821eb62023-07-19 06:37:39 -0700363 "reset", "control recursive updating of submodules",
364 PARSE_OPT_OPTARG,
365 option_parse_recurse_submodules_worktree_updater),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200366 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700367 OPT_BOOL('N', "intent-to-add", &intent_to_add,
368 N_("record only the fact that removed paths will be added later")),
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000369 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
370 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100371 OPT_END()
372 };
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200373
Stefan Beller046b4822017-05-31 17:30:47 -0700374 git_config(git_reset_config, NULL);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200375
Stephen Boyd37782922009-05-23 11:53:12 -0700376 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
Carlos Rica5eee6b22008-03-04 23:11:34 +0100377 PARSE_OPT_KEEP_DASHDASH);
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700378 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200379
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000380 if (pathspec_from_file) {
381 if (patch_mode)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000382 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000383
384 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000385 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000386
387 parse_pathspec_file(&pathspec, 0,
388 PATHSPEC_PREFER_FULL,
389 prefix, pathspec_from_file, pathspec_file_nul);
390 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000391 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000392 }
393
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200394 unborn = !strcmp(rev, "HEAD") && repo_get_oid(the_repository, "HEAD",
395 &oid);
Martin von Zweigbergk166ec2e2013-01-14 21:47:50 -0800396 if (unborn) {
397 /* reset on unborn branch: treat as reset to empty tree */
brian m. carlsond8448522018-05-02 00:26:02 +0000398 oidcpy(&oid, the_hash_algo->empty_tree);
Nika Layzell0a8e3032019-11-24 20:25:49 +0000399 } else if (!pathspec.nr && !patch_mode) {
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800400 struct commit *commit;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200401 if (repo_get_oid_committish(the_repository, rev, &oid))
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800402 die(_("Failed to resolve '%s' as a valid revision."), rev);
Stefan Beller2122f672018-06-28 18:21:58 -0700403 commit = lookup_commit_reference(the_repository, &oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800404 if (!commit)
405 die(_("Could not parse object '%s'."), rev);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000406 oidcpy(&oid, &commit->object.oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800407 } else {
408 struct tree *tree;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200409 if (repo_get_oid_treeish(the_repository, rev, &oid))
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800410 die(_("Failed to resolve '%s' as a valid tree."), rev);
brian m. carlsona9dbc172017-05-06 22:10:37 +0000411 tree = parse_tree_indirect(&oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800412 if (!tree)
413 die(_("Could not parse object '%s'."), rev);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000414 oidcpy(&oid, &tree->object.oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800415 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200416
Thomas Rastd002ef42009-08-15 13:48:31 +0200417 if (patch_mode) {
418 if (reset_type != NONE)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000419 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
Jeff Hostetlerc18b6c12019-02-22 14:25:09 -0800420 trace2_cmd_mode("patch-interactive");
Junio C Hamano72972ea2023-02-22 14:55:45 -0800421 update_ref_status = !!run_add_p(the_repository, ADD_P_RESET, rev,
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100422 &pathspec);
Ævar Arnfjörð Bjarmason7615cf92023-02-07 00:07:40 +0100423 goto cleanup;
Thomas Rastd002ef42009-08-15 13:48:31 +0200424 }
425
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200426 /* git reset tree [--] paths... can be used to
427 * load chosen paths from the tree into the index without
428 * affecting the working tree nor HEAD. */
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700429 if (pathspec.nr) {
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200430 if (reset_type == MIXED)
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +0000431 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200432 else if (reset_type != NONE)
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000433 die(_("Cannot do %s reset with paths."),
434 _(reset_type_names[reset_type]));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200435 }
436 if (reset_type == NONE)
437 reset_type = MIXED; /* by default */
438
Jeff Hostetlerc18b6c12019-02-22 14:25:09 -0800439 if (pathspec.nr)
440 trace2_cmd_mode("path");
441 else
442 trace2_cmd_mode(reset_type_names[reset_type]);
443
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700444 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
Jeff Kingcd0f0f62009-12-30 03:47:03 -0500445 setup_work_tree();
Jeff King49b93622007-12-31 02:13:52 -0500446
Christian Couder2b06b0a2009-12-30 06:54:44 +0100447 if (reset_type == MIXED && is_bare_repository())
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000448 die(_("%s reset is not allowed in a bare repository"),
449 _(reset_type_names[reset_type]));
Christian Couder2b06b0a2009-12-30 06:54:44 +0100450
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700451 if (intent_to_add && reset_type != MIXED)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000452 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700453
Victoria Dyec01b1cb2021-11-29 15:52:40 +0000454 prepare_repo_settings(the_repository);
455 the_repository->settings.command_requires_full_index = 0;
456
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100457 if (repo_read_index(the_repository) < 0)
Victoria Dyec01b1cb2021-11-29 15:52:40 +0000458 die(_("index file corrupt"));
459
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200460 /* Soft reset does not touch the index file nor the working tree
461 * at all, but requires them in a good order. Other resets reset
462 * the index file to the tree object we are switching to. */
Martin von Zweigbergk352f58a2013-01-14 21:47:40 -0800463 if (reset_type == SOFT || reset_type == KEEP)
Christian Couder812d2a32010-01-19 05:26:01 +0100464 die_if_unmerged_cache(reset_type);
Martin von Zweigbergk352f58a2013-01-14 21:47:40 -0800465
466 if (reset_type != SOFT) {
Jeff Kingbfffb482017-09-05 08:15:21 -0400467 struct lock_file lock = LOCK_INIT;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100468 repo_hold_locked_index(the_repository, &lock,
469 LOCK_DIE_ON_ERROR);
Martin von Zweigbergk3fde3862013-01-14 21:47:51 -0800470 if (reset_type == MIXED) {
Felipe Contrerasf38798f2013-08-30 16:56:45 -0500471 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
Ævar Arnfjörð Bjarmason7615cf92023-02-07 00:07:40 +0100472 if (read_from_tree(&pathspec, &oid, intent_to_add)) {
473 update_ref_status = 1;
474 goto cleanup;
475 }
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200476 the_repository->index->updated_skipworktree = 1;
Junio C Hamano5891c762022-03-24 10:33:10 -0700477 if (!no_refresh && get_git_work_tree()) {
Ben Peart649bf3a2018-10-23 15:04:23 -0400478 uint64_t t_begin, t_delta_in_ms;
479
480 t_begin = getnanotime();
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200481 refresh_index(the_repository->index, flags, NULL, NULL,
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700482 _("Unstaged changes after reset:"));
Ben Peart649bf3a2018-10-23 15:04:23 -0400483 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
Victoria Dyed492abb2022-03-15 01:49:41 +0000484 if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
Victoria Dye93962512022-03-15 01:49:40 +0000485 advise(_("It took %.2f seconds to refresh the index after reset. You can use\n"
Victoria Dye7cff6762022-03-23 18:18:00 +0000486 "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0);
Ben Peart649bf3a2018-10-23 15:04:23 -0400487 }
488 }
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800489 } else {
brian m. carlson4cf76f62020-03-16 18:05:07 +0000490 struct object_id dummy;
491 char *ref = NULL;
492 int err;
493
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +0200494 repo_dwim_ref(the_repository, rev, strlen(rev),
495 &dummy, &ref, 0);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000496 if (ref && !starts_with(ref, "refs/"))
Andrzej Hunte901de62021-03-14 18:47:35 +0000497 FREE_AND_NULL(ref);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000498
499 err = reset_index(ref, &oid, reset_type, quiet);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800500 if (reset_type == KEEP && !err)
brian m. carlson4cf76f62020-03-16 18:05:07 +0000501 err = reset_index(ref, &oid, MIXED, quiet);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800502 if (err)
503 die(_("Could not reset index file to revision '%s'."), rev);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000504 free(ref);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800505 }
Martin von Zweigbergkbc41bf42013-01-14 21:47:46 -0800506
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200507 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK))
Martin von Zweigbergk1ca38f82013-01-14 21:47:42 -0800508 die(_("Could not write new index file."));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200509 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200510
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700511 if (!pathspec.nr && !unborn) {
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800512 /* Any resets without paths update HEAD to the head being
513 * switched to, saving the previous head in ORIG_HEAD before. */
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000514 update_ref_status = reset_refs(rev, &oid);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200515
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800516 if (reset_type == HARD && !update_ref_status && !quiet)
Stefan Beller2122f672018-06-28 18:21:58 -0700517 print_new_head_line(lookup_commit_reference(the_repository, &oid));
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800518 }
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700519 if (!pathspec.nr)
Nguyễn Thái Ngọc Duyf4a4b9a2019-03-29 17:38:59 +0700520 remove_branch_state(the_repository, 0);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200521
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200522 discard_index(the_repository->index);
Ævar Arnfjörð Bjarmasonab2cf372022-11-08 19:17:38 +0100523
Ævar Arnfjörð Bjarmason7615cf92023-02-07 00:07:40 +0100524cleanup:
525 clear_pathspec(&pathspec);
Jeff King7ce40882023-03-04 05:31:22 -0500526 free(pathspec_from_file);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200527 return update_ref_status;
528}