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