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