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