]>
Commit | Line | Data |
---|---|---|
bc052d7f | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
16da134b | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
f8a9d428 | 4 | #include "dir.h" |
16da134b JS |
5 | #include "tree.h" |
6 | #include "tree-walk.h" | |
076b0adc | 7 | #include "cache-tree.h" |
16da134b | 8 | #include "unpack-trees.h" |
96a02f8f | 9 | #include "progress.h" |
0cf73755 | 10 | #include "refs.h" |
06f33c17 | 11 | #include "attr.h" |
5fc2fc8f | 12 | #include "split-index.h" |
e931371a | 13 | #include "dir.h" |
a7bc845a SB |
14 | #include "submodule.h" |
15 | #include "submodule-config.h" | |
16da134b | 16 | |
8ccba008 JH |
17 | /* |
18 | * Error messages expected by scripts out of plumbing commands such as | |
19 | * read-tree. Non-scripted Porcelain is not required to use these messages | |
20 | * and in fact are encouraged to reword them to better suit their particular | |
23cbf11b | 21 | * situation better. See how "git checkout" and "git merge" replaces |
dc1166e6 | 22 | * them using setup_unpack_trees_porcelain(), for example. |
8ccba008 | 23 | */ |
c2e86add | 24 | static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { |
08353ebb | 25 | /* ERROR_WOULD_OVERWRITE */ |
8ccba008 JH |
26 | "Entry '%s' would be overwritten by merge. Cannot merge.", |
27 | ||
08353ebb | 28 | /* ERROR_NOT_UPTODATE_FILE */ |
8ccba008 JH |
29 | "Entry '%s' not uptodate. Cannot merge.", |
30 | ||
08353ebb | 31 | /* ERROR_NOT_UPTODATE_DIR */ |
8ccba008 JH |
32 | "Updating '%s' would lose untracked files in it", |
33 | ||
08402b04 MM |
34 | /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ |
35 | "Untracked working tree file '%s' would be overwritten by merge.", | |
8ccba008 | 36 | |
08402b04 MM |
37 | /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ |
38 | "Untracked working tree file '%s' would be removed by merge.", | |
8ccba008 | 39 | |
08353ebb | 40 | /* ERROR_BIND_OVERLAP */ |
8ccba008 | 41 | "Entry '%s' overlaps with '%s'. Cannot bind.", |
e800ec9d | 42 | |
08353ebb | 43 | /* ERROR_SPARSE_NOT_UPTODATE_FILE */ |
e800ec9d NTND |
44 | "Entry '%s' not uptodate. Cannot update sparse checkout.", |
45 | ||
08402b04 MM |
46 | /* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ |
47 | "Working tree file '%s' would be overwritten by sparse checkout update.", | |
48 | ||
49 | /* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ | |
50 | "Working tree file '%s' would be removed by sparse checkout update.", | |
a7bc845a SB |
51 | |
52 | /* ERROR_WOULD_LOSE_SUBMODULE */ | |
53 | "Submodule '%s' cannot checkout new HEAD.", | |
8ccba008 JH |
54 | }; |
55 | ||
08353ebb MM |
56 | #define ERRORMSG(o,type) \ |
57 | ( ((o) && (o)->msgs[(type)]) \ | |
58 | ? ((o)->msgs[(type)]) \ | |
59 | : (unpack_plumbing_errors[(type)]) ) | |
8ccba008 | 60 | |
3d415425 SB |
61 | static const char *super_prefixed(const char *path) |
62 | { | |
63 | /* | |
64 | * It is necessary and sufficient to have two static buffers | |
65 | * here, as the return value of this function is fed to | |
66 | * error() using the unpack_*_errors[] templates we see above. | |
67 | */ | |
68 | static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; | |
69 | static int super_prefix_len = -1; | |
70 | static unsigned idx = ARRAY_SIZE(buf) - 1; | |
71 | ||
72 | if (super_prefix_len < 0) { | |
73 | const char *super_prefix = get_super_prefix(); | |
74 | if (!super_prefix) { | |
75 | super_prefix_len = 0; | |
76 | } else { | |
77 | int i; | |
78 | for (i = 0; i < ARRAY_SIZE(buf); i++) | |
79 | strbuf_addstr(&buf[i], super_prefix); | |
80 | super_prefix_len = buf[0].len; | |
81 | } | |
82 | } | |
83 | ||
84 | if (!super_prefix_len) | |
85 | return path; | |
86 | ||
87 | if (++idx >= ARRAY_SIZE(buf)) | |
88 | idx = 0; | |
89 | ||
90 | strbuf_setlen(&buf[idx], super_prefix_len); | |
91 | strbuf_addstr(&buf[idx], path); | |
92 | ||
93 | return buf[idx].buf; | |
94 | } | |
95 | ||
e294030f MM |
96 | void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, |
97 | const char *cmd) | |
dc1166e6 | 98 | { |
7980872d | 99 | int i; |
e294030f | 100 | const char **msgs = opts->msgs; |
dc1166e6 | 101 | const char *msg; |
fa3f60b7 | 102 | |
2e3926b9 VA |
103 | if (!strcmp(cmd, "checkout")) |
104 | msg = advice_commit_before_merge | |
105 | ? _("Your local changes to the following files would be overwritten by checkout:\n%%s" | |
c2691e2a | 106 | "Please commit your changes or stash them before you switch branches.") |
2e3926b9 VA |
107 | : _("Your local changes to the following files would be overwritten by checkout:\n%%s"); |
108 | else if (!strcmp(cmd, "merge")) | |
109 | msg = advice_commit_before_merge | |
110 | ? _("Your local changes to the following files would be overwritten by merge:\n%%s" | |
c2691e2a | 111 | "Please commit your changes or stash them before you merge.") |
2e3926b9 | 112 | : _("Your local changes to the following files would be overwritten by merge:\n%%s"); |
dc1166e6 | 113 | else |
2e3926b9 VA |
114 | msg = advice_commit_before_merge |
115 | ? _("Your local changes to the following files would be overwritten by %s:\n%%s" | |
c2691e2a | 116 | "Please commit your changes or stash them before you %s.") |
2e3926b9 | 117 | : _("Your local changes to the following files would be overwritten by %s:\n%%s"); |
fa3f60b7 | 118 | msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = |
2e3926b9 | 119 | xstrfmt(msg, cmd, cmd); |
dc1166e6 MM |
120 | |
121 | msgs[ERROR_NOT_UPTODATE_DIR] = | |
584f99c8 | 122 | _("Updating the following directories would lose untracked files in them:\n%s"); |
dc1166e6 | 123 | |
2e3926b9 VA |
124 | if (!strcmp(cmd, "checkout")) |
125 | msg = advice_commit_before_merge | |
126 | ? _("The following untracked working tree files would be removed by checkout:\n%%s" | |
c2691e2a | 127 | "Please move or remove them before you switch branches.") |
2e3926b9 VA |
128 | : _("The following untracked working tree files would be removed by checkout:\n%%s"); |
129 | else if (!strcmp(cmd, "merge")) | |
130 | msg = advice_commit_before_merge | |
131 | ? _("The following untracked working tree files would be removed by merge:\n%%s" | |
c2691e2a | 132 | "Please move or remove them before you merge.") |
2e3926b9 | 133 | : _("The following untracked working tree files would be removed by merge:\n%%s"); |
dc1166e6 | 134 | else |
2e3926b9 VA |
135 | msg = advice_commit_before_merge |
136 | ? _("The following untracked working tree files would be removed by %s:\n%%s" | |
c2691e2a | 137 | "Please move or remove them before you %s.") |
2e3926b9 VA |
138 | : _("The following untracked working tree files would be removed by %s:\n%%s"); |
139 | msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = xstrfmt(msg, cmd, cmd); | |
140 | ||
141 | if (!strcmp(cmd, "checkout")) | |
142 | msg = advice_commit_before_merge | |
143 | ? _("The following untracked working tree files would be overwritten by checkout:\n%%s" | |
c2691e2a | 144 | "Please move or remove them before you switch branches.") |
2e3926b9 VA |
145 | : _("The following untracked working tree files would be overwritten by checkout:\n%%s"); |
146 | else if (!strcmp(cmd, "merge")) | |
147 | msg = advice_commit_before_merge | |
148 | ? _("The following untracked working tree files would be overwritten by merge:\n%%s" | |
c2691e2a | 149 | "Please move or remove them before you merge.") |
2e3926b9 VA |
150 | : _("The following untracked working tree files would be overwritten by merge:\n%%s"); |
151 | else | |
152 | msg = advice_commit_before_merge | |
153 | ? _("The following untracked working tree files would be overwritten by %s:\n%%s" | |
c2691e2a | 154 | "Please move or remove them before you %s.") |
2e3926b9 VA |
155 | : _("The following untracked working tree files would be overwritten by %s:\n%%s"); |
156 | msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = xstrfmt(msg, cmd, cmd); | |
dc1166e6 MM |
157 | |
158 | /* | |
159 | * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we | |
160 | * cannot easily display it as a list. | |
161 | */ | |
ed47fdf7 | 162 | msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind."); |
dc1166e6 MM |
163 | |
164 | msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = | |
ed47fdf7 | 165 | _("Cannot update sparse checkout: the following entries are not up-to-date:\n%s"); |
dc1166e6 | 166 | msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = |
a1c80446 | 167 | _("The following working tree files would be overwritten by sparse checkout update:\n%s"); |
dc1166e6 | 168 | msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = |
a1c80446 | 169 | _("The following working tree files would be removed by sparse checkout update:\n%s"); |
a7bc845a | 170 | msgs[ERROR_WOULD_LOSE_SUBMODULE] = |
6362ed0b | 171 | _("Cannot update submodule:\n%s"); |
5e65ee35 MM |
172 | |
173 | opts->show_all_errors = 1; | |
7980872d CB |
174 | /* rejected paths may not have a static buffer */ |
175 | for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++) | |
176 | opts->unpack_rejects[i].strdup_strings = 1; | |
dc1166e6 MM |
177 | } |
178 | ||
46169180 | 179 | static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce, |
6ff264ee | 180 | unsigned int set, unsigned int clear) |
b48d5a05 | 181 | { |
419a597f | 182 | clear |= CE_HASHED; |
34110cd4 | 183 | |
700e66d6 NTND |
184 | if (set & CE_REMOVE) |
185 | set |= CE_WT_REMOVE; | |
186 | ||
6ff264ee | 187 | ce->ce_flags = (ce->ce_flags & ~clear) | set; |
46169180 JK |
188 | return add_index_entry(&o->result, ce, |
189 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
6ff264ee RS |
190 | } |
191 | ||
a33bd4d3 | 192 | static struct cache_entry *dup_entry(const struct cache_entry *ce) |
6ff264ee RS |
193 | { |
194 | unsigned int size = ce_size(ce); | |
195 | struct cache_entry *new = xmalloc(size); | |
196 | ||
34110cd4 | 197 | memcpy(new, ce, size); |
a33bd4d3 RS |
198 | return new; |
199 | } | |
200 | ||
201 | static void add_entry(struct unpack_trees_options *o, | |
202 | const struct cache_entry *ce, | |
203 | unsigned int set, unsigned int clear) | |
204 | { | |
205 | do_add_entry(o, dup_entry(ce), set, clear); | |
b48d5a05 LT |
206 | } |
207 | ||
e6c111b4 MM |
208 | /* |
209 | * add error messages on path <path> | |
210 | * corresponding to the type <e> with the message <msg> | |
211 | * indicating if it should be display in porcelain or not | |
212 | */ | |
213 | static int add_rejected_path(struct unpack_trees_options *o, | |
214 | enum unpack_trees_error_types e, | |
215 | const char *path) | |
216 | { | |
e6c111b4 | 217 | if (!o->show_all_errors) |
3d415425 | 218 | return error(ERRORMSG(o, e), super_prefixed(path)); |
e6c111b4 MM |
219 | |
220 | /* | |
221 | * Otherwise, insert in a list for future display by | |
222 | * display_error_msgs() | |
223 | */ | |
7980872d | 224 | string_list_append(&o->unpack_rejects[e], path); |
e6c111b4 MM |
225 | return -1; |
226 | } | |
227 | ||
e6c111b4 MM |
228 | /* |
229 | * display all the error messages stored in a nice way | |
230 | */ | |
231 | static void display_error_msgs(struct unpack_trees_options *o) | |
232 | { | |
7980872d | 233 | int e, i; |
e6c111b4 MM |
234 | int something_displayed = 0; |
235 | for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { | |
7980872d CB |
236 | struct string_list *rejects = &o->unpack_rejects[e]; |
237 | if (rejects->nr > 0) { | |
e6c111b4 MM |
238 | struct strbuf path = STRBUF_INIT; |
239 | something_displayed = 1; | |
7980872d CB |
240 | for (i = 0; i < rejects->nr; i++) |
241 | strbuf_addf(&path, "\t%s\n", rejects->items[i].string); | |
3d415425 | 242 | error(ERRORMSG(o, e), super_prefixed(path.buf)); |
e6c111b4 | 243 | strbuf_release(&path); |
e6c111b4 | 244 | } |
7980872d | 245 | string_list_clear(rejects, 0); |
e6c111b4 MM |
246 | } |
247 | if (something_displayed) | |
ed47fdf7 | 248 | fprintf(stderr, _("Aborting\n")); |
e6c111b4 MM |
249 | } |
250 | ||
a7bc845a SB |
251 | static int check_submodule_move_head(const struct cache_entry *ce, |
252 | const char *old_id, | |
253 | const char *new_id, | |
254 | struct unpack_trees_options *o) | |
255 | { | |
cd279e2e | 256 | unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; |
a7bc845a SB |
257 | const struct submodule *sub = submodule_from_ce(ce); |
258 | if (!sub) | |
259 | return 0; | |
260 | ||
cd279e2e SB |
261 | if (o->reset) |
262 | flags |= SUBMODULE_MOVE_HEAD_FORCE; | |
263 | ||
a7bc845a SB |
264 | switch (sub->update_strategy.type) { |
265 | case SM_UPDATE_UNSPECIFIED: | |
266 | case SM_UPDATE_CHECKOUT: | |
cd279e2e | 267 | if (submodule_move_head(ce->name, old_id, new_id, flags)) |
a7bc845a SB |
268 | return o->gently ? -1 : |
269 | add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); | |
270 | return 0; | |
271 | case SM_UPDATE_NONE: | |
272 | return 0; | |
273 | case SM_UPDATE_REBASE: | |
274 | case SM_UPDATE_MERGE: | |
275 | case SM_UPDATE_COMMAND: | |
276 | default: | |
277 | warning(_("submodule update strategy not supported for submodule '%s'"), ce->name); | |
278 | return -1; | |
279 | } | |
280 | } | |
281 | ||
282 | static void reload_gitmodules_file(struct index_state *index, | |
283 | struct checkout *state) | |
284 | { | |
285 | int i; | |
286 | for (i = 0; i < index->cache_nr; i++) { | |
287 | struct cache_entry *ce = index->cache[i]; | |
288 | if (ce->ce_flags & CE_UPDATE) { | |
289 | int r = strcmp(ce->name, ".gitmodules"); | |
290 | if (r < 0) | |
291 | continue; | |
292 | else if (r == 0) { | |
293 | submodule_free(); | |
294 | checkout_entry(ce, state, NULL); | |
295 | gitmodules_config(); | |
296 | git_config(submodule_config, NULL); | |
297 | } else | |
298 | break; | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
78478927 KB |
303 | /* |
304 | * Unlink the last component and schedule the leading directories for | |
305 | * removal, such that empty directories get removed. | |
16da134b | 306 | */ |
9c5e6c80 | 307 | static void unlink_entry(const struct cache_entry *ce) |
16da134b | 308 | { |
a7bc845a SB |
309 | const struct submodule *sub = submodule_from_ce(ce); |
310 | if (sub) { | |
311 | switch (sub->update_strategy.type) { | |
312 | case SM_UPDATE_UNSPECIFIED: | |
313 | case SM_UPDATE_CHECKOUT: | |
314 | case SM_UPDATE_REBASE: | |
315 | case SM_UPDATE_MERGE: | |
cd279e2e | 316 | /* state.force is set at the caller. */ |
a7bc845a SB |
317 | submodule_move_head(ce->name, "HEAD", NULL, |
318 | SUBMODULE_MOVE_HEAD_FORCE); | |
319 | break; | |
320 | case SM_UPDATE_NONE: | |
321 | case SM_UPDATE_COMMAND: | |
322 | return; /* Do not touch the submodule. */ | |
323 | } | |
324 | } | |
f66caaf9 | 325 | if (!check_leading_path(ce->name, ce_namelen(ce))) |
16a4c617 | 326 | return; |
80d706af PC |
327 | if (remove_or_warn(ce->ce_mode, ce->name)) |
328 | return; | |
78478927 | 329 | schedule_dir_for_removal(ce->name, ce_namelen(ce)); |
16da134b JS |
330 | } |
331 | ||
384f1a16 | 332 | static struct progress *get_progress(struct unpack_trees_options *o) |
16da134b | 333 | { |
96a02f8f | 334 | unsigned cnt = 0, total = 0; |
384f1a16 SB |
335 | struct index_state *index = &o->result; |
336 | ||
337 | if (!o->update || !o->verbose_update) | |
338 | return NULL; | |
339 | ||
340 | for (; cnt < index->cache_nr; cnt++) { | |
341 | const struct cache_entry *ce = index->cache[cnt]; | |
342 | if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) | |
343 | total++; | |
344 | } | |
345 | ||
346 | return start_progress_delay(_("Checking out files"), | |
347 | total, 50, 1); | |
348 | } | |
349 | ||
350 | static int check_updates(struct unpack_trees_options *o) | |
351 | { | |
352 | unsigned cnt = 0; | |
30ac275b | 353 | int errs = 0; |
dc6a0757 | 354 | struct progress *progress = NULL; |
34110cd4 | 355 | struct index_state *index = &o->result; |
30ac275b | 356 | struct checkout state = CHECKOUT_INIT; |
33ecf7eb | 357 | int i; |
16da134b | 358 | |
30ac275b SB |
359 | state.force = 1; |
360 | state.quiet = 1; | |
361 | state.refresh_cache = 1; | |
362 | state.istate = index; | |
16da134b | 363 | |
384f1a16 | 364 | progress = get_progress(o); |
16da134b | 365 | |
66985e66 | 366 | if (o->update) |
30ac275b | 367 | git_attr_set_direction(GIT_ATTR_CHECKOUT, index); |
34110cd4 | 368 | for (i = 0; i < index->cache_nr; i++) { |
9c5e6c80 | 369 | const struct cache_entry *ce = index->cache[i]; |
16da134b | 370 | |
e663db2f NTND |
371 | if (ce->ce_flags & CE_WT_REMOVE) { |
372 | display_progress(progress, ++cnt); | |
2c9078d0 | 373 | if (o->update && !o->dry_run) |
e663db2f | 374 | unlink_entry(ce); |
e663db2f | 375 | } |
1fa6ead4 | 376 | } |
30ac275b | 377 | remove_marked_cache_entries(index); |
78478927 | 378 | remove_scheduled_dirs(); |
1fa6ead4 | 379 | |
a7bc845a SB |
380 | if (should_update_submodules() && o->update && !o->dry_run) |
381 | reload_gitmodules_file(index, &state); | |
382 | ||
2841e8f8 | 383 | enable_delayed_checkout(&state); |
1fa6ead4 LT |
384 | for (i = 0; i < index->cache_nr; i++) { |
385 | struct cache_entry *ce = index->cache[i]; | |
386 | ||
7a51ed66 | 387 | if (ce->ce_flags & CE_UPDATE) { |
7d782416 DT |
388 | if (ce->ce_flags & CE_WT_REMOVE) |
389 | die("BUG: both update and delete flags are set on %s", | |
390 | ce->name); | |
1fa6ead4 | 391 | display_progress(progress, ++cnt); |
7a51ed66 | 392 | ce->ce_flags &= ~CE_UPDATE; |
2c9078d0 | 393 | if (o->update && !o->dry_run) { |
30ac275b | 394 | errs |= checkout_entry(ce, &state, NULL); |
16a4c617 | 395 | } |
16da134b JS |
396 | } |
397 | } | |
2841e8f8 | 398 | errs |= finish_delayed_checkout(&state); |
4d4fcc54 | 399 | stop_progress(&progress); |
66985e66 JH |
400 | if (o->update) |
401 | git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); | |
c4758d3c | 402 | return errs != 0; |
16da134b JS |
403 | } |
404 | ||
eb9ae4b5 RS |
405 | static int verify_uptodate_sparse(const struct cache_entry *ce, |
406 | struct unpack_trees_options *o); | |
407 | static int verify_absent_sparse(const struct cache_entry *ce, | |
408 | enum unpack_trees_error_types, | |
409 | struct unpack_trees_options *o); | |
e800ec9d | 410 | |
a5c446f1 NTND |
411 | static int apply_sparse_checkout(struct index_state *istate, |
412 | struct cache_entry *ce, | |
413 | struct unpack_trees_options *o) | |
e800ec9d NTND |
414 | { |
415 | int was_skip_worktree = ce_skip_worktree(ce); | |
416 | ||
2431afbf | 417 | if (ce->ce_flags & CE_NEW_SKIP_WORKTREE) |
e800ec9d NTND |
418 | ce->ce_flags |= CE_SKIP_WORKTREE; |
419 | else | |
420 | ce->ce_flags &= ~CE_SKIP_WORKTREE; | |
078a58e8 NTND |
421 | if (was_skip_worktree != ce_skip_worktree(ce)) { |
422 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
a5c446f1 | 423 | istate->cache_changed |= CE_ENTRY_CHANGED; |
078a58e8 | 424 | } |
e800ec9d NTND |
425 | |
426 | /* | |
eec3fc03 NTND |
427 | * if (!was_skip_worktree && !ce_skip_worktree()) { |
428 | * This is perfectly normal. Move on; | |
429 | * } | |
e800ec9d | 430 | */ |
eec3fc03 NTND |
431 | |
432 | /* | |
433 | * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout | |
434 | * area as a result of ce_skip_worktree() shortcuts in | |
700e66d6 NTND |
435 | * verify_absent() and verify_uptodate(). |
436 | * Make sure they don't modify worktree if they are already | |
437 | * outside checkout area | |
eec3fc03 | 438 | */ |
700e66d6 NTND |
439 | if (was_skip_worktree && ce_skip_worktree(ce)) { |
440 | ce->ce_flags &= ~CE_UPDATE; | |
441 | ||
442 | /* | |
443 | * By default, when CE_REMOVE is on, CE_WT_REMOVE is also | |
444 | * on to get that file removed from both index and worktree. | |
445 | * If that file is already outside worktree area, don't | |
446 | * bother remove it. | |
447 | */ | |
448 | if (ce->ce_flags & CE_REMOVE) | |
449 | ce->ce_flags &= ~CE_WT_REMOVE; | |
450 | } | |
e800ec9d NTND |
451 | |
452 | if (!was_skip_worktree && ce_skip_worktree(ce)) { | |
453 | /* | |
454 | * If CE_UPDATE is set, verify_uptodate() must be called already | |
455 | * also stat info may have lost after merged_entry() so calling | |
456 | * verify_uptodate() again may fail | |
457 | */ | |
458 | if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o)) | |
459 | return -1; | |
460 | ce->ce_flags |= CE_WT_REMOVE; | |
7d782416 | 461 | ce->ce_flags &= ~CE_UPDATE; |
e800ec9d NTND |
462 | } |
463 | if (was_skip_worktree && !ce_skip_worktree(ce)) { | |
08402b04 | 464 | if (verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) |
e800ec9d NTND |
465 | return -1; |
466 | ce->ce_flags |= CE_UPDATE; | |
467 | } | |
468 | return 0; | |
469 | } | |
470 | ||
5828e835 RS |
471 | static inline int call_unpack_fn(const struct cache_entry * const *src, |
472 | struct unpack_trees_options *o) | |
01904572 | 473 | { |
34110cd4 LT |
474 | int ret = o->fn(src, o); |
475 | if (ret > 0) | |
01904572 | 476 | ret = 0; |
01904572 LT |
477 | return ret; |
478 | } | |
479 | ||
da165f47 JH |
480 | static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) |
481 | { | |
482 | ce->ce_flags |= CE_UNPACKED; | |
483 | ||
484 | if (o->cache_bottom < o->src_index->cache_nr && | |
485 | o->src_index->cache[o->cache_bottom] == ce) { | |
486 | int bottom = o->cache_bottom; | |
487 | while (bottom < o->src_index->cache_nr && | |
488 | o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) | |
489 | bottom++; | |
490 | o->cache_bottom = bottom; | |
491 | } | |
492 | } | |
493 | ||
494 | static void mark_all_ce_unused(struct index_state *index) | |
495 | { | |
496 | int i; | |
497 | for (i = 0; i < index->cache_nr; i++) | |
2431afbf | 498 | index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); |
da165f47 JH |
499 | } |
500 | ||
eb9ae4b5 | 501 | static int locate_in_src_index(const struct cache_entry *ce, |
da165f47 JH |
502 | struct unpack_trees_options *o) |
503 | { | |
504 | struct index_state *index = o->src_index; | |
505 | int len = ce_namelen(ce); | |
506 | int pos = index_name_pos(index, ce->name, len); | |
507 | if (pos < 0) | |
508 | pos = -1 - pos; | |
509 | return pos; | |
510 | } | |
511 | ||
512 | /* | |
513 | * We call unpack_index_entry() with an unmerged cache entry | |
514 | * only in diff-index, and it wants a single callback. Skip | |
515 | * the other unmerged entry with the same name. | |
516 | */ | |
517 | static void mark_ce_used_same_name(struct cache_entry *ce, | |
518 | struct unpack_trees_options *o) | |
519 | { | |
520 | struct index_state *index = o->src_index; | |
521 | int len = ce_namelen(ce); | |
522 | int pos; | |
523 | ||
524 | for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { | |
525 | struct cache_entry *next = index->cache[pos]; | |
526 | if (len != ce_namelen(next) || | |
527 | memcmp(ce->name, next->name, len)) | |
528 | break; | |
529 | mark_ce_used(next, o); | |
530 | } | |
531 | } | |
532 | ||
533 | static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) | |
534 | { | |
535 | const struct index_state *index = o->src_index; | |
536 | int pos = o->cache_bottom; | |
537 | ||
538 | while (pos < index->cache_nr) { | |
539 | struct cache_entry *ce = index->cache[pos]; | |
540 | if (!(ce->ce_flags & CE_UNPACKED)) | |
541 | return ce; | |
542 | pos++; | |
543 | } | |
544 | return NULL; | |
545 | } | |
546 | ||
9c5e6c80 | 547 | static void add_same_unmerged(const struct cache_entry *ce, |
da165f47 JH |
548 | struct unpack_trees_options *o) |
549 | { | |
550 | struct index_state *index = o->src_index; | |
551 | int len = ce_namelen(ce); | |
552 | int pos = index_name_pos(index, ce->name, len); | |
553 | ||
554 | if (0 <= pos) | |
555 | die("programming error in a caller of mark_ce_used_same_name"); | |
556 | for (pos = -pos - 1; pos < index->cache_nr; pos++) { | |
557 | struct cache_entry *next = index->cache[pos]; | |
558 | if (len != ce_namelen(next) || | |
559 | memcmp(ce->name, next->name, len)) | |
560 | break; | |
561 | add_entry(o, next, 0, 0); | |
562 | mark_ce_used(next, o); | |
563 | } | |
564 | } | |
565 | ||
566 | static int unpack_index_entry(struct cache_entry *ce, | |
567 | struct unpack_trees_options *o) | |
01904572 | 568 | { |
5828e835 | 569 | const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
da165f47 | 570 | int ret; |
34110cd4 | 571 | |
66dbfd55 GV |
572 | src[0] = ce; |
573 | ||
da165f47 | 574 | mark_ce_used(ce, o); |
01904572 LT |
575 | if (ce_stage(ce)) { |
576 | if (o->skip_unmerged) { | |
34110cd4 LT |
577 | add_entry(o, ce, 0, 0); |
578 | return 0; | |
01904572 | 579 | } |
01904572 | 580 | } |
da165f47 JH |
581 | ret = call_unpack_fn(src, o); |
582 | if (ce_stage(ce)) | |
583 | mark_ce_used_same_name(ce, o); | |
584 | return ret; | |
01904572 LT |
585 | } |
586 | ||
730f7284 JH |
587 | static int find_cache_pos(struct traverse_info *, const struct name_entry *); |
588 | ||
589 | static void restore_cache_bottom(struct traverse_info *info, int bottom) | |
590 | { | |
591 | struct unpack_trees_options *o = info->data; | |
592 | ||
593 | if (o->diff_index_cached) | |
594 | return; | |
595 | o->cache_bottom = bottom; | |
596 | } | |
597 | ||
598 | static int switch_cache_bottom(struct traverse_info *info) | |
599 | { | |
600 | struct unpack_trees_options *o = info->data; | |
601 | int ret, pos; | |
602 | ||
603 | if (o->diff_index_cached) | |
604 | return 0; | |
605 | ret = o->cache_bottom; | |
606 | pos = find_cache_pos(info->prev, &info->name); | |
607 | ||
608 | if (pos < -1) | |
609 | o->cache_bottom = -2 - pos; | |
610 | else if (pos < 0) | |
611 | o->cache_bottom = o->src_index->cache_nr; | |
612 | return ret; | |
01904572 LT |
613 | } |
614 | ||
d12a8cf0 JH |
615 | static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k) |
616 | { | |
617 | return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); | |
618 | } | |
619 | ||
84563a62 JH |
620 | static int traverse_trees_recursive(int n, unsigned long dirmask, |
621 | unsigned long df_conflicts, | |
622 | struct name_entry *names, | |
623 | struct traverse_info *info) | |
16da134b | 624 | { |
730f7284 | 625 | int i, ret, bottom; |
d12a8cf0 | 626 | int nr_buf = 0; |
ca885a4f | 627 | struct tree_desc t[MAX_UNPACK_TREES]; |
1ce584b0 | 628 | void *buf[MAX_UNPACK_TREES]; |
01904572 LT |
629 | struct traverse_info newinfo; |
630 | struct name_entry *p; | |
631 | ||
632 | p = names; | |
633 | while (!p->mode) | |
634 | p++; | |
635 | ||
636 | newinfo = *info; | |
637 | newinfo.prev = info; | |
40e37256 | 638 | newinfo.pathspec = info->pathspec; |
01904572 | 639 | newinfo.name = *p; |
0de16337 | 640 | newinfo.pathlen += tree_entry_len(p) + 1; |
603d2498 | 641 | newinfo.df_conflicts |= df_conflicts; |
01904572 | 642 | |
d12a8cf0 JH |
643 | /* |
644 | * Fetch the tree from the ODB for each peer directory in the | |
645 | * n commits. | |
646 | * | |
647 | * For 2- and 3-way traversals, we try to avoid hitting the | |
648 | * ODB twice for the same OID. This should yield a nice speed | |
649 | * up in checkouts and merges when the commits are similar. | |
650 | * | |
651 | * We don't bother doing the full O(n^2) search for larger n, | |
652 | * because wider traversals don't happen that often and we | |
653 | * avoid the search setup. | |
654 | * | |
655 | * When 2 peer OIDs are the same, we just copy the tree | |
656 | * descriptor data. This implicitly borrows the buffer | |
657 | * data from the earlier cell. | |
658 | */ | |
01904572 | 659 | for (i = 0; i < n; i++, dirmask >>= 1) { |
d12a8cf0 JH |
660 | if (i > 0 && are_same_oid(&names[i], &names[i - 1])) |
661 | t[i] = t[i - 1]; | |
662 | else if (i > 1 && are_same_oid(&names[i], &names[i - 2])) | |
663 | t[i] = t[i - 2]; | |
664 | else { | |
665 | const unsigned char *sha1 = NULL; | |
666 | if (dirmask & 1) | |
667 | sha1 = names[i].oid->hash; | |
668 | buf[nr_buf++] = fill_tree_descriptor(t+i, sha1); | |
669 | } | |
01904572 | 670 | } |
730f7284 JH |
671 | |
672 | bottom = switch_cache_bottom(&newinfo); | |
673 | ret = traverse_trees(n, t, &newinfo); | |
674 | restore_cache_bottom(&newinfo, bottom); | |
1ce584b0 | 675 | |
d12a8cf0 | 676 | for (i = 0; i < nr_buf; i++) |
1ce584b0 JN |
677 | free(buf[i]); |
678 | ||
730f7284 | 679 | return ret; |
01904572 LT |
680 | } |
681 | ||
682 | /* | |
683 | * Compare the traverse-path to the cache entry without actually | |
684 | * having to generate the textual representation of the traverse | |
685 | * path. | |
686 | * | |
687 | * NOTE! This *only* compares up to the size of the traverse path | |
688 | * itself - the caller needs to do the final check for the cache | |
689 | * entry having more data at the end! | |
690 | */ | |
d9c2bd56 | 691 | static int do_compare_entry_piecewise(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) |
01904572 LT |
692 | { |
693 | int len, pathlen, ce_len; | |
694 | const char *ce_name; | |
695 | ||
696 | if (info->prev) { | |
d9c2bd56 DT |
697 | int cmp = do_compare_entry_piecewise(ce, info->prev, |
698 | &info->name); | |
01904572 LT |
699 | if (cmp) |
700 | return cmp; | |
701 | } | |
702 | pathlen = info->pathlen; | |
703 | ce_len = ce_namelen(ce); | |
704 | ||
705 | /* If ce_len < pathlen then we must have previously hit "name == directory" entry */ | |
706 | if (ce_len < pathlen) | |
707 | return -1; | |
708 | ||
709 | ce_len -= pathlen; | |
710 | ce_name = ce->name + pathlen; | |
711 | ||
0de16337 | 712 | len = tree_entry_len(n); |
01904572 LT |
713 | return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); |
714 | } | |
715 | ||
d9c2bd56 DT |
716 | static int do_compare_entry(const struct cache_entry *ce, |
717 | const struct traverse_info *info, | |
718 | const struct name_entry *n) | |
719 | { | |
720 | int len, pathlen, ce_len; | |
721 | const char *ce_name; | |
722 | int cmp; | |
723 | ||
724 | /* | |
725 | * If we have not precomputed the traverse path, it is quicker | |
726 | * to avoid doing so. But if we have precomputed it, | |
727 | * it is quicker to use the precomputed version. | |
728 | */ | |
729 | if (!info->traverse_path) | |
730 | return do_compare_entry_piecewise(ce, info, n); | |
731 | ||
732 | cmp = strncmp(ce->name, info->traverse_path, info->pathlen); | |
733 | if (cmp) | |
734 | return cmp; | |
735 | ||
736 | pathlen = info->pathlen; | |
737 | ce_len = ce_namelen(ce); | |
738 | ||
739 | if (ce_len < pathlen) | |
740 | return -1; | |
741 | ||
742 | ce_len -= pathlen; | |
743 | ce_name = ce->name + pathlen; | |
744 | ||
745 | len = tree_entry_len(n); | |
746 | return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); | |
747 | } | |
748 | ||
01904572 LT |
749 | static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) |
750 | { | |
751 | int cmp = do_compare_entry(ce, info, n); | |
752 | if (cmp) | |
753 | return cmp; | |
754 | ||
755 | /* | |
756 | * Even if the beginning compared identically, the ce should | |
757 | * compare as bigger than a directory leading up to it! | |
758 | */ | |
759 | return ce_namelen(ce) > traverse_path_len(info, n); | |
760 | } | |
761 | ||
da165f47 JH |
762 | static int ce_in_traverse_path(const struct cache_entry *ce, |
763 | const struct traverse_info *info) | |
764 | { | |
765 | if (!info->prev) | |
766 | return 1; | |
767 | if (do_compare_entry(ce, info->prev, &info->name)) | |
768 | return 0; | |
769 | /* | |
770 | * If ce (blob) is the same name as the path (which is a tree | |
771 | * we will be descending into), it won't be inside it. | |
772 | */ | |
773 | return (info->pathlen < ce_namelen(ce)); | |
774 | } | |
775 | ||
01904572 LT |
776 | static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage) |
777 | { | |
778 | int len = traverse_path_len(info, n); | |
779 | struct cache_entry *ce = xcalloc(1, cache_entry_size(len)); | |
780 | ||
781 | ce->ce_mode = create_ce_mode(n->mode); | |
b60e188c TG |
782 | ce->ce_flags = create_ce_flags(stage); |
783 | ce->ce_namelen = len; | |
99d1a986 | 784 | oidcpy(&ce->oid, n->oid); |
01904572 LT |
785 | make_traverse_path(ce->name, info, n); |
786 | ||
787 | return ce; | |
788 | } | |
789 | ||
c7cddc1a RS |
790 | static int unpack_nondirectories(int n, unsigned long mask, |
791 | unsigned long dirmask, | |
792 | struct cache_entry **src, | |
793 | const struct name_entry *names, | |
794 | const struct traverse_info *info) | |
01904572 LT |
795 | { |
796 | int i; | |
797 | struct unpack_trees_options *o = info->data; | |
603d2498 | 798 | unsigned long conflicts = info->df_conflicts | dirmask; |
01904572 LT |
799 | |
800 | /* Do we have *only* directories? Nothing to do */ | |
801 | if (mask == dirmask && !src[0]) | |
802 | return 0; | |
803 | ||
01904572 LT |
804 | /* |
805 | * Ok, we've filled in up to any potential index entry in src[0], | |
806 | * now do the rest. | |
807 | */ | |
808 | for (i = 0; i < n; i++) { | |
809 | int stage; | |
810 | unsigned int bit = 1ul << i; | |
811 | if (conflicts & bit) { | |
812 | src[i + o->merge] = o->df_conflict_entry; | |
813 | continue; | |
814 | } | |
815 | if (!(mask & bit)) | |
816 | continue; | |
817 | if (!o->merge) | |
818 | stage = 0; | |
819 | else if (i + 1 < o->head_idx) | |
820 | stage = 1; | |
821 | else if (i + 1 > o->head_idx) | |
822 | stage = 3; | |
823 | else | |
824 | stage = 2; | |
825 | src[i + o->merge] = create_ce_entry(info, names + i, stage); | |
826 | } | |
827 | ||
5d80ef5a RS |
828 | if (o->merge) { |
829 | int rc = call_unpack_fn((const struct cache_entry * const *)src, | |
830 | o); | |
831 | for (i = 0; i < n; i++) { | |
832 | struct cache_entry *ce = src[i + o->merge]; | |
833 | if (ce != o->df_conflict_entry) | |
834 | free(ce); | |
835 | } | |
836 | return rc; | |
837 | } | |
01904572 | 838 | |
01904572 | 839 | for (i = 0; i < n; i++) |
aab3b9a1 | 840 | if (src[i] && src[i] != o->df_conflict_entry) |
46169180 JK |
841 | if (do_add_entry(o, src[i], 0, 0)) |
842 | return -1; | |
843 | ||
01904572 LT |
844 | return 0; |
845 | } | |
846 | ||
353c5eeb JH |
847 | static int unpack_failed(struct unpack_trees_options *o, const char *message) |
848 | { | |
849 | discard_index(&o->result); | |
b4194828 | 850 | if (!o->gently && !o->exiting_early) { |
353c5eeb JH |
851 | if (message) |
852 | return error("%s", message); | |
853 | return -1; | |
854 | } | |
855 | return -1; | |
856 | } | |
857 | ||
730f7284 JH |
858 | /* |
859 | * The tree traversal is looking at name p. If we have a matching entry, | |
860 | * return it. If name p is a directory in the index, do not return | |
861 | * anything, as we will want to match it when the traversal descends into | |
862 | * the directory. | |
863 | */ | |
864 | static int find_cache_pos(struct traverse_info *info, | |
865 | const struct name_entry *p) | |
866 | { | |
867 | int pos; | |
868 | struct unpack_trees_options *o = info->data; | |
869 | struct index_state *index = o->src_index; | |
870 | int pfxlen = info->pathlen; | |
0de16337 | 871 | int p_len = tree_entry_len(p); |
730f7284 JH |
872 | |
873 | for (pos = o->cache_bottom; pos < index->cache_nr; pos++) { | |
9c5e6c80 | 874 | const struct cache_entry *ce = index->cache[pos]; |
730f7284 JH |
875 | const char *ce_name, *ce_slash; |
876 | int cmp, ce_len; | |
877 | ||
e53e6b44 BD |
878 | if (ce->ce_flags & CE_UNPACKED) { |
879 | /* | |
880 | * cache_bottom entry is already unpacked, so | |
881 | * we can never match it; don't check it | |
882 | * again. | |
883 | */ | |
884 | if (pos == o->cache_bottom) | |
885 | ++o->cache_bottom; | |
730f7284 | 886 | continue; |
e53e6b44 | 887 | } |
a6720955 DT |
888 | if (!ce_in_traverse_path(ce, info)) { |
889 | /* | |
890 | * Check if we can skip future cache checks | |
891 | * (because we're already past all possible | |
892 | * entries in the traverse path). | |
893 | */ | |
894 | if (info->traverse_path) { | |
895 | if (strncmp(ce->name, info->traverse_path, | |
896 | info->pathlen) > 0) | |
897 | break; | |
898 | } | |
730f7284 | 899 | continue; |
a6720955 | 900 | } |
730f7284 JH |
901 | ce_name = ce->name + pfxlen; |
902 | ce_slash = strchr(ce_name, '/'); | |
903 | if (ce_slash) | |
904 | ce_len = ce_slash - ce_name; | |
905 | else | |
906 | ce_len = ce_namelen(ce) - pfxlen; | |
907 | cmp = name_compare(p->path, p_len, ce_name, ce_len); | |
908 | /* | |
909 | * Exact match; if we have a directory we need to | |
910 | * delay returning it. | |
911 | */ | |
912 | if (!cmp) | |
913 | return ce_slash ? -2 - pos : pos; | |
914 | if (0 < cmp) | |
915 | continue; /* keep looking */ | |
916 | /* | |
917 | * ce_name sorts after p->path; could it be that we | |
918 | * have files under p->path directory in the index? | |
919 | * E.g. ce_name == "t-i", and p->path == "t"; we may | |
920 | * have "t/a" in the index. | |
921 | */ | |
922 | if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) && | |
923 | ce_name[p_len] < '/') | |
924 | continue; /* keep looking */ | |
925 | break; | |
926 | } | |
927 | return -1; | |
928 | } | |
929 | ||
930 | static struct cache_entry *find_cache_entry(struct traverse_info *info, | |
931 | const struct name_entry *p) | |
932 | { | |
933 | int pos = find_cache_pos(info, p); | |
934 | struct unpack_trees_options *o = info->data; | |
935 | ||
936 | if (0 <= pos) | |
937 | return o->src_index->cache[pos]; | |
938 | else | |
939 | return NULL; | |
940 | } | |
941 | ||
ba655da5 JH |
942 | static void debug_path(struct traverse_info *info) |
943 | { | |
944 | if (info->prev) { | |
945 | debug_path(info->prev); | |
946 | if (*info->prev->name.path) | |
947 | putchar('/'); | |
948 | } | |
949 | printf("%s", info->name.path); | |
950 | } | |
951 | ||
952 | static void debug_name_entry(int i, struct name_entry *n) | |
953 | { | |
954 | printf("ent#%d %06o %s\n", i, | |
955 | n->path ? n->mode : 0, | |
956 | n->path ? n->path : "(missing)"); | |
957 | } | |
958 | ||
959 | static void debug_unpack_callback(int n, | |
960 | unsigned long mask, | |
961 | unsigned long dirmask, | |
962 | struct name_entry *names, | |
963 | struct traverse_info *info) | |
964 | { | |
965 | int i; | |
966 | printf("* unpack mask %lu, dirmask %lu, cnt %d ", | |
967 | mask, dirmask, n); | |
968 | debug_path(info); | |
969 | putchar('\n'); | |
970 | for (i = 0; i < n; i++) | |
971 | debug_name_entry(i, names + i); | |
972 | } | |
973 | ||
01904572 LT |
974 | static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) |
975 | { | |
c7cddc1a | 976 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
01904572 | 977 | struct unpack_trees_options *o = info->data; |
01904572 LT |
978 | const struct name_entry *p = names; |
979 | ||
980 | /* Find first entry with a real name (we could use "mask" too) */ | |
981 | while (!p->mode) | |
982 | p++; | |
983 | ||
ba655da5 JH |
984 | if (o->debug_unpack) |
985 | debug_unpack_callback(n, mask, dirmask, names, info); | |
986 | ||
01904572 LT |
987 | /* Are we supposed to look at the index too? */ |
988 | if (o->merge) { | |
da165f47 | 989 | while (1) { |
da165f47 | 990 | int cmp; |
730f7284 JH |
991 | struct cache_entry *ce; |
992 | ||
993 | if (o->diff_index_cached) | |
994 | ce = next_cache_entry(o); | |
995 | else | |
996 | ce = find_cache_entry(info, p); | |
997 | ||
da165f47 JH |
998 | if (!ce) |
999 | break; | |
1000 | cmp = compare_entry(ce, info, p); | |
01904572 LT |
1001 | if (cmp < 0) { |
1002 | if (unpack_index_entry(ce, o) < 0) | |
353c5eeb | 1003 | return unpack_failed(o, NULL); |
01904572 LT |
1004 | continue; |
1005 | } | |
1006 | if (!cmp) { | |
1007 | if (ce_stage(ce)) { | |
1008 | /* | |
da165f47 JH |
1009 | * If we skip unmerged index |
1010 | * entries, we'll skip this | |
1011 | * entry *and* the tree | |
1012 | * entries associated with it! | |
01904572 | 1013 | */ |
34110cd4 | 1014 | if (o->skip_unmerged) { |
da165f47 | 1015 | add_same_unmerged(ce, o); |
01904572 | 1016 | return mask; |
34110cd4 | 1017 | } |
01904572 LT |
1018 | } |
1019 | src[0] = ce; | |
01904572 LT |
1020 | } |
1021 | break; | |
1022 | } | |
1023 | } | |
1024 | ||
34110cd4 | 1025 | if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0) |
01904572 LT |
1026 | return -1; |
1027 | ||
97e5954b | 1028 | if (o->merge && src[0]) { |
da165f47 JH |
1029 | if (ce_stage(src[0])) |
1030 | mark_ce_used_same_name(src[0], o); | |
1031 | else | |
1032 | mark_ce_used(src[0], o); | |
1033 | } | |
1034 | ||
01904572 LT |
1035 | /* Now handle any directories.. */ |
1036 | if (dirmask) { | |
b65982b6 JH |
1037 | /* special case: "diff-index --cached" looking at a tree */ |
1038 | if (o->diff_index_cached && | |
1039 | n == 1 && dirmask == 1 && S_ISDIR(names->mode)) { | |
1040 | int matches; | |
1041 | matches = cache_tree_matches_traversal(o->src_index->cache_tree, | |
1042 | names, info); | |
1043 | /* | |
da165f47 JH |
1044 | * Everything under the name matches; skip the |
1045 | * entire hierarchy. diff_index_cached codepath | |
1046 | * special cases D/F conflicts in such a way that | |
1047 | * it does not do any look-ahead, so this is safe. | |
b65982b6 JH |
1048 | */ |
1049 | if (matches) { | |
da165f47 | 1050 | o->cache_bottom += matches; |
b65982b6 JH |
1051 | return mask; |
1052 | } | |
1053 | } | |
1054 | ||
603d2498 | 1055 | if (traverse_trees_recursive(n, dirmask, mask & ~dirmask, |
542c264b JH |
1056 | names, info) < 0) |
1057 | return -1; | |
01904572 LT |
1058 | return mask; |
1059 | } | |
1060 | ||
1061 | return mask; | |
1062 | } | |
1063 | ||
28911091 | 1064 | static int clear_ce_flags_1(struct cache_entry **cache, int nr, |
fc2b6214 | 1065 | struct strbuf *prefix, |
28911091 NTND |
1066 | int select_mask, int clear_mask, |
1067 | struct exclude_list *el, int defval); | |
1068 | ||
9037026d NTND |
1069 | /* Whole directory matching */ |
1070 | static int clear_ce_flags_dir(struct cache_entry **cache, int nr, | |
fc2b6214 | 1071 | struct strbuf *prefix, |
9037026d NTND |
1072 | char *basename, |
1073 | int select_mask, int clear_mask, | |
28911091 | 1074 | struct exclude_list *el, int defval) |
9037026d | 1075 | { |
28911091 | 1076 | struct cache_entry **cache_end; |
9037026d | 1077 | int dtype = DT_DIR; |
fc2b6214 | 1078 | int ret = is_excluded_from_list(prefix->buf, prefix->len, |
fba92be8 | 1079 | basename, &dtype, el, &the_index); |
fc2b6214 | 1080 | int rc; |
9037026d | 1081 | |
fc2b6214 | 1082 | strbuf_addch(prefix, '/'); |
9037026d | 1083 | |
28911091 NTND |
1084 | /* If undecided, use matching result of parent dir in defval */ |
1085 | if (ret < 0) | |
1086 | ret = defval; | |
9037026d | 1087 | |
28911091 NTND |
1088 | for (cache_end = cache; cache_end != cache + nr; cache_end++) { |
1089 | struct cache_entry *ce = *cache_end; | |
fc2b6214 | 1090 | if (strncmp(ce->name, prefix->buf, prefix->len)) |
28911091 | 1091 | break; |
9037026d NTND |
1092 | } |
1093 | ||
28911091 NTND |
1094 | /* |
1095 | * TODO: check el, if there are no patterns that may conflict | |
1096 | * with ret (iow, we know in advance the incl/excl | |
1097 | * decision for the entire directory), clear flag here without | |
1098 | * calling clear_ce_flags_1(). That function will call | |
07958050 | 1099 | * the expensive is_excluded_from_list() on every entry. |
28911091 | 1100 | */ |
fc2b6214 AP |
1101 | rc = clear_ce_flags_1(cache, cache_end - cache, |
1102 | prefix, | |
1103 | select_mask, clear_mask, | |
1104 | el, ret); | |
1105 | strbuf_setlen(prefix, prefix->len - 1); | |
1106 | return rc; | |
9037026d NTND |
1107 | } |
1108 | ||
1109 | /* | |
1110 | * Traverse the index, find every entry that matches according to | |
1111 | * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the | |
1112 | * number of traversed entries. | |
1113 | * | |
1114 | * If select_mask is non-zero, only entries whose ce_flags has on of | |
1115 | * those bits enabled are traversed. | |
1116 | * | |
1117 | * cache : pointer to an index entry | |
1118 | * prefix_len : an offset to its path | |
1119 | * | |
1120 | * The current path ("prefix") including the trailing '/' is | |
1121 | * cache[0]->name[0..(prefix_len-1)] | |
1122 | * Top level path has prefix_len zero. | |
1123 | */ | |
1124 | static int clear_ce_flags_1(struct cache_entry **cache, int nr, | |
fc2b6214 | 1125 | struct strbuf *prefix, |
9037026d | 1126 | int select_mask, int clear_mask, |
28911091 | 1127 | struct exclude_list *el, int defval) |
9037026d NTND |
1128 | { |
1129 | struct cache_entry **cache_end = cache + nr; | |
1130 | ||
1131 | /* | |
1132 | * Process all entries that have the given prefix and meet | |
1133 | * select_mask condition | |
1134 | */ | |
1135 | while(cache != cache_end) { | |
1136 | struct cache_entry *ce = *cache; | |
1137 | const char *name, *slash; | |
28911091 | 1138 | int len, dtype, ret; |
9037026d NTND |
1139 | |
1140 | if (select_mask && !(ce->ce_flags & select_mask)) { | |
1141 | cache++; | |
1142 | continue; | |
1143 | } | |
1144 | ||
fc2b6214 | 1145 | if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len)) |
9037026d NTND |
1146 | break; |
1147 | ||
fc2b6214 | 1148 | name = ce->name + prefix->len; |
9037026d NTND |
1149 | slash = strchr(name, '/'); |
1150 | ||
1151 | /* If it's a directory, try whole directory match first */ | |
1152 | if (slash) { | |
1153 | int processed; | |
1154 | ||
1155 | len = slash - name; | |
fc2b6214 | 1156 | strbuf_add(prefix, name, len); |
9037026d | 1157 | |
9037026d | 1158 | processed = clear_ce_flags_dir(cache, cache_end - cache, |
fc2b6214 AP |
1159 | prefix, |
1160 | prefix->buf + prefix->len - len, | |
9037026d | 1161 | select_mask, clear_mask, |
28911091 | 1162 | el, defval); |
9037026d NTND |
1163 | |
1164 | /* clear_c_f_dir eats a whole dir already? */ | |
1165 | if (processed) { | |
1166 | cache += processed; | |
fc2b6214 | 1167 | strbuf_setlen(prefix, prefix->len - len); |
9037026d NTND |
1168 | continue; |
1169 | } | |
1170 | ||
fc2b6214 | 1171 | strbuf_addch(prefix, '/'); |
9037026d | 1172 | cache += clear_ce_flags_1(cache, cache_end - cache, |
fc2b6214 | 1173 | prefix, |
28911091 | 1174 | select_mask, clear_mask, el, defval); |
fc2b6214 | 1175 | strbuf_setlen(prefix, prefix->len - len - 1); |
9037026d NTND |
1176 | continue; |
1177 | } | |
1178 | ||
1179 | /* Non-directory */ | |
1180 | dtype = ce_to_dtype(ce); | |
07958050 | 1181 | ret = is_excluded_from_list(ce->name, ce_namelen(ce), |
fba92be8 | 1182 | name, &dtype, el, &the_index); |
28911091 NTND |
1183 | if (ret < 0) |
1184 | ret = defval; | |
1185 | if (ret > 0) | |
9037026d NTND |
1186 | ce->ce_flags &= ~clear_mask; |
1187 | cache++; | |
1188 | } | |
1189 | return nr - (cache_end - cache); | |
1190 | } | |
1191 | ||
1192 | static int clear_ce_flags(struct cache_entry **cache, int nr, | |
1193 | int select_mask, int clear_mask, | |
1194 | struct exclude_list *el) | |
1195 | { | |
fc2b6214 AP |
1196 | static struct strbuf prefix = STRBUF_INIT; |
1197 | ||
1198 | strbuf_reset(&prefix); | |
1199 | ||
9037026d | 1200 | return clear_ce_flags_1(cache, nr, |
fc2b6214 | 1201 | &prefix, |
9037026d | 1202 | select_mask, clear_mask, |
28911091 | 1203 | el, 0); |
9037026d NTND |
1204 | } |
1205 | ||
2431afbf NTND |
1206 | /* |
1207 | * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout | |
1208 | */ | |
1209 | static void mark_new_skip_worktree(struct exclude_list *el, | |
1210 | struct index_state *the_index, | |
1211 | int select_flag, int skip_wt_flag) | |
1212 | { | |
1213 | int i; | |
1214 | ||
9037026d NTND |
1215 | /* |
1216 | * 1. Pretend the narrowest worktree: only unmerged entries | |
1217 | * are checked out | |
1218 | */ | |
2431afbf NTND |
1219 | for (i = 0; i < the_index->cache_nr; i++) { |
1220 | struct cache_entry *ce = the_index->cache[i]; | |
1221 | ||
1222 | if (select_flag && !(ce->ce_flags & select_flag)) | |
1223 | continue; | |
1224 | ||
9037026d | 1225 | if (!ce_stage(ce)) |
2431afbf NTND |
1226 | ce->ce_flags |= skip_wt_flag; |
1227 | else | |
1228 | ce->ce_flags &= ~skip_wt_flag; | |
1229 | } | |
9037026d NTND |
1230 | |
1231 | /* | |
1232 | * 2. Widen worktree according to sparse-checkout file. | |
1233 | * Matched entries will have skip_wt_flag cleared (i.e. "in") | |
1234 | */ | |
1235 | clear_ce_flags(the_index->cache, the_index->cache_nr, | |
1236 | select_flag, skip_wt_flag, el); | |
2431afbf NTND |
1237 | } |
1238 | ||
eb9ae4b5 RS |
1239 | static int verify_absent(const struct cache_entry *, |
1240 | enum unpack_trees_error_types, | |
1241 | struct unpack_trees_options *); | |
2e2b887d JH |
1242 | /* |
1243 | * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the | |
1244 | * resulting index, -2 on failure to reflect the changes to the work tree. | |
2431afbf NTND |
1245 | * |
1246 | * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally | |
2e2b887d | 1247 | */ |
01904572 LT |
1248 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) |
1249 | { | |
e800ec9d | 1250 | int i, ret; |
0fb1eaa8 | 1251 | static struct cache_entry *dfc; |
08aefc9e | 1252 | struct exclude_list el; |
16da134b | 1253 | |
ca885a4f JH |
1254 | if (len > MAX_UNPACK_TREES) |
1255 | die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES); | |
16da134b | 1256 | |
08aefc9e NTND |
1257 | memset(&el, 0, sizeof(el)); |
1258 | if (!core_apply_sparse_checkout || !o->update) | |
1259 | o->skip_sparse_checkout = 1; | |
1260 | if (!o->skip_sparse_checkout) { | |
fcd12db6 | 1261 | char *sparse = git_pathdup("info/sparse-checkout"); |
473e3930 | 1262 | if (add_excludes_from_file_to_list(sparse, "", 0, &el, NULL) < 0) |
08aefc9e NTND |
1263 | o->skip_sparse_checkout = 1; |
1264 | else | |
1265 | o->el = ⪙ | |
fcd12db6 | 1266 | free(sparse); |
08aefc9e NTND |
1267 | } |
1268 | ||
34110cd4 | 1269 | memset(&o->result, 0, sizeof(o->result)); |
913e0e99 | 1270 | o->result.initialized = 1; |
da165f47 JH |
1271 | o->result.timestamp.sec = o->src_index->timestamp.sec; |
1272 | o->result.timestamp.nsec = o->src_index->timestamp.nsec; | |
9170c7ab | 1273 | o->result.version = o->src_index->version; |
5fc2fc8f NTND |
1274 | o->result.split_index = o->src_index->split_index; |
1275 | if (o->result.split_index) | |
1276 | o->result.split_index->refcount++; | |
e93021b2 | 1277 | hashcpy(o->result.sha1, o->src_index->sha1); |
16da134b | 1278 | o->merge_size = len; |
da165f47 | 1279 | mark_all_ce_unused(o->src_index); |
0fb1eaa8 | 1280 | |
2431afbf NTND |
1281 | /* |
1282 | * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries | |
1283 | */ | |
1284 | if (!o->skip_sparse_checkout) | |
1285 | mark_new_skip_worktree(o->el, o->src_index, 0, CE_NEW_SKIP_WORKTREE); | |
1286 | ||
0fb1eaa8 | 1287 | if (!dfc) |
13494ed1 | 1288 | dfc = xcalloc(1, cache_entry_size(0)); |
0fb1eaa8 | 1289 | o->df_conflict_entry = dfc; |
16da134b JS |
1290 | |
1291 | if (len) { | |
01904572 LT |
1292 | const char *prefix = o->prefix ? o->prefix : ""; |
1293 | struct traverse_info info; | |
1294 | ||
1295 | setup_traverse_info(&info, prefix); | |
1296 | info.fn = unpack_callback; | |
1297 | info.data = o; | |
e6c111b4 | 1298 | info.show_all_errors = o->show_all_errors; |
40e37256 | 1299 | info.pathspec = o->pathspec; |
01904572 | 1300 | |
da165f47 JH |
1301 | if (o->prefix) { |
1302 | /* | |
1303 | * Unpack existing index entries that sort before the | |
1304 | * prefix the tree is spliced into. Note that o->merge | |
1305 | * is always true in this case. | |
1306 | */ | |
1307 | while (1) { | |
1308 | struct cache_entry *ce = next_cache_entry(o); | |
1309 | if (!ce) | |
1310 | break; | |
1311 | if (ce_in_traverse_path(ce, &info)) | |
1312 | break; | |
1313 | if (unpack_index_entry(ce, o) < 0) | |
1314 | goto return_failed; | |
1315 | } | |
08aefc9e | 1316 | } |
da165f47 | 1317 | |
01904572 | 1318 | if (traverse_trees(len, t, &info) < 0) |
da165f47 | 1319 | goto return_failed; |
16da134b JS |
1320 | } |
1321 | ||
01904572 LT |
1322 | /* Any left-over entries in the index? */ |
1323 | if (o->merge) { | |
da165f47 JH |
1324 | while (1) { |
1325 | struct cache_entry *ce = next_cache_entry(o); | |
1326 | if (!ce) | |
1327 | break; | |
01904572 | 1328 | if (unpack_index_entry(ce, o) < 0) |
da165f47 | 1329 | goto return_failed; |
17e46426 | 1330 | } |
17e46426 | 1331 | } |
da165f47 | 1332 | mark_all_ce_unused(o->src_index); |
16da134b | 1333 | |
08aefc9e NTND |
1334 | if (o->trivial_merges_only && o->nontrivial_merge) { |
1335 | ret = unpack_failed(o, "Merge requires file-level merging"); | |
1336 | goto done; | |
1337 | } | |
01904572 | 1338 | |
e800ec9d | 1339 | if (!o->skip_sparse_checkout) { |
9e1afb16 | 1340 | int empty_worktree = 1; |
2431afbf NTND |
1341 | |
1342 | /* | |
1343 | * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1 | |
1344 | * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE | |
1345 | * so apply_sparse_checkout() won't attempt to remove it from worktree | |
1346 | */ | |
1347 | mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); | |
1348 | ||
17d26a4d | 1349 | ret = 0; |
2431afbf | 1350 | for (i = 0; i < o->result.cache_nr; i++) { |
e800ec9d NTND |
1351 | struct cache_entry *ce = o->result.cache[i]; |
1352 | ||
2431afbf NTND |
1353 | /* |
1354 | * Entries marked with CE_ADDED in merged_entry() do not have | |
1355 | * verify_absent() check (the check is effectively disabled | |
1356 | * because CE_NEW_SKIP_WORKTREE is set unconditionally). | |
1357 | * | |
1358 | * Do the real check now because we have had | |
1359 | * correct CE_NEW_SKIP_WORKTREE | |
1360 | */ | |
1361 | if (ce->ce_flags & CE_ADDED && | |
17d26a4d NTND |
1362 | verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { |
1363 | if (!o->show_all_errors) | |
1364 | goto return_failed; | |
1365 | ret = -1; | |
1366 | } | |
2431afbf | 1367 | |
a5c446f1 | 1368 | if (apply_sparse_checkout(&o->result, ce, o)) { |
17d26a4d NTND |
1369 | if (!o->show_all_errors) |
1370 | goto return_failed; | |
e800ec9d | 1371 | ret = -1; |
e800ec9d | 1372 | } |
eec3fc03 | 1373 | if (!ce_skip_worktree(ce)) |
9e1afb16 | 1374 | empty_worktree = 0; |
f1f523ea | 1375 | |
e800ec9d | 1376 | } |
17d26a4d NTND |
1377 | if (ret < 0) |
1378 | goto return_failed; | |
a7bc906f NTND |
1379 | /* |
1380 | * Sparse checkout is meant to narrow down checkout area | |
1381 | * but it does not make sense to narrow down to empty working | |
1382 | * tree. This is usually a mistake in sparse checkout rules. | |
1383 | * Do not allow users to do that. | |
1384 | */ | |
9e1afb16 NTND |
1385 | if (o->result.cache_nr && empty_worktree) { |
1386 | ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory"); | |
1387 | goto done; | |
1388 | } | |
e800ec9d | 1389 | } |
01904572 | 1390 | |
34110cd4 | 1391 | o->src_index = NULL; |
30ac275b | 1392 | ret = check_updates(o) ? (-2) : 0; |
e28f7641 | 1393 | if (o->dst_index) { |
52fca218 BD |
1394 | if (!ret) { |
1395 | if (!o->result.cache_tree) | |
1396 | o->result.cache_tree = cache_tree(); | |
1397 | if (!cache_tree_fully_valid(o->result.cache_tree)) | |
1398 | cache_tree_update(&o->result, | |
1399 | WRITE_TREE_SILENT | | |
1400 | WRITE_TREE_REPAIR); | |
1401 | } | |
edf3b905 | 1402 | move_index_extensions(&o->result, o->dst_index); |
e28f7641 | 1403 | discard_index(o->dst_index); |
34110cd4 | 1404 | *o->dst_index = o->result; |
a16cc8b2 JH |
1405 | } else { |
1406 | discard_index(&o->result); | |
e28f7641 | 1407 | } |
08aefc9e NTND |
1408 | |
1409 | done: | |
f6198812 | 1410 | clear_exclude_list(&el); |
2e2b887d | 1411 | return ret; |
da165f47 JH |
1412 | |
1413 | return_failed: | |
e6c111b4 MM |
1414 | if (o->show_all_errors) |
1415 | display_error_msgs(o); | |
da165f47 | 1416 | mark_all_ce_unused(o->src_index); |
026680f8 | 1417 | ret = unpack_failed(o, NULL); |
b4194828 JH |
1418 | if (o->exiting_early) |
1419 | ret = 0; | |
026680f8 | 1420 | goto done; |
16da134b | 1421 | } |
076b0adc JS |
1422 | |
1423 | /* Here come the merge functions */ | |
1424 | ||
eb9ae4b5 RS |
1425 | static int reject_merge(const struct cache_entry *ce, |
1426 | struct unpack_trees_options *o) | |
076b0adc | 1427 | { |
6a143aa2 JN |
1428 | return o->gently ? -1 : |
1429 | add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name); | |
076b0adc JS |
1430 | } |
1431 | ||
eb9ae4b5 | 1432 | static int same(const struct cache_entry *a, const struct cache_entry *b) |
076b0adc JS |
1433 | { |
1434 | if (!!a != !!b) | |
1435 | return 0; | |
1436 | if (!a && !b) | |
1437 | return 1; | |
e11d7b59 JH |
1438 | if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED) |
1439 | return 0; | |
076b0adc | 1440 | return a->ce_mode == b->ce_mode && |
99d1a986 | 1441 | !oidcmp(&a->oid, &b->oid); |
076b0adc JS |
1442 | } |
1443 | ||
1444 | ||
1445 | /* | |
1446 | * When a CE gets turned into an unmerged entry, we | |
1447 | * want it to be up-to-date | |
1448 | */ | |
eb9ae4b5 RS |
1449 | static int verify_uptodate_1(const struct cache_entry *ce, |
1450 | struct unpack_trees_options *o, | |
1451 | enum unpack_trees_error_types error_type) | |
076b0adc JS |
1452 | { |
1453 | struct stat st; | |
1454 | ||
d5b66299 NTND |
1455 | if (o->index_only) |
1456 | return 0; | |
1457 | ||
1458 | /* | |
1459 | * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again | |
1460 | * if this entry is truly up-to-date because this file may be | |
1461 | * overwritten. | |
1462 | */ | |
1463 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) | |
1464 | ; /* keep checking */ | |
1465 | else if (o->reset || ce_uptodate(ce)) | |
203a2fe1 | 1466 | return 0; |
076b0adc JS |
1467 | |
1468 | if (!lstat(ce->name, &st)) { | |
d5b66299 NTND |
1469 | int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE; |
1470 | unsigned changed = ie_match_stat(o->src_index, ce, &st, flags); | |
a7bc845a SB |
1471 | |
1472 | if (submodule_from_ce(ce)) { | |
1473 | int r = check_submodule_move_head(ce, | |
1474 | "HEAD", oid_to_hex(&ce->oid), o); | |
1475 | if (r) | |
1476 | return o->gently ? -1 : | |
1477 | add_rejected_path(o, error_type, ce->name); | |
1478 | return 0; | |
1479 | } | |
1480 | ||
076b0adc | 1481 | if (!changed) |
203a2fe1 | 1482 | return 0; |
936492d3 | 1483 | /* |
a7bc845a SB |
1484 | * Historic default policy was to allow submodule to be out |
1485 | * of sync wrt the superproject index. If the submodule was | |
1486 | * not considered interesting above, we don't care here. | |
936492d3 | 1487 | */ |
7a51ed66 | 1488 | if (S_ISGITLINK(ce->ce_mode)) |
203a2fe1 | 1489 | return 0; |
a7bc845a | 1490 | |
076b0adc JS |
1491 | errno = 0; |
1492 | } | |
076b0adc | 1493 | if (errno == ENOENT) |
203a2fe1 | 1494 | return 0; |
17e46426 | 1495 | return o->gently ? -1 : |
e6c111b4 | 1496 | add_rejected_path(o, error_type, ce->name); |
35a5aa79 NTND |
1497 | } |
1498 | ||
eb9ae4b5 | 1499 | static int verify_uptodate(const struct cache_entry *ce, |
35a5aa79 NTND |
1500 | struct unpack_trees_options *o) |
1501 | { | |
2431afbf | 1502 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) |
f1f523ea | 1503 | return 0; |
08402b04 | 1504 | return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE); |
076b0adc JS |
1505 | } |
1506 | ||
eb9ae4b5 | 1507 | static int verify_uptodate_sparse(const struct cache_entry *ce, |
e800ec9d NTND |
1508 | struct unpack_trees_options *o) |
1509 | { | |
08402b04 | 1510 | return verify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE); |
076b0adc JS |
1511 | } |
1512 | ||
eb9ae4b5 RS |
1513 | static void invalidate_ce_path(const struct cache_entry *ce, |
1514 | struct unpack_trees_options *o) | |
076b0adc | 1515 | { |
e931371a NTND |
1516 | if (!ce) |
1517 | return; | |
1518 | cache_tree_invalidate_path(o->src_index, ce->name); | |
1519 | untracked_cache_invalidate_path(o->src_index, ce->name); | |
076b0adc JS |
1520 | } |
1521 | ||
0cf73755 SV |
1522 | /* |
1523 | * Check that checking out ce->sha1 in subdir ce->name is not | |
1524 | * going to overwrite any working files. | |
1525 | * | |
1526 | * Currently, git does not checkout subprojects during a superproject | |
1527 | * checkout, so it is not going to overwrite anything. | |
1528 | */ | |
d6b12300 SB |
1529 | static int verify_clean_submodule(const char *old_sha1, |
1530 | const struct cache_entry *ce, | |
eb9ae4b5 RS |
1531 | enum unpack_trees_error_types error_type, |
1532 | struct unpack_trees_options *o) | |
0cf73755 | 1533 | { |
a7bc845a SB |
1534 | if (!submodule_from_ce(ce)) |
1535 | return 0; | |
1536 | ||
1537 | return check_submodule_move_head(ce, old_sha1, | |
1538 | oid_to_hex(&ce->oid), o); | |
0cf73755 SV |
1539 | } |
1540 | ||
eb9ae4b5 RS |
1541 | static int verify_clean_subdirectory(const struct cache_entry *ce, |
1542 | enum unpack_trees_error_types error_type, | |
1543 | struct unpack_trees_options *o) | |
c8193534 JH |
1544 | { |
1545 | /* | |
0cf73755 | 1546 | * we are about to extract "ce->name"; we would not want to lose |
c8193534 JH |
1547 | * anything in the existing directory there. |
1548 | */ | |
1549 | int namelen; | |
7b9e3ce0 | 1550 | int i; |
c8193534 JH |
1551 | struct dir_struct d; |
1552 | char *pathbuf; | |
1553 | int cnt = 0; | |
0cf73755 | 1554 | |
d6b12300 SB |
1555 | if (S_ISGITLINK(ce->ce_mode)) { |
1556 | unsigned char sha1[20]; | |
1557 | int sub_head = resolve_gitlink_ref(ce->name, "HEAD", sha1); | |
1558 | /* | |
1559 | * If we are not going to update the submodule, then | |
0cf73755 SV |
1560 | * we don't care. |
1561 | */ | |
d6b12300 | 1562 | if (!sub_head && !hashcmp(sha1, ce->oid.hash)) |
0cf73755 | 1563 | return 0; |
d6b12300 SB |
1564 | return verify_clean_submodule(sub_head ? NULL : sha1_to_hex(sha1), |
1565 | ce, error_type, o); | |
0cf73755 | 1566 | } |
c8193534 JH |
1567 | |
1568 | /* | |
1569 | * First let's make sure we do not have a local modification | |
1570 | * in that directory. | |
1571 | */ | |
68c4f6a5 | 1572 | namelen = ce_namelen(ce); |
da165f47 JH |
1573 | for (i = locate_in_src_index(ce, o); |
1574 | i < o->src_index->cache_nr; | |
1575 | i++) { | |
837e5fe9 CB |
1576 | struct cache_entry *ce2 = o->src_index->cache[i]; |
1577 | int len = ce_namelen(ce2); | |
c8193534 | 1578 | if (len < namelen || |
837e5fe9 CB |
1579 | strncmp(ce->name, ce2->name, namelen) || |
1580 | ce2->name[namelen] != '/') | |
c8193534 JH |
1581 | break; |
1582 | /* | |
da165f47 JH |
1583 | * ce2->name is an entry in the subdirectory to be |
1584 | * removed. | |
c8193534 | 1585 | */ |
837e5fe9 CB |
1586 | if (!ce_stage(ce2)) { |
1587 | if (verify_uptodate(ce2, o)) | |
203a2fe1 | 1588 | return -1; |
837e5fe9 | 1589 | add_entry(o, ce2, CE_REMOVE, 0); |
da165f47 | 1590 | mark_ce_used(ce2, o); |
c8193534 JH |
1591 | } |
1592 | cnt++; | |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * Then we need to make sure that we do not lose a locally | |
1597 | * present file that is not ignored. | |
1598 | */ | |
75faa45a | 1599 | pathbuf = xstrfmt("%.*s/", namelen, ce->name); |
c8193534 JH |
1600 | |
1601 | memset(&d, 0, sizeof(d)); | |
1602 | if (o->dir) | |
1603 | d.exclude_per_dir = o->dir->exclude_per_dir; | |
2c1eb104 | 1604 | i = read_directory(&d, &the_index, pathbuf, namelen+1, NULL); |
c8193534 | 1605 | if (i) |
17e46426 | 1606 | return o->gently ? -1 : |
e6c111b4 | 1607 | add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); |
c8193534 JH |
1608 | free(pathbuf); |
1609 | return cnt; | |
1610 | } | |
1611 | ||
32260ad5 LT |
1612 | /* |
1613 | * This gets called when there was no index entry for the tree entry 'dst', | |
1614 | * but we found a file in the working tree that 'lstat()' said was fine, | |
1615 | * and we're on a case-insensitive filesystem. | |
1616 | * | |
1617 | * See if we can find a case-insensitive match in the index that also | |
1618 | * matches the stat information, and assume it's that other file! | |
1619 | */ | |
a9307f5a | 1620 | static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st) |
32260ad5 | 1621 | { |
9c5e6c80 | 1622 | const struct cache_entry *src; |
32260ad5 | 1623 | |
ebbd7439 | 1624 | src = index_file_exists(o->src_index, name, len, 1); |
56cac48c | 1625 | return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); |
32260ad5 LT |
1626 | } |
1627 | ||
a9307f5a | 1628 | static int check_ok_to_remove(const char *name, int len, int dtype, |
eb9ae4b5 | 1629 | const struct cache_entry *ce, struct stat *st, |
a9307f5a CB |
1630 | enum unpack_trees_error_types error_type, |
1631 | struct unpack_trees_options *o) | |
1632 | { | |
9c5e6c80 | 1633 | const struct cache_entry *result; |
a9307f5a CB |
1634 | |
1635 | /* | |
1636 | * It may be that the 'lstat()' succeeded even though | |
1637 | * target 'ce' was absent, because there is an old | |
1638 | * entry that is different only in case.. | |
1639 | * | |
1640 | * Ignore that lstat() if it matches. | |
1641 | */ | |
1642 | if (ignore_case && icase_exists(o, name, len, st)) | |
1643 | return 0; | |
1644 | ||
589570db | 1645 | if (o->dir && |
a0bba65b | 1646 | is_excluded(o->dir, &the_index, name, &dtype)) |
a9307f5a CB |
1647 | /* |
1648 | * ce->name is explicitly excluded, so it is Ok to | |
1649 | * overwrite it. | |
1650 | */ | |
1651 | return 0; | |
1652 | if (S_ISDIR(st->st_mode)) { | |
1653 | /* | |
1654 | * We are checking out path "foo" and | |
1655 | * found "foo/." in the working tree. | |
1656 | * This is tricky -- if we have modified | |
1657 | * files that are in "foo/" we would lose | |
1658 | * them. | |
1659 | */ | |
1660 | if (verify_clean_subdirectory(ce, error_type, o) < 0) | |
1661 | return -1; | |
1662 | return 0; | |
1663 | } | |
1664 | ||
1665 | /* | |
1666 | * The previous round may already have decided to | |
1667 | * delete this path, which is in a subdirectory that | |
1668 | * is being replaced with a blob. | |
1669 | */ | |
ebbd7439 | 1670 | result = index_file_exists(&o->result, name, len, 0); |
a9307f5a CB |
1671 | if (result) { |
1672 | if (result->ce_flags & CE_REMOVE) | |
1673 | return 0; | |
1674 | } | |
1675 | ||
1676 | return o->gently ? -1 : | |
1677 | add_rejected_path(o, error_type, name); | |
1678 | } | |
1679 | ||
076b0adc JS |
1680 | /* |
1681 | * We do not want to remove or overwrite a working tree file that | |
f8a9d428 | 1682 | * is not tracked, unless it is ignored. |
076b0adc | 1683 | */ |
eb9ae4b5 RS |
1684 | static int verify_absent_1(const struct cache_entry *ce, |
1685 | enum unpack_trees_error_types error_type, | |
1686 | struct unpack_trees_options *o) | |
076b0adc | 1687 | { |
f66caaf9 | 1688 | int len; |
076b0adc JS |
1689 | struct stat st; |
1690 | ||
1691 | if (o->index_only || o->reset || !o->update) | |
203a2fe1 | 1692 | return 0; |
c8193534 | 1693 | |
f66caaf9 CB |
1694 | len = check_leading_path(ce->name, ce_namelen(ce)); |
1695 | if (!len) | |
203a2fe1 | 1696 | return 0; |
f66caaf9 | 1697 | else if (len > 0) { |
f514ef97 JK |
1698 | char *path; |
1699 | int ret; | |
1700 | ||
1701 | path = xmemdupz(ce->name, len); | |
a93e5301 | 1702 | if (lstat(path, &st)) |
43c728e2 | 1703 | ret = error_errno("cannot stat '%s'", path); |
a7bc845a SB |
1704 | else { |
1705 | if (submodule_from_ce(ce)) | |
1706 | ret = check_submodule_move_head(ce, | |
1707 | oid_to_hex(&ce->oid), | |
1708 | NULL, o); | |
1709 | else | |
1710 | ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL, | |
1711 | &st, error_type, o); | |
1712 | } | |
f514ef97 JK |
1713 | free(path); |
1714 | return ret; | |
92fda79e JN |
1715 | } else if (lstat(ce->name, &st)) { |
1716 | if (errno != ENOENT) | |
43c728e2 | 1717 | return error_errno("cannot stat '%s'", ce->name); |
92fda79e JN |
1718 | return 0; |
1719 | } else { | |
a7bc845a SB |
1720 | if (submodule_from_ce(ce)) |
1721 | return check_submodule_move_head(ce, oid_to_hex(&ce->oid), | |
1722 | NULL, o); | |
1723 | ||
a9307f5a | 1724 | return check_ok_to_remove(ce->name, ce_namelen(ce), |
92fda79e JN |
1725 | ce_to_dtype(ce), ce, &st, |
1726 | error_type, o); | |
1727 | } | |
076b0adc | 1728 | } |
a9307f5a | 1729 | |
eb9ae4b5 | 1730 | static int verify_absent(const struct cache_entry *ce, |
08402b04 | 1731 | enum unpack_trees_error_types error_type, |
35a5aa79 NTND |
1732 | struct unpack_trees_options *o) |
1733 | { | |
2431afbf | 1734 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) |
f1f523ea | 1735 | return 0; |
08402b04 | 1736 | return verify_absent_1(ce, error_type, o); |
35a5aa79 | 1737 | } |
076b0adc | 1738 | |
eb9ae4b5 RS |
1739 | static int verify_absent_sparse(const struct cache_entry *ce, |
1740 | enum unpack_trees_error_types error_type, | |
1741 | struct unpack_trees_options *o) | |
e800ec9d | 1742 | { |
08402b04 MM |
1743 | enum unpack_trees_error_types orphaned_error = error_type; |
1744 | if (orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN) | |
1745 | orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN; | |
1746 | ||
1747 | return verify_absent_1(ce, orphaned_error, o); | |
e800ec9d | 1748 | } |
076b0adc | 1749 | |
f2fa3542 | 1750 | static int merged_entry(const struct cache_entry *ce, |
eb9ae4b5 | 1751 | const struct cache_entry *old, |
f2fa3542 | 1752 | struct unpack_trees_options *o) |
076b0adc | 1753 | { |
7f8ab8dc | 1754 | int update = CE_UPDATE; |
f2fa3542 | 1755 | struct cache_entry *merge = dup_entry(ce); |
7f8ab8dc | 1756 | |
e11d7b59 | 1757 | if (!old) { |
2431afbf NTND |
1758 | /* |
1759 | * New index entries. In sparse checkout, the following | |
1760 | * verify_absent() will be delayed until after | |
1761 | * traverse_trees() finishes in unpack_trees(), then: | |
1762 | * | |
1763 | * - CE_NEW_SKIP_WORKTREE will be computed correctly | |
1764 | * - verify_absent() be called again, this time with | |
1765 | * correct CE_NEW_SKIP_WORKTREE | |
1766 | * | |
1767 | * verify_absent() call here does nothing in sparse | |
1768 | * checkout (i.e. o->skip_sparse_checkout == 0) | |
1769 | */ | |
1770 | update |= CE_ADDED; | |
1771 | merge->ce_flags |= CE_NEW_SKIP_WORKTREE; | |
1772 | ||
f2fa3542 RS |
1773 | if (verify_absent(merge, |
1774 | ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { | |
1775 | free(merge); | |
e11d7b59 | 1776 | return -1; |
f2fa3542 | 1777 | } |
e11d7b59 | 1778 | invalidate_ce_path(merge, o); |
a7bc845a SB |
1779 | |
1780 | if (submodule_from_ce(ce)) { | |
1781 | int ret = check_submodule_move_head(ce, NULL, | |
1782 | oid_to_hex(&ce->oid), | |
1783 | o); | |
1784 | if (ret) | |
1785 | return ret; | |
1786 | } | |
1787 | ||
e11d7b59 | 1788 | } else if (!(old->ce_flags & CE_CONFLICTED)) { |
076b0adc JS |
1789 | /* |
1790 | * See if we can re-use the old CE directly? | |
1791 | * That way we get the uptodate stat info. | |
1792 | * | |
7f8ab8dc LT |
1793 | * This also removes the UPDATE flag on a match; otherwise |
1794 | * we will end up overwriting local changes in the work tree. | |
076b0adc JS |
1795 | */ |
1796 | if (same(old, merge)) { | |
eb7a2f1d | 1797 | copy_cache_entry(merge, old); |
7f8ab8dc | 1798 | update = 0; |
076b0adc | 1799 | } else { |
f2fa3542 RS |
1800 | if (verify_uptodate(old, o)) { |
1801 | free(merge); | |
203a2fe1 | 1802 | return -1; |
f2fa3542 | 1803 | } |
2431afbf NTND |
1804 | /* Migrate old flags over */ |
1805 | update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); | |
bc052d7f | 1806 | invalidate_ce_path(old, o); |
076b0adc | 1807 | } |
a7bc845a SB |
1808 | |
1809 | if (submodule_from_ce(ce)) { | |
1810 | int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid), | |
1811 | oid_to_hex(&ce->oid), | |
1812 | o); | |
1813 | if (ret) | |
1814 | return ret; | |
1815 | } | |
e11d7b59 JH |
1816 | } else { |
1817 | /* | |
1818 | * Previously unmerged entry left as an existence | |
1819 | * marker by read_index_unmerged(); | |
1820 | */ | |
1821 | invalidate_ce_path(old, o); | |
076b0adc JS |
1822 | } |
1823 | ||
f2fa3542 | 1824 | do_add_entry(o, merge, update, CE_STAGEMASK); |
076b0adc JS |
1825 | return 1; |
1826 | } | |
1827 | ||
eb9ae4b5 RS |
1828 | static int deleted_entry(const struct cache_entry *ce, |
1829 | const struct cache_entry *old, | |
1830 | struct unpack_trees_options *o) | |
076b0adc | 1831 | { |
34110cd4 LT |
1832 | /* Did it exist in the index? */ |
1833 | if (!old) { | |
08402b04 | 1834 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) |
203a2fe1 | 1835 | return -1; |
34110cd4 LT |
1836 | return 0; |
1837 | } | |
e11d7b59 | 1838 | if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o)) |
34110cd4 LT |
1839 | return -1; |
1840 | add_entry(o, ce, CE_REMOVE, 0); | |
bc052d7f | 1841 | invalidate_ce_path(ce, o); |
076b0adc JS |
1842 | return 1; |
1843 | } | |
1844 | ||
eb9ae4b5 RS |
1845 | static int keep_entry(const struct cache_entry *ce, |
1846 | struct unpack_trees_options *o) | |
076b0adc | 1847 | { |
34110cd4 | 1848 | add_entry(o, ce, 0, 0); |
076b0adc JS |
1849 | return 1; |
1850 | } | |
1851 | ||
1852 | #if DBRT_DEBUG | |
1853 | static void show_stage_entry(FILE *o, | |
1854 | const char *label, const struct cache_entry *ce) | |
1855 | { | |
1856 | if (!ce) | |
1857 | fprintf(o, "%s (missing)\n", label); | |
1858 | else | |
1859 | fprintf(o, "%s%06o %s %d\t%s\n", | |
1860 | label, | |
7a51ed66 | 1861 | ce->ce_mode, |
99d1a986 | 1862 | oid_to_hex(&ce->oid), |
076b0adc JS |
1863 | ce_stage(ce), |
1864 | ce->name); | |
1865 | } | |
1866 | #endif | |
1867 | ||
5828e835 RS |
1868 | int threeway_merge(const struct cache_entry * const *stages, |
1869 | struct unpack_trees_options *o) | |
076b0adc | 1870 | { |
eb9ae4b5 RS |
1871 | const struct cache_entry *index; |
1872 | const struct cache_entry *head; | |
1873 | const struct cache_entry *remote = stages[o->head_idx + 1]; | |
076b0adc JS |
1874 | int count; |
1875 | int head_match = 0; | |
1876 | int remote_match = 0; | |
076b0adc JS |
1877 | |
1878 | int df_conflict_head = 0; | |
1879 | int df_conflict_remote = 0; | |
1880 | ||
1881 | int any_anc_missing = 0; | |
1882 | int no_anc_exists = 1; | |
1883 | int i; | |
1884 | ||
1885 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 1886 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
076b0adc | 1887 | any_anc_missing = 1; |
4c4caafc | 1888 | else |
076b0adc | 1889 | no_anc_exists = 0; |
076b0adc JS |
1890 | } |
1891 | ||
1892 | index = stages[0]; | |
1893 | head = stages[o->head_idx]; | |
1894 | ||
1895 | if (head == o->df_conflict_entry) { | |
1896 | df_conflict_head = 1; | |
1897 | head = NULL; | |
1898 | } | |
1899 | ||
1900 | if (remote == o->df_conflict_entry) { | |
1901 | df_conflict_remote = 1; | |
1902 | remote = NULL; | |
1903 | } | |
1904 | ||
cee2d6ae JH |
1905 | /* |
1906 | * First, if there's a #16 situation, note that to prevent #13 | |
076b0adc JS |
1907 | * and #14. |
1908 | */ | |
1909 | if (!same(remote, head)) { | |
1910 | for (i = 1; i < o->head_idx; i++) { | |
1911 | if (same(stages[i], head)) { | |
1912 | head_match = i; | |
1913 | } | |
1914 | if (same(stages[i], remote)) { | |
1915 | remote_match = i; | |
1916 | } | |
1917 | } | |
1918 | } | |
1919 | ||
cee2d6ae JH |
1920 | /* |
1921 | * We start with cases where the index is allowed to match | |
076b0adc JS |
1922 | * something other than the head: #14(ALT) and #2ALT, where it |
1923 | * is permitted to match the result instead. | |
1924 | */ | |
1925 | /* #14, #14ALT, #2ALT */ | |
1926 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
1927 | if (index && !same(index, remote) && !same(index, head)) | |
6a143aa2 | 1928 | return reject_merge(index, o); |
076b0adc JS |
1929 | return merged_entry(remote, index, o); |
1930 | } | |
1931 | /* | |
1932 | * If we have an entry in the index cache, then we want to | |
1933 | * make sure that it matches head. | |
1934 | */ | |
4e7c4571 | 1935 | if (index && !same(index, head)) |
6a143aa2 | 1936 | return reject_merge(index, o); |
076b0adc JS |
1937 | |
1938 | if (head) { | |
1939 | /* #5ALT, #15 */ | |
1940 | if (same(head, remote)) | |
1941 | return merged_entry(head, index, o); | |
1942 | /* #13, #3ALT */ | |
1943 | if (!df_conflict_remote && remote_match && !head_match) | |
1944 | return merged_entry(head, index, o); | |
1945 | } | |
1946 | ||
1947 | /* #1 */ | |
34110cd4 | 1948 | if (!head && !remote && any_anc_missing) |
076b0adc JS |
1949 | return 0; |
1950 | ||
cee2d6ae JH |
1951 | /* |
1952 | * Under the "aggressive" rule, we resolve mostly trivial | |
076b0adc JS |
1953 | * cases that we historically had git-merge-one-file resolve. |
1954 | */ | |
1955 | if (o->aggressive) { | |
cee2d6ae JH |
1956 | int head_deleted = !head; |
1957 | int remote_deleted = !remote; | |
eb9ae4b5 | 1958 | const struct cache_entry *ce = NULL; |
4c4caafc JH |
1959 | |
1960 | if (index) | |
0cf73755 | 1961 | ce = index; |
4c4caafc | 1962 | else if (head) |
0cf73755 | 1963 | ce = head; |
4c4caafc | 1964 | else if (remote) |
0cf73755 | 1965 | ce = remote; |
4c4caafc JH |
1966 | else { |
1967 | for (i = 1; i < o->head_idx; i++) { | |
1968 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
0cf73755 | 1969 | ce = stages[i]; |
4c4caafc JH |
1970 | break; |
1971 | } | |
1972 | } | |
1973 | } | |
1974 | ||
076b0adc JS |
1975 | /* |
1976 | * Deleted in both. | |
1977 | * Deleted in one and unchanged in the other. | |
1978 | */ | |
1979 | if ((head_deleted && remote_deleted) || | |
1980 | (head_deleted && remote && remote_match) || | |
1981 | (remote_deleted && head && head_match)) { | |
1982 | if (index) | |
1983 | return deleted_entry(index, index, o); | |
34110cd4 | 1984 | if (ce && !head_deleted) { |
08402b04 | 1985 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) |
203a2fe1 DB |
1986 | return -1; |
1987 | } | |
076b0adc JS |
1988 | return 0; |
1989 | } | |
1990 | /* | |
1991 | * Added in both, identically. | |
1992 | */ | |
1993 | if (no_anc_exists && head && remote && same(head, remote)) | |
1994 | return merged_entry(head, index, o); | |
1995 | ||
1996 | } | |
1997 | ||
1998 | /* Below are "no merge" cases, which require that the index be | |
1999 | * up-to-date to avoid the files getting overwritten with | |
2000 | * conflict resolution files. | |
2001 | */ | |
2002 | if (index) { | |
203a2fe1 DB |
2003 | if (verify_uptodate(index, o)) |
2004 | return -1; | |
076b0adc | 2005 | } |
076b0adc JS |
2006 | |
2007 | o->nontrivial_merge = 1; | |
2008 | ||
ea4b52a8 | 2009 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
076b0adc JS |
2010 | count = 0; |
2011 | if (!head_match || !remote_match) { | |
2012 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 2013 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
7f7932ab | 2014 | keep_entry(stages[i], o); |
076b0adc JS |
2015 | count++; |
2016 | break; | |
2017 | } | |
2018 | } | |
2019 | } | |
2020 | #if DBRT_DEBUG | |
2021 | else { | |
2022 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
2023 | show_stage_entry(stderr, "head ", stages[head_match]); | |
2024 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
2025 | } | |
2026 | #endif | |
7f7932ab JH |
2027 | if (head) { count += keep_entry(head, o); } |
2028 | if (remote) { count += keep_entry(remote, o); } | |
076b0adc JS |
2029 | return count; |
2030 | } | |
2031 | ||
2032 | /* | |
2033 | * Two-way merge. | |
2034 | * | |
2035 | * The rule is to "carry forward" what is in the index without losing | |
a75d7b54 | 2036 | * information across a "fast-forward", favoring a successful merge |
076b0adc JS |
2037 | * over a merge failure when it makes sense. For details of the |
2038 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
2039 | * | |
2040 | */ | |
5828e835 RS |
2041 | int twoway_merge(const struct cache_entry * const *src, |
2042 | struct unpack_trees_options *o) | |
076b0adc | 2043 | { |
eb9ae4b5 RS |
2044 | const struct cache_entry *current = src[0]; |
2045 | const struct cache_entry *oldtree = src[1]; | |
2046 | const struct cache_entry *newtree = src[2]; | |
076b0adc JS |
2047 | |
2048 | if (o->merge_size != 2) | |
2049 | return error("Cannot do a twoway merge of %d trees", | |
2050 | o->merge_size); | |
2051 | ||
b8ba1535 JH |
2052 | if (oldtree == o->df_conflict_entry) |
2053 | oldtree = NULL; | |
2054 | if (newtree == o->df_conflict_entry) | |
2055 | newtree = NULL; | |
2056 | ||
076b0adc | 2057 | if (current) { |
b018ff60 JK |
2058 | if (current->ce_flags & CE_CONFLICTED) { |
2059 | if (same(oldtree, newtree) || o->reset) { | |
2060 | if (!newtree) | |
2061 | return deleted_entry(current, current, o); | |
2062 | else | |
2063 | return merged_entry(newtree, current, o); | |
2064 | } | |
6a143aa2 | 2065 | return reject_merge(current, o); |
6c1db1b3 | 2066 | } else if ((!oldtree && !newtree) || /* 4 and 5 */ |
b018ff60 JK |
2067 | (!oldtree && newtree && |
2068 | same(current, newtree)) || /* 6 and 7 */ | |
2069 | (oldtree && newtree && | |
2070 | same(oldtree, newtree)) || /* 14 and 15 */ | |
2071 | (oldtree && newtree && | |
2072 | !same(oldtree, newtree) && /* 18 and 19 */ | |
2073 | same(current, newtree))) { | |
7f7932ab | 2074 | return keep_entry(current, o); |
6c1db1b3 | 2075 | } else if (oldtree && !newtree && same(current, oldtree)) { |
076b0adc JS |
2076 | /* 10 or 11 */ |
2077 | return deleted_entry(oldtree, current, o); | |
6c1db1b3 | 2078 | } else if (oldtree && newtree && |
076b0adc JS |
2079 | same(current, oldtree) && !same(current, newtree)) { |
2080 | /* 20 or 21 */ | |
2081 | return merged_entry(newtree, current, o); | |
6c1db1b3 | 2082 | } else |
6a143aa2 | 2083 | return reject_merge(current, o); |
076b0adc | 2084 | } |
55218834 JH |
2085 | else if (newtree) { |
2086 | if (oldtree && !o->initial_checkout) { | |
2087 | /* | |
2088 | * deletion of the path was staged; | |
2089 | */ | |
2090 | if (same(oldtree, newtree)) | |
2091 | return 1; | |
2092 | return reject_merge(oldtree, o); | |
2093 | } | |
076b0adc | 2094 | return merged_entry(newtree, current, o); |
55218834 | 2095 | } |
d699676d | 2096 | return deleted_entry(oldtree, current, o); |
076b0adc JS |
2097 | } |
2098 | ||
2099 | /* | |
2100 | * Bind merge. | |
2101 | * | |
2102 | * Keep the index entries at stage0, collapse stage1 but make sure | |
2103 | * stage0 does not have anything there. | |
2104 | */ | |
5828e835 RS |
2105 | int bind_merge(const struct cache_entry * const *src, |
2106 | struct unpack_trees_options *o) | |
076b0adc | 2107 | { |
eb9ae4b5 RS |
2108 | const struct cache_entry *old = src[0]; |
2109 | const struct cache_entry *a = src[1]; | |
076b0adc JS |
2110 | |
2111 | if (o->merge_size != 1) | |
82247e9b | 2112 | return error("Cannot do a bind merge of %d trees", |
076b0adc JS |
2113 | o->merge_size); |
2114 | if (a && old) | |
17e46426 | 2115 | return o->gently ? -1 : |
3d415425 SB |
2116 | error(ERRORMSG(o, ERROR_BIND_OVERLAP), |
2117 | super_prefixed(a->name), | |
2118 | super_prefixed(old->name)); | |
076b0adc | 2119 | if (!a) |
7f7932ab | 2120 | return keep_entry(old, o); |
076b0adc JS |
2121 | else |
2122 | return merged_entry(a, NULL, o); | |
2123 | } | |
2124 | ||
2125 | /* | |
2126 | * One-way merge. | |
2127 | * | |
2128 | * The rule is: | |
2129 | * - take the stat information from stage0, take the data from stage1 | |
2130 | */ | |
5828e835 RS |
2131 | int oneway_merge(const struct cache_entry * const *src, |
2132 | struct unpack_trees_options *o) | |
076b0adc | 2133 | { |
eb9ae4b5 RS |
2134 | const struct cache_entry *old = src[0]; |
2135 | const struct cache_entry *a = src[1]; | |
076b0adc JS |
2136 | |
2137 | if (o->merge_size != 1) | |
2138 | return error("Cannot do a oneway merge of %d trees", | |
2139 | o->merge_size); | |
2140 | ||
78d3b06e | 2141 | if (!a || a == o->df_conflict_entry) |
076b0adc | 2142 | return deleted_entry(old, old, o); |
34110cd4 | 2143 | |
076b0adc | 2144 | if (old && same(old, a)) { |
34110cd4 | 2145 | int update = 0; |
686b2de0 | 2146 | if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) { |
076b0adc JS |
2147 | struct stat st; |
2148 | if (lstat(old->name, &st) || | |
56cac48c | 2149 | ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE)) |
34110cd4 | 2150 | update |= CE_UPDATE; |
076b0adc | 2151 | } |
34110cd4 LT |
2152 | add_entry(o, old, update, 0); |
2153 | return 0; | |
076b0adc JS |
2154 | } |
2155 | return merged_entry(a, old, o); | |
2156 | } |