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