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" |
16da134b | 11 | |
8ccba008 JH |
12 | /* |
13 | * Error messages expected by scripts out of plumbing commands such as | |
14 | * read-tree. Non-scripted Porcelain is not required to use these messages | |
15 | * and in fact are encouraged to reword them to better suit their particular | |
16 | * situation better. See how "git checkout" replaces not_uptodate_file to | |
17 | * explain why it does not allow switching between branches when you have | |
18 | * local changes, for example. | |
19 | */ | |
20 | static struct unpack_trees_error_msgs unpack_plumbing_errors = { | |
21 | /* would_overwrite */ | |
22 | "Entry '%s' would be overwritten by merge. Cannot merge.", | |
23 | ||
24 | /* not_uptodate_file */ | |
25 | "Entry '%s' not uptodate. Cannot merge.", | |
26 | ||
27 | /* not_uptodate_dir */ | |
28 | "Updating '%s' would lose untracked files in it", | |
29 | ||
30 | /* would_lose_untracked */ | |
31 | "Untracked working tree file '%s' would be %s by merge.", | |
32 | ||
33 | /* bind_overlap */ | |
34 | "Entry '%s' overlaps with '%s'. Cannot bind.", | |
35 | }; | |
36 | ||
37 | #define ERRORMSG(o,fld) \ | |
38 | ( ((o) && (o)->msgs.fld) \ | |
39 | ? ((o)->msgs.fld) \ | |
40 | : (unpack_plumbing_errors.fld) ) | |
41 | ||
34110cd4 LT |
42 | static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce, |
43 | unsigned int set, unsigned int clear) | |
b48d5a05 | 44 | { |
34110cd4 LT |
45 | unsigned int size = ce_size(ce); |
46 | struct cache_entry *new = xmalloc(size); | |
47 | ||
48 | clear |= CE_HASHED | CE_UNHASHED; | |
49 | ||
50 | memcpy(new, ce, size); | |
51 | new->next = NULL; | |
52 | new->ce_flags = (new->ce_flags & ~clear) | set; | |
aab3b9a1 | 53 | add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
b48d5a05 LT |
54 | } |
55 | ||
78478927 KB |
56 | /* |
57 | * Unlink the last component and schedule the leading directories for | |
58 | * removal, such that empty directories get removed. | |
16da134b | 59 | */ |
c40641b7 | 60 | static void unlink_entry(struct cache_entry *ce) |
16da134b | 61 | { |
57199892 | 62 | if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce))) |
16a4c617 | 63 | return; |
691f1a28 | 64 | if (unlink_or_warn(ce->name)) |
16da134b | 65 | return; |
78478927 | 66 | schedule_dir_for_removal(ce->name, ce_namelen(ce)); |
16da134b JS |
67 | } |
68 | ||
16da134b | 69 | static struct checkout state; |
c4758d3c | 70 | static int check_updates(struct unpack_trees_options *o) |
16da134b | 71 | { |
96a02f8f | 72 | unsigned cnt = 0, total = 0; |
dc6a0757 | 73 | struct progress *progress = NULL; |
34110cd4 | 74 | struct index_state *index = &o->result; |
33ecf7eb | 75 | int i; |
c4758d3c | 76 | int errs = 0; |
16da134b JS |
77 | |
78 | if (o->update && o->verbose_update) { | |
34110cd4 LT |
79 | for (total = cnt = 0; cnt < index->cache_nr; cnt++) { |
80 | struct cache_entry *ce = index->cache[cnt]; | |
e663db2f | 81 | if (ce->ce_flags & (CE_UPDATE | CE_REMOVE | CE_WT_REMOVE)) |
16da134b JS |
82 | total++; |
83 | } | |
84 | ||
dc6a0757 | 85 | progress = start_progress_delay("Checking out files", |
e8548645 | 86 | total, 50, 1); |
16da134b JS |
87 | cnt = 0; |
88 | } | |
89 | ||
66985e66 JH |
90 | if (o->update) |
91 | git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result); | |
34110cd4 LT |
92 | for (i = 0; i < index->cache_nr; i++) { |
93 | struct cache_entry *ce = index->cache[i]; | |
16da134b | 94 | |
e663db2f NTND |
95 | if (ce->ce_flags & CE_WT_REMOVE) { |
96 | display_progress(progress, ++cnt); | |
97 | if (o->update) | |
98 | unlink_entry(ce); | |
99 | continue; | |
100 | } | |
101 | ||
7a51ed66 | 102 | if (ce->ce_flags & CE_REMOVE) { |
1fa6ead4 | 103 | display_progress(progress, ++cnt); |
16da134b | 104 | if (o->update) |
c40641b7 | 105 | unlink_entry(ce); |
16da134b | 106 | } |
1fa6ead4 | 107 | } |
36419c8e | 108 | remove_marked_cache_entries(&o->result); |
78478927 | 109 | remove_scheduled_dirs(); |
1fa6ead4 LT |
110 | |
111 | for (i = 0; i < index->cache_nr; i++) { | |
112 | struct cache_entry *ce = index->cache[i]; | |
113 | ||
7a51ed66 | 114 | if (ce->ce_flags & CE_UPDATE) { |
1fa6ead4 | 115 | display_progress(progress, ++cnt); |
7a51ed66 | 116 | ce->ce_flags &= ~CE_UPDATE; |
16a4c617 | 117 | if (o->update) { |
c4758d3c | 118 | errs |= checkout_entry(ce, &state, NULL); |
16a4c617 | 119 | } |
16da134b JS |
120 | } |
121 | } | |
4d4fcc54 | 122 | stop_progress(&progress); |
66985e66 JH |
123 | if (o->update) |
124 | git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); | |
c4758d3c | 125 | return errs != 0; |
16da134b JS |
126 | } |
127 | ||
34110cd4 | 128 | static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o) |
01904572 | 129 | { |
34110cd4 LT |
130 | int ret = o->fn(src, o); |
131 | if (ret > 0) | |
01904572 | 132 | ret = 0; |
01904572 LT |
133 | return ret; |
134 | } | |
135 | ||
136 | static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o) | |
137 | { | |
0039ba7e | 138 | struct cache_entry *src[5] = { ce, NULL, }; |
34110cd4 LT |
139 | |
140 | o->pos++; | |
01904572 LT |
141 | if (ce_stage(ce)) { |
142 | if (o->skip_unmerged) { | |
34110cd4 LT |
143 | add_entry(o, ce, 0, 0); |
144 | return 0; | |
01904572 | 145 | } |
01904572 | 146 | } |
34110cd4 | 147 | return call_unpack_fn(src, o); |
01904572 LT |
148 | } |
149 | ||
2af202be | 150 | static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info) |
16da134b | 151 | { |
16da134b | 152 | int i; |
ca885a4f | 153 | struct tree_desc t[MAX_UNPACK_TREES]; |
01904572 LT |
154 | struct traverse_info newinfo; |
155 | struct name_entry *p; | |
156 | ||
157 | p = names; | |
158 | while (!p->mode) | |
159 | p++; | |
160 | ||
161 | newinfo = *info; | |
162 | newinfo.prev = info; | |
163 | newinfo.name = *p; | |
164 | newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1; | |
165 | newinfo.conflicts |= df_conflicts; | |
166 | ||
167 | for (i = 0; i < n; i++, dirmask >>= 1) { | |
168 | const unsigned char *sha1 = NULL; | |
169 | if (dirmask & 1) | |
170 | sha1 = names[i].sha1; | |
171 | fill_tree_descriptor(t+i, sha1); | |
172 | } | |
542c264b | 173 | return traverse_trees(n, t, &newinfo); |
01904572 LT |
174 | } |
175 | ||
176 | /* | |
177 | * Compare the traverse-path to the cache entry without actually | |
178 | * having to generate the textual representation of the traverse | |
179 | * path. | |
180 | * | |
181 | * NOTE! This *only* compares up to the size of the traverse path | |
182 | * itself - the caller needs to do the final check for the cache | |
183 | * entry having more data at the end! | |
184 | */ | |
185 | static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) | |
186 | { | |
187 | int len, pathlen, ce_len; | |
188 | const char *ce_name; | |
189 | ||
190 | if (info->prev) { | |
191 | int cmp = do_compare_entry(ce, info->prev, &info->name); | |
192 | if (cmp) | |
193 | return cmp; | |
194 | } | |
195 | pathlen = info->pathlen; | |
196 | ce_len = ce_namelen(ce); | |
197 | ||
198 | /* If ce_len < pathlen then we must have previously hit "name == directory" entry */ | |
199 | if (ce_len < pathlen) | |
200 | return -1; | |
201 | ||
202 | ce_len -= pathlen; | |
203 | ce_name = ce->name + pathlen; | |
204 | ||
205 | len = tree_entry_len(n->path, n->sha1); | |
206 | return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); | |
207 | } | |
208 | ||
209 | static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) | |
210 | { | |
211 | int cmp = do_compare_entry(ce, info, n); | |
212 | if (cmp) | |
213 | return cmp; | |
214 | ||
215 | /* | |
216 | * Even if the beginning compared identically, the ce should | |
217 | * compare as bigger than a directory leading up to it! | |
218 | */ | |
219 | return ce_namelen(ce) > traverse_path_len(info, n); | |
220 | } | |
221 | ||
222 | static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage) | |
223 | { | |
224 | int len = traverse_path_len(info, n); | |
225 | struct cache_entry *ce = xcalloc(1, cache_entry_size(len)); | |
226 | ||
227 | ce->ce_mode = create_ce_mode(n->mode); | |
228 | ce->ce_flags = create_ce_flags(len, stage); | |
229 | hashcpy(ce->sha1, n->sha1); | |
230 | make_traverse_path(ce->name, info, n); | |
231 | ||
232 | return ce; | |
233 | } | |
234 | ||
c7cddc1a RS |
235 | static int unpack_nondirectories(int n, unsigned long mask, |
236 | unsigned long dirmask, | |
237 | struct cache_entry **src, | |
238 | const struct name_entry *names, | |
239 | const struct traverse_info *info) | |
01904572 LT |
240 | { |
241 | int i; | |
242 | struct unpack_trees_options *o = info->data; | |
243 | unsigned long conflicts; | |
244 | ||
245 | /* Do we have *only* directories? Nothing to do */ | |
246 | if (mask == dirmask && !src[0]) | |
247 | return 0; | |
248 | ||
249 | conflicts = info->conflicts; | |
250 | if (o->merge) | |
251 | conflicts >>= 1; | |
252 | conflicts |= dirmask; | |
253 | ||
254 | /* | |
255 | * Ok, we've filled in up to any potential index entry in src[0], | |
256 | * now do the rest. | |
257 | */ | |
258 | for (i = 0; i < n; i++) { | |
259 | int stage; | |
260 | unsigned int bit = 1ul << i; | |
261 | if (conflicts & bit) { | |
262 | src[i + o->merge] = o->df_conflict_entry; | |
263 | continue; | |
264 | } | |
265 | if (!(mask & bit)) | |
266 | continue; | |
267 | if (!o->merge) | |
268 | stage = 0; | |
269 | else if (i + 1 < o->head_idx) | |
270 | stage = 1; | |
271 | else if (i + 1 > o->head_idx) | |
272 | stage = 3; | |
273 | else | |
274 | stage = 2; | |
275 | src[i + o->merge] = create_ce_entry(info, names + i, stage); | |
276 | } | |
277 | ||
278 | if (o->merge) | |
34110cd4 | 279 | return call_unpack_fn(src, o); |
01904572 | 280 | |
01904572 | 281 | for (i = 0; i < n; i++) |
aab3b9a1 JH |
282 | if (src[i] && src[i] != o->df_conflict_entry) |
283 | add_entry(o, src[i], 0, 0); | |
01904572 LT |
284 | return 0; |
285 | } | |
286 | ||
287 | static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) | |
288 | { | |
c7cddc1a | 289 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
01904572 | 290 | struct unpack_trees_options *o = info->data; |
01904572 LT |
291 | const struct name_entry *p = names; |
292 | ||
293 | /* Find first entry with a real name (we could use "mask" too) */ | |
294 | while (!p->mode) | |
295 | p++; | |
296 | ||
297 | /* Are we supposed to look at the index too? */ | |
298 | if (o->merge) { | |
34110cd4 LT |
299 | while (o->pos < o->src_index->cache_nr) { |
300 | struct cache_entry *ce = o->src_index->cache[o->pos]; | |
01904572 LT |
301 | int cmp = compare_entry(ce, info, p); |
302 | if (cmp < 0) { | |
303 | if (unpack_index_entry(ce, o) < 0) | |
304 | return -1; | |
305 | continue; | |
306 | } | |
307 | if (!cmp) { | |
34110cd4 | 308 | o->pos++; |
01904572 LT |
309 | if (ce_stage(ce)) { |
310 | /* | |
311 | * If we skip unmerged index entries, we'll skip this | |
312 | * entry *and* the tree entries associated with it! | |
313 | */ | |
34110cd4 LT |
314 | if (o->skip_unmerged) { |
315 | add_entry(o, ce, 0, 0); | |
01904572 | 316 | return mask; |
34110cd4 | 317 | } |
01904572 LT |
318 | } |
319 | src[0] = ce; | |
01904572 LT |
320 | } |
321 | break; | |
322 | } | |
323 | } | |
324 | ||
34110cd4 | 325 | if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0) |
01904572 LT |
326 | return -1; |
327 | ||
328 | /* Now handle any directories.. */ | |
329 | if (dirmask) { | |
330 | unsigned long conflicts = mask & ~dirmask; | |
331 | if (o->merge) { | |
332 | conflicts <<= 1; | |
333 | if (src[0]) | |
334 | conflicts |= 1; | |
335 | } | |
b65982b6 JH |
336 | |
337 | /* special case: "diff-index --cached" looking at a tree */ | |
338 | if (o->diff_index_cached && | |
339 | n == 1 && dirmask == 1 && S_ISDIR(names->mode)) { | |
340 | int matches; | |
341 | matches = cache_tree_matches_traversal(o->src_index->cache_tree, | |
342 | names, info); | |
343 | /* | |
344 | * Everything under the name matches. Adjust o->pos to | |
345 | * skip the entire hierarchy. | |
346 | */ | |
347 | if (matches) { | |
348 | o->pos += matches; | |
349 | return mask; | |
350 | } | |
351 | } | |
352 | ||
542c264b JH |
353 | if (traverse_trees_recursive(n, dirmask, conflicts, |
354 | names, info) < 0) | |
355 | return -1; | |
01904572 LT |
356 | return mask; |
357 | } | |
358 | ||
359 | return mask; | |
360 | } | |
361 | ||
362 | static int unpack_failed(struct unpack_trees_options *o, const char *message) | |
363 | { | |
1caeacc1 | 364 | discard_index(&o->result); |
01904572 LT |
365 | if (!o->gently) { |
366 | if (message) | |
9db56f71 | 367 | return error("%s", message); |
01904572 LT |
368 | return -1; |
369 | } | |
01904572 LT |
370 | return -1; |
371 | } | |
372 | ||
2e2b887d JH |
373 | /* |
374 | * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the | |
375 | * resulting index, -2 on failure to reflect the changes to the work tree. | |
376 | */ | |
01904572 LT |
377 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) |
378 | { | |
2e2b887d | 379 | int ret; |
0fb1eaa8 | 380 | static struct cache_entry *dfc; |
08aefc9e | 381 | struct exclude_list el; |
16da134b | 382 | |
ca885a4f JH |
383 | if (len > MAX_UNPACK_TREES) |
384 | die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES); | |
076b0adc | 385 | memset(&state, 0, sizeof(state)); |
16da134b JS |
386 | state.base_dir = ""; |
387 | state.force = 1; | |
388 | state.quiet = 1; | |
389 | state.refresh_cache = 1; | |
390 | ||
08aefc9e NTND |
391 | memset(&el, 0, sizeof(el)); |
392 | if (!core_apply_sparse_checkout || !o->update) | |
393 | o->skip_sparse_checkout = 1; | |
394 | if (!o->skip_sparse_checkout) { | |
395 | if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0) | |
396 | o->skip_sparse_checkout = 1; | |
397 | else | |
398 | o->el = ⪙ | |
399 | } | |
400 | ||
34110cd4 | 401 | memset(&o->result, 0, sizeof(o->result)); |
913e0e99 | 402 | o->result.initialized = 1; |
fba2f38a KB |
403 | if (o->src_index) { |
404 | o->result.timestamp.sec = o->src_index->timestamp.sec; | |
fba2f38a | 405 | o->result.timestamp.nsec = o->src_index->timestamp.nsec; |
fba2f38a | 406 | } |
16da134b | 407 | o->merge_size = len; |
0fb1eaa8 JH |
408 | |
409 | if (!dfc) | |
13494ed1 | 410 | dfc = xcalloc(1, cache_entry_size(0)); |
0fb1eaa8 | 411 | o->df_conflict_entry = dfc; |
16da134b JS |
412 | |
413 | if (len) { | |
01904572 LT |
414 | const char *prefix = o->prefix ? o->prefix : ""; |
415 | struct traverse_info info; | |
416 | ||
417 | setup_traverse_info(&info, prefix); | |
418 | info.fn = unpack_callback; | |
419 | info.data = o; | |
420 | ||
08aefc9e NTND |
421 | if (traverse_trees(len, t, &info) < 0) { |
422 | ret = unpack_failed(o, NULL); | |
423 | goto done; | |
424 | } | |
16da134b JS |
425 | } |
426 | ||
01904572 LT |
427 | /* Any left-over entries in the index? */ |
428 | if (o->merge) { | |
34110cd4 LT |
429 | while (o->pos < o->src_index->cache_nr) { |
430 | struct cache_entry *ce = o->src_index->cache[o->pos]; | |
08aefc9e NTND |
431 | if (unpack_index_entry(ce, o) < 0) { |
432 | ret = unpack_failed(o, NULL); | |
433 | goto done; | |
434 | } | |
17e46426 | 435 | } |
17e46426 | 436 | } |
16da134b | 437 | |
08aefc9e NTND |
438 | if (o->trivial_merges_only && o->nontrivial_merge) { |
439 | ret = unpack_failed(o, "Merge requires file-level merging"); | |
440 | goto done; | |
441 | } | |
01904572 | 442 | |
34110cd4 | 443 | o->src_index = NULL; |
2e2b887d | 444 | ret = check_updates(o) ? (-2) : 0; |
34110cd4 LT |
445 | if (o->dst_index) |
446 | *o->dst_index = o->result; | |
08aefc9e NTND |
447 | |
448 | done: | |
449 | for (i = 0;i < el.nr;i++) | |
450 | free(el.excludes[i]); | |
451 | if (el.excludes) | |
452 | free(el.excludes); | |
453 | ||
2e2b887d | 454 | return ret; |
16da134b | 455 | } |
076b0adc JS |
456 | |
457 | /* Here come the merge functions */ | |
458 | ||
8ccba008 | 459 | static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc | 460 | { |
8ccba008 | 461 | return error(ERRORMSG(o, would_overwrite), ce->name); |
076b0adc JS |
462 | } |
463 | ||
464 | static int same(struct cache_entry *a, struct cache_entry *b) | |
465 | { | |
466 | if (!!a != !!b) | |
467 | return 0; | |
468 | if (!a && !b) | |
469 | return 1; | |
470 | return a->ce_mode == b->ce_mode && | |
a89fccd2 | 471 | !hashcmp(a->sha1, b->sha1); |
076b0adc JS |
472 | } |
473 | ||
474 | ||
475 | /* | |
476 | * When a CE gets turned into an unmerged entry, we | |
477 | * want it to be up-to-date | |
478 | */ | |
35a5aa79 NTND |
479 | static int verify_uptodate_1(struct cache_entry *ce, |
480 | struct unpack_trees_options *o, | |
481 | const char *error_msg) | |
076b0adc JS |
482 | { |
483 | struct stat st; | |
484 | ||
52030836 | 485 | if (o->index_only || (!ce_skip_worktree(ce) && (o->reset || ce_uptodate(ce)))) |
203a2fe1 | 486 | return 0; |
076b0adc JS |
487 | |
488 | if (!lstat(ce->name, &st)) { | |
34110cd4 | 489 | unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID); |
076b0adc | 490 | if (!changed) |
203a2fe1 | 491 | return 0; |
936492d3 JH |
492 | /* |
493 | * NEEDSWORK: the current default policy is to allow | |
494 | * submodule to be out of sync wrt the supermodule | |
495 | * index. This needs to be tightened later for | |
496 | * submodules that are marked to be automatically | |
497 | * checked out. | |
498 | */ | |
7a51ed66 | 499 | if (S_ISGITLINK(ce->ce_mode)) |
203a2fe1 | 500 | return 0; |
076b0adc JS |
501 | errno = 0; |
502 | } | |
076b0adc | 503 | if (errno == ENOENT) |
203a2fe1 | 504 | return 0; |
17e46426 | 505 | return o->gently ? -1 : |
35a5aa79 NTND |
506 | error(error_msg, ce->name); |
507 | } | |
508 | ||
509 | static int verify_uptodate(struct cache_entry *ce, | |
510 | struct unpack_trees_options *o) | |
511 | { | |
512 | return verify_uptodate_1(ce, o, ERRORMSG(o, not_uptodate_file)); | |
076b0adc JS |
513 | } |
514 | ||
bc052d7f | 515 | static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc JS |
516 | { |
517 | if (ce) | |
34110cd4 | 518 | cache_tree_invalidate_path(o->src_index->cache_tree, ce->name); |
076b0adc JS |
519 | } |
520 | ||
0cf73755 SV |
521 | /* |
522 | * Check that checking out ce->sha1 in subdir ce->name is not | |
523 | * going to overwrite any working files. | |
524 | * | |
525 | * Currently, git does not checkout subprojects during a superproject | |
526 | * checkout, so it is not going to overwrite anything. | |
527 | */ | |
528 | static int verify_clean_submodule(struct cache_entry *ce, const char *action, | |
529 | struct unpack_trees_options *o) | |
530 | { | |
531 | return 0; | |
532 | } | |
533 | ||
534 | static int verify_clean_subdirectory(struct cache_entry *ce, const char *action, | |
c8193534 JH |
535 | struct unpack_trees_options *o) |
536 | { | |
537 | /* | |
0cf73755 | 538 | * we are about to extract "ce->name"; we would not want to lose |
c8193534 JH |
539 | * anything in the existing directory there. |
540 | */ | |
541 | int namelen; | |
7b9e3ce0 | 542 | int i; |
c8193534 JH |
543 | struct dir_struct d; |
544 | char *pathbuf; | |
545 | int cnt = 0; | |
0cf73755 SV |
546 | unsigned char sha1[20]; |
547 | ||
7a51ed66 | 548 | if (S_ISGITLINK(ce->ce_mode) && |
0cf73755 SV |
549 | resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) { |
550 | /* If we are not going to update the submodule, then | |
551 | * we don't care. | |
552 | */ | |
553 | if (!hashcmp(sha1, ce->sha1)) | |
554 | return 0; | |
555 | return verify_clean_submodule(ce, action, o); | |
556 | } | |
c8193534 JH |
557 | |
558 | /* | |
559 | * First let's make sure we do not have a local modification | |
560 | * in that directory. | |
561 | */ | |
0cf73755 | 562 | namelen = strlen(ce->name); |
7b9e3ce0 | 563 | for (i = o->pos; i < o->src_index->cache_nr; i++) { |
837e5fe9 CB |
564 | struct cache_entry *ce2 = o->src_index->cache[i]; |
565 | int len = ce_namelen(ce2); | |
c8193534 | 566 | if (len < namelen || |
837e5fe9 CB |
567 | strncmp(ce->name, ce2->name, namelen) || |
568 | ce2->name[namelen] != '/') | |
c8193534 JH |
569 | break; |
570 | /* | |
837e5fe9 | 571 | * ce2->name is an entry in the subdirectory. |
c8193534 | 572 | */ |
837e5fe9 CB |
573 | if (!ce_stage(ce2)) { |
574 | if (verify_uptodate(ce2, o)) | |
203a2fe1 | 575 | return -1; |
837e5fe9 | 576 | add_entry(o, ce2, CE_REMOVE, 0); |
c8193534 JH |
577 | } |
578 | cnt++; | |
579 | } | |
580 | ||
581 | /* | |
582 | * Then we need to make sure that we do not lose a locally | |
583 | * present file that is not ignored. | |
584 | */ | |
585 | pathbuf = xmalloc(namelen + 2); | |
0cf73755 | 586 | memcpy(pathbuf, ce->name, namelen); |
c8193534 JH |
587 | strcpy(pathbuf+namelen, "/"); |
588 | ||
589 | memset(&d, 0, sizeof(d)); | |
590 | if (o->dir) | |
591 | d.exclude_per_dir = o->dir->exclude_per_dir; | |
dba2e203 | 592 | i = read_directory(&d, pathbuf, namelen+1, NULL); |
c8193534 | 593 | if (i) |
17e46426 | 594 | return o->gently ? -1 : |
8ccba008 | 595 | error(ERRORMSG(o, not_uptodate_dir), ce->name); |
c8193534 JH |
596 | free(pathbuf); |
597 | return cnt; | |
598 | } | |
599 | ||
32260ad5 LT |
600 | /* |
601 | * This gets called when there was no index entry for the tree entry 'dst', | |
602 | * but we found a file in the working tree that 'lstat()' said was fine, | |
603 | * and we're on a case-insensitive filesystem. | |
604 | * | |
605 | * See if we can find a case-insensitive match in the index that also | |
606 | * matches the stat information, and assume it's that other file! | |
607 | */ | |
608 | static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st) | |
609 | { | |
610 | struct cache_entry *src; | |
611 | ||
612 | src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1); | |
613 | return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID); | |
614 | } | |
615 | ||
076b0adc JS |
616 | /* |
617 | * We do not want to remove or overwrite a working tree file that | |
f8a9d428 | 618 | * is not tracked, unless it is ignored. |
076b0adc | 619 | */ |
35a5aa79 NTND |
620 | static int verify_absent_1(struct cache_entry *ce, const char *action, |
621 | struct unpack_trees_options *o, | |
622 | const char *error_msg) | |
076b0adc JS |
623 | { |
624 | struct stat st; | |
625 | ||
626 | if (o->index_only || o->reset || !o->update) | |
203a2fe1 | 627 | return 0; |
c8193534 | 628 | |
57199892 | 629 | if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce))) |
203a2fe1 | 630 | return 0; |
ec0603e1 | 631 | |
0cf73755 | 632 | if (!lstat(ce->name, &st)) { |
6b9315d5 | 633 | int ret; |
6831a88a | 634 | int dtype = ce_to_dtype(ce); |
df292c79 | 635 | struct cache_entry *result; |
c8193534 | 636 | |
32260ad5 LT |
637 | /* |
638 | * It may be that the 'lstat()' succeeded even though | |
639 | * target 'ce' was absent, because there is an old | |
640 | * entry that is different only in case.. | |
641 | * | |
642 | * Ignore that lstat() if it matches. | |
643 | */ | |
644 | if (ignore_case && icase_exists(o, ce, &st)) | |
645 | return 0; | |
646 | ||
6831a88a | 647 | if (o->dir && excluded(o->dir, ce->name, &dtype)) |
c8193534 | 648 | /* |
0cf73755 | 649 | * ce->name is explicitly excluded, so it is Ok to |
c8193534 JH |
650 | * overwrite it. |
651 | */ | |
203a2fe1 | 652 | return 0; |
c8193534 JH |
653 | if (S_ISDIR(st.st_mode)) { |
654 | /* | |
655 | * We are checking out path "foo" and | |
656 | * found "foo/." in the working tree. | |
657 | * This is tricky -- if we have modified | |
658 | * files that are in "foo/" we would lose | |
659 | * it. | |
660 | */ | |
6b9315d5 CB |
661 | ret = verify_clean_subdirectory(ce, action, o); |
662 | if (ret < 0) | |
663 | return ret; | |
c8193534 JH |
664 | |
665 | /* | |
666 | * If this removed entries from the index, | |
667 | * what that means is: | |
668 | * | |
837e5fe9 | 669 | * (1) the caller unpack_callback() saw path/foo |
c8193534 JH |
670 | * in the index, and it has not removed it because |
671 | * it thinks it is handling 'path' as blob with | |
672 | * D/F conflict; | |
673 | * (2) we will return "ok, we placed a merged entry | |
674 | * in the index" which would cause o->pos to be | |
675 | * incremented by one; | |
676 | * (3) however, original o->pos now has 'path/foo' | |
677 | * marked with "to be removed". | |
678 | * | |
679 | * We need to increment it by the number of | |
680 | * deleted entries here. | |
681 | */ | |
6b9315d5 | 682 | o->pos += ret; |
203a2fe1 | 683 | return 0; |
c8193534 JH |
684 | } |
685 | ||
686 | /* | |
687 | * The previous round may already have decided to | |
688 | * delete this path, which is in a subdirectory that | |
689 | * is being replaced with a blob. | |
690 | */ | |
cd2fef59 | 691 | result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0); |
df292c79 LT |
692 | if (result) { |
693 | if (result->ce_flags & CE_REMOVE) | |
203a2fe1 | 694 | return 0; |
c8193534 JH |
695 | } |
696 | ||
17e46426 | 697 | return o->gently ? -1 : |
8ccba008 | 698 | error(ERRORMSG(o, would_lose_untracked), ce->name, action); |
c8193534 | 699 | } |
203a2fe1 | 700 | return 0; |
076b0adc | 701 | } |
35a5aa79 NTND |
702 | static int verify_absent(struct cache_entry *ce, const char *action, |
703 | struct unpack_trees_options *o) | |
704 | { | |
705 | return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_untracked)); | |
706 | } | |
076b0adc JS |
707 | |
708 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, | |
709 | struct unpack_trees_options *o) | |
710 | { | |
7f8ab8dc LT |
711 | int update = CE_UPDATE; |
712 | ||
076b0adc JS |
713 | if (old) { |
714 | /* | |
715 | * See if we can re-use the old CE directly? | |
716 | * That way we get the uptodate stat info. | |
717 | * | |
7f8ab8dc LT |
718 | * This also removes the UPDATE flag on a match; otherwise |
719 | * we will end up overwriting local changes in the work tree. | |
076b0adc JS |
720 | */ |
721 | if (same(old, merge)) { | |
eb7a2f1d | 722 | copy_cache_entry(merge, old); |
7f8ab8dc | 723 | update = 0; |
076b0adc | 724 | } else { |
203a2fe1 DB |
725 | if (verify_uptodate(old, o)) |
726 | return -1; | |
32f54ca3 NTND |
727 | if (ce_skip_worktree(old)) |
728 | update |= CE_SKIP_WORKTREE; | |
bc052d7f | 729 | invalidate_ce_path(old, o); |
076b0adc JS |
730 | } |
731 | } | |
732 | else { | |
203a2fe1 DB |
733 | if (verify_absent(merge, "overwritten", o)) |
734 | return -1; | |
bc052d7f | 735 | invalidate_ce_path(merge, o); |
076b0adc JS |
736 | } |
737 | ||
7f8ab8dc | 738 | add_entry(o, merge, update, CE_STAGEMASK); |
076b0adc JS |
739 | return 1; |
740 | } | |
741 | ||
742 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, | |
743 | struct unpack_trees_options *o) | |
744 | { | |
34110cd4 LT |
745 | /* Did it exist in the index? */ |
746 | if (!old) { | |
203a2fe1 DB |
747 | if (verify_absent(ce, "removed", o)) |
748 | return -1; | |
34110cd4 LT |
749 | return 0; |
750 | } | |
751 | if (verify_uptodate(old, o)) | |
752 | return -1; | |
753 | add_entry(o, ce, CE_REMOVE, 0); | |
bc052d7f | 754 | invalidate_ce_path(ce, o); |
076b0adc JS |
755 | return 1; |
756 | } | |
757 | ||
7f7932ab | 758 | static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc | 759 | { |
34110cd4 | 760 | add_entry(o, ce, 0, 0); |
076b0adc JS |
761 | return 1; |
762 | } | |
763 | ||
764 | #if DBRT_DEBUG | |
765 | static void show_stage_entry(FILE *o, | |
766 | const char *label, const struct cache_entry *ce) | |
767 | { | |
768 | if (!ce) | |
769 | fprintf(o, "%s (missing)\n", label); | |
770 | else | |
771 | fprintf(o, "%s%06o %s %d\t%s\n", | |
772 | label, | |
7a51ed66 | 773 | ce->ce_mode, |
076b0adc JS |
774 | sha1_to_hex(ce->sha1), |
775 | ce_stage(ce), | |
776 | ce->name); | |
777 | } | |
778 | #endif | |
779 | ||
34110cd4 | 780 | int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o) |
076b0adc JS |
781 | { |
782 | struct cache_entry *index; | |
783 | struct cache_entry *head; | |
784 | struct cache_entry *remote = stages[o->head_idx + 1]; | |
785 | int count; | |
786 | int head_match = 0; | |
787 | int remote_match = 0; | |
076b0adc JS |
788 | |
789 | int df_conflict_head = 0; | |
790 | int df_conflict_remote = 0; | |
791 | ||
792 | int any_anc_missing = 0; | |
793 | int no_anc_exists = 1; | |
794 | int i; | |
795 | ||
796 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 797 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
076b0adc | 798 | any_anc_missing = 1; |
4c4caafc | 799 | else |
076b0adc | 800 | no_anc_exists = 0; |
076b0adc JS |
801 | } |
802 | ||
803 | index = stages[0]; | |
804 | head = stages[o->head_idx]; | |
805 | ||
806 | if (head == o->df_conflict_entry) { | |
807 | df_conflict_head = 1; | |
808 | head = NULL; | |
809 | } | |
810 | ||
811 | if (remote == o->df_conflict_entry) { | |
812 | df_conflict_remote = 1; | |
813 | remote = NULL; | |
814 | } | |
815 | ||
076b0adc JS |
816 | /* First, if there's a #16 situation, note that to prevent #13 |
817 | * and #14. | |
818 | */ | |
819 | if (!same(remote, head)) { | |
820 | for (i = 1; i < o->head_idx; i++) { | |
821 | if (same(stages[i], head)) { | |
822 | head_match = i; | |
823 | } | |
824 | if (same(stages[i], remote)) { | |
825 | remote_match = i; | |
826 | } | |
827 | } | |
828 | } | |
829 | ||
830 | /* We start with cases where the index is allowed to match | |
831 | * something other than the head: #14(ALT) and #2ALT, where it | |
832 | * is permitted to match the result instead. | |
833 | */ | |
834 | /* #14, #14ALT, #2ALT */ | |
835 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
836 | if (index && !same(index, remote) && !same(index, head)) | |
8ccba008 | 837 | return o->gently ? -1 : reject_merge(index, o); |
076b0adc JS |
838 | return merged_entry(remote, index, o); |
839 | } | |
840 | /* | |
841 | * If we have an entry in the index cache, then we want to | |
842 | * make sure that it matches head. | |
843 | */ | |
4e7c4571 | 844 | if (index && !same(index, head)) |
8ccba008 | 845 | return o->gently ? -1 : reject_merge(index, o); |
076b0adc JS |
846 | |
847 | if (head) { | |
848 | /* #5ALT, #15 */ | |
849 | if (same(head, remote)) | |
850 | return merged_entry(head, index, o); | |
851 | /* #13, #3ALT */ | |
852 | if (!df_conflict_remote && remote_match && !head_match) | |
853 | return merged_entry(head, index, o); | |
854 | } | |
855 | ||
856 | /* #1 */ | |
34110cd4 | 857 | if (!head && !remote && any_anc_missing) |
076b0adc JS |
858 | return 0; |
859 | ||
860 | /* Under the new "aggressive" rule, we resolve mostly trivial | |
861 | * cases that we historically had git-merge-one-file resolve. | |
862 | */ | |
863 | if (o->aggressive) { | |
864 | int head_deleted = !head && !df_conflict_head; | |
865 | int remote_deleted = !remote && !df_conflict_remote; | |
0cf73755 | 866 | struct cache_entry *ce = NULL; |
4c4caafc JH |
867 | |
868 | if (index) | |
0cf73755 | 869 | ce = index; |
4c4caafc | 870 | else if (head) |
0cf73755 | 871 | ce = head; |
4c4caafc | 872 | else if (remote) |
0cf73755 | 873 | ce = remote; |
4c4caafc JH |
874 | else { |
875 | for (i = 1; i < o->head_idx; i++) { | |
876 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
0cf73755 | 877 | ce = stages[i]; |
4c4caafc JH |
878 | break; |
879 | } | |
880 | } | |
881 | } | |
882 | ||
076b0adc JS |
883 | /* |
884 | * Deleted in both. | |
885 | * Deleted in one and unchanged in the other. | |
886 | */ | |
887 | if ((head_deleted && remote_deleted) || | |
888 | (head_deleted && remote && remote_match) || | |
889 | (remote_deleted && head && head_match)) { | |
890 | if (index) | |
891 | return deleted_entry(index, index, o); | |
34110cd4 | 892 | if (ce && !head_deleted) { |
203a2fe1 DB |
893 | if (verify_absent(ce, "removed", o)) |
894 | return -1; | |
895 | } | |
076b0adc JS |
896 | return 0; |
897 | } | |
898 | /* | |
899 | * Added in both, identically. | |
900 | */ | |
901 | if (no_anc_exists && head && remote && same(head, remote)) | |
902 | return merged_entry(head, index, o); | |
903 | ||
904 | } | |
905 | ||
906 | /* Below are "no merge" cases, which require that the index be | |
907 | * up-to-date to avoid the files getting overwritten with | |
908 | * conflict resolution files. | |
909 | */ | |
910 | if (index) { | |
203a2fe1 DB |
911 | if (verify_uptodate(index, o)) |
912 | return -1; | |
076b0adc | 913 | } |
076b0adc JS |
914 | |
915 | o->nontrivial_merge = 1; | |
916 | ||
ea4b52a8 | 917 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
076b0adc JS |
918 | count = 0; |
919 | if (!head_match || !remote_match) { | |
920 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 921 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
7f7932ab | 922 | keep_entry(stages[i], o); |
076b0adc JS |
923 | count++; |
924 | break; | |
925 | } | |
926 | } | |
927 | } | |
928 | #if DBRT_DEBUG | |
929 | else { | |
930 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
931 | show_stage_entry(stderr, "head ", stages[head_match]); | |
932 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
933 | } | |
934 | #endif | |
7f7932ab JH |
935 | if (head) { count += keep_entry(head, o); } |
936 | if (remote) { count += keep_entry(remote, o); } | |
076b0adc JS |
937 | return count; |
938 | } | |
939 | ||
940 | /* | |
941 | * Two-way merge. | |
942 | * | |
943 | * The rule is to "carry forward" what is in the index without losing | |
944 | * information across a "fast forward", favoring a successful merge | |
945 | * over a merge failure when it makes sense. For details of the | |
946 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
947 | * | |
948 | */ | |
34110cd4 | 949 | int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o) |
076b0adc JS |
950 | { |
951 | struct cache_entry *current = src[0]; | |
b8ba1535 JH |
952 | struct cache_entry *oldtree = src[1]; |
953 | struct cache_entry *newtree = src[2]; | |
076b0adc JS |
954 | |
955 | if (o->merge_size != 2) | |
956 | return error("Cannot do a twoway merge of %d trees", | |
957 | o->merge_size); | |
958 | ||
b8ba1535 JH |
959 | if (oldtree == o->df_conflict_entry) |
960 | oldtree = NULL; | |
961 | if (newtree == o->df_conflict_entry) | |
962 | newtree = NULL; | |
963 | ||
076b0adc JS |
964 | if (current) { |
965 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
966 | (!oldtree && newtree && | |
967 | same(current, newtree)) || /* 6 and 7 */ | |
968 | (oldtree && newtree && | |
969 | same(oldtree, newtree)) || /* 14 and 15 */ | |
970 | (oldtree && newtree && | |
b8ba1535 | 971 | !same(oldtree, newtree) && /* 18 and 19 */ |
076b0adc | 972 | same(current, newtree))) { |
7f7932ab | 973 | return keep_entry(current, o); |
076b0adc JS |
974 | } |
975 | else if (oldtree && !newtree && same(current, oldtree)) { | |
976 | /* 10 or 11 */ | |
977 | return deleted_entry(oldtree, current, o); | |
978 | } | |
979 | else if (oldtree && newtree && | |
980 | same(current, oldtree) && !same(current, newtree)) { | |
981 | /* 20 or 21 */ | |
982 | return merged_entry(newtree, current, o); | |
983 | } | |
984 | else { | |
985 | /* all other failures */ | |
986 | if (oldtree) | |
8ccba008 | 987 | return o->gently ? -1 : reject_merge(oldtree, o); |
076b0adc | 988 | if (current) |
8ccba008 | 989 | return o->gently ? -1 : reject_merge(current, o); |
076b0adc | 990 | if (newtree) |
8ccba008 | 991 | return o->gently ? -1 : reject_merge(newtree, o); |
076b0adc JS |
992 | return -1; |
993 | } | |
994 | } | |
55218834 JH |
995 | else if (newtree) { |
996 | if (oldtree && !o->initial_checkout) { | |
997 | /* | |
998 | * deletion of the path was staged; | |
999 | */ | |
1000 | if (same(oldtree, newtree)) | |
1001 | return 1; | |
1002 | return reject_merge(oldtree, o); | |
1003 | } | |
076b0adc | 1004 | return merged_entry(newtree, current, o); |
55218834 | 1005 | } |
d699676d | 1006 | return deleted_entry(oldtree, current, o); |
076b0adc JS |
1007 | } |
1008 | ||
1009 | /* | |
1010 | * Bind merge. | |
1011 | * | |
1012 | * Keep the index entries at stage0, collapse stage1 but make sure | |
1013 | * stage0 does not have anything there. | |
1014 | */ | |
1015 | int bind_merge(struct cache_entry **src, | |
34110cd4 | 1016 | struct unpack_trees_options *o) |
076b0adc JS |
1017 | { |
1018 | struct cache_entry *old = src[0]; | |
1019 | struct cache_entry *a = src[1]; | |
1020 | ||
1021 | if (o->merge_size != 1) | |
1022 | return error("Cannot do a bind merge of %d trees\n", | |
1023 | o->merge_size); | |
1024 | if (a && old) | |
17e46426 | 1025 | return o->gently ? -1 : |
8ccba008 | 1026 | error(ERRORMSG(o, bind_overlap), a->name, old->name); |
076b0adc | 1027 | if (!a) |
7f7932ab | 1028 | return keep_entry(old, o); |
076b0adc JS |
1029 | else |
1030 | return merged_entry(a, NULL, o); | |
1031 | } | |
1032 | ||
1033 | /* | |
1034 | * One-way merge. | |
1035 | * | |
1036 | * The rule is: | |
1037 | * - take the stat information from stage0, take the data from stage1 | |
1038 | */ | |
34110cd4 | 1039 | int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o) |
076b0adc JS |
1040 | { |
1041 | struct cache_entry *old = src[0]; | |
1042 | struct cache_entry *a = src[1]; | |
1043 | ||
1044 | if (o->merge_size != 1) | |
1045 | return error("Cannot do a oneway merge of %d trees", | |
1046 | o->merge_size); | |
1047 | ||
78d3b06e | 1048 | if (!a || a == o->df_conflict_entry) |
076b0adc | 1049 | return deleted_entry(old, old, o); |
34110cd4 | 1050 | |
076b0adc | 1051 | if (old && same(old, a)) { |
34110cd4 | 1052 | int update = 0; |
52030836 | 1053 | if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) { |
076b0adc JS |
1054 | struct stat st; |
1055 | if (lstat(old->name, &st) || | |
34110cd4 LT |
1056 | ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID)) |
1057 | update |= CE_UPDATE; | |
076b0adc | 1058 | } |
34110cd4 LT |
1059 | add_entry(o, old, update, 0); |
1060 | return 0; | |
076b0adc JS |
1061 | } |
1062 | return merged_entry(a, old, o); | |
1063 | } |