Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 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 | */ |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 10 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 11 | #include "builtin.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 12 | #include "advice.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 13 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 14 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 15 | #include "gettext.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 16 | #include "hash.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 17 | #include "hex.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 18 | #include "lockfile.h" |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 19 | #include "object.h" |
Olga Telezhnaya | cf39471 | 2017-12-12 08:55:35 +0000 | [diff] [blame] | 20 | #include "pretty.h" |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 21 | #include "refs.h" |
| 22 | #include "diff.h" |
| 23 | #include "diffcore.h" |
| 24 | #include "tree.h" |
Daniel Barkalow | c369e7b | 2008-02-07 11:40:16 -0500 | [diff] [blame] | 25 | #include "branch.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 26 | #include "object-name.h" |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 27 | #include "parse-options.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 28 | #include "path.h" |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 29 | #include "unpack-trees.h" |
| 30 | #include "cache-tree.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 31 | #include "setup.h" |
Elijah Newren | baf889c | 2023-05-16 06:33:51 +0000 | [diff] [blame] | 32 | #include "sparse-index.h" |
Stefan Beller | 35b96d1 | 2017-04-21 10:39:53 -0700 | [diff] [blame] | 33 | #include "submodule.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 34 | #include "trace.h" |
| 35 | #include "trace2.h" |
Victoria Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 36 | #include "dir.h" |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 37 | #include "add-interactive.h" |
Stefan Beller | 35b96d1 | 2017-04-21 10:39:53 -0700 | [diff] [blame] | 38 | |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 39 | #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000) |
| 40 | |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 41 | static const char * const git_reset_usage[] = { |
Nguyễn Thái Ngọc Duy | c1e9c2a | 2012-08-20 19:32:39 +0700 | [diff] [blame] | 42 | N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"), |
Alexandr Miloslavskiy | d137b50 | 2019-11-19 16:48:52 +0000 | [diff] [blame] | 43 | N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."), |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 44 | N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"), |
Alexandr Miloslavskiy | d137b50 | 2019-11-19 16:48:52 +0000 | [diff] [blame] | 45 | N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"), |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 46 | NULL |
| 47 | }; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 48 | |
Christian Couder | 9bc454d | 2010-01-19 05:25:57 +0100 | [diff] [blame] | 49 | enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE }; |
| 50 | static const char *reset_type_names[] = { |
Ævar Arnfjörð Bjarmason | 8b2a57b | 2011-02-22 23:42:07 +0000 | [diff] [blame] | 51 | N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL |
Christian Couder | 9bc454d | 2010-01-19 05:25:57 +0100 | [diff] [blame] | 52 | }; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 53 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 54 | static inline int is_merge(void) |
| 55 | { |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 56 | return !access(git_path_merge_head(the_repository), F_OK); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 57 | } |
| 58 | |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 59 | static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 60 | { |
Jeff King | afbb883 | 2017-09-05 09:04:51 -0400 | [diff] [blame] | 61 | int i, nr = 0; |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 62 | struct tree_desc desc[2]; |
Thomas Rast | 6c52ec8 | 2011-12-06 18:43:39 +0100 | [diff] [blame] | 63 | struct tree *tree; |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 64 | struct unpack_trees_options opts; |
Jeff King | afbb883 | 2017-09-05 09:04:51 -0400 | [diff] [blame] | 65 | int ret = -1; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 66 | |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 67 | memset(&opts, 0, sizeof(opts)); |
| 68 | opts.head_idx = 1; |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 69 | opts.src_index = the_repository->index; |
| 70 | opts.dst_index = the_repository->index; |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 71 | opts.fn = oneway_merge; |
| 72 | opts.merge = 1; |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 73 | init_checkout_metadata(&opts.meta, ref, oid, NULL); |
Jamis Buck | 5aa965a | 2008-05-31 18:10:58 -0700 | [diff] [blame] | 74 | if (!quiet) |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 75 | opts.verbose_update = 1; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 76 | switch (reset_type) { |
Christian Couder | 9bc454d | 2010-01-19 05:25:57 +0100 | [diff] [blame] | 77 | case KEEP: |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 78 | case MERGE: |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 79 | opts.update = 1; |
Elijah Newren | 1b5f373 | 2021-09-27 16:33:43 +0000 | [diff] [blame] | 80 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 81 | break; |
| 82 | case HARD: |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 83 | opts.update = 1; |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 84 | opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED; |
Victoria Dye | 0e47bca | 2022-11-10 19:06:03 +0000 | [diff] [blame] | 85 | opts.skip_cache_tree_update = 1; |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 86 | break; |
| 87 | case MIXED: |
| 88 | opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; |
Victoria Dye | 0e47bca | 2022-11-10 19:06:03 +0000 | [diff] [blame] | 89 | opts.skip_cache_tree_update = 1; |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 90 | /* but opts.update=0, so working tree not updated */ |
| 91 | break; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 92 | default: |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 93 | BUG("invalid reset_type passed to reset_index"); |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 94 | } |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 95 | |
Ævar Arnfjörð Bjarmason | fbc1ed6 | 2022-11-19 14:07:30 +0100 | [diff] [blame] | 96 | repo_read_index_unmerged(the_repository); |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 97 | |
Christian Couder | 9bc454d | 2010-01-19 05:25:57 +0100 | [diff] [blame] | 98 | if (reset_type == KEEP) { |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 99 | struct object_id head_oid; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 100 | if (repo_get_oid(the_repository, "HEAD", &head_oid)) |
Ævar Arnfjörð Bjarmason | b50a64e | 2011-02-22 23:42:06 +0000 | [diff] [blame] | 101 | return error(_("You do not have a valid HEAD.")); |
Nguyễn Thái Ngọc Duy | 5e57580 | 2019-06-27 16:28:48 +0700 | [diff] [blame] | 102 | if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid)) |
Ævar Arnfjörð Bjarmason | b50a64e | 2011-02-22 23:42:06 +0000 | [diff] [blame] | 103 | return error(_("Failed to find tree of HEAD.")); |
Christian Couder | 9bc454d | 2010-01-19 05:25:57 +0100 | [diff] [blame] | 104 | nr++; |
| 105 | opts.fn = twoway_merge; |
| 106 | } |
| 107 | |
Nguyễn Thái Ngọc Duy | 5e57580 | 2019-06-27 16:28:48 +0700 | [diff] [blame] | 108 | if (!fill_tree_descriptor(the_repository, desc + nr, oid)) { |
Jeff King | afbb883 | 2017-09-05 09:04:51 -0400 | [diff] [blame] | 109 | error(_("Failed to find tree of %s."), oid_to_hex(oid)); |
| 110 | goto out; |
| 111 | } |
Jeff King | e9ce897 | 2017-09-05 09:04:28 -0400 | [diff] [blame] | 112 | nr++; |
| 113 | |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 114 | if (unpack_trees(nr, desc, &opts)) |
Jeff King | afbb883 | 2017-09-05 09:04:51 -0400 | [diff] [blame] | 115 | goto out; |
Thomas Rast | 6c52ec8 | 2011-12-06 18:43:39 +0100 | [diff] [blame] | 116 | |
| 117 | if (reset_type == MIXED || reset_type == HARD) { |
brian m. carlson | a9dbc17 | 2017-05-06 22:10:37 +0000 | [diff] [blame] | 118 | tree = parse_tree_indirect(oid); |
Johannes Schindelin | aa9f618 | 2024-02-23 08:34:23 +0000 | [diff] [blame] | 119 | if (!tree) { |
| 120 | error(_("unable to read tree (%s)"), oid_to_hex(oid)); |
| 121 | goto out; |
| 122 | } |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 123 | prime_cache_tree(the_repository, the_repository->index, tree); |
Thomas Rast | 6c52ec8 | 2011-12-06 18:43:39 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Jeff King | afbb883 | 2017-09-05 09:04:51 -0400 | [diff] [blame] | 126 | ret = 0; |
| 127 | |
| 128 | out: |
| 129 | for (i = 0; i < nr; i++) |
| 130 | free((void *)desc[i].buffer); |
| 131 | return ret; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void print_new_head_line(struct commit *commit) |
| 135 | { |
Thomas Gummerer | 1cf823f | 2018-02-01 20:57:21 +0000 | [diff] [blame] | 136 | struct strbuf buf = STRBUF_INIT; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 137 | |
Thomas Gummerer | 1cf823f | 2018-02-01 20:57:21 +0000 | [diff] [blame] | 138 | printf(_("HEAD is now at %s"), |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 139 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); |
Thomas Gummerer | 1cf823f | 2018-02-01 20:57:21 +0000 | [diff] [blame] | 140 | |
| 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 Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 148 | static void update_index_from_diff(struct diff_queue_struct *q, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 149 | struct diff_options *opt UNUSED, |
| 150 | void *data) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 151 | { |
| 152 | int i; |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 153 | int intent_to_add = *(int *)data; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 154 | |
| 155 | for (i = 0; i < q->nr; i++) { |
Victoria Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 156 | int pos; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 157 | struct diff_filespec *one = q->queue[i]->one; |
Victoria Dye | 1f86b7c | 2021-10-07 21:15:31 +0000 | [diff] [blame] | 158 | int is_in_reset_tree = one->mode && !is_null_oid(&one->oid); |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 159 | struct cache_entry *ce; |
| 160 | |
Victoria Dye | 1f86b7c | 2021-10-07 21:15:31 +0000 | [diff] [blame] | 161 | if (!is_in_reset_tree && !intent_to_add) { |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 162 | remove_file_from_index(the_repository->index, one->path); |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 166 | ce = make_cache_entry(the_repository->index, one->mode, &one->oid, one->path, |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 167 | 0, 0); |
Victoria Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 168 | |
| 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 Dye | 4d1cfc1 | 2021-11-29 15:52:42 +0000 | [diff] [blame] | 173 | * 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 Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 176 | */ |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 177 | 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 Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 180 | ce->ce_flags |= CE_SKIP_WORKTREE; |
| 181 | |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 182 | if (!ce) |
| 183 | die(_("make_cache_entry failed for path '%s'"), |
| 184 | one->path); |
Victoria Dye | 1f86b7c | 2021-10-07 21:15:31 +0000 | [diff] [blame] | 185 | if (!is_in_reset_tree) { |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 186 | ce->ce_flags |= CE_INTENT_TO_ADD; |
| 187 | set_object_name_for_intent_to_add_entry(ce); |
| 188 | } |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 189 | add_index_entry(the_repository->index, ce, |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 190 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 15:35:58 +0700 | [diff] [blame] | 194 | static int read_from_tree(const struct pathspec *pathspec, |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 195 | struct object_id *tree_oid, |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 196 | int intent_to_add) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 197 | { |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 198 | struct diff_options opt; |
| 199 | |
| 200 | memset(&opt, 0, sizeof(opt)); |
Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 15:35:58 +0700 | [diff] [blame] | 201 | copy_pathspec(&opt.pathspec, pathspec); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 202 | opt.output_format = DIFF_FORMAT_CALLBACK; |
| 203 | opt.format_callback = update_index_from_diff; |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 204 | opt.format_callback_data = &intent_to_add; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 205 | opt.flags.override_submodule_config = 1; |
Victoria Dye | 4d1cfc1 | 2021-11-29 15:52:42 +0000 | [diff] [blame] | 206 | opt.flags.recursive = 1; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 207 | opt.repo = the_repository; |
Victoria Dye | 4d1cfc1 | 2021-11-29 15:52:42 +0000 | [diff] [blame] | 208 | opt.change = diff_change; |
| 209 | opt.add_remove = diff_addremove; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 210 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 211 | if (pathspec->nr && pathspec_needs_expanded_index(the_repository->index, pathspec)) |
| 212 | ensure_full_index(the_repository->index); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 213 | |
brian m. carlson | 944cffb | 2017-05-06 22:10:35 +0000 | [diff] [blame] | 214 | if (do_diff_cache(tree_oid, &opt)) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 215 | return 1; |
| 216 | diffcore_std(&opt); |
| 217 | diff_flush(&opt); |
Johannes Schindelin | 2e7a978 | 2007-11-03 13:12:17 +0000 | [diff] [blame] | 218 | |
Martin von Zweigbergk | bf883f3 | 2013-01-14 21:47:44 -0800 | [diff] [blame] | 219 | return 0; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Jeff King | d04520e | 2011-07-22 10:12:23 -0600 | [diff] [blame] | 222 | static void set_reflog_message(struct strbuf *sb, const char *action, |
| 223 | const char *rev) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 224 | { |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 225 | const char *rla = getenv("GIT_REFLOG_ACTION"); |
Jeff King | d04520e | 2011-07-22 10:12:23 -0600 | [diff] [blame] | 226 | |
| 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 Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 236 | static void die_if_unmerged_cache(int reset_type) |
| 237 | { |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 238 | if (is_merge() || unmerged_index(the_repository->index)) |
Ævar Arnfjörð Bjarmason | 8b2a57b | 2011-02-22 23:42:07 +0000 | [diff] [blame] | 239 | die(_("Cannot do a %s reset in the middle of a merge."), |
| 240 | _(reset_type_names[reset_type])); |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 241 | |
| 242 | } |
| 243 | |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 244 | static void parse_args(struct pathspec *pathspec, |
| 245 | const char **argv, const char *prefix, |
| 246 | int patch_mode, |
| 247 | const char **rev_ret) |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 248 | { |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 249 | const char *rev = "HEAD"; |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 250 | struct object_id unused; |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 251 | /* |
| 252 | * Possible arguments are: |
| 253 | * |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 254 | * git reset [-opts] [<rev>] |
| 255 | * git reset [-opts] <tree> [<paths>...] |
| 256 | * git reset [-opts] <tree> -- [<paths>...] |
| 257 | * git reset [-opts] -- [<paths>...] |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 258 | * git reset [-opts] <paths>... |
| 259 | * |
Martin von Zweigbergk | dca48cf | 2013-01-14 21:47:38 -0800 | [diff] [blame] | 260 | * At this point, argv points immediately after [-opts]. |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 261 | */ |
| 262 | |
Martin von Zweigbergk | dca48cf | 2013-01-14 21:47:38 -0800 | [diff] [blame] | 263 | 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 Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 269 | } |
| 270 | /* |
Martin von Zweigbergk | dca48cf | 2013-01-14 21:47:38 -0800 | [diff] [blame] | 271 | * Otherwise, argv[0] could be either <rev> or <paths> and |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 272 | * has to be unambiguous. If there is a single argument, it |
| 273 | * can not be a tree |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 274 | */ |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 275 | 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 Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 277 | /* |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 278 | * Ok, argv[0] looks like a commit/tree; it should not |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 279 | * be a filename. |
| 280 | */ |
Martin von Zweigbergk | dca48cf | 2013-01-14 21:47:38 -0800 | [diff] [blame] | 281 | verify_non_filename(prefix, argv[0]); |
| 282 | rev = *argv++; |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 283 | } else { |
| 284 | /* Otherwise we treat this as a filename */ |
Martin von Zweigbergk | dca48cf | 2013-01-14 21:47:38 -0800 | [diff] [blame] | 285 | verify_filename(prefix, argv[0], 1); |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
Ghanshyam Thakkar | 5a8ed3f | 2024-02-13 05:35:29 +0530 | [diff] [blame] | 288 | |
| 289 | /* treat '@' as a shortcut for 'HEAD' */ |
| 290 | *rev_ret = !strcmp("@", rev) ? "HEAD" : rev; |
John Keeping | 2c63d6e | 2013-09-12 20:25:01 +0100 | [diff] [blame] | 291 | |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 292 | parse_pathspec(pathspec, 0, |
| 293 | PATHSPEC_PREFER_FULL | |
| 294 | (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0), |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 295 | prefix, argv); |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 296 | } |
| 297 | |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 298 | static int reset_refs(const char *rev, const struct object_id *oid) |
Martin von Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 299 | { |
| 300 | int update_ref_status; |
| 301 | struct strbuf msg = STRBUF_INIT; |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 302 | struct object_id *orig = NULL, oid_orig, |
| 303 | *old_orig = NULL, oid_old_orig; |
Martin von Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 304 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 305 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 306 | old_orig = &oid_old_orig; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 307 | if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) { |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 308 | orig = &oid_orig; |
Martin von Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 309 | set_reflog_message(&msg, "updating ORIG_HEAD", NULL); |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 310 | 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 Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 313 | } else if (old_orig) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 314 | refs_delete_ref(get_main_ref_store(the_repository), NULL, |
| 315 | "ORIG_HEAD", old_orig, 0); |
Martin von Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 316 | set_reflog_message(&msg, "updating HEAD", rev); |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 317 | 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 Zweigbergk | 7bca0e4 | 2013-01-14 21:47:39 -0800 | [diff] [blame] | 320 | strbuf_release(&msg); |
| 321 | return update_ref_status; |
| 322 | } |
| 323 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 324 | static int git_reset_config(const char *var, const char *value, |
| 325 | const struct config_context *ctx, void *cb) |
Stefan Beller | 046b482 | 2017-05-31 17:30:47 -0700 | [diff] [blame] | 326 | { |
| 327 | if (!strcmp(var, "submodule.recurse")) |
| 328 | return git_default_submodule_config(var, value, cb); |
| 329 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 330 | return git_default_config(var, value, ctx, cb); |
Stefan Beller | 046b482 | 2017-05-31 17:30:47 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 333 | int cmd_reset(int argc, const char **argv, const char *prefix) |
| 334 | { |
Martin von Zweigbergk | 39ea722 | 2013-01-14 21:47:37 -0800 | [diff] [blame] | 335 | int reset_type = NONE, update_ref_status = 0, quiet = 0; |
Junio C Hamano | 5891c76 | 2022-03-24 10:33:10 -0700 | [diff] [blame] | 336 | int no_refresh = 0; |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 337 | int patch_mode = 0, pathspec_file_nul = 0, unborn; |
Jeff King | 7ce4088 | 2023-03-04 05:31:22 -0500 | [diff] [blame] | 338 | const char *rev; |
| 339 | char *pathspec_from_file = NULL; |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 340 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 341 | struct pathspec pathspec; |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 342 | int intent_to_add = 0; |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 343 | const struct option options[] = { |
Nguyễn Thái Ngọc Duy | c1e9c2a | 2012-08-20 19:32:39 +0700 | [diff] [blame] | 344 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), |
Junio C Hamano | 5891c76 | 2022-03-24 10:33:10 -0700 | [diff] [blame] | 345 | OPT_BOOL(0, "no-refresh", &no_refresh, |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 346 | N_("skip refreshing the index after reset")), |
Junio C Hamano | 3821eb6 | 2023-07-19 06:37:39 -0700 | [diff] [blame] | 347 | 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 Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 362 | OPT_CALLBACK_F(0, "recurse-submodules", NULL, |
Junio C Hamano | 3821eb6 | 2023-07-19 06:37:39 -0700 | [diff] [blame] | 363 | "reset", "control recursive updating of submodules", |
| 364 | PARSE_OPT_OPTARG, |
| 365 | option_parse_recurse_submodules_worktree_updater), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 366 | OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")), |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 367 | OPT_BOOL('N', "intent-to-add", &intent_to_add, |
| 368 | N_("record only the fact that removed paths will be added later")), |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 369 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
| 370 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 371 | OPT_END() |
| 372 | }; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 373 | |
Stefan Beller | 046b482 | 2017-05-31 17:30:47 -0700 | [diff] [blame] | 374 | git_config(git_reset_config, NULL); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 375 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 376 | argc = parse_options(argc, argv, prefix, options, git_reset_usage, |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 377 | PARSE_OPT_KEEP_DASHDASH); |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 378 | parse_args(&pathspec, argv, prefix, patch_mode, &rev); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 379 | |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 380 | if (pathspec_from_file) { |
| 381 | if (patch_mode) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 382 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 383 | |
| 384 | if (pathspec.nr) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 385 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 386 | |
| 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 Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 391 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
Alexandr Miloslavskiy | 64bac8d | 2019-11-19 16:48:53 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 394 | unborn = !strcmp(rev, "HEAD") && repo_get_oid(the_repository, "HEAD", |
| 395 | &oid); |
Martin von Zweigbergk | 166ec2e | 2013-01-14 21:47:50 -0800 | [diff] [blame] | 396 | if (unborn) { |
| 397 | /* reset on unborn branch: treat as reset to empty tree */ |
brian m. carlson | d844852 | 2018-05-02 00:26:02 +0000 | [diff] [blame] | 398 | oidcpy(&oid, the_hash_algo->empty_tree); |
Nika Layzell | 0a8e303 | 2019-11-24 20:25:49 +0000 | [diff] [blame] | 399 | } else if (!pathspec.nr && !patch_mode) { |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 400 | struct commit *commit; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 401 | if (repo_get_oid_committish(the_repository, rev, &oid)) |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 402 | die(_("Failed to resolve '%s' as a valid revision."), rev); |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 403 | commit = lookup_commit_reference(the_repository, &oid); |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 404 | if (!commit) |
| 405 | die(_("Could not parse object '%s'."), rev); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 406 | oidcpy(&oid, &commit->object.oid); |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 407 | } else { |
| 408 | struct tree *tree; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 409 | if (repo_get_oid_treeish(the_repository, rev, &oid)) |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 410 | die(_("Failed to resolve '%s' as a valid tree."), rev); |
brian m. carlson | a9dbc17 | 2017-05-06 22:10:37 +0000 | [diff] [blame] | 411 | tree = parse_tree_indirect(&oid); |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 412 | if (!tree) |
| 413 | die(_("Could not parse object '%s'."), rev); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 414 | oidcpy(&oid, &tree->object.oid); |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 415 | } |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 416 | |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 417 | if (patch_mode) { |
| 418 | if (reset_type != NONE) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 419 | die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}"); |
Jeff Hostetler | c18b6c1 | 2019-02-22 14:25:09 -0800 | [diff] [blame] | 420 | trace2_cmd_mode("patch-interactive"); |
Junio C Hamano | 72972ea | 2023-02-22 14:55:45 -0800 | [diff] [blame] | 421 | update_ref_status = !!run_add_p(the_repository, ADD_P_RESET, rev, |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 422 | &pathspec); |
Ævar Arnfjörð Bjarmason | 7615cf9 | 2023-02-07 00:07:40 +0100 | [diff] [blame] | 423 | goto cleanup; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 426 | /* 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 Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 429 | if (pathspec.nr) { |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 430 | if (reset_type == MIXED) |
Ævar Arnfjörð Bjarmason | b50a64e | 2011-02-22 23:42:06 +0000 | [diff] [blame] | 431 | warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 432 | else if (reset_type != NONE) |
Ævar Arnfjörð Bjarmason | 8b2a57b | 2011-02-22 23:42:07 +0000 | [diff] [blame] | 433 | die(_("Cannot do %s reset with paths."), |
| 434 | _(reset_type_names[reset_type])); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 435 | } |
| 436 | if (reset_type == NONE) |
| 437 | reset_type = MIXED; /* by default */ |
| 438 | |
Jeff Hostetler | c18b6c1 | 2019-02-22 14:25:09 -0800 | [diff] [blame] | 439 | 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 Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 444 | if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree())) |
Jeff King | cd0f0f6 | 2009-12-30 03:47:03 -0500 | [diff] [blame] | 445 | setup_work_tree(); |
Jeff King | 49b9362 | 2007-12-31 02:13:52 -0500 | [diff] [blame] | 446 | |
Christian Couder | 2b06b0a | 2009-12-30 06:54:44 +0100 | [diff] [blame] | 447 | if (reset_type == MIXED && is_bare_repository()) |
Ævar Arnfjörð Bjarmason | 8b2a57b | 2011-02-22 23:42:07 +0000 | [diff] [blame] | 448 | die(_("%s reset is not allowed in a bare repository"), |
| 449 | _(reset_type_names[reset_type])); |
Christian Couder | 2b06b0a | 2009-12-30 06:54:44 +0100 | [diff] [blame] | 450 | |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 451 | if (intent_to_add && reset_type != MIXED) |
Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 452 | die(_("the option '%s' requires '%s'"), "-N", "--mixed"); |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 453 | |
Victoria Dye | c01b1cb | 2021-11-29 15:52:40 +0000 | [diff] [blame] | 454 | prepare_repo_settings(the_repository); |
| 455 | the_repository->settings.command_requires_full_index = 0; |
| 456 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 457 | if (repo_read_index(the_repository) < 0) |
Victoria Dye | c01b1cb | 2021-11-29 15:52:40 +0000 | [diff] [blame] | 458 | die(_("index file corrupt")); |
| 459 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 460 | /* 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 Zweigbergk | 352f58a | 2013-01-14 21:47:40 -0800 | [diff] [blame] | 463 | if (reset_type == SOFT || reset_type == KEEP) |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 464 | die_if_unmerged_cache(reset_type); |
Martin von Zweigbergk | 352f58a | 2013-01-14 21:47:40 -0800 | [diff] [blame] | 465 | |
| 466 | if (reset_type != SOFT) { |
Jeff King | bfffb48 | 2017-09-05 08:15:21 -0400 | [diff] [blame] | 467 | struct lock_file lock = LOCK_INIT; |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 468 | repo_hold_locked_index(the_repository, &lock, |
| 469 | LOCK_DIE_ON_ERROR); |
Martin von Zweigbergk | 3fde386 | 2013-01-14 21:47:51 -0800 | [diff] [blame] | 470 | if (reset_type == MIXED) { |
Felipe Contreras | f38798f | 2013-08-30 16:56:45 -0500 | [diff] [blame] | 471 | int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN; |
Ævar Arnfjörð Bjarmason | 7615cf9 | 2023-02-07 00:07:40 +0100 | [diff] [blame] | 472 | if (read_from_tree(&pathspec, &oid, intent_to_add)) { |
| 473 | update_ref_status = 1; |
| 474 | goto cleanup; |
| 475 | } |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 476 | the_repository->index->updated_skipworktree = 1; |
Junio C Hamano | 5891c76 | 2022-03-24 10:33:10 -0700 | [diff] [blame] | 477 | if (!no_refresh && get_git_work_tree()) { |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 478 | uint64_t t_begin, t_delta_in_ms; |
| 479 | |
| 480 | t_begin = getnanotime(); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 481 | refresh_index(the_repository->index, flags, NULL, NULL, |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 482 | _("Unstaged changes after reset:")); |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 483 | t_delta_in_ms = (getnanotime() - t_begin) / 1000000; |
Victoria Dye | d492abb | 2022-03-15 01:49:41 +0000 | [diff] [blame] | 484 | if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) { |
Victoria Dye | 9396251 | 2022-03-15 01:49:40 +0000 | [diff] [blame] | 485 | advise(_("It took %.2f seconds to refresh the index after reset. You can use\n" |
Victoria Dye | 7cff676 | 2022-03-23 18:18:00 +0000 | [diff] [blame] | 486 | "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0); |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 487 | } |
| 488 | } |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 489 | } else { |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 490 | struct object_id dummy; |
| 491 | char *ref = NULL; |
| 492 | int err; |
| 493 | |
Ævar Arnfjörð Bjarmason | 12cb1c1 | 2023-03-28 15:58:54 +0200 | [diff] [blame] | 494 | repo_dwim_ref(the_repository, rev, strlen(rev), |
| 495 | &dummy, &ref, 0); |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 496 | if (ref && !starts_with(ref, "refs/")) |
Andrzej Hunt | e901de6 | 2021-03-14 18:47:35 +0000 | [diff] [blame] | 497 | FREE_AND_NULL(ref); |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 498 | |
| 499 | err = reset_index(ref, &oid, reset_type, quiet); |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 500 | if (reset_type == KEEP && !err) |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 501 | err = reset_index(ref, &oid, MIXED, quiet); |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 502 | if (err) |
| 503 | die(_("Could not reset index file to revision '%s'."), rev); |
brian m. carlson | 4cf76f6 | 2020-03-16 18:05:07 +0000 | [diff] [blame] | 504 | free(ref); |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 505 | } |
Martin von Zweigbergk | bc41bf4 | 2013-01-14 21:47:46 -0800 | [diff] [blame] | 506 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 507 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK)) |
Martin von Zweigbergk | 1ca38f8 | 2013-01-14 21:47:42 -0800 | [diff] [blame] | 508 | die(_("Could not write new index file.")); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 509 | } |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 510 | |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 511 | if (!pathspec.nr && !unborn) { |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 512 | /* Any resets without paths update HEAD to the head being |
| 513 | * switched to, saving the previous head in ORIG_HEAD before. */ |
brian m. carlson | 3a5d7c5 | 2016-09-05 20:08:11 +0000 | [diff] [blame] | 514 | update_ref_status = reset_refs(rev, &oid); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 515 | |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 516 | if (reset_type == HARD && !update_ref_status && !quiet) |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 517 | print_new_head_line(lookup_commit_reference(the_repository, &oid)); |
Martin von Zweigbergk | 3bbf2f2 | 2013-01-14 21:47:47 -0800 | [diff] [blame] | 518 | } |
Nguyễn Thái Ngọc Duy | f8144c9 | 2013-07-14 15:35:47 +0700 | [diff] [blame] | 519 | if (!pathspec.nr) |
Nguyễn Thái Ngọc Duy | f4a4b9a | 2019-03-29 17:38:59 +0700 | [diff] [blame] | 520 | remove_branch_state(the_repository, 0); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 521 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 522 | discard_index(the_repository->index); |
Ævar Arnfjörð Bjarmason | ab2cf37 | 2022-11-08 19:17:38 +0100 | [diff] [blame] | 523 | |
Ævar Arnfjörð Bjarmason | 7615cf9 | 2023-02-07 00:07:40 +0100 | [diff] [blame] | 524 | cleanup: |
| 525 | clear_pathspec(&pathspec); |
Jeff King | 7ce4088 | 2023-03-04 05:31:22 -0500 | [diff] [blame] | 526 | free(pathspec_from_file); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 527 | return update_ref_status; |
| 528 | } |