Merge branch 'ob/test-lib-rebase-fake-editor-updates'
[alt-git.git] / reset.c
blob48da0adf851e1b09edfc22ffbbc0b1128cbbc5ab
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 "run-command.h"
10 #include "tree-walk.h"
11 #include "tree.h"
12 #include "unpack-trees.h"
13 #include "hook.h"
15 static int update_refs(const struct reset_head_opts *opts,
16 const struct object_id *oid,
17 const struct object_id *head)
19 unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
20 unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
21 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
22 const struct object_id *orig_head = opts->orig_head;
23 const char *switch_to_branch = opts->branch;
24 const char *reflog_branch = opts->branch_msg;
25 const char *reflog_head = opts->head_msg;
26 const char *reflog_orig_head = opts->orig_head_msg;
27 const char *default_reflog_action = opts->default_reflog_action;
28 struct object_id *old_orig = NULL, oid_old_orig;
29 struct strbuf msg = STRBUF_INIT;
30 const char *reflog_action;
31 size_t prefix_len;
32 int ret;
34 if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
35 if (!default_reflog_action)
36 BUG("default_reflog_action must be given when reflog messages are omitted");
37 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
38 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
39 default_reflog_action);
41 prefix_len = msg.len;
43 if (update_orig_head) {
44 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
45 old_orig = &oid_old_orig;
46 if (head) {
47 if (!reflog_orig_head) {
48 strbuf_addstr(&msg, "updating ORIG_HEAD");
49 reflog_orig_head = msg.buf;
51 update_ref(reflog_orig_head, "ORIG_HEAD",
52 orig_head ? orig_head : head,
53 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
54 } else if (old_orig)
55 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
58 if (!reflog_head) {
59 strbuf_setlen(&msg, prefix_len);
60 strbuf_addstr(&msg, "updating HEAD");
61 reflog_head = msg.buf;
63 if (!switch_to_branch)
64 ret = update_ref(reflog_head, "HEAD", oid, head,
65 detach_head ? REF_NO_DEREF : 0,
66 UPDATE_REFS_MSG_ON_ERR);
67 else {
68 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
69 switch_to_branch, oid, NULL, 0,
70 UPDATE_REFS_MSG_ON_ERR);
71 if (!ret)
72 ret = create_symref("HEAD", switch_to_branch,
73 reflog_head);
75 if (!ret && run_hook)
76 run_hooks_l("post-checkout",
77 oid_to_hex(head ? head : null_oid()),
78 oid_to_hex(oid), "1", NULL);
79 strbuf_release(&msg);
80 return ret;
83 int reset_head(struct repository *r, const struct reset_head_opts *opts)
85 const struct object_id *oid = opts->oid;
86 const char *switch_to_branch = opts->branch;
87 unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
88 unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
89 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
90 struct object_id *head = NULL, head_oid;
91 struct tree_desc desc[2] = { { NULL }, { NULL } };
92 struct lock_file lock = LOCK_INIT;
93 struct unpack_trees_options unpack_tree_opts = { 0 };
94 struct tree *tree;
95 const char *action;
96 int ret = 0, nr = 0;
98 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
99 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
101 if (opts->orig_head_msg && !update_orig_head)
102 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
104 if (opts->branch_msg && !opts->branch)
105 BUG("branch reflog message given without a branch");
107 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
108 ret = -1;
109 goto leave_reset_head;
112 if (!repo_get_oid(r, "HEAD", &head_oid)) {
113 head = &head_oid;
114 } else if (!oid || !reset_hard) {
115 ret = error(_("could not determine HEAD revision"));
116 goto leave_reset_head;
119 if (!oid)
120 oid = &head_oid;
122 if (refs_only)
123 return update_refs(opts, oid, head);
125 action = reset_hard ? "reset" : "checkout";
126 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
127 unpack_tree_opts.head_idx = 1;
128 unpack_tree_opts.src_index = r->index;
129 unpack_tree_opts.dst_index = r->index;
130 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
131 unpack_tree_opts.update = 1;
132 unpack_tree_opts.merge = 1;
133 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
134 unpack_tree_opts.skip_cache_tree_update = 1;
135 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
136 if (reset_hard)
137 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
139 if (repo_read_index_unmerged(r) < 0) {
140 ret = error(_("could not read index"));
141 goto leave_reset_head;
144 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
145 ret = error(_("failed to find tree of %s"),
146 oid_to_hex(&head_oid));
147 goto leave_reset_head;
150 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
151 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
152 goto leave_reset_head;
155 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
156 ret = -1;
157 goto leave_reset_head;
160 tree = parse_tree_indirect(oid);
161 prime_cache_tree(r, r->index, tree);
163 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
164 ret = error(_("could not write index"));
165 goto leave_reset_head;
168 if (oid != &head_oid || update_orig_head || switch_to_branch)
169 ret = update_refs(opts, oid, head);
171 leave_reset_head:
172 rollback_lock_file(&lock);
173 clear_unpack_trees_porcelain(&unpack_tree_opts);
174 while (nr)
175 free((void *)desc[--nr].buffer);
176 return ret;