t/unit-tests: fix typos
[git/gitster.git] / reset.c
blobb22b1be792d03104d5f736c5e7e695fb946a61ae
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "cache-tree.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "lockfile.h"
8 #include "object-name.h"
9 #include "refs.h"
10 #include "reset.h"
11 #include "tree-walk.h"
12 #include "tree.h"
13 #include "unpack-trees.h"
14 #include "hook.h"
16 static int update_refs(const struct reset_head_opts *opts,
17 const struct object_id *oid,
18 const struct object_id *head)
20 unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
21 unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
22 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
23 const struct object_id *orig_head = opts->orig_head;
24 const char *switch_to_branch = opts->branch;
25 const char *reflog_branch = opts->branch_msg;
26 const char *reflog_head = opts->head_msg;
27 const char *reflog_orig_head = opts->orig_head_msg;
28 const char *default_reflog_action = opts->default_reflog_action;
29 struct object_id *old_orig = NULL, oid_old_orig;
30 struct strbuf msg = STRBUF_INIT;
31 const char *reflog_action;
32 size_t prefix_len;
33 int ret;
35 if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
36 if (!default_reflog_action)
37 BUG("default_reflog_action must be given when reflog messages are omitted");
38 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
39 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
40 default_reflog_action);
42 prefix_len = msg.len;
44 if (update_orig_head) {
45 if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
46 old_orig = &oid_old_orig;
47 if (head) {
48 if (!reflog_orig_head) {
49 strbuf_addstr(&msg, "updating ORIG_HEAD");
50 reflog_orig_head = msg.buf;
52 refs_update_ref(get_main_ref_store(the_repository),
53 reflog_orig_head, "ORIG_HEAD",
54 orig_head ? orig_head : head,
55 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
56 } else if (old_orig)
57 refs_delete_ref(get_main_ref_store(the_repository),
58 NULL, "ORIG_HEAD", old_orig, 0);
61 if (!reflog_head) {
62 strbuf_setlen(&msg, prefix_len);
63 strbuf_addstr(&msg, "updating HEAD");
64 reflog_head = msg.buf;
66 if (!switch_to_branch)
67 ret = refs_update_ref(get_main_ref_store(the_repository),
68 reflog_head, "HEAD", oid, head,
69 detach_head ? REF_NO_DEREF : 0,
70 UPDATE_REFS_MSG_ON_ERR);
71 else {
72 ret = refs_update_ref(get_main_ref_store(the_repository),
73 reflog_branch ? reflog_branch : reflog_head,
74 switch_to_branch, oid, NULL, 0,
75 UPDATE_REFS_MSG_ON_ERR);
76 if (!ret)
77 ret = refs_update_symref(get_main_ref_store(the_repository),
78 "HEAD", switch_to_branch,
79 reflog_head);
81 if (!ret && run_hook)
82 run_hooks_l(the_repository, "post-checkout",
83 oid_to_hex(head ? head : null_oid()),
84 oid_to_hex(oid), "1", NULL);
85 strbuf_release(&msg);
86 return ret;
89 int reset_head(struct repository *r, const struct reset_head_opts *opts)
91 const struct object_id *oid = opts->oid;
92 const char *switch_to_branch = opts->branch;
93 unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
94 unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
95 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
96 struct object_id *head = NULL, head_oid;
97 struct tree_desc desc[2] = { { NULL }, { NULL } };
98 struct lock_file lock = LOCK_INIT;
99 struct unpack_trees_options unpack_tree_opts = { 0 };
100 struct tree *tree;
101 const char *action;
102 int ret = 0, nr = 0;
104 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
105 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
107 if (opts->orig_head_msg && !update_orig_head)
108 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
110 if (opts->branch_msg && !opts->branch)
111 BUG("branch reflog message given without a branch");
113 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
114 ret = -1;
115 goto leave_reset_head;
118 if (!repo_get_oid(r, "HEAD", &head_oid)) {
119 head = &head_oid;
120 } else if (!oid || !reset_hard) {
121 ret = error(_("could not determine HEAD revision"));
122 goto leave_reset_head;
125 if (!oid)
126 oid = &head_oid;
128 if (refs_only)
129 return update_refs(opts, oid, head);
131 action = reset_hard ? "reset" : "checkout";
132 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
133 unpack_tree_opts.head_idx = 1;
134 unpack_tree_opts.src_index = r->index;
135 unpack_tree_opts.dst_index = r->index;
136 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
137 unpack_tree_opts.update = 1;
138 unpack_tree_opts.merge = 1;
139 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
140 unpack_tree_opts.skip_cache_tree_update = 1;
141 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
142 if (reset_hard)
143 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
145 if (repo_read_index_unmerged(r) < 0) {
146 ret = error(_("could not read index"));
147 goto leave_reset_head;
150 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
151 ret = error(_("failed to find tree of %s"),
152 oid_to_hex(&head_oid));
153 goto leave_reset_head;
156 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
157 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
158 goto leave_reset_head;
161 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
162 ret = -1;
163 goto leave_reset_head;
166 tree = parse_tree_indirect(oid);
167 if (!tree) {
168 ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
169 goto leave_reset_head;
172 prime_cache_tree(r, r->index, tree);
174 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
175 ret = error(_("could not write index"));
176 goto leave_reset_head;
179 if (oid != &head_oid || update_orig_head || switch_to_branch)
180 ret = update_refs(opts, oid, head);
182 leave_reset_head:
183 rollback_lock_file(&lock);
184 clear_unpack_trees_porcelain(&unpack_tree_opts);
185 while (nr)
186 free((void *)desc[--nr].buffer);
187 return ret;