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