Doc: fix Asciidoctor css workaround
[alt-git.git] / reset.c
blobd619cb7115323febadbf4ae84e6764200f962cab
1 #include "git-compat-util.h"
2 #include "cache-tree.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "object-name.h"
7 #include "refs.h"
8 #include "reset.h"
9 #include "tree-walk.h"
10 #include "tree.h"
11 #include "unpack-trees.h"
12 #include "hook.h"
14 static int update_refs(const struct reset_head_opts *opts,
15 const struct object_id *oid,
16 const struct object_id *head)
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;
21 const struct object_id *orig_head = opts->orig_head;
22 const char *switch_to_branch = opts->branch;
23 const char *reflog_branch = opts->branch_msg;
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;
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;
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);
40 prefix_len = msg.len;
42 if (update_orig_head) {
43 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
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;
50 update_ref(reflog_orig_head, "ORIG_HEAD",
51 orig_head ? orig_head : head,
52 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
53 } else if (old_orig)
54 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
57 if (!reflog_head) {
58 strbuf_setlen(&msg, prefix_len);
59 strbuf_addstr(&msg, "updating HEAD");
60 reflog_head = msg.buf;
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 {
67 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
68 switch_to_branch, oid, NULL, 0,
69 UPDATE_REFS_MSG_ON_ERR);
70 if (!ret)
71 ret = create_symref("HEAD", switch_to_branch,
72 reflog_head);
74 if (!ret && run_hook)
75 run_hooks_l("post-checkout",
76 oid_to_hex(head ? head : null_oid()),
77 oid_to_hex(oid), "1", NULL);
78 strbuf_release(&msg);
79 return ret;
82 int reset_head(struct repository *r, const struct reset_head_opts *opts)
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;
89 struct object_id *head = NULL, head_oid;
90 struct tree_desc desc[2] = { { NULL }, { NULL } };
91 struct lock_file lock = LOCK_INIT;
92 struct unpack_trees_options unpack_tree_opts = { 0 };
93 struct tree *tree;
94 const char *action;
95 int ret = 0, nr = 0;
97 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
98 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
100 if (opts->orig_head_msg && !update_orig_head)
101 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
103 if (opts->branch_msg && !opts->branch)
104 BUG("branch reflog message given without a branch");
106 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
107 ret = -1;
108 goto leave_reset_head;
111 if (!repo_get_oid(r, "HEAD", &head_oid)) {
112 head = &head_oid;
113 } else if (!oid || !reset_hard) {
114 ret = error(_("could not determine HEAD revision"));
115 goto leave_reset_head;
118 if (!oid)
119 oid = &head_oid;
121 if (refs_only)
122 return update_refs(opts, oid, head);
124 action = reset_hard ? "reset" : "checkout";
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;
132 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
133 unpack_tree_opts.skip_cache_tree_update = 1;
134 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
135 if (reset_hard)
136 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
138 if (repo_read_index_unmerged(r) < 0) {
139 ret = error(_("could not read index"));
140 goto leave_reset_head;
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;
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;
154 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
155 ret = -1;
156 goto leave_reset_head;
159 tree = parse_tree_indirect(oid);
160 if (!tree) {
161 ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
162 goto leave_reset_head;
165 prime_cache_tree(r, r->index, tree);
167 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
168 ret = error(_("could not write index"));
169 goto leave_reset_head;
172 if (oid != &head_oid || update_orig_head || switch_to_branch)
173 ret = update_refs(opts, oid, head);
175 leave_reset_head:
176 rollback_lock_file(&lock);
177 clear_unpack_trees_porcelain(&unpack_tree_opts);
178 while (nr)
179 free((void *)desc[--nr].buffer);
180 return ret;