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