fsmonitor--daemon: don't translate literal commands
[git/debian.git] / reset.c
blobe3383a93343e3df16ee9eda6ee7886a49e980a1a
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"
10 #include "hook.h"
12 static int update_refs(const struct reset_head_opts *opts,
13 const struct object_id *oid,
14 const struct object_id *head)
16 unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
17 unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
18 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
19 const struct object_id *orig_head = opts->orig_head;
20 const char *switch_to_branch = opts->branch;
21 const char *reflog_branch = opts->branch_msg;
22 const char *reflog_head = opts->head_msg;
23 const char *reflog_orig_head = opts->orig_head_msg;
24 const char *default_reflog_action = opts->default_reflog_action;
25 struct object_id *old_orig = NULL, oid_old_orig;
26 struct strbuf msg = STRBUF_INIT;
27 const char *reflog_action;
28 size_t prefix_len;
29 int ret;
31 if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
32 if (!default_reflog_action)
33 BUG("default_reflog_action must be given when reflog messages are omitted");
34 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
35 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
36 default_reflog_action);
38 prefix_len = msg.len;
40 if (update_orig_head) {
41 if (!get_oid("ORIG_HEAD", &oid_old_orig))
42 old_orig = &oid_old_orig;
43 if (head) {
44 if (!reflog_orig_head) {
45 strbuf_addstr(&msg, "updating ORIG_HEAD");
46 reflog_orig_head = msg.buf;
48 update_ref(reflog_orig_head, "ORIG_HEAD",
49 orig_head ? orig_head : head,
50 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
51 } else if (old_orig)
52 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
55 if (!reflog_head) {
56 strbuf_setlen(&msg, prefix_len);
57 strbuf_addstr(&msg, "updating HEAD");
58 reflog_head = msg.buf;
60 if (!switch_to_branch)
61 ret = update_ref(reflog_head, "HEAD", oid, head,
62 detach_head ? REF_NO_DEREF : 0,
63 UPDATE_REFS_MSG_ON_ERR);
64 else {
65 ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
66 switch_to_branch, oid, NULL, 0,
67 UPDATE_REFS_MSG_ON_ERR);
68 if (!ret)
69 ret = create_symref("HEAD", switch_to_branch,
70 reflog_head);
72 if (!ret && run_hook)
73 run_hooks_l("post-checkout",
74 oid_to_hex(head ? head : null_oid()),
75 oid_to_hex(oid), "1", NULL);
76 strbuf_release(&msg);
77 return ret;
80 int reset_head(struct repository *r, const struct reset_head_opts *opts)
82 const struct object_id *oid = opts->oid;
83 const char *switch_to_branch = opts->branch;
84 unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
85 unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
86 unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
87 struct object_id *head = NULL, head_oid;
88 struct tree_desc desc[2] = { { NULL }, { NULL } };
89 struct lock_file lock = LOCK_INIT;
90 struct unpack_trees_options unpack_tree_opts = { 0 };
91 struct tree *tree;
92 const char *action;
93 int ret = 0, nr = 0;
95 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
96 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
98 if (opts->orig_head_msg && !update_orig_head)
99 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
101 if (opts->branch_msg && !opts->branch)
102 BUG("branch reflog message given without a branch");
104 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
105 ret = -1;
106 goto leave_reset_head;
109 if (!get_oid("HEAD", &head_oid)) {
110 head = &head_oid;
111 } else if (!oid || !reset_hard) {
112 ret = error(_("could not determine HEAD revision"));
113 goto leave_reset_head;
116 if (!oid)
117 oid = &head_oid;
119 if (refs_only)
120 return update_refs(opts, oid, head);
122 action = reset_hard ? "reset" : "checkout";
123 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
124 unpack_tree_opts.head_idx = 1;
125 unpack_tree_opts.src_index = r->index;
126 unpack_tree_opts.dst_index = r->index;
127 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
128 unpack_tree_opts.update = 1;
129 unpack_tree_opts.merge = 1;
130 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
131 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
132 if (reset_hard)
133 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
135 if (repo_read_index_unmerged(r) < 0) {
136 ret = error(_("could not read index"));
137 goto leave_reset_head;
140 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
141 ret = error(_("failed to find tree of %s"),
142 oid_to_hex(&head_oid));
143 goto leave_reset_head;
146 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
147 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
148 goto leave_reset_head;
151 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
152 ret = -1;
153 goto leave_reset_head;
156 tree = parse_tree_indirect(oid);
157 prime_cache_tree(r, r->index, tree);
159 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
160 ret = error(_("could not write index"));
161 goto leave_reset_head;
164 if (oid != &head_oid || update_orig_head || switch_to_branch)
165 ret = update_refs(opts, oid, head);
167 leave_reset_head:
168 rollback_lock_file(&lock);
169 clear_unpack_trees_porcelain(&unpack_tree_opts);
170 while (nr)
171 free((void *)desc[--nr].buffer);
172 return ret;