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