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