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