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