1#include "cache.h"
2#include "dir.h"
3#include "tree.h"
4#include "tree-walk.h"
5#include "cache-tree.h"
6#include "unpack-trees.h"
7#include "progress.h"
8#include "refs.h"
9
10#define DBRT_DEBUG 1
11
12struct tree_entry_list {
13 struct tree_entry_list *next;
14 unsigned int mode;
15 const char *name;
16 const unsigned char *sha1;
17};
18
19static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc)
20{
21 struct name_entry one;
22 struct tree_entry_list *ret = NULL;
23 struct tree_entry_list **list_p = &ret;
24
25 while (tree_entry(desc, &one)) {
26 struct tree_entry_list *entry;
27
28 entry = xmalloc(sizeof(struct tree_entry_list));
29 entry->name = one.path;
30 entry->sha1 = one.sha1;
31 entry->mode = one.mode;
32 entry->next = NULL;
33
34 *list_p = entry;
35 list_p = &entry->next;
36 }
37 return ret;
38}
39
40static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
41{
42 int len1 = strlen(name1);
43 int len2 = strlen(name2);
44 int len = len1 < len2 ? len1 : len2;
45 int ret = memcmp(name1, name2, len);
46 unsigned char c1, c2;
47 if (ret)
48 return ret;
49 c1 = name1[len];
50 c2 = name2[len];
51 if (!c1 && dir1)
52 c1 = '/';
53 if (!c2 && dir2)
54 c2 = '/';
55 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
56 if (c1 && c2 && !ret)
57 ret = len1 - len2;
58 return ret;
59}
60
61static inline void remove_entry(int remove)
62{
63 if (remove >= 0)
64 remove_cache_entry_at(remove);
65}
66
67static int unpack_trees_rec(struct tree_entry_list **posns, int len,
68 const char *base, struct unpack_trees_options *o,
69 struct tree_entry_list *df_conflict_list)
70{
71 int remove;
72 int baselen = strlen(base);
73 int src_size = len + 1;
74 int retval = 0;
75
76 do {
77 int i;
78 const char *first;
79 int firstdir = 0;
80 int pathlen;
81 unsigned ce_size;
82 struct tree_entry_list **subposns;
83 struct cache_entry **src;
84 int any_files = 0;
85 int any_dirs = 0;
86 char *cache_name;
87 int ce_stage;
88 int skip_entry = 0;
89
90 /* Find the first name in the input. */
91
92 first = NULL;
93 cache_name = NULL;
94
95 /* Check the cache */
96 if (o->merge && o->pos < active_nr) {
97 /* This is a bit tricky: */
98 /* If the index has a subdirectory (with
99 * contents) as the first name, it'll get a
100 * filename like "foo/bar". But that's after
101 * "foo", so the entry in trees will get
102 * handled first, at which point we'll go into
103 * "foo", and deal with "bar" from the index,
104 * because the base will be "foo/". The only
105 * way we can actually have "foo/bar" first of
106 * all the things is if the trees don't
107 * contain "foo" at all, in which case we'll
108 * handle "foo/bar" without going into the
109 * directory, but that's fine (and will return
110 * an error anyway, with the added unknown
111 * file case.
112 */
113
114 cache_name = active_cache[o->pos]->name;
115 if (strlen(cache_name) > baselen &&
116 !memcmp(cache_name, base, baselen)) {
117 cache_name += baselen;
118 first = cache_name;
119 } else {
120 cache_name = NULL;
121 }
122 }
123
124#if DBRT_DEBUG > 1
125 if (first)
126 fprintf(stderr, "index %s\n", first);
127#endif
128 for (i = 0; i < len; i++) {
129 if (!posns[i] || posns[i] == df_conflict_list)
130 continue;
131#if DBRT_DEBUG > 1
132 fprintf(stderr, "%d %s\n", i + 1, posns[i]->name);
133#endif
134 if (!first || entcmp(first, firstdir,
135 posns[i]->name,
136 S_ISDIR(posns[i]->mode)) > 0) {
137 first = posns[i]->name;
138 firstdir = S_ISDIR(posns[i]->mode);
139 }
140 }
141 /* No name means we're done */
142 if (!first)
143 goto leave_directory;
144
145 pathlen = strlen(first);
146 ce_size = cache_entry_size(baselen + pathlen);
147
148 src = xcalloc(src_size, sizeof(struct cache_entry *));
149
150 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
151
152 remove = -1;
153 if (cache_name && !strcmp(cache_name, first)) {
154 any_files = 1;
155 src[0] = active_cache[o->pos];
156 remove = o->pos;
157 if (o->skip_unmerged && ce_stage(src[0]))
158 skip_entry = 1;
159 }
160
161 for (i = 0; i < len; i++) {
162 struct cache_entry *ce;
163
164 if (!posns[i] ||
165 (posns[i] != df_conflict_list &&
166 strcmp(first, posns[i]->name))) {
167 continue;
168 }
169
170 if (posns[i] == df_conflict_list) {
171 src[i + o->merge] = o->df_conflict_entry;
172 continue;
173 }
174
175 if (S_ISDIR(posns[i]->mode)) {
176 struct tree *tree = lookup_tree(posns[i]->sha1);
177 struct tree_desc t;
178 any_dirs = 1;
179 parse_tree(tree);
180 init_tree_desc(&t, tree->buffer, tree->size);
181 subposns[i] = create_tree_entry_list(&t);
182 posns[i] = posns[i]->next;
183 src[i + o->merge] = o->df_conflict_entry;
184 continue;
185 }
186
187 if (skip_entry) {
188 subposns[i] = df_conflict_list;
189 posns[i] = posns[i]->next;
190 continue;
191 }
192
193 if (!o->merge)
194 ce_stage = 0;
195 else if (i + 1 < o->head_idx)
196 ce_stage = 1;
197 else if (i + 1 > o->head_idx)
198 ce_stage = 3;
199 else
200 ce_stage = 2;
201
202 ce = xcalloc(1, ce_size);
203 ce->ce_mode = create_ce_mode(posns[i]->mode);
204 ce->ce_flags = create_ce_flags(baselen + pathlen,
205 ce_stage);
206 memcpy(ce->name, base, baselen);
207 memcpy(ce->name + baselen, first, pathlen + 1);
208
209 any_files = 1;
210
211 hashcpy(ce->sha1, posns[i]->sha1);
212 src[i + o->merge] = ce;
213 subposns[i] = df_conflict_list;
214 posns[i] = posns[i]->next;
215 }
216 if (any_files) {
217 if (skip_entry) {
218 o->pos++;
219 while (o->pos < active_nr &&
220 !strcmp(active_cache[o->pos]->name,
221 src[0]->name))
222 o->pos++;
223 } else if (o->merge) {
224 int ret;
225
226#if DBRT_DEBUG > 1
227 fprintf(stderr, "%s:\n", first);
228 for (i = 0; i < src_size; i++) {
229 fprintf(stderr, " %d ", i);
230 if (src[i])
231 fprintf(stderr, "%06x %s\n", src[i]->ce_mode, sha1_to_hex(src[i]->sha1));
232 else
233 fprintf(stderr, "\n");
234 }
235#endif
236 ret = o->fn(src, o, remove);
237 if (ret < 0)
238 return ret;
239
240#if DBRT_DEBUG > 1
241 fprintf(stderr, "Added %d entries\n", ret);
242#endif
243 o->pos += ret;
244 } else {
245 remove_entry(remove);
246 for (i = 0; i < src_size; i++) {
247 if (src[i]) {
248 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
249 }
250 }
251 }
252 }
253 if (any_dirs) {
254 char *newbase = xmalloc(baselen + 2 + pathlen);
255 memcpy(newbase, base, baselen);
256 memcpy(newbase + baselen, first, pathlen);
257 newbase[baselen + pathlen] = '/';
258 newbase[baselen + pathlen + 1] = '\0';
259 if (unpack_trees_rec(subposns, len, newbase, o,
260 df_conflict_list)) {
261 retval = -1;
262 goto leave_directory;
263 }
264 free(newbase);
265 }
266 free(subposns);
267 free(src);
268 } while (1);
269
270 leave_directory:
271 return retval;
272}
273
274/* Unlink the last component and attempt to remove leading
275 * directories, in case this unlink is the removal of the
276 * last entry in the directory -- empty directories are removed.
277 */
278static void unlink_entry(char *name, char *last_symlink)
279{
280 char *cp, *prev;
281
282 if (has_symlink_leading_path(name, last_symlink))
283 return;
284 if (unlink(name))
285 return;
286 prev = NULL;
287 while (1) {
288 int status;
289 cp = strrchr(name, '/');
290 if (prev)
291 *prev = '/';
292 if (!cp)
293 break;
294
295 *cp = 0;
296 status = rmdir(name);
297 if (status) {
298 *cp = '/';
299 break;
300 }
301 prev = cp;
302 }
303}
304
305static struct checkout state;
306static void check_updates(struct unpack_trees_options *o)
307{
308 unsigned cnt = 0, total = 0;
309 struct progress *progress = NULL;
310 char last_symlink[PATH_MAX];
311 int i;
312
313 if (o->update && o->verbose_update) {
314 for (total = cnt = 0; cnt < active_nr; cnt++) {
315 struct cache_entry *ce = active_cache[cnt];
316 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
317 total++;
318 }
319
320 progress = start_progress_delay("Checking out files",
321 total, 50, 2);
322 cnt = 0;
323 }
324
325 *last_symlink = '\0';
326 for (i = 0; i < active_nr; i++) {
327 struct cache_entry *ce = active_cache[i];
328
329 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
330 display_progress(progress, ++cnt);
331 if (ce->ce_flags & CE_REMOVE) {
332 if (o->update)
333 unlink_entry(ce->name, last_symlink);
334 remove_cache_entry_at(i);
335 i--;
336 continue;
337 }
338 if (ce->ce_flags & CE_UPDATE) {
339 ce->ce_flags &= ~CE_UPDATE;
340 if (o->update) {
341 checkout_entry(ce, &state, NULL);
342 *last_symlink = '\0';
343 }
344 }
345 }
346 stop_progress(&progress);
347}
348
349int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
350{
351 struct tree_entry_list **posns;
352 int i;
353 struct tree_entry_list df_conflict_list;
354 static struct cache_entry *dfc;
355
356 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
357 df_conflict_list.next = &df_conflict_list;
358 memset(&state, 0, sizeof(state));
359 state.base_dir = "";
360 state.force = 1;
361 state.quiet = 1;
362 state.refresh_cache = 1;
363
364 o->merge_size = len;
365
366 if (!dfc)
367 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
368 o->df_conflict_entry = dfc;
369
370 if (len) {
371 posns = xmalloc(len * sizeof(struct tree_entry_list *));
372 for (i = 0; i < len; i++)
373 posns[i] = create_tree_entry_list(t+i);
374
375 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
376 o, &df_conflict_list)) {
377 if (o->gently) {
378 discard_cache();
379 read_cache();
380 }
381 return -1;
382 }
383 }
384
385 if (o->trivial_merges_only && o->nontrivial_merge) {
386 if (o->gently) {
387 discard_cache();
388 read_cache();
389 }
390 return o->gently ? -1 :
391 error("Merge requires file-level merging");
392 }
393
394 check_updates(o);
395 return 0;
396}
397
398/* Here come the merge functions */
399
400static int reject_merge(struct cache_entry *ce)
401{
402 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
403 ce->name);
404}
405
406static int same(struct cache_entry *a, struct cache_entry *b)
407{
408 if (!!a != !!b)
409 return 0;
410 if (!a && !b)
411 return 1;
412 return a->ce_mode == b->ce_mode &&
413 !hashcmp(a->sha1, b->sha1);
414}
415
416
417/*
418 * When a CE gets turned into an unmerged entry, we
419 * want it to be up-to-date
420 */
421static int verify_uptodate(struct cache_entry *ce,
422 struct unpack_trees_options *o)
423{
424 struct stat st;
425
426 if (o->index_only || o->reset)
427 return 0;
428
429 if (!lstat(ce->name, &st)) {
430 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
431 if (!changed)
432 return 0;
433 /*
434 * NEEDSWORK: the current default policy is to allow
435 * submodule to be out of sync wrt the supermodule
436 * index. This needs to be tightened later for
437 * submodules that are marked to be automatically
438 * checked out.
439 */
440 if (S_ISGITLINK(ce->ce_mode))
441 return 0;
442 errno = 0;
443 }
444 if (errno == ENOENT)
445 return 0;
446 return o->gently ? -1 :
447 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
448}
449
450static void invalidate_ce_path(struct cache_entry *ce)
451{
452 if (ce)
453 cache_tree_invalidate_path(active_cache_tree, ce->name);
454}
455
456/*
457 * Check that checking out ce->sha1 in subdir ce->name is not
458 * going to overwrite any working files.
459 *
460 * Currently, git does not checkout subprojects during a superproject
461 * checkout, so it is not going to overwrite anything.
462 */
463static int verify_clean_submodule(struct cache_entry *ce, const char *action,
464 struct unpack_trees_options *o)
465{
466 return 0;
467}
468
469static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
470 struct unpack_trees_options *o)
471{
472 /*
473 * we are about to extract "ce->name"; we would not want to lose
474 * anything in the existing directory there.
475 */
476 int namelen;
477 int pos, i;
478 struct dir_struct d;
479 char *pathbuf;
480 int cnt = 0;
481 unsigned char sha1[20];
482
483 if (S_ISGITLINK(ce->ce_mode) &&
484 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
485 /* If we are not going to update the submodule, then
486 * we don't care.
487 */
488 if (!hashcmp(sha1, ce->sha1))
489 return 0;
490 return verify_clean_submodule(ce, action, o);
491 }
492
493 /*
494 * First let's make sure we do not have a local modification
495 * in that directory.
496 */
497 namelen = strlen(ce->name);
498 pos = cache_name_pos(ce->name, namelen);
499 if (0 <= pos)
500 return cnt; /* we have it as nondirectory */
501 pos = -pos - 1;
502 for (i = pos; i < active_nr; i++) {
503 struct cache_entry *ce = active_cache[i];
504 int len = ce_namelen(ce);
505 if (len < namelen ||
506 strncmp(ce->name, ce->name, namelen) ||
507 ce->name[namelen] != '/')
508 break;
509 /*
510 * ce->name is an entry in the subdirectory.
511 */
512 if (!ce_stage(ce)) {
513 if (verify_uptodate(ce, o))
514 return -1;
515 ce->ce_flags |= CE_REMOVE;
516 }
517 cnt++;
518 }
519
520 /*
521 * Then we need to make sure that we do not lose a locally
522 * present file that is not ignored.
523 */
524 pathbuf = xmalloc(namelen + 2);
525 memcpy(pathbuf, ce->name, namelen);
526 strcpy(pathbuf+namelen, "/");
527
528 memset(&d, 0, sizeof(d));
529 if (o->dir)
530 d.exclude_per_dir = o->dir->exclude_per_dir;
531 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
532 if (i)
533 return o->gently ? -1 :
534 error("Updating '%s' would lose untracked files in it",
535 ce->name);
536 free(pathbuf);
537 return cnt;
538}
539
540/*
541 * We do not want to remove or overwrite a working tree file that
542 * is not tracked, unless it is ignored.
543 */
544static int verify_absent(struct cache_entry *ce, const char *action,
545 struct unpack_trees_options *o)
546{
547 struct stat st;
548
549 if (o->index_only || o->reset || !o->update)
550 return 0;
551
552 if (has_symlink_leading_path(ce->name, NULL))
553 return 0;
554
555 if (!lstat(ce->name, &st)) {
556 int cnt;
557
558 if (o->dir && excluded(o->dir, ce->name))
559 /*
560 * ce->name is explicitly excluded, so it is Ok to
561 * overwrite it.
562 */
563 return 0;
564 if (S_ISDIR(st.st_mode)) {
565 /*
566 * We are checking out path "foo" and
567 * found "foo/." in the working tree.
568 * This is tricky -- if we have modified
569 * files that are in "foo/" we would lose
570 * it.
571 */
572 cnt = verify_clean_subdirectory(ce, action, o);
573
574 /*
575 * If this removed entries from the index,
576 * what that means is:
577 *
578 * (1) the caller unpack_trees_rec() saw path/foo
579 * in the index, and it has not removed it because
580 * it thinks it is handling 'path' as blob with
581 * D/F conflict;
582 * (2) we will return "ok, we placed a merged entry
583 * in the index" which would cause o->pos to be
584 * incremented by one;
585 * (3) however, original o->pos now has 'path/foo'
586 * marked with "to be removed".
587 *
588 * We need to increment it by the number of
589 * deleted entries here.
590 */
591 o->pos += cnt;
592 return 0;
593 }
594
595 /*
596 * The previous round may already have decided to
597 * delete this path, which is in a subdirectory that
598 * is being replaced with a blob.
599 */
600 cnt = cache_name_pos(ce->name, strlen(ce->name));
601 if (0 <= cnt) {
602 struct cache_entry *ce = active_cache[cnt];
603 if (ce->ce_flags & CE_REMOVE)
604 return 0;
605 }
606
607 return o->gently ? -1 :
608 error("Untracked working tree file '%s' "
609 "would be %s by merge.", ce->name, action);
610 }
611 return 0;
612}
613
614static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
615 struct unpack_trees_options *o)
616{
617 merge->ce_flags |= CE_UPDATE;
618 if (old) {
619 /*
620 * See if we can re-use the old CE directly?
621 * That way we get the uptodate stat info.
622 *
623 * This also removes the UPDATE flag on
624 * a match.
625 */
626 if (same(old, merge)) {
627 memcpy(merge, old, offsetof(struct cache_entry, name));
628 } else {
629 if (verify_uptodate(old, o))
630 return -1;
631 invalidate_ce_path(old);
632 }
633 }
634 else {
635 if (verify_absent(merge, "overwritten", o))
636 return -1;
637 invalidate_ce_path(merge);
638 }
639
640 merge->ce_flags &= ~CE_STAGEMASK;
641 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
642 return 1;
643}
644
645static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
646 struct unpack_trees_options *o)
647{
648 if (old) {
649 if (verify_uptodate(old, o))
650 return -1;
651 } else
652 if (verify_absent(ce, "removed", o))
653 return -1;
654 ce->ce_flags |= CE_REMOVE;
655 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
656 invalidate_ce_path(ce);
657 return 1;
658}
659
660static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
661{
662 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
663 return 1;
664}
665
666#if DBRT_DEBUG
667static void show_stage_entry(FILE *o,
668 const char *label, const struct cache_entry *ce)
669{
670 if (!ce)
671 fprintf(o, "%s (missing)\n", label);
672 else
673 fprintf(o, "%s%06o %s %d\t%s\n",
674 label,
675 ce->ce_mode,
676 sha1_to_hex(ce->sha1),
677 ce_stage(ce),
678 ce->name);
679}
680#endif
681
682int threeway_merge(struct cache_entry **stages,
683 struct unpack_trees_options *o,
684 int remove)
685{
686 struct cache_entry *index;
687 struct cache_entry *head;
688 struct cache_entry *remote = stages[o->head_idx + 1];
689 int count;
690 int head_match = 0;
691 int remote_match = 0;
692
693 int df_conflict_head = 0;
694 int df_conflict_remote = 0;
695
696 int any_anc_missing = 0;
697 int no_anc_exists = 1;
698 int i;
699
700 for (i = 1; i < o->head_idx; i++) {
701 if (!stages[i] || stages[i] == o->df_conflict_entry)
702 any_anc_missing = 1;
703 else
704 no_anc_exists = 0;
705 }
706
707 index = stages[0];
708 head = stages[o->head_idx];
709
710 if (head == o->df_conflict_entry) {
711 df_conflict_head = 1;
712 head = NULL;
713 }
714
715 if (remote == o->df_conflict_entry) {
716 df_conflict_remote = 1;
717 remote = NULL;
718 }
719
720 /* First, if there's a #16 situation, note that to prevent #13
721 * and #14.
722 */
723 if (!same(remote, head)) {
724 for (i = 1; i < o->head_idx; i++) {
725 if (same(stages[i], head)) {
726 head_match = i;
727 }
728 if (same(stages[i], remote)) {
729 remote_match = i;
730 }
731 }
732 }
733
734 /* We start with cases where the index is allowed to match
735 * something other than the head: #14(ALT) and #2ALT, where it
736 * is permitted to match the result instead.
737 */
738 /* #14, #14ALT, #2ALT */
739 if (remote && !df_conflict_head && head_match && !remote_match) {
740 if (index && !same(index, remote) && !same(index, head))
741 return o->gently ? -1 : reject_merge(index);
742 return merged_entry(remote, index, o);
743 }
744 /*
745 * If we have an entry in the index cache, then we want to
746 * make sure that it matches head.
747 */
748 if (index && !same(index, head))
749 return o->gently ? -1 : reject_merge(index);
750
751 if (head) {
752 /* #5ALT, #15 */
753 if (same(head, remote))
754 return merged_entry(head, index, o);
755 /* #13, #3ALT */
756 if (!df_conflict_remote && remote_match && !head_match)
757 return merged_entry(head, index, o);
758 }
759
760 /* #1 */
761 if (!head && !remote && any_anc_missing) {
762 remove_entry(remove);
763 return 0;
764 }
765
766 /* Under the new "aggressive" rule, we resolve mostly trivial
767 * cases that we historically had git-merge-one-file resolve.
768 */
769 if (o->aggressive) {
770 int head_deleted = !head && !df_conflict_head;
771 int remote_deleted = !remote && !df_conflict_remote;
772 struct cache_entry *ce = NULL;
773
774 if (index)
775 ce = index;
776 else if (head)
777 ce = head;
778 else if (remote)
779 ce = remote;
780 else {
781 for (i = 1; i < o->head_idx; i++) {
782 if (stages[i] && stages[i] != o->df_conflict_entry) {
783 ce = stages[i];
784 break;
785 }
786 }
787 }
788
789 /*
790 * Deleted in both.
791 * Deleted in one and unchanged in the other.
792 */
793 if ((head_deleted && remote_deleted) ||
794 (head_deleted && remote && remote_match) ||
795 (remote_deleted && head && head_match)) {
796 remove_entry(remove);
797 if (index)
798 return deleted_entry(index, index, o);
799 else if (ce && !head_deleted) {
800 if (verify_absent(ce, "removed", o))
801 return -1;
802 }
803 return 0;
804 }
805 /*
806 * Added in both, identically.
807 */
808 if (no_anc_exists && head && remote && same(head, remote))
809 return merged_entry(head, index, o);
810
811 }
812
813 /* Below are "no merge" cases, which require that the index be
814 * up-to-date to avoid the files getting overwritten with
815 * conflict resolution files.
816 */
817 if (index) {
818 if (verify_uptodate(index, o))
819 return -1;
820 }
821
822 remove_entry(remove);
823 o->nontrivial_merge = 1;
824
825 /* #2, #3, #4, #6, #7, #9, #10, #11. */
826 count = 0;
827 if (!head_match || !remote_match) {
828 for (i = 1; i < o->head_idx; i++) {
829 if (stages[i] && stages[i] != o->df_conflict_entry) {
830 keep_entry(stages[i], o);
831 count++;
832 break;
833 }
834 }
835 }
836#if DBRT_DEBUG
837 else {
838 fprintf(stderr, "read-tree: warning #16 detected\n");
839 show_stage_entry(stderr, "head ", stages[head_match]);
840 show_stage_entry(stderr, "remote ", stages[remote_match]);
841 }
842#endif
843 if (head) { count += keep_entry(head, o); }
844 if (remote) { count += keep_entry(remote, o); }
845 return count;
846}
847
848/*
849 * Two-way merge.
850 *
851 * The rule is to "carry forward" what is in the index without losing
852 * information across a "fast forward", favoring a successful merge
853 * over a merge failure when it makes sense. For details of the
854 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
855 *
856 */
857int twoway_merge(struct cache_entry **src,
858 struct unpack_trees_options *o,
859 int remove)
860{
861 struct cache_entry *current = src[0];
862 struct cache_entry *oldtree = src[1];
863 struct cache_entry *newtree = src[2];
864
865 if (o->merge_size != 2)
866 return error("Cannot do a twoway merge of %d trees",
867 o->merge_size);
868
869 if (oldtree == o->df_conflict_entry)
870 oldtree = NULL;
871 if (newtree == o->df_conflict_entry)
872 newtree = NULL;
873
874 if (current) {
875 if ((!oldtree && !newtree) || /* 4 and 5 */
876 (!oldtree && newtree &&
877 same(current, newtree)) || /* 6 and 7 */
878 (oldtree && newtree &&
879 same(oldtree, newtree)) || /* 14 and 15 */
880 (oldtree && newtree &&
881 !same(oldtree, newtree) && /* 18 and 19 */
882 same(current, newtree))) {
883 return keep_entry(current, o);
884 }
885 else if (oldtree && !newtree && same(current, oldtree)) {
886 /* 10 or 11 */
887 remove_entry(remove);
888 return deleted_entry(oldtree, current, o);
889 }
890 else if (oldtree && newtree &&
891 same(current, oldtree) && !same(current, newtree)) {
892 /* 20 or 21 */
893 return merged_entry(newtree, current, o);
894 }
895 else {
896 /* all other failures */
897 remove_entry(remove);
898 if (oldtree)
899 return o->gently ? -1 : reject_merge(oldtree);
900 if (current)
901 return o->gently ? -1 : reject_merge(current);
902 if (newtree)
903 return o->gently ? -1 : reject_merge(newtree);
904 return -1;
905 }
906 }
907 else if (newtree)
908 return merged_entry(newtree, current, o);
909 remove_entry(remove);
910 return deleted_entry(oldtree, current, o);
911}
912
913/*
914 * Bind merge.
915 *
916 * Keep the index entries at stage0, collapse stage1 but make sure
917 * stage0 does not have anything there.
918 */
919int bind_merge(struct cache_entry **src,
920 struct unpack_trees_options *o,
921 int remove)
922{
923 struct cache_entry *old = src[0];
924 struct cache_entry *a = src[1];
925
926 if (o->merge_size != 1)
927 return error("Cannot do a bind merge of %d trees\n",
928 o->merge_size);
929 if (a && old)
930 return o->gently ? -1 :
931 error("Entry '%s' overlaps. Cannot bind.", a->name);
932 if (!a)
933 return keep_entry(old, o);
934 else
935 return merged_entry(a, NULL, o);
936}
937
938/*
939 * One-way merge.
940 *
941 * The rule is:
942 * - take the stat information from stage0, take the data from stage1
943 */
944int oneway_merge(struct cache_entry **src,
945 struct unpack_trees_options *o,
946 int remove)
947{
948 struct cache_entry *old = src[0];
949 struct cache_entry *a = src[1];
950
951 if (o->merge_size != 1)
952 return error("Cannot do a oneway merge of %d trees",
953 o->merge_size);
954
955 if (!a) {
956 remove_entry(remove);
957 return deleted_entry(old, old, o);
958 }
959 if (old && same(old, a)) {
960 if (o->reset) {
961 struct stat st;
962 if (lstat(old->name, &st) ||
963 ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
964 old->ce_flags |= CE_UPDATE;
965 }
966 return keep_entry(old, o);
967 }
968 return merged_entry(a, old, o);
969}