| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "cache-tree.h" |
| Elijah Newren | f394e09 | 2023-03-21 06:25:54 | [diff] [blame] | 3 | #include "gettext.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 4 | #include "hex.h" |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 5 | #include "lockfile.h" |
| Elijah Newren | dabab1d | 2023-04-11 07:41:49 | [diff] [blame] | 6 | #include "object-name.h" |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 7 | #include "refs.h" |
| 8 | #include "reset.h" |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 9 | #include "tree-walk.h" |
| 10 | #include "tree.h" |
| 11 | #include "unpack-trees.h" |
| Emily Shaffer | 72ddf34 | 2021-12-22 03:59:35 | [diff] [blame] | 12 | #include "hook.h" |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 13 | |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 14 | static int update_refs(const struct reset_head_opts *opts, |
| 15 | const struct object_id *oid, |
| 16 | const struct object_id *head) |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 17 | { |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 18 | unsigned detach_head = opts->flags & RESET_HEAD_DETACH; |
| 19 | unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; |
| 20 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; |
| Phillip Wood | cd1528e | 2022-01-26 13:05:48 | [diff] [blame] | 21 | const struct object_id *orig_head = opts->orig_head; |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 22 | const char *switch_to_branch = opts->branch; |
| Phillip Wood | 7700ab0 | 2022-01-26 13:05:47 | [diff] [blame] | 23 | const char *reflog_branch = opts->branch_msg; |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 24 | const char *reflog_head = opts->head_msg; |
| 25 | const char *reflog_orig_head = opts->orig_head_msg; |
| 26 | const char *default_reflog_action = opts->default_reflog_action; |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 27 | struct object_id *old_orig = NULL, oid_old_orig; |
| 28 | struct strbuf msg = STRBUF_INIT; |
| 29 | const char *reflog_action; |
| 30 | size_t prefix_len; |
| 31 | int ret; |
| 32 | |
| Phillip Wood | 1526d0f | 2022-01-26 13:05:43 | [diff] [blame] | 33 | if ((update_orig_head && !reflog_orig_head) || !reflog_head) { |
| 34 | if (!default_reflog_action) |
| 35 | BUG("default_reflog_action must be given when reflog messages are omitted"); |
| 36 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); |
| 37 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : |
| 38 | default_reflog_action); |
| 39 | } |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 40 | prefix_len = msg.len; |
| 41 | |
| 42 | if (update_orig_head) { |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 43 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 44 | old_orig = &oid_old_orig; |
| 45 | if (head) { |
| 46 | if (!reflog_orig_head) { |
| 47 | strbuf_addstr(&msg, "updating ORIG_HEAD"); |
| 48 | reflog_orig_head = msg.buf; |
| 49 | } |
| Phillip Wood | cd1528e | 2022-01-26 13:05:48 | [diff] [blame] | 50 | update_ref(reflog_orig_head, "ORIG_HEAD", |
| 51 | orig_head ? orig_head : head, |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 52 | old_orig, 0, UPDATE_REFS_MSG_ON_ERR); |
| 53 | } else if (old_orig) |
| 54 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); |
| 55 | } |
| 56 | |
| 57 | if (!reflog_head) { |
| 58 | strbuf_setlen(&msg, prefix_len); |
| 59 | strbuf_addstr(&msg, "updating HEAD"); |
| 60 | reflog_head = msg.buf; |
| 61 | } |
| 62 | if (!switch_to_branch) |
| 63 | ret = update_ref(reflog_head, "HEAD", oid, head, |
| 64 | detach_head ? REF_NO_DEREF : 0, |
| 65 | UPDATE_REFS_MSG_ON_ERR); |
| 66 | else { |
| Phillip Wood | 7700ab0 | 2022-01-26 13:05:47 | [diff] [blame] | 67 | ret = update_ref(reflog_branch ? reflog_branch : reflog_head, |
| 68 | switch_to_branch, oid, NULL, 0, |
| 69 | UPDATE_REFS_MSG_ON_ERR); |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 70 | if (!ret) |
| 71 | ret = create_symref("HEAD", switch_to_branch, |
| 72 | reflog_head); |
| 73 | } |
| 74 | if (!ret && run_hook) |
| Junio C Hamano | bcd020f | 2022-02-18 21:53:27 | [diff] [blame] | 75 | run_hooks_l("post-checkout", |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 76 | oid_to_hex(head ? head : null_oid()), |
| 77 | oid_to_hex(oid), "1", NULL); |
| 78 | strbuf_release(&msg); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 82 | int reset_head(struct repository *r, const struct reset_head_opts *opts) |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 83 | { |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 84 | const struct object_id *oid = opts->oid; |
| 85 | const char *switch_to_branch = opts->branch; |
| 86 | unsigned reset_hard = opts->flags & RESET_HEAD_HARD; |
| 87 | unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY; |
| 88 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; |
| Phillip Wood | 69f4c23 | 2022-01-26 13:05:38 | [diff] [blame] | 89 | struct object_id *head = NULL, head_oid; |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 90 | struct tree_desc desc[2] = { { NULL }, { NULL } }; |
| 91 | struct lock_file lock = LOCK_INIT; |
| Andrzej Hunt | 9a863b3 | 2021-07-25 13:08:30 | [diff] [blame] | 92 | struct unpack_trees_options unpack_tree_opts = { 0 }; |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 93 | struct tree *tree; |
| Phillip Wood | d6a9f5e | 2022-01-26 13:05:42 | [diff] [blame] | 94 | const char *action; |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 95 | int ret = 0, nr = 0; |
| 96 | |
| 97 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
| 98 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); |
| 99 | |
| Phillip Wood | 7700ab0 | 2022-01-26 13:05:47 | [diff] [blame] | 100 | if (opts->orig_head_msg && !update_orig_head) |
| 101 | BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD"); |
| 102 | |
| 103 | if (opts->branch_msg && !opts->branch) |
| 104 | BUG("branch reflog message given without a branch"); |
| 105 | |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 106 | if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
| 107 | ret = -1; |
| 108 | goto leave_reset_head; |
| 109 | } |
| 110 | |
| Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 13:58:58 | [diff] [blame] | 111 | if (!repo_get_oid(r, "HEAD", &head_oid)) { |
| Phillip Wood | 69f4c23 | 2022-01-26 13:05:38 | [diff] [blame] | 112 | head = &head_oid; |
| 113 | } else if (!oid || !reset_hard) { |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 114 | ret = error(_("could not determine HEAD revision")); |
| 115 | goto leave_reset_head; |
| 116 | } |
| 117 | |
| 118 | if (!oid) |
| 119 | oid = &head_oid; |
| 120 | |
| 121 | if (refs_only) |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 122 | return update_refs(opts, oid, head); |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 123 | |
| Phillip Wood | 1946d45 | 2022-01-26 13:05:41 | [diff] [blame] | 124 | action = reset_hard ? "reset" : "checkout"; |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 125 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); |
| 126 | unpack_tree_opts.head_idx = 1; |
| 127 | unpack_tree_opts.src_index = r->index; |
| 128 | unpack_tree_opts.dst_index = r->index; |
| 129 | unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; |
| 130 | unpack_tree_opts.update = 1; |
| 131 | unpack_tree_opts.merge = 1; |
| Elijah Newren | 1b5f373 | 2021-09-27 16:33:43 | [diff] [blame] | 132 | unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
| Victoria Dye | 652bd02 | 2022-11-10 19:06:05 | [diff] [blame] | 133 | unpack_tree_opts.skip_cache_tree_update = 1; |
| Junio C Hamano | bf10200 | 2020-04-29 23:15:27 | [diff] [blame] | 134 | init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL); |
| Phillip Wood | ab2fba0 | 2022-01-26 13:05:39 | [diff] [blame] | 135 | if (reset_hard) |
| Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 | [diff] [blame] | 136 | unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 137 | |
| 138 | if (repo_read_index_unmerged(r) < 0) { |
| 139 | ret = error(_("could not read index")); |
| 140 | goto leave_reset_head; |
| 141 | } |
| 142 | |
| 143 | if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) { |
| 144 | ret = error(_("failed to find tree of %s"), |
| 145 | oid_to_hex(&head_oid)); |
| 146 | goto leave_reset_head; |
| 147 | } |
| 148 | |
| 149 | if (!fill_tree_descriptor(r, &desc[nr++], oid)) { |
| 150 | ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); |
| 151 | goto leave_reset_head; |
| 152 | } |
| 153 | |
| 154 | if (unpack_trees(nr, desc, &unpack_tree_opts)) { |
| 155 | ret = -1; |
| 156 | goto leave_reset_head; |
| 157 | } |
| 158 | |
| 159 | tree = parse_tree_indirect(oid); |
| 160 | prime_cache_tree(r, r->index, tree); |
| 161 | |
| 162 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) { |
| 163 | ret = error(_("could not write index")); |
| 164 | goto leave_reset_head; |
| 165 | } |
| 166 | |
| Phillip Wood | 1526d0f | 2022-01-26 13:05:43 | [diff] [blame] | 167 | if (oid != &head_oid || update_orig_head || switch_to_branch) |
| Phillip Wood | 6ae8086 | 2022-01-26 13:05:46 | [diff] [blame] | 168 | ret = update_refs(opts, oid, head); |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 169 | |
| 170 | leave_reset_head: |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 171 | rollback_lock_file(&lock); |
| Andrzej Hunt | 9a863b3 | 2021-07-25 13:08:30 | [diff] [blame] | 172 | clear_unpack_trees_porcelain(&unpack_tree_opts); |
| Denton Liu | b309a97 | 2020-04-07 14:28:00 | [diff] [blame] | 173 | while (nr) |
| 174 | free((void *)desc[--nr].buffer); |
| 175 | return ret; |
| 176 | |
| 177 | } |