@matheusbsbs / matheus-bagliano / commits / 076b0adcf9
Fork 0

read-tree: move merge functions to the library

This will allow merge-recursive to use the read-tree functionality without exec()ing git-read-tree. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 30, 2006 at 20:26 UTC 076b0adcf9dac7bd9d18624087f679cc811aeb77
3 files changed +410 -397
builtin-read-tree.c
+1 -397
@@ -3,10 +3,8 @@
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 -#define DBRT_DEBUG 1
6
7 #include "cache.h"
9 -
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
@@ -16,12 +14,6 @@
14
15 static struct object_list *trees = NULL;
16
19 -static void reject_merge(struct cache_entry *ce)
20 -{
21 - die("Entry '%s' would be overwritten by merge. Cannot merge.",
22 - ce->name);
23 -}
24 -
17 static int list_tree(unsigned char *sha1)
18 {
19 struct tree *tree = parse_tree_indirect(sha1);
@@ -31,394 +23,6 @@ static int list_tree(unsigned char *sha1)
23 return 0;
24 }
25
34 -static int same(struct cache_entry *a, struct cache_entry *b)
35 -{
36 - if (!!a != !!b)
37 - return 0;
38 - if (!a && !b)
39 - return 1;
40 - return a->ce_mode == b->ce_mode &&
41 - !memcmp(a->sha1, b->sha1, 20);
42 -}
43 -
44 -
45 -/*
46 - * When a CE gets turned into an unmerged entry, we
47 - * want it to be up-to-date
48 - */
49 -static void verify_uptodate(struct cache_entry *ce,
50 - struct unpack_trees_options *o)
51 -{
52 - struct stat st;
53 -
54 - if (o->index_only || o->reset)
55 - return;
56 -
57 - if (!lstat(ce->name, &st)) {
58 - unsigned changed = ce_match_stat(ce, &st, 1);
59 - if (!changed)
60 - return;
61 - errno = 0;
62 - }
63 - if (o->reset) {
64 - ce->ce_flags |= htons(CE_UPDATE);
65 - return;
66 - }
67 - if (errno == ENOENT)
68 - return;
69 - die("Entry '%s' not uptodate. Cannot merge.", ce->name);
70 -}
71 -
72 -static void invalidate_ce_path(struct cache_entry *ce)
73 -{
74 - if (ce)
75 - cache_tree_invalidate_path(active_cache_tree, ce->name);
76 -}
77 -
78 -/*
79 - * We do not want to remove or overwrite a working tree file that
80 - * is not tracked.
81 - */
82 -static void verify_absent(const char *path, const char *action,
83 - struct unpack_trees_options *o)
84 -{
85 - struct stat st;
86 -
87 - if (o->index_only || o->reset || !o->update)
88 - return;
89 - if (!lstat(path, &st))
90 - die("Untracked working tree file '%s' "
91 - "would be %s by merge.", path, action);
92 -}
93 -
94 -static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
95 - struct unpack_trees_options *o)
96 -{
97 - merge->ce_flags |= htons(CE_UPDATE);
98 - if (old) {
99 - /*
100 - * See if we can re-use the old CE directly?
101 - * That way we get the uptodate stat info.
102 - *
103 - * This also removes the UPDATE flag on
104 - * a match.
105 - */
106 - if (same(old, merge)) {
107 - *merge = *old;
108 - } else {
109 - verify_uptodate(old, o);
110 - invalidate_ce_path(old);
111 - }
112 - }
113 - else {
114 - verify_absent(merge->name, "overwritten", o);
115 - invalidate_ce_path(merge);
116 - }
117 -
118 - merge->ce_flags &= ~htons(CE_STAGEMASK);
119 - add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
120 - return 1;
121 -}
122 -
123 -static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
124 - struct unpack_trees_options *o)
125 -{
126 - if (old)
127 - verify_uptodate(old, o);
128 - else
129 - verify_absent(ce->name, "removed", o);
130 - ce->ce_mode = 0;
131 - add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
132 - invalidate_ce_path(ce);
133 - return 1;
134 -}
135 -
136 -static int keep_entry(struct cache_entry *ce)
137 -{
138 - add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
139 - return 1;
140 -}
141 -
142 -#if DBRT_DEBUG
143 -static void show_stage_entry(FILE *o,
144 - const char *label, const struct cache_entry *ce)
145 -{
146 - if (!ce)
147 - fprintf(o, "%s (missing)\n", label);
148 - else
149 - fprintf(o, "%s%06o %s %d\t%s\n",
150 - label,
151 - ntohl(ce->ce_mode),
152 - sha1_to_hex(ce->sha1),
153 - ce_stage(ce),
154 - ce->name);
155 -}
156 -#endif
157 -
158 -static int threeway_merge(struct cache_entry **stages,
159 - struct unpack_trees_options *o)
160 -{
161 - struct cache_entry *index;
162 - struct cache_entry *head;
163 - struct cache_entry *remote = stages[o->head_idx + 1];
164 - int count;
165 - int head_match = 0;
166 - int remote_match = 0;
167 - const char *path = NULL;
168 -
169 - int df_conflict_head = 0;
170 - int df_conflict_remote = 0;
171 -
172 - int any_anc_missing = 0;
173 - int no_anc_exists = 1;
174 - int i;
175 -
176 - for (i = 1; i < o->head_idx; i++) {
177 - if (!stages[i])
178 - any_anc_missing = 1;
179 - else {
180 - if (!path)
181 - path = stages[i]->name;
182 - no_anc_exists = 0;
183 - }
184 - }
185 -
186 - index = stages[0];
187 - head = stages[o->head_idx];
188 -
189 - if (head == &o->df_conflict_entry) {
190 - df_conflict_head = 1;
191 - head = NULL;
192 - }
193 -
194 - if (remote == &o->df_conflict_entry) {
195 - df_conflict_remote = 1;
196 - remote = NULL;
197 - }
198 -
199 - if (!path && index)
200 - path = index->name;
201 - if (!path && head)
202 - path = head->name;
203 - if (!path && remote)
204 - path = remote->name;
205 -
206 - /* First, if there's a #16 situation, note that to prevent #13
207 - * and #14.
208 - */
209 - if (!same(remote, head)) {
210 - for (i = 1; i < o->head_idx; i++) {
211 - if (same(stages[i], head)) {
212 - head_match = i;
213 - }
214 - if (same(stages[i], remote)) {
215 - remote_match = i;
216 - }
217 - }
218 - }
219 -
220 - /* We start with cases where the index is allowed to match
221 - * something other than the head: #14(ALT) and #2ALT, where it
222 - * is permitted to match the result instead.
223 - */
224 - /* #14, #14ALT, #2ALT */
225 - if (remote && !df_conflict_head && head_match && !remote_match) {
226 - if (index && !same(index, remote) && !same(index, head))
227 - reject_merge(index);
228 - return merged_entry(remote, index, o);
229 - }
230 - /*
231 - * If we have an entry in the index cache, then we want to
232 - * make sure that it matches head.
233 - */
234 - if (index && !same(index, head)) {
235 - reject_merge(index);
236 - }
237 -
238 - if (head) {
239 - /* #5ALT, #15 */
240 - if (same(head, remote))
241 - return merged_entry(head, index, o);
242 - /* #13, #3ALT */
243 - if (!df_conflict_remote && remote_match && !head_match)
244 - return merged_entry(head, index, o);
245 - }
246 -
247 - /* #1 */
248 - if (!head && !remote && any_anc_missing)
249 - return 0;
250 -
251 - /* Under the new "aggressive" rule, we resolve mostly trivial
252 - * cases that we historically had git-merge-one-file resolve.
253 - */
254 - if (o->aggressive) {
255 - int head_deleted = !head && !df_conflict_head;
256 - int remote_deleted = !remote && !df_conflict_remote;
257 - /*
258 - * Deleted in both.
259 - * Deleted in one and unchanged in the other.
260 - */
261 - if ((head_deleted && remote_deleted) ||
262 - (head_deleted && remote && remote_match) ||
263 - (remote_deleted && head && head_match)) {
264 - if (index)
265 - return deleted_entry(index, index, o);
266 - else if (path)
267 - verify_absent(path, "removed", o);
268 - return 0;
269 - }
270 - /*
271 - * Added in both, identically.
272 - */
273 - if (no_anc_exists && head && remote && same(head, remote))
274 - return merged_entry(head, index, o);
275 -
276 - }
277 -
278 - /* Below are "no merge" cases, which require that the index be
279 - * up-to-date to avoid the files getting overwritten with
280 - * conflict resolution files.
281 - */
282 - if (index) {
283 - verify_uptodate(index, o);
284 - }
285 - else if (path)
286 - verify_absent(path, "overwritten", o);
287 -
288 - o->nontrivial_merge = 1;
289 -
290 - /* #2, #3, #4, #6, #7, #9, #11. */
291 - count = 0;
292 - if (!head_match || !remote_match) {
293 - for (i = 1; i < o->head_idx; i++) {
294 - if (stages[i]) {
295 - keep_entry(stages[i]);
296 - count++;
297 - break;
298 - }
299 - }
300 - }
301 -#if DBRT_DEBUG
302 - else {
303 - fprintf(stderr, "read-tree: warning #16 detected\n");
304 - show_stage_entry(stderr, "head ", stages[head_match]);
305 - show_stage_entry(stderr, "remote ", stages[remote_match]);
306 - }
307 -#endif
308 - if (head) { count += keep_entry(head); }
309 - if (remote) { count += keep_entry(remote); }
310 - return count;
311 -}
312 -
313 -/*
314 - * Two-way merge.
315 - *
316 - * The rule is to "carry forward" what is in the index without losing
317 - * information across a "fast forward", favoring a successful merge
318 - * over a merge failure when it makes sense. For details of the
319 - * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
320 - *
321 - */
322 -static int twoway_merge(struct cache_entry **src,
323 - struct unpack_trees_options *o)
324 -{
325 - struct cache_entry *current = src[0];
326 - struct cache_entry *oldtree = src[1], *newtree = src[2];
327 -
328 - if (o->merge_size != 2)
329 - return error("Cannot do a twoway merge of %d trees",
330 - o->merge_size);
331 -
332 - if (current) {
333 - if ((!oldtree && !newtree) || /* 4 and 5 */
334 - (!oldtree && newtree &&
335 - same(current, newtree)) || /* 6 and 7 */
336 - (oldtree && newtree &&
337 - same(oldtree, newtree)) || /* 14 and 15 */
338 - (oldtree && newtree &&
339 - !same(oldtree, newtree) && /* 18 and 19*/
340 - same(current, newtree))) {
341 - return keep_entry(current);
342 - }
343 - else if (oldtree && !newtree && same(current, oldtree)) {
344 - /* 10 or 11 */
345 - return deleted_entry(oldtree, current, o);
346 - }
347 - else if (oldtree && newtree &&
348 - same(current, oldtree) && !same(current, newtree)) {
349 - /* 20 or 21 */
350 - return merged_entry(newtree, current, o);
351 - }
352 - else {
353 - /* all other failures */
354 - if (oldtree)
355 - reject_merge(oldtree);
356 - if (current)
357 - reject_merge(current);
358 - if (newtree)
359 - reject_merge(newtree);
360 - return -1;
361 - }
362 - }
363 - else if (newtree)
364 - return merged_entry(newtree, current, o);
365 - else
366 - return deleted_entry(oldtree, current, o);
367 -}
368 -
369 -/*
370 - * Bind merge.
371 - *
372 - * Keep the index entries at stage0, collapse stage1 but make sure
373 - * stage0 does not have anything there.
374 - */
375 -static int bind_merge(struct cache_entry **src,
376 - struct unpack_trees_options *o)
377 -{
378 - struct cache_entry *old = src[0];
379 - struct cache_entry *a = src[1];
380 -
381 - if (o->merge_size != 1)
382 - return error("Cannot do a bind merge of %d trees\n",
383 - o->merge_size);
384 - if (a && old)
385 - die("Entry '%s' overlaps. Cannot bind.", a->name);
386 - if (!a)
387 - return keep_entry(old);
388 - else
389 - return merged_entry(a, NULL, o);
390 -}
391 -
392 -/*
393 - * One-way merge.
394 - *
395 - * The rule is:
396 - * - take the stat information from stage0, take the data from stage1
397 - */
398 -static int oneway_merge(struct cache_entry **src,
399 - struct unpack_trees_options *o)
400 -{
401 - struct cache_entry *old = src[0];
402 - struct cache_entry *a = src[1];
403 -
404 - if (o->merge_size != 1)
405 - return error("Cannot do a oneway merge of %d trees",
406 - o->merge_size);
407 -
408 - if (!a)
409 - return deleted_entry(old, old, o);
410 - if (old && same(old, a)) {
411 - if (o->reset) {
412 - struct stat st;
413 - if (lstat(old->name, &st) ||
414 - ce_match_stat(old, &st, 1))
415 - old->ce_flags |= htons(CE_UPDATE);
416 - }
417 - return keep_entry(old);
418 - }
419 - return merged_entry(a, old, o);
420 -}
421 -
26 static int read_cache_unmerged(void)
27 {
28 int i;
@@ -432,7 +36,7 @@ static int read_cache_unmerged(void)
36 if (ce_stage(ce)) {
37 if (last && !strcmp(ce->name, last->name))
38 continue;
435 - invalidate_ce_path(ce);
39 + cache_tree_invalidate_path(active_cache_tree, ce->name);
40 last = ce;
41 ce->ce_mode = 0;
42 ce->ce_flags &= ~htons(CE_STAGEMASK);
unpack-trees.c
+404
@@ -3,8 +3,11 @@
3 #include "cache.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 +#include "cache-tree.h"
7 #include "unpack-trees.h"
8
9 +#define DBRT_DEBUG 1
10 +
11 struct tree_entry_list {
12 struct tree_entry_list *next;
13 unsigned directory : 1;
@@ -22,6 +25,9 @@ static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
25 struct tree_entry_list *ret = NULL;
26 struct tree_entry_list **list_p = &ret;
27
28 + if (!tree->object.parsed)
29 + parse_tree(tree);
30 +
31 desc.buf = tree->buffer;
32 desc.size = tree->size;
33
@@ -368,12 +374,14 @@ int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
374
375 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
376 df_conflict_list.next = &df_conflict_list;
377 + memset(&state, 0, sizeof(state));
378 state.base_dir = "";
379 state.force = 1;
380 state.quiet = 1;
381 state.refresh_cache = 1;
382
383 o->merge_size = len;
384 + memset(&df_conflict_entry, 0, sizeof(df_conflict_entry));
385 o->df_conflict_entry = &df_conflict_entry;
386
387 if (len) {
@@ -393,3 +401,399 @@ int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
401 check_updates(active_cache, active_nr, o);
402 return 0;
403 }
404 +
405 +/* Here come the merge functions */
406 +
407 +static void reject_merge(struct cache_entry *ce)
408 +{
409 + die("Entry '%s' would be overwritten by merge. Cannot merge.",
410 + ce->name);
411 +}
412 +
413 +static int same(struct cache_entry *a, struct cache_entry *b)
414 +{
415 + if (!!a != !!b)
416 + return 0;
417 + if (!a && !b)
418 + return 1;
419 + return a->ce_mode == b->ce_mode &&
420 + !memcmp(a->sha1, b->sha1, 20);
421 +}
422 +
423 +
424 +/*
425 + * When a CE gets turned into an unmerged entry, we
426 + * want it to be up-to-date
427 + */
428 +static void verify_uptodate(struct cache_entry *ce,
429 + struct unpack_trees_options *o)
430 +{
431 + struct stat st;
432 +
433 + if (o->index_only || o->reset)
434 + return;
435 +
436 + if (!lstat(ce->name, &st)) {
437 + unsigned changed = ce_match_stat(ce, &st, 1);
438 + if (!changed)
439 + return;
440 + errno = 0;
441 + }
442 + if (o->reset) {
443 + ce->ce_flags |= htons(CE_UPDATE);
444 + return;
445 + }
446 + if (errno == ENOENT)
447 + return;
448 + die("Entry '%s' not uptodate. Cannot merge.", ce->name);
449 +}
450 +
451 +static void invalidate_ce_path(struct cache_entry *ce)
452 +{
453 + if (ce)
454 + cache_tree_invalidate_path(active_cache_tree, ce->name);
455 +}
456 +
457 +/*
458 + * We do not want to remove or overwrite a working tree file that
459 + * is not tracked.
460 + */
461 +static void verify_absent(const char *path, const char *action,
462 + struct unpack_trees_options *o)
463 +{
464 + struct stat st;
465 +
466 + if (o->index_only || o->reset || !o->update)
467 + return;
468 + if (!lstat(path, &st))
469 + die("Untracked working tree file '%s' "
470 + "would be %s by merge.", path, action);
471 +}
472 +
473 +static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
474 + struct unpack_trees_options *o)
475 +{
476 + merge->ce_flags |= htons(CE_UPDATE);
477 + if (old) {
478 + /*
479 + * See if we can re-use the old CE directly?
480 + * That way we get the uptodate stat info.
481 + *
482 + * This also removes the UPDATE flag on
483 + * a match.
484 + */
485 + if (same(old, merge)) {
486 + *merge = *old;
487 + } else {
488 + verify_uptodate(old, o);
489 + invalidate_ce_path(old);
490 + }
491 + }
492 + else {
493 + verify_absent(merge->name, "overwritten", o);
494 + invalidate_ce_path(merge);
495 + }
496 +
497 + merge->ce_flags &= ~htons(CE_STAGEMASK);
498 + add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
499 + return 1;
500 +}
501 +
502 +static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
503 + struct unpack_trees_options *o)
504 +{
505 + if (old)
506 + verify_uptodate(old, o);
507 + else
508 + verify_absent(ce->name, "removed", o);
509 + ce->ce_mode = 0;
510 + add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
511 + invalidate_ce_path(ce);
512 + return 1;
513 +}
514 +
515 +static int keep_entry(struct cache_entry *ce)
516 +{
517 + add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
518 + return 1;
519 +}
520 +
521 +#if DBRT_DEBUG
522 +static void show_stage_entry(FILE *o,
523 + const char *label, const struct cache_entry *ce)
524 +{
525 + if (!ce)
526 + fprintf(o, "%s (missing)\n", label);
527 + else
528 + fprintf(o, "%s%06o %s %d\t%s\n",
529 + label,
530 + ntohl(ce->ce_mode),
531 + sha1_to_hex(ce->sha1),
532 + ce_stage(ce),
533 + ce->name);
534 +}
535 +#endif
536 +
537 +int threeway_merge(struct cache_entry **stages,
538 + struct unpack_trees_options *o)
539 +{
540 + struct cache_entry *index;
541 + struct cache_entry *head;
542 + struct cache_entry *remote = stages[o->head_idx + 1];
543 + int count;
544 + int head_match = 0;
545 + int remote_match = 0;
546 + const char *path = NULL;
547 +
548 + int df_conflict_head = 0;
549 + int df_conflict_remote = 0;
550 +
551 + int any_anc_missing = 0;
552 + int no_anc_exists = 1;
553 + int i;
554 +
555 + for (i = 1; i < o->head_idx; i++) {
556 + if (!stages[i])
557 + any_anc_missing = 1;
558 + else {
559 + if (!path)
560 + path = stages[i]->name;
561 + no_anc_exists = 0;
562 + }
563 + }
564 +
565 + index = stages[0];
566 + head = stages[o->head_idx];
567 +
568 + if (head == o->df_conflict_entry) {
569 + df_conflict_head = 1;
570 + head = NULL;
571 + }
572 +
573 + if (remote == o->df_conflict_entry) {
574 + df_conflict_remote = 1;
575 + remote = NULL;
576 + }
577 +
578 + if (!path && index)
579 + path = index->name;
580 + if (!path && head)
581 + path = head->name;
582 + if (!path && remote)
583 + path = remote->name;
584 +
585 + /* First, if there's a #16 situation, note that to prevent #13
586 + * and #14.
587 + */
588 + if (!same(remote, head)) {
589 + for (i = 1; i < o->head_idx; i++) {
590 + if (same(stages[i], head)) {
591 + head_match = i;
592 + }
593 + if (same(stages[i], remote)) {
594 + remote_match = i;
595 + }
596 + }
597 + }
598 +
599 + /* We start with cases where the index is allowed to match
600 + * something other than the head: #14(ALT) and #2ALT, where it
601 + * is permitted to match the result instead.
602 + */
603 + /* #14, #14ALT, #2ALT */
604 + if (remote && !df_conflict_head && head_match && !remote_match) {
605 + if (index && !same(index, remote) && !same(index, head))
606 + reject_merge(index);
607 + return merged_entry(remote, index, o);
608 + }
609 + /*
610 + * If we have an entry in the index cache, then we want to
611 + * make sure that it matches head.
612 + */
613 + if (index && !same(index, head)) {
614 + reject_merge(index);
615 + }
616 +
617 + if (head) {
618 + /* #5ALT, #15 */
619 + if (same(head, remote))
620 + return merged_entry(head, index, o);
621 + /* #13, #3ALT */
622 + if (!df_conflict_remote && remote_match && !head_match)
623 + return merged_entry(head, index, o);
624 + }
625 +
626 + /* #1 */
627 + if (!head && !remote && any_anc_missing)
628 + return 0;
629 +
630 + /* Under the new "aggressive" rule, we resolve mostly trivial
631 + * cases that we historically had git-merge-one-file resolve.
632 + */
633 + if (o->aggressive) {
634 + int head_deleted = !head && !df_conflict_head;
635 + int remote_deleted = !remote && !df_conflict_remote;
636 + /*
637 + * Deleted in both.
638 + * Deleted in one and unchanged in the other.
639 + */
640 + if ((head_deleted && remote_deleted) ||
641 + (head_deleted && remote && remote_match) ||
642 + (remote_deleted && head && head_match)) {
643 + if (index)
644 + return deleted_entry(index, index, o);
645 + else if (path)
646 + verify_absent(path, "removed", o);
647 + return 0;
648 + }
649 + /*
650 + * Added in both, identically.
651 + */
652 + if (no_anc_exists && head && remote && same(head, remote))
653 + return merged_entry(head, index, o);
654 +
655 + }
656 +
657 + /* Below are "no merge" cases, which require that the index be
658 + * up-to-date to avoid the files getting overwritten with
659 + * conflict resolution files.
660 + */
661 + if (index) {
662 + verify_uptodate(index, o);
663 + }
664 + else if (path)
665 + verify_absent(path, "overwritten", o);
666 +
667 + o->nontrivial_merge = 1;
668 +
669 + /* #2, #3, #4, #6, #7, #9, #11. */
670 + count = 0;
671 + if (!head_match || !remote_match) {
672 + for (i = 1; i < o->head_idx; i++) {
673 + if (stages[i]) {
674 + keep_entry(stages[i]);
675 + count++;
676 + break;
677 + }
678 + }
679 + }
680 +#if DBRT_DEBUG
681 + else {
682 + fprintf(stderr, "read-tree: warning #16 detected\n");
683 + show_stage_entry(stderr, "head ", stages[head_match]);
684 + show_stage_entry(stderr, "remote ", stages[remote_match]);
685 + }
686 +#endif
687 + if (head) { count += keep_entry(head); }
688 + if (remote) { count += keep_entry(remote); }
689 + return count;
690 +}
691 +
692 +/*
693 + * Two-way merge.
694 + *
695 + * The rule is to "carry forward" what is in the index without losing
696 + * information across a "fast forward", favoring a successful merge
697 + * over a merge failure when it makes sense. For details of the
698 + * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
699 + *
700 + */
701 +int twoway_merge(struct cache_entry **src,
702 + struct unpack_trees_options *o)
703 +{
704 + struct cache_entry *current = src[0];
705 + struct cache_entry *oldtree = src[1], *newtree = src[2];
706 +
707 + if (o->merge_size != 2)
708 + return error("Cannot do a twoway merge of %d trees",
709 + o->merge_size);
710 +
711 + if (current) {
712 + if ((!oldtree && !newtree) || /* 4 and 5 */
713 + (!oldtree && newtree &&
714 + same(current, newtree)) || /* 6 and 7 */
715 + (oldtree && newtree &&
716 + same(oldtree, newtree)) || /* 14 and 15 */
717 + (oldtree && newtree &&
718 + !same(oldtree, newtree) && /* 18 and 19*/
719 + same(current, newtree))) {
720 + return keep_entry(current);
721 + }
722 + else if (oldtree && !newtree && same(current, oldtree)) {
723 + /* 10 or 11 */
724 + return deleted_entry(oldtree, current, o);
725 + }
726 + else if (oldtree && newtree &&
727 + same(current, oldtree) && !same(current, newtree)) {
728 + /* 20 or 21 */
729 + return merged_entry(newtree, current, o);
730 + }
731 + else {
732 + /* all other failures */
733 + if (oldtree)
734 + reject_merge(oldtree);
735 + if (current)
736 + reject_merge(current);
737 + if (newtree)
738 + reject_merge(newtree);
739 + return -1;
740 + }
741 + }
742 + else if (newtree)
743 + return merged_entry(newtree, current, o);
744 + else
745 + return deleted_entry(oldtree, current, o);
746 +}
747 +
748 +/*
749 + * Bind merge.
750 + *
751 + * Keep the index entries at stage0, collapse stage1 but make sure
752 + * stage0 does not have anything there.
753 + */
754 +int bind_merge(struct cache_entry **src,
755 + struct unpack_trees_options *o)
756 +{
757 + struct cache_entry *old = src[0];
758 + struct cache_entry *a = src[1];
759 +
760 + if (o->merge_size != 1)
761 + return error("Cannot do a bind merge of %d trees\n",
762 + o->merge_size);
763 + if (a && old)
764 + die("Entry '%s' overlaps. Cannot bind.", a->name);
765 + if (!a)
766 + return keep_entry(old);
767 + else
768 + return merged_entry(a, NULL, o);
769 +}
770 +
771 +/*
772 + * One-way merge.
773 + *
774 + * The rule is:
775 + * - take the stat information from stage0, take the data from stage1
776 + */
777 +int oneway_merge(struct cache_entry **src,
778 + struct unpack_trees_options *o)
779 +{
780 + struct cache_entry *old = src[0];
781 + struct cache_entry *a = src[1];
782 +
783 + if (o->merge_size != 1)
784 + return error("Cannot do a oneway merge of %d trees",
785 + o->merge_size);
786 +
787 + if (!a)
788 + return deleted_entry(old, old, o);
789 + if (old && same(old, a)) {
790 + if (o->reset) {
791 + struct stat st;
792 + if (lstat(old->name, &st) ||
793 + ce_match_stat(old, &st, 1))
794 + old->ce_flags |= htons(CE_UPDATE);
795 + }
796 + return keep_entry(old);
797 + }
798 + return merged_entry(a, old, o);
799 +}
unpack-trees.h
+5
@@ -27,4 +27,9 @@ struct unpack_trees_options {
27 extern int unpack_trees(struct object_list *trees,
28 struct unpack_trees_options *options);
29
30 +int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o);
31 +int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o);
32 +int bind_merge(struct cache_entry **src, struct unpack_trees_options *o);
33 +int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o);
34 +
35 #endif