rebase --apply: set ORIG_HEAD correctly
[git.git] / reset.c
blob448cb3fd78576ed72f0415f2d3b0bdfc3b60a8a4
1 #include "git-compat-util.h"
2 #include "cache-tree.h"
3 #include "lockfile.h"
4 #include "refs.h"
5 #include "reset.h"
6 #include "run-command.h"
7 #include "tree-walk.h"
8 #include "tree.h"
9 #include "unpack-trees.h"
11 static int update_refs(const struct reset_head_opts *opts,
12 const struct object_id *oid,
13 const struct object_id *head)
15 unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
16 unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
17 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
18 const struct object_id *orig_head = opts->orig_head;
19 const char *switch_to_branch = opts->branch;
20 const char *reflog_branch = opts->branch_msg;
21 const char *reflog_head = opts->head_msg;
22 const char *reflog_orig_head = opts->orig_head_msg;
23 const char *default_reflog_action = opts->default_reflog_action;
24 struct object_id *old_orig = NULL, oid_old_orig;
25 struct strbuf msg = STRBUF_INIT;
26 const char *reflog_action;
27 size_t prefix_len;
28 int ret;
30 if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
31 if (!default_reflog_action)
32 BUG("default_reflog_action must be given when reflog messages are omitted");
33 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
34 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
35 default_reflog_action);
37 prefix_len = msg.len;
39 if (update_orig_head) {
40 if (!get_oid("ORIG_HEAD", &oid_old_orig))
41 old_orig = &oid_old_orig;
42 if (head) {
43 if (!reflog_orig_head) {
44 strbuf_addstr(&msg, "updating ORIG_HEAD");
45 reflog_orig_head = msg.buf;
47 update_ref(reflog_orig_head, "ORIG_HEAD",
48 orig_head ? orig_head : head,
49 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
50 } else if (old_orig)
51 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
54 if (!reflog_head) {
55 strbuf_setlen(&msg, prefix_len);
56 strbuf_addstr(&msg, "updating HEAD");
57 reflog_head = msg.buf;
59 if (!switch_to_branch)
60 ret = update_ref(reflog_head, "HEAD", oid, head,
61 detach_head ? REF_NO_DEREF : 0,
62 UPDATE_REFS_MSG_ON_ERR);
63 else {
64 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
65 switch_to_branch, oid, NULL, 0,
66 UPDATE_REFS_MSG_ON_ERR);
67 if (!ret)
68 ret = create_symref("HEAD", switch_to_branch,
69 reflog_head);
71 if (!ret && run_hook)
72 run_hook_le(NULL, "post-checkout",
73 oid_to_hex(head ? head : null_oid()),
74 oid_to_hex(oid), "1", NULL);
75 strbuf_release(&msg);
76 return ret;
79 int reset_head(struct repository *r, const struct reset_head_opts *opts)
81 const struct object_id *oid = opts->oid;
82 const char *switch_to_branch = opts->branch;
83 unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
84 unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
85 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
86 struct object_id *head = NULL, head_oid;
87 struct tree_desc desc[2] = { { NULL }, { NULL } };
88 struct lock_file lock = LOCK_INIT;
89 struct unpack_trees_options unpack_tree_opts = { 0 };
90 struct tree *tree;
91 const char *action;
92 int ret = 0, nr = 0;
94 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
95 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
97 if (opts->orig_head_msg && !update_orig_head)
98 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
100 if (opts->branch_msg && !opts->branch)
101 BUG("branch reflog message given without a branch");
103 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
104 ret = -1;
105 goto leave_reset_head;
108 if (!get_oid("HEAD", &head_oid)) {
109 head = &head_oid;
110 } else if (!oid || !reset_hard) {
111 ret = error(_("could not determine HEAD revision"));
112 goto leave_reset_head;
115 if (!oid)
116 oid = &head_oid;
118 if (refs_only)
119 return update_refs(opts, oid, head);
121 action = reset_hard ? "reset" : "checkout";
122 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
123 unpack_tree_opts.head_idx = 1;
124 unpack_tree_opts.src_index = r->index;
125 unpack_tree_opts.dst_index = r->index;
126 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
127 unpack_tree_opts.update = 1;
128 unpack_tree_opts.merge = 1;
129 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
130 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
131 if (reset_hard)
132 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
134 if (repo_read_index_unmerged(r) < 0) {
135 ret = error(_("could not read index"));
136 goto leave_reset_head;
139 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
140 ret = error(_("failed to find tree of %s"),
141 oid_to_hex(&head_oid));
142 goto leave_reset_head;
145 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
146 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
147 goto leave_reset_head;
150 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
151 ret = -1;
152 goto leave_reset_head;
155 tree = parse_tree_indirect(oid);
156 prime_cache_tree(r, r->index, tree);
158 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
159 ret = error(_("could not write index"));
160 goto leave_reset_head;
163 if (oid != &head_oid || update_orig_head || switch_to_branch)
164 ret = update_refs(opts, oid, head);
166 leave_reset_head:
167 rollback_lock_file(&lock);
168 clear_unpack_trees_porcelain(&unpack_tree_opts);
169 while (nr)
170 free((void *)desc[--nr].buffer);
171 return ret;