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