blob: b97745ee94e5a30a25a477f432a275d6ae953ee2 [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 */
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070010#define USE_THE_INDEX_COMPATIBILITY_MACROS
Stephen Boydc2e86ad2011-03-22 00:51:05 -070011#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070012#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020013#include "lockfile.h"
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020014#include "tag.h"
15#include "object.h"
Olga Telezhnayacf394712017-12-12 08:55:35 +000016#include "pretty.h"
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020017#include "run-command.h"
18#include "refs.h"
19#include "diff.h"
20#include "diffcore.h"
21#include "tree.h"
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050022#include "branch.h"
Carlos Rica5eee6b22008-03-04 23:11:34 +010023#include "parse-options.h"
Stephan Beyerd0f379c2009-12-30 06:54:47 +010024#include "unpack-trees.h"
25#include "cache-tree.h"
Stefan Beller35b96d12017-04-21 10:39:53 -070026#include "submodule.h"
27#include "submodule-config.h"
Victoria Dye71471b22021-10-27 14:39:17 +000028#include "dir.h"
Stefan Beller35b96d12017-04-21 10:39:53 -070029
Ben Peart649bf3a2018-10-23 15:04:23 -040030#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
31
Carlos Rica5eee6b22008-03-04 23:11:34 +010032static const char * const git_reset_usage[] = {
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +070033 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
Alexandr Miloslavskiyd137b502019-11-19 16:48:52 +000034 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +000035 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
Alexandr Miloslavskiyd137b502019-11-19 16:48:52 +000036 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
Carlos Rica5eee6b22008-03-04 23:11:34 +010037 NULL
38};
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020039
Christian Couder9bc454d2010-01-19 05:25:57 +010040enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
41static const char *reset_type_names[] = {
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +000042 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
Christian Couder9bc454d2010-01-19 05:25:57 +010043};
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080044
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020045static inline int is_merge(void)
46{
Stefan Beller102de882018-05-17 15:51:51 -070047 return !access(git_path_merge_head(the_repository), F_OK);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020048}
49
brian m. carlson4cf76f62020-03-16 18:05:07 +000050static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020051{
Jeff Kingafbb8832017-09-05 09:04:51 -040052 int i, nr = 0;
Stephan Beyerd0f379c2009-12-30 06:54:47 +010053 struct tree_desc desc[2];
Thomas Rast6c52ec82011-12-06 18:43:39 +010054 struct tree *tree;
Stephan Beyerd0f379c2009-12-30 06:54:47 +010055 struct unpack_trees_options opts;
Jeff Kingafbb8832017-09-05 09:04:51 -040056 int ret = -1;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020057
Stephan Beyerd0f379c2009-12-30 06:54:47 +010058 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;
brian m. carlson4cf76f62020-03-16 18:05:07 +000064 init_checkout_metadata(&opts.meta, ref, oid, NULL);
Jamis Buck5aa965a2008-05-31 18:10:58 -070065 if (!quiet)
Stephan Beyerd0f379c2009-12-30 06:54:47 +010066 opts.verbose_update = 1;
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080067 switch (reset_type) {
Christian Couder9bc454d2010-01-19 05:25:57 +010068 case KEEP:
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080069 case MERGE:
Stephan Beyerd0f379c2009-12-30 06:54:47 +010070 opts.update = 1;
Elijah Newren1b5f3732021-09-27 16:33:43 +000071 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080072 break;
73 case HARD:
Stephan Beyerd0f379c2009-12-30 06:54:47 +010074 opts.update = 1;
Elijah Newren480d3d62021-09-27 16:33:44 +000075 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
76 break;
77 case MIXED:
78 opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
79 /* but opts.update=0, so working tree not updated */
80 break;
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080081 default:
Elijah Newren480d3d62021-09-27 16:33:44 +000082 BUG("invalid reset_type passed to reset_index");
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080083 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020084
Stephan Beyerd0f379c2009-12-30 06:54:47 +010085 read_cache_unmerged();
86
Christian Couder9bc454d2010-01-19 05:25:57 +010087 if (reset_type == KEEP) {
brian m. carlson3a5d7c52016-09-05 20:08:11 +000088 struct object_id head_oid;
89 if (get_oid("HEAD", &head_oid))
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +000090 return error(_("You do not have a valid HEAD."));
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +070091 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +000092 return error(_("Failed to find tree of HEAD."));
Christian Couder9bc454d2010-01-19 05:25:57 +010093 nr++;
94 opts.fn = twoway_merge;
95 }
96
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +070097 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
Jeff Kingafbb8832017-09-05 09:04:51 -040098 error(_("Failed to find tree of %s."), oid_to_hex(oid));
99 goto out;
100 }
Jeff Kinge9ce8972017-09-05 09:04:28 -0400101 nr++;
102
Stephan Beyerd0f379c2009-12-30 06:54:47 +0100103 if (unpack_trees(nr, desc, &opts))
Jeff Kingafbb8832017-09-05 09:04:51 -0400104 goto out;
Thomas Rast6c52ec82011-12-06 18:43:39 +0100105
106 if (reset_type == MIXED || reset_type == HARD) {
brian m. carlsona9dbc172017-05-06 22:10:37 +0000107 tree = parse_tree_indirect(oid);
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100108 prime_cache_tree(the_repository, the_repository->index, tree);
Thomas Rast6c52ec82011-12-06 18:43:39 +0100109 }
110
Jeff Kingafbb8832017-09-05 09:04:51 -0400111 ret = 0;
112
113out:
114 for (i = 0; i < nr; i++)
115 free((void *)desc[i].buffer);
116 return ret;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200117}
118
119static void print_new_head_line(struct commit *commit)
120{
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000121 struct strbuf buf = STRBUF_INIT;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200122
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000123 printf(_("HEAD is now at %s"),
brian m. carlsonaab95832018-03-12 02:27:30 +0000124 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer1cf823f2018-02-01 20:57:21 +0000125
126 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
127 if (buf.len > 0)
128 printf(" %s", buf.buf);
129 putchar('\n');
130 strbuf_release(&buf);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200131}
132
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200133static void update_index_from_diff(struct diff_queue_struct *q,
134 struct diff_options *opt, void *data)
135{
136 int i;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700137 int intent_to_add = *(int *)data;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200138
139 for (i = 0; i < q->nr; i++) {
Victoria Dye71471b22021-10-27 14:39:17 +0000140 int pos;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200141 struct diff_filespec *one = q->queue[i]->one;
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000142 int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700143 struct cache_entry *ce;
144
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000145 if (!is_in_reset_tree && !intent_to_add) {
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200146 remove_file_from_cache(one->path);
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700147 continue;
148 }
149
Jameson Millera8497352018-07-02 19:49:31 +0000150 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700151 0, 0);
Victoria Dye71471b22021-10-27 14:39:17 +0000152
153 /*
154 * If the file 1) corresponds to an existing index entry with
155 * skip-worktree set, or 2) does not exist in the index but is
156 * outside the sparse checkout definition, add a skip-worktree bit
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000157 * to the new index entry. Note that a sparse index will be expanded
158 * if this entry is outside the sparse cone - this is necessary
159 * to properly construct the reset sparse directory.
Victoria Dye71471b22021-10-27 14:39:17 +0000160 */
161 pos = cache_name_pos(one->path, strlen(one->path));
162 if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
163 (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
164 ce->ce_flags |= CE_SKIP_WORKTREE;
165
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700166 if (!ce)
167 die(_("make_cache_entry failed for path '%s'"),
168 one->path);
Victoria Dye1f86b7c2021-10-07 21:15:31 +0000169 if (!is_in_reset_tree) {
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700170 ce->ce_flags |= CE_INTENT_TO_ADD;
171 set_object_name_for_intent_to_add_entry(ce);
172 }
173 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200174 }
175}
176
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000177static int pathspec_needs_expanded_index(const struct pathspec *pathspec)
178{
179 unsigned int i, pos;
180 int res = 0;
181 char *skip_worktree_seen = NULL;
182
183 /*
184 * When using a magic pathspec, assume for the sake of simplicity that
185 * the index needs to be expanded to match all matchable files.
186 */
187 if (pathspec->magic)
188 return 1;
189
190 for (i = 0; i < pathspec->nr; i++) {
191 struct pathspec_item item = pathspec->items[i];
192
193 /*
194 * If the pathspec item has a wildcard, the index should be expanded
195 * if the pathspec has the possibility of matching a subset of entries inside
196 * of a sparse directory (but not the entire directory).
197 *
198 * If the pathspec item is a literal path, the index only needs to be expanded
199 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
200 * expand for in-cone files) and b) it doesn't match any sparse directories
201 * (since we can reset whole sparse directories without expanding them).
202 */
203 if (item.nowildcard_len < item.len) {
204 /*
205 * Special case: if the pattern is a path inside the cone
206 * followed by only wildcards, the pattern cannot match
207 * partial sparse directories, so we don't expand the index.
208 */
209 if (path_in_cone_mode_sparse_checkout(item.original, &the_index) &&
210 strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len)
211 continue;
212
213 for (pos = 0; pos < active_nr; pos++) {
214 struct cache_entry *ce = active_cache[pos];
215
216 if (!S_ISSPARSEDIR(ce->ce_mode))
217 continue;
218
219 /*
220 * If the pre-wildcard length is longer than the sparse
221 * directory name and the sparse directory is the first
222 * component of the pathspec, need to expand the index.
223 */
224 if (item.nowildcard_len > ce_namelen(ce) &&
225 !strncmp(item.original, ce->name, ce_namelen(ce))) {
226 res = 1;
227 break;
228 }
229
230 /*
231 * If the pre-wildcard length is shorter than the sparse
232 * directory and the pathspec does not match the whole
233 * directory, need to expand the index.
234 */
235 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
236 wildmatch(item.original, ce->name, 0)) {
237 res = 1;
238 break;
239 }
240 }
241 } else if (!path_in_cone_mode_sparse_checkout(item.original, &the_index) &&
242 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
243 res = 1;
244
245 if (res > 0)
246 break;
247 }
248
249 free(skip_worktree_seen);
250 return res;
251}
252
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700253static int read_from_tree(const struct pathspec *pathspec,
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000254 struct object_id *tree_oid,
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700255 int intent_to_add)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200256{
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200257 struct diff_options opt;
258
259 memset(&opt, 0, sizeof(opt));
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700260 copy_pathspec(&opt.pathspec, pathspec);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200261 opt.output_format = DIFF_FORMAT_CALLBACK;
262 opt.format_callback = update_index_from_diff;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700263 opt.format_callback_data = &intent_to_add;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700264 opt.flags.override_submodule_config = 1;
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000265 opt.flags.recursive = 1;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200266 opt.repo = the_repository;
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000267 opt.change = diff_change;
268 opt.add_remove = diff_addremove;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200269
Victoria Dye4d1cfc12021-11-29 15:52:42 +0000270 if (pathspec->nr && the_index.sparse_index && pathspec_needs_expanded_index(pathspec))
271 ensure_full_index(&the_index);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200272
brian m. carlson944cffb2017-05-06 22:10:35 +0000273 if (do_diff_cache(tree_oid, &opt))
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200274 return 1;
275 diffcore_std(&opt);
276 diff_flush(&opt);
Junio C Hamanoed6e8032016-06-02 14:09:22 -0700277 clear_pathspec(&opt.pathspec);
Johannes Schindelin2e7a9782007-11-03 13:12:17 +0000278
Martin von Zweigbergkbf883f32013-01-14 21:47:44 -0800279 return 0;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200280}
281
Jeff Kingd04520e2011-07-22 10:12:23 -0600282static void set_reflog_message(struct strbuf *sb, const char *action,
283 const char *rev)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200284{
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200285 const char *rla = getenv("GIT_REFLOG_ACTION");
Jeff Kingd04520e2011-07-22 10:12:23 -0600286
287 strbuf_reset(sb);
288 if (rla)
289 strbuf_addf(sb, "%s: %s", rla, action);
290 else if (rev)
291 strbuf_addf(sb, "reset: moving to %s", rev);
292 else
293 strbuf_addf(sb, "reset: %s", action);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200294}
295
Christian Couder812d2a32010-01-19 05:26:01 +0100296static void die_if_unmerged_cache(int reset_type)
297{
John Keeping2c63d6e2013-09-12 20:25:01 +0100298 if (is_merge() || unmerged_cache())
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000299 die(_("Cannot do a %s reset in the middle of a merge."),
300 _(reset_type_names[reset_type]));
Christian Couder812d2a32010-01-19 05:26:01 +0100301
302}
303
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700304static void parse_args(struct pathspec *pathspec,
305 const char **argv, const char *prefix,
306 int patch_mode,
307 const char **rev_ret)
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800308{
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800309 const char *rev = "HEAD";
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000310 struct object_id unused;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800311 /*
312 * Possible arguments are:
313 *
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800314 * git reset [-opts] [<rev>]
315 * git reset [-opts] <tree> [<paths>...]
316 * git reset [-opts] <tree> -- [<paths>...]
317 * git reset [-opts] -- [<paths>...]
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800318 * git reset [-opts] <paths>...
319 *
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800320 * At this point, argv points immediately after [-opts].
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800321 */
322
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800323 if (argv[0]) {
324 if (!strcmp(argv[0], "--")) {
325 argv++; /* reset to HEAD, possibly with paths */
326 } else if (argv[1] && !strcmp(argv[1], "--")) {
327 rev = argv[0];
328 argv += 2;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800329 }
330 /*
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800331 * Otherwise, argv[0] could be either <rev> or <paths> and
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800332 * has to be unambiguous. If there is a single argument, it
333 * can not be a tree
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800334 */
brian m. carlsone82caf32017-07-13 23:49:28 +0000335 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
336 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800337 /*
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800338 * Ok, argv[0] looks like a commit/tree; it should not
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800339 * be a filename.
340 */
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800341 verify_non_filename(prefix, argv[0]);
342 rev = *argv++;
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800343 } else {
344 /* Otherwise we treat this as a filename */
Martin von Zweigbergkdca48cf2013-01-14 21:47:38 -0800345 verify_filename(prefix, argv[0], 1);
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800346 }
347 }
348 *rev_ret = rev;
John Keeping2c63d6e2013-09-12 20:25:01 +0100349
Nguyễn Thái Ngọc Duy480ca642013-07-14 15:35:50 +0700350 parse_pathspec(pathspec, 0,
351 PATHSPEC_PREFER_FULL |
352 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700353 prefix, argv);
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800354}
355
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000356static int reset_refs(const char *rev, const struct object_id *oid)
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800357{
358 int update_ref_status;
359 struct strbuf msg = STRBUF_INIT;
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000360 struct object_id *orig = NULL, oid_orig,
361 *old_orig = NULL, oid_old_orig;
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800362
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000363 if (!get_oid("ORIG_HEAD", &oid_old_orig))
364 old_orig = &oid_old_orig;
365 if (!get_oid("HEAD", &oid_orig)) {
366 orig = &oid_orig;
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800367 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
brian m. carlsonae077772017-10-15 22:06:51 +0000368 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +0200369 UPDATE_REFS_MSG_ON_ERR);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800370 } else if (old_orig)
brian m. carlson2616a5e2017-10-15 22:06:50 +0000371 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800372 set_reflog_message(&msg, "updating HEAD", rev);
brian m. carlsonae077772017-10-15 22:06:51 +0000373 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +0200374 UPDATE_REFS_MSG_ON_ERR);
Martin von Zweigbergk7bca0e42013-01-14 21:47:39 -0800375 strbuf_release(&msg);
376 return update_ref_status;
377}
378
Stefan Beller046b4822017-05-31 17:30:47 -0700379static int git_reset_config(const char *var, const char *value, void *cb)
380{
381 if (!strcmp(var, "submodule.recurse"))
382 return git_default_submodule_config(var, value, cb);
383
384 return git_default_config(var, value, cb);
385}
386
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200387int cmd_reset(int argc, const char **argv, const char *prefix)
388{
Martin von Zweigbergk39ea7222013-01-14 21:47:37 -0800389 int reset_type = NONE, update_ref_status = 0, quiet = 0;
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000390 int patch_mode = 0, pathspec_file_nul = 0, unborn;
391 const char *rev, *pathspec_from_file = NULL;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000392 struct object_id oid;
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700393 struct pathspec pathspec;
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700394 int intent_to_add = 0;
Carlos Rica5eee6b22008-03-04 23:11:34 +0100395 const struct option options[] = {
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700396 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100397 OPT_SET_INT(0, "mixed", &reset_type,
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700398 N_("reset HEAD and index"), MIXED),
399 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100400 OPT_SET_INT(0, "hard", &reset_type,
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700401 N_("reset HEAD, index and working tree"), HARD),
Linus Torvalds9e8ecea2008-12-01 09:30:31 -0800402 OPT_SET_INT(0, "merge", &reset_type,
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700403 N_("reset HEAD, index and working tree"), MERGE),
Christian Couder9bc454d2010-01-19 05:25:57 +0100404 OPT_SET_INT(0, "keep", &reset_type,
Nguyễn Thái Ngọc Duyc1e9c2a2012-08-20 19:32:39 +0700405 N_("reset HEAD but keep local changes"), KEEP),
Denton Liu203c8532020-04-28 04:36:28 -0400406 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
Stefan Beller35b96d12017-04-21 10:39:53 -0700407 "reset", "control recursive updating of submodules",
Denton Liu203c8532020-04-28 04:36:28 -0400408 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200409 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700410 OPT_BOOL('N', "intent-to-add", &intent_to_add,
411 N_("record only the fact that removed paths will be added later")),
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000412 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
413 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100414 OPT_END()
415 };
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200416
Stefan Beller046b4822017-05-31 17:30:47 -0700417 git_config(git_reset_config, NULL);
Ben Peart4c3abd02018-10-23 15:04:22 -0400418 git_config_get_bool("reset.quiet", &quiet);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200419
Stephen Boyd37782922009-05-23 11:53:12 -0700420 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
Carlos Rica5eee6b22008-03-04 23:11:34 +0100421 PARSE_OPT_KEEP_DASHDASH);
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700422 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200423
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000424 if (pathspec_from_file) {
425 if (patch_mode)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000426 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000427
428 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000429 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000430
431 parse_pathspec_file(&pathspec, 0,
432 PATHSPEC_PREFER_FULL,
433 prefix, pathspec_from_file, pathspec_file_nul);
434 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000435 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiy64bac8d2019-11-19 16:48:53 +0000436 }
437
brian m. carlsone82caf32017-07-13 23:49:28 +0000438 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
Martin von Zweigbergk166ec2e2013-01-14 21:47:50 -0800439 if (unborn) {
440 /* reset on unborn branch: treat as reset to empty tree */
brian m. carlsond8448522018-05-02 00:26:02 +0000441 oidcpy(&oid, the_hash_algo->empty_tree);
Nika Layzell0a8e3032019-11-24 20:25:49 +0000442 } else if (!pathspec.nr && !patch_mode) {
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800443 struct commit *commit;
brian m. carlsone82caf32017-07-13 23:49:28 +0000444 if (get_oid_committish(rev, &oid))
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800445 die(_("Failed to resolve '%s' as a valid revision."), rev);
Stefan Beller2122f672018-06-28 18:21:58 -0700446 commit = lookup_commit_reference(the_repository, &oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800447 if (!commit)
448 die(_("Could not parse object '%s'."), rev);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000449 oidcpy(&oid, &commit->object.oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800450 } else {
451 struct tree *tree;
brian m. carlsone82caf32017-07-13 23:49:28 +0000452 if (get_oid_treeish(rev, &oid))
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800453 die(_("Failed to resolve '%s' as a valid tree."), rev);
brian m. carlsona9dbc172017-05-06 22:10:37 +0000454 tree = parse_tree_indirect(&oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800455 if (!tree)
456 die(_("Could not parse object '%s'."), rev);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000457 oidcpy(&oid, &tree->object.oid);
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800458 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200459
Thomas Rastd002ef42009-08-15 13:48:31 +0200460 if (patch_mode) {
461 if (reset_type != NONE)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000462 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
Jeff Hostetlerc18b6c12019-02-22 14:25:09 -0800463 trace2_cmd_mode("patch-interactive");
Jeff Kingb3e9ce12013-10-25 02:54:06 -0400464 return run_add_interactive(rev, "--patch=reset", &pathspec);
Thomas Rastd002ef42009-08-15 13:48:31 +0200465 }
466
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200467 /* git reset tree [--] paths... can be used to
468 * load chosen paths from the tree into the index without
469 * affecting the working tree nor HEAD. */
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700470 if (pathspec.nr) {
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200471 if (reset_type == MIXED)
Ævar Arnfjörð Bjarmasonb50a64e2011-02-22 23:42:06 +0000472 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200473 else if (reset_type != NONE)
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000474 die(_("Cannot do %s reset with paths."),
475 _(reset_type_names[reset_type]));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200476 }
477 if (reset_type == NONE)
478 reset_type = MIXED; /* by default */
479
Jeff Hostetlerc18b6c12019-02-22 14:25:09 -0800480 if (pathspec.nr)
481 trace2_cmd_mode("path");
482 else
483 trace2_cmd_mode(reset_type_names[reset_type]);
484
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700485 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
Jeff Kingcd0f0f62009-12-30 03:47:03 -0500486 setup_work_tree();
Jeff King49b93622007-12-31 02:13:52 -0500487
Christian Couder2b06b0a2009-12-30 06:54:44 +0100488 if (reset_type == MIXED && is_bare_repository())
Ævar Arnfjörð Bjarmason8b2a57b2011-02-22 23:42:07 +0000489 die(_("%s reset is not allowed in a bare repository"),
490 _(reset_type_names[reset_type]));
Christian Couder2b06b0a2009-12-30 06:54:44 +0100491
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700492 if (intent_to_add && reset_type != MIXED)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000493 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700494
Victoria Dyec01b1cb2021-11-29 15:52:40 +0000495 prepare_repo_settings(the_repository);
496 the_repository->settings.command_requires_full_index = 0;
497
498 if (read_cache() < 0)
499 die(_("index file corrupt"));
500
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200501 /* Soft reset does not touch the index file nor the working tree
502 * at all, but requires them in a good order. Other resets reset
503 * the index file to the tree object we are switching to. */
Martin von Zweigbergk352f58a2013-01-14 21:47:40 -0800504 if (reset_type == SOFT || reset_type == KEEP)
Christian Couder812d2a32010-01-19 05:26:01 +0100505 die_if_unmerged_cache(reset_type);
Martin von Zweigbergk352f58a2013-01-14 21:47:40 -0800506
507 if (reset_type != SOFT) {
Jeff Kingbfffb482017-09-05 08:15:21 -0400508 struct lock_file lock = LOCK_INIT;
509 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
Martin von Zweigbergk3fde3862013-01-14 21:47:51 -0800510 if (reset_type == MIXED) {
Felipe Contrerasf38798f2013-08-30 16:56:45 -0500511 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000512 if (read_from_tree(&pathspec, &oid, intent_to_add))
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800513 return 1;
Ben Peart1956ecd2019-02-15 12:59:21 -0500514 the_index.updated_skipworktree = 1;
Ben Peart649bf3a2018-10-23 15:04:23 -0400515 if (!quiet && get_git_work_tree()) {
516 uint64_t t_begin, t_delta_in_ms;
517
518 t_begin = getnanotime();
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700519 refresh_index(&the_index, flags, NULL, NULL,
520 _("Unstaged changes after reset:"));
Ben Peart649bf3a2018-10-23 15:04:23 -0400521 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
Ben Boeckeled9bff02021-08-23 12:44:00 +0200522 if (advice_enabled(ADVICE_RESET_QUIET_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
Ben Peart649bf3a2018-10-23 15:04:23 -0400523 printf(_("\nIt took %.2f seconds to enumerate unstaged changes after reset. You can\n"
524 "use '--quiet' to avoid this. Set the config setting reset.quiet to true\n"
525 "to make this the default.\n"), t_delta_in_ms / 1000.0);
526 }
527 }
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800528 } else {
brian m. carlson4cf76f62020-03-16 18:05:07 +0000529 struct object_id dummy;
530 char *ref = NULL;
531 int err;
532
Jonathan Tanf24c30e2020-09-01 15:28:09 -0700533 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000534 if (ref && !starts_with(ref, "refs/"))
Andrzej Hunte901de62021-03-14 18:47:35 +0000535 FREE_AND_NULL(ref);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000536
537 err = reset_index(ref, &oid, reset_type, quiet);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800538 if (reset_type == KEEP && !err)
brian m. carlson4cf76f62020-03-16 18:05:07 +0000539 err = reset_index(ref, &oid, MIXED, quiet);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800540 if (err)
541 die(_("Could not reset index file to revision '%s'."), rev);
brian m. carlson4cf76f62020-03-16 18:05:07 +0000542 free(ref);
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800543 }
Martin von Zweigbergkbc41bf42013-01-14 21:47:46 -0800544
Jeff Kingbfffb482017-09-05 08:15:21 -0400545 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
Martin von Zweigbergk1ca38f82013-01-14 21:47:42 -0800546 die(_("Could not write new index file."));
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200547 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200548
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700549 if (!pathspec.nr && !unborn) {
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800550 /* Any resets without paths update HEAD to the head being
551 * switched to, saving the previous head in ORIG_HEAD before. */
brian m. carlson3a5d7c52016-09-05 20:08:11 +0000552 update_ref_status = reset_refs(rev, &oid);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200553
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800554 if (reset_type == HARD && !update_ref_status && !quiet)
Stefan Beller2122f672018-06-28 18:21:58 -0700555 print_new_head_line(lookup_commit_reference(the_repository, &oid));
Martin von Zweigbergk3bbf2f22013-01-14 21:47:47 -0800556 }
Nguyễn Thái Ngọc Duyf8144c92013-07-14 15:35:47 +0700557 if (!pathspec.nr)
Nguyễn Thái Ngọc Duyf4a4b9a2019-03-29 17:38:59 +0700558 remove_branch_state(the_repository, 0);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200559
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200560 return update_ref_status;
561}