🌐 AI搜索 & 代理 主页
blob: 0f2ff0fe31531fc5602025cfcd866a9348cfcf63 [file] [log] [blame]
Denton Liub309a972020-04-07 14:28:001#include "git-compat-util.h"
2#include "cache-tree.h"
Elijah Newrenf394e092023-03-21 06:25:543#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:274#include "hex.h"
Denton Liub309a972020-04-07 14:28:005#include "lockfile.h"
Elijah Newrendabab1d2023-04-11 07:41:496#include "object-name.h"
Denton Liub309a972020-04-07 14:28:007#include "refs.h"
8#include "reset.h"
Denton Liub309a972020-04-07 14:28:009#include "tree-walk.h"
10#include "tree.h"
11#include "unpack-trees.h"
Emily Shaffer72ddf342021-12-22 03:59:3512#include "hook.h"
Denton Liub309a972020-04-07 14:28:0013
Phillip Wood6ae80862022-01-26 13:05:4614static int update_refs(const struct reset_head_opts *opts,
15 const struct object_id *oid,
16 const struct object_id *head)
Denton Liub309a972020-04-07 14:28:0017{
Phillip Wood6ae80862022-01-26 13:05:4618 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 Woodcd1528e2022-01-26 13:05:4821 const struct object_id *orig_head = opts->orig_head;
Phillip Wood6ae80862022-01-26 13:05:4622 const char *switch_to_branch = opts->branch;
Phillip Wood7700ab02022-01-26 13:05:4723 const char *reflog_branch = opts->branch_msg;
Phillip Wood6ae80862022-01-26 13:05:4624 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 Woodd6a9f5e2022-01-26 13:05:4227 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 Wood1526d0f2022-01-26 13:05:4333 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 Woodd6a9f5e2022-01-26 13:05:4240 prefix_len = msg.len;
41
42 if (update_orig_head) {
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:4643 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
Phillip Woodd6a9f5e2022-01-26 13:05:4244 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 Woodcd1528e2022-01-26 13:05:4850 update_ref(reflog_orig_head, "ORIG_HEAD",
51 orig_head ? orig_head : head,
Phillip Woodd6a9f5e2022-01-26 13:05:4252 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 Wood7700ab02022-01-26 13:05:4767 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
68 switch_to_branch, oid, NULL, 0,
69 UPDATE_REFS_MSG_ON_ERR);
Phillip Woodd6a9f5e2022-01-26 13:05:4270 if (!ret)
71 ret = create_symref("HEAD", switch_to_branch,
72 reflog_head);
73 }
74 if (!ret && run_hook)
Junio C Hamanobcd020f2022-02-18 21:53:2775 run_hooks_l("post-checkout",
Phillip Woodd6a9f5e2022-01-26 13:05:4276 oid_to_hex(head ? head : null_oid()),
77 oid_to_hex(oid), "1", NULL);
78 strbuf_release(&msg);
79 return ret;
80}
81
Phillip Wood6ae80862022-01-26 13:05:4682int reset_head(struct repository *r, const struct reset_head_opts *opts)
Denton Liub309a972020-04-07 14:28:0083{
Phillip Wood6ae80862022-01-26 13:05:4684 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 Wood69f4c232022-01-26 13:05:3889 struct object_id *head = NULL, head_oid;
Denton Liub309a972020-04-07 14:28:0090 struct tree_desc desc[2] = { { NULL }, { NULL } };
91 struct lock_file lock = LOCK_INIT;
Andrzej Hunt9a863b32021-07-25 13:08:3092 struct unpack_trees_options unpack_tree_opts = { 0 };
Denton Liub309a972020-04-07 14:28:0093 struct tree *tree;
Phillip Woodd6a9f5e2022-01-26 13:05:4294 const char *action;
Denton Liub309a972020-04-07 14:28:0095 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 Wood7700ab02022-01-26 13:05:47100 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 Liub309a972020-04-07 14:28:00106 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ð Bjarmason4a93b892023-03-28 13:58:58111 if (!repo_get_oid(r, "HEAD", &head_oid)) {
Phillip Wood69f4c232022-01-26 13:05:38112 head = &head_oid;
113 } else if (!oid || !reset_hard) {
Denton Liub309a972020-04-07 14:28:00114 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 Wood6ae80862022-01-26 13:05:46122 return update_refs(opts, oid, head);
Denton Liub309a972020-04-07 14:28:00123
Phillip Wood1946d452022-01-26 13:05:41124 action = reset_hard ? "reset" : "checkout";
Denton Liub309a972020-04-07 14:28:00125 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 Newren1b5f3732021-09-27 16:33:43132 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
Victoria Dye652bd022022-11-10 19:06:05133 unpack_tree_opts.skip_cache_tree_update = 1;
Junio C Hamanobf102002020-04-29 23:15:27134 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
Phillip Woodab2fba02022-01-26 13:05:39135 if (reset_hard)
Elijah Newren480d3d62021-09-27 16:33:44136 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
Denton Liub309a972020-04-07 14:28:00137
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 Wood1526d0f2022-01-26 13:05:43167 if (oid != &head_oid || update_orig_head || switch_to_branch)
Phillip Wood6ae80862022-01-26 13:05:46168 ret = update_refs(opts, oid, head);
Denton Liub309a972020-04-07 14:28:00169
170leave_reset_head:
Denton Liub309a972020-04-07 14:28:00171 rollback_lock_file(&lock);
Andrzej Hunt9a863b32021-07-25 13:08:30172 clear_unpack_trees_porcelain(&unpack_tree_opts);
Denton Liub309a972020-04-07 14:28:00173 while (nr)
174 free((void *)desc[--nr].buffer);
175 return ret;
176
177}