Raw
1 #include "git-compat-util.h"
2 #include "cache-tree.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "object-name.h"
7 #include "refs.h"
8 #include "reset.h"
9 #include "tree-walk.h"
10 #include "tree.h"
11 #include "unpack-trees.h"
12 #include "hook.h"
13
14 static int update_refs(struct repository *repo,
15 const struct reset_working_tree_options *opts,
16 const struct object_id *oid,
17 const struct object_id *head)
18 {
19 unsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;
20 unsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;
21 unsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;
22 const struct object_id *orig_head = opts->orig_head;
23 const char *switch_to_branch = opts->branch;
24 const char *reflog_branch = opts->branch_msg;
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;
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
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 }
41 prefix_len = msg.len;
42
43 if (update_orig_head) {
44 if (!repo_get_oid(repo, "ORIG_HEAD", &oid_old_orig))
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 }
51 refs_update_ref(get_main_ref_store(repo),
52 reflog_orig_head, "ORIG_HEAD",
53 orig_head ? orig_head : head,
54 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
55 } else if (old_orig)
56 refs_delete_ref(get_main_ref_store(repo),
57 NULL, "ORIG_HEAD", old_orig, 0);
58 }
59
60 if (!reflog_head) {
61 strbuf_setlen(&msg, prefix_len);
62 strbuf_addstr(&msg, "updating HEAD");
63 reflog_head = msg.buf;
64 }
65 if (!switch_to_branch)
66 ret = refs_update_ref(get_main_ref_store(repo),
67 reflog_head, "HEAD", oid, head,
68 detach_head ? REF_NO_DEREF : 0,
69 UPDATE_REFS_MSG_ON_ERR);
70 else {
71 ret = refs_update_ref(get_main_ref_store(repo),
72 reflog_branch ? reflog_branch : reflog_head,
73 switch_to_branch, oid, NULL, 0,
74 UPDATE_REFS_MSG_ON_ERR);
75 if (!ret)
76 ret = refs_update_symref(get_main_ref_store(repo),
77 "HEAD", switch_to_branch,
78 reflog_head);
79 }
80 if (!ret && run_hook)
81 run_hooks_l(repo, "post-checkout",
82 oid_to_hex(head ? head : null_oid(repo->hash_algo)),
83 oid_to_hex(oid), "1", NULL);
84 strbuf_release(&msg);
85 return ret;
86 }
87
88 int reset_working_tree(struct repository *r,
89 const struct reset_working_tree_options *opts)
90 {
91 const struct object_id *oid = opts->oid;
92 const char *switch_to_branch = opts->branch;
93 unsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;
94 unsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;
95 unsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;
96 unsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;
97 unsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;
98 struct object_id *head = NULL, head_oid;
99 struct tree_desc desc[2] = { { NULL }, { NULL } };
100 struct lock_file lock = LOCK_INIT;
101 struct unpack_trees_options unpack_tree_opts = { 0 };
102 struct tree *tree;
103 struct index_state scratch_index = INDEX_STATE_INIT(r);
104 struct index_state *istate;
105 const char *action;
106 int ret = 0, nr = 0;
107
108 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
109 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
110
111 if (opts->orig_head_msg && !update_orig_head)
112 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
113
114 if (opts->branch_msg && !opts->branch)
115 BUG("branch reflog message given without a branch");
116
117 if (update_orig_head && !update_head)
118 BUG("cannot update ORIG_HEAD without updating HEAD");
119
120 if (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
121 ret = -1;
122 goto leave_reset_head;
123 }
124
125 if (opts->oid_from) {
126 oidcpy(&head_oid, opts->oid_from);
127 head = &head_oid;
128 } else if (!repo_get_oid(r, "HEAD", &head_oid)) {
129 head = &head_oid;
130 } else if (!oid || !reset_hard) {
131 ret = error(_("could not determine HEAD revision"));
132 goto leave_reset_head;
133 }
134
135 if (!oid)
136 oid = &head_oid;
137
138 if (refs_only) {
139 if (!dry_run && update_head)
140 return update_refs(r, opts, oid, head);
141 return 0;
142 }
143
144 if (dry_run) {
145 if (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||
146 index_state_unmerged_to_stage0(&scratch_index) < 0) {
147 ret = error(_("could not read index"));
148 goto leave_reset_head;
149 }
150
151 istate = &scratch_index;
152 } else {
153 if (repo_read_index_unmerged(r) < 0) {
154 ret = error(_("could not read index"));
155 goto leave_reset_head;
156 }
157 istate = r->index;
158 }
159
160 action = reset_hard ? "reset" : "checkout";
161 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
162 unpack_tree_opts.head_idx = 1;
163 unpack_tree_opts.src_index = istate;
164 unpack_tree_opts.dst_index = istate;
165 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
166 unpack_tree_opts.update = !dry_run;
167 unpack_tree_opts.dry_run = dry_run;
168 unpack_tree_opts.merge = 1;
169 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
170 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
171 if (reset_hard) {
172 unpack_tree_opts.skip_cache_tree_update = 1;
173 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
174 }
175
176 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
177 ret = error(_("failed to find tree of %s"),
178 oid_to_hex(&head_oid));
179 goto leave_reset_head;
180 }
181
182 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
183 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
184 goto leave_reset_head;
185 }
186
187 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
188 ret = -1;
189 goto leave_reset_head;
190 }
191
192 if (dry_run)
193 goto leave_reset_head;
194
195 tree = repo_parse_tree_indirect(r, oid);
196 if (!tree) {
197 ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
198 goto leave_reset_head;
199 }
200
201 if (reset_hard)
202 prime_cache_tree(r, r->index, tree);
203
204 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
205 ret = error(_("could not write index"));
206 goto leave_reset_head;
207 }
208
209 if (update_head &&
210 (oid != &head_oid || update_orig_head || switch_to_branch))
211 ret = update_refs(r, opts, oid, head);
212
213 leave_reset_head:
214 rollback_lock_file(&lock);
215 clear_unpack_trees_porcelain(&unpack_tree_opts);
216 release_index(&scratch_index);
217 while (nr)
218 free((void *)desc[--nr].buffer);
219 return ret;
220
221 }