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