]> git.ipfire.org Git - thirdparty/git.git/blame - reset.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / reset.c
CommitLineData
b309a971
DL
1#include "git-compat-util.h"
2#include "cache-tree.h"
f394e093 3#include "gettext.h"
41771fa4 4#include "hex.h"
b309a971 5#include "lockfile.h"
dabab1d6 6#include "object-name.h"
b309a971
DL
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"
72ddf34d 13#include "hook.h"
b309a971 14
6ae80861
PW
15static int update_refs(const struct reset_head_opts *opts,
16 const struct object_id *oid,
17 const struct object_id *head)
b309a971 18{
6ae80861
PW
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;
cd1528ef 22 const struct object_id *orig_head = opts->orig_head;
6ae80861 23 const char *switch_to_branch = opts->branch;
7700ab08 24 const char *reflog_branch = opts->branch_msg;
6ae80861
PW
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;
d6a9f5ea
PW
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;
33
1526d0fc
PW
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);
40 }
d6a9f5ea
PW
41 prefix_len = msg.len;
42
43 if (update_orig_head) {
d850b7a5 44 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
d6a9f5ea
PW
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;
50 }
cd1528ef
PW
51 update_ref(reflog_orig_head, "ORIG_HEAD",
52 orig_head ? orig_head : head,
d6a9f5ea
PW
53 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
54 } else if (old_orig)
55 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
56 }
57
58 if (!reflog_head) {
59 strbuf_setlen(&msg, prefix_len);
60 strbuf_addstr(&msg, "updating HEAD");
61 reflog_head = msg.buf;
62 }
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 {
7700ab08
PW
68 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
69 switch_to_branch, oid, NULL, 0,
70 UPDATE_REFS_MSG_ON_ERR);
d6a9f5ea
PW
71 if (!ret)
72 ret = create_symref("HEAD", switch_to_branch,
73 reflog_head);
74 }
75 if (!ret && run_hook)
bcd020f8 76 run_hooks_l("post-checkout",
d6a9f5ea
PW
77 oid_to_hex(head ? head : null_oid()),
78 oid_to_hex(oid), "1", NULL);
79 strbuf_release(&msg);
80 return ret;
81}
82
6ae80861 83int reset_head(struct repository *r, const struct reset_head_opts *opts)
b309a971 84{
6ae80861
PW
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;
69f4c230 90 struct object_id *head = NULL, head_oid;
b309a971
DL
91 struct tree_desc desc[2] = { { NULL }, { NULL } };
92 struct lock_file lock = LOCK_INIT;
9a863b33 93 struct unpack_trees_options unpack_tree_opts = { 0 };
b309a971 94 struct tree *tree;
d6a9f5ea 95 const char *action;
b309a971
DL
96 int ret = 0, nr = 0;
97
98 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
99 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
100
7700ab08
PW
101 if (opts->orig_head_msg && !update_orig_head)
102 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
103
104 if (opts->branch_msg && !opts->branch)
105 BUG("branch reflog message given without a branch");
106
b309a971
DL
107 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
108 ret = -1;
109 goto leave_reset_head;
110 }
111
4a93b899 112 if (!repo_get_oid(r, "HEAD", &head_oid)) {
69f4c230
PW
113 head = &head_oid;
114 } else if (!oid || !reset_hard) {
b309a971
DL
115 ret = error(_("could not determine HEAD revision"));
116 goto leave_reset_head;
117 }
118
119 if (!oid)
120 oid = &head_oid;
121
122 if (refs_only)
6ae80861 123 return update_refs(opts, oid, head);
b309a971 124
1946d458 125 action = reset_hard ? "reset" : "checkout";
b309a971
DL
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;
1b5f3733 133 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
652bd021 134 unpack_tree_opts.skip_cache_tree_update = 1;
bf102008 135 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
ab2fba08 136 if (reset_hard)
480d3d6b 137 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
b309a971
DL
138
139 if (repo_read_index_unmerged(r) < 0) {
140 ret = error(_("could not read index"));
141 goto leave_reset_head;
142 }
143
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;
148 }
149
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;
153 }
154
155 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
156 ret = -1;
157 goto leave_reset_head;
158 }
159
160 tree = parse_tree_indirect(oid);
161 prime_cache_tree(r, r->index, tree);
162
163 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
164 ret = error(_("could not write index"));
165 goto leave_reset_head;
166 }
167
1526d0fc 168 if (oid != &head_oid || update_orig_head || switch_to_branch)
6ae80861 169 ret = update_refs(opts, oid, head);
b309a971
DL
170
171leave_reset_head:
b309a971 172 rollback_lock_file(&lock);
9a863b33 173 clear_unpack_trees_porcelain(&unpack_tree_opts);
b309a971
DL
174 while (nr)
175 free((void *)desc[--nr].buffer);
176 return ret;
177
178}