git-pull: refuse default merge without branch.*.merge
[git/git-svn.git] / unpack-trees.c
blobb8689ebc86bb69c21fd650572c55a4c3d246a183
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"
10 #define DBRT_DEBUG 1
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;
22 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
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;
29 if (!tree->object.parsed)
30 parse_tree(tree);
32 desc.buf = tree->buffer;
33 desc.size = tree->size;
35 while (tree_entry(&desc, &one)) {
36 struct tree_entry_list *entry;
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;
47 *list_p = entry;
48 list_p = &entry->next;
50 return ret;
53 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
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;
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)
79 int baselen = strlen(base);
80 int src_size = len + 1;
81 int i_stk = i_stk;
82 int retval = 0;
84 if (o->dir)
85 i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
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;
100 /* Find the first name in the input. */
102 first = NULL;
103 cache_name = NULL;
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.
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;
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;
151 /* No name means we're done */
152 if (!first)
153 goto leave_directory;
155 pathlen = strlen(first);
156 ce_size = cache_entry_size(baselen + pathlen);
158 src = xcalloc(src_size, sizeof(struct cache_entry *));
160 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
162 if (cache_name && !strcmp(cache_name, first)) {
163 any_files = 1;
164 src[0] = active_cache[*indpos];
165 remove_cache_entry_at(*indpos);
168 for (i = 0; i < len; i++) {
169 struct cache_entry *ce;
171 if (!posns[i] ||
172 (posns[i] != df_conflict_list &&
173 strcmp(first, posns[i]->name))) {
174 continue;
177 if (posns[i] == df_conflict_list) {
178 src[i + o->merge] = o->df_conflict_entry;
179 continue;
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;
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;
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);
208 any_files = 1;
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;
215 if (any_files) {
216 if (o->merge) {
217 int ret;
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");
228 #endif
229 ret = o->fn(src, o);
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);
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;
254 free(newbase);
256 free(subposns);
257 free(src);
258 } while (1);
260 leave_directory:
261 if (o->dir)
262 pop_exclude_per_directory(o->dir, i_stk);
263 return retval;
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.
270 static void unlink_entry(char *name)
272 char *cp, *prev;
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;
285 *cp = 0;
286 status = rmdir(name);
287 if (status) {
288 *cp = '/';
289 break;
291 prev = cp;
295 static volatile sig_atomic_t progress_update;
297 static void progress_interval(int signum)
299 progress_update = 1;
302 static void setup_progress_signal(void)
304 struct sigaction sa;
305 struct itimerval v;
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);
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);
319 static struct checkout state;
320 static void check_updates(struct cache_entry **src, int nr,
321 struct unpack_trees_options *o)
323 unsigned short mask = htons(CE_UPDATE);
324 unsigned last_percent = 200, cnt = 0, total = 0;
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++;
333 /* Don't bother doing this for very small updates */
334 if (total < 250)
335 total = 0;
337 if (total) {
338 fprintf(stderr, "Checking files out...\n");
339 setup_progress_signal();
340 progress_update = 1;
342 cnt = 0;
345 while (nr--) {
346 struct cache_entry *ce = *src++;
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;
362 if (!ce->ce_mode) {
363 if (o->update)
364 unlink_entry(ce->name);
365 continue;
367 if (ce->ce_flags & mask) {
368 ce->ce_flags &= ~mask;
369 if (o->update)
370 checkout_entry(ce, &state, NULL);
373 if (total) {
374 signal(SIGALRM, SIG_IGN);
375 fputc('\n', stderr);
379 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
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;
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;
397 o->merge_size = len;
399 if (!dfc)
400 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
401 o->df_conflict_entry = dfc;
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;
409 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
410 o, &indpos, &df_conflict_list))
411 return -1;
414 if (o->trivial_merges_only && o->nontrivial_merge)
415 die("Merge requires file-level merging");
417 check_updates(active_cache, active_nr, o);
418 return 0;
421 /* Here come the merge functions */
423 static void reject_merge(struct cache_entry *ce)
425 die("Entry '%s' would be overwritten by merge. Cannot merge.",
426 ce->name);
429 static int same(struct cache_entry *a, struct cache_entry *b)
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);
441 * When a CE gets turned into an unmerged entry, we
442 * want it to be up-to-date
444 static void verify_uptodate(struct cache_entry *ce,
445 struct unpack_trees_options *o)
447 struct stat st;
449 if (o->index_only || o->reset)
450 return;
452 if (!lstat(ce->name, &st)) {
453 unsigned changed = ce_match_stat(ce, &st, 1);
454 if (!changed)
455 return;
456 errno = 0;
458 if (o->reset) {
459 ce->ce_flags |= htons(CE_UPDATE);
460 return;
462 if (errno == ENOENT)
463 return;
464 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
467 static void invalidate_ce_path(struct cache_entry *ce)
469 if (ce)
470 cache_tree_invalidate_path(active_cache_tree, ce->name);
474 * We do not want to remove or overwrite a working tree file that
475 * is not tracked, unless it is ignored.
477 static void verify_absent(const char *path, const char *action,
478 struct unpack_trees_options *o)
480 struct stat st;
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);
489 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
490 struct unpack_trees_options *o)
492 merge->ce_flags |= htons(CE_UPDATE);
493 if (old) {
495 * See if we can re-use the old CE directly?
496 * That way we get the uptodate stat info.
498 * This also removes the UPDATE flag on
499 * a match.
501 if (same(old, merge)) {
502 *merge = *old;
503 } else {
504 verify_uptodate(old, o);
505 invalidate_ce_path(old);
508 else {
509 verify_absent(merge->name, "overwritten", o);
510 invalidate_ce_path(merge);
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;
518 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
519 struct unpack_trees_options *o)
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;
531 static int keep_entry(struct cache_entry *ce)
533 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
534 return 1;
537 #if DBRT_DEBUG
538 static void show_stage_entry(FILE *o,
539 const char *label, const struct cache_entry *ce)
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);
551 #endif
553 int threeway_merge(struct cache_entry **stages,
554 struct unpack_trees_options *o)
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;
564 int df_conflict_head = 0;
565 int df_conflict_remote = 0;
567 int any_anc_missing = 0;
568 int no_anc_exists = 1;
569 int i;
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;
581 index = stages[0];
582 head = stages[o->head_idx];
584 if (head == o->df_conflict_entry) {
585 df_conflict_head = 1;
586 head = NULL;
589 if (remote == o->df_conflict_entry) {
590 df_conflict_remote = 1;
591 remote = NULL;
594 if (!path && index)
595 path = index->name;
596 if (!path && head)
597 path = head->name;
598 if (!path && remote)
599 path = remote->name;
601 /* First, if there's a #16 situation, note that to prevent #13
602 * and #14.
604 if (!same(remote, head)) {
605 for (i = 1; i < o->head_idx; i++) {
606 if (same(stages[i], head)) {
607 head_match = i;
609 if (same(stages[i], remote)) {
610 remote_match = i;
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.
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);
626 * If we have an entry in the index cache, then we want to
627 * make sure that it matches head.
629 if (index && !same(index, head)) {
630 reject_merge(index);
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);
642 /* #1 */
643 if (!head && !remote && any_anc_missing)
644 return 0;
646 /* Under the new "aggressive" rule, we resolve mostly trivial
647 * cases that we historically had git-merge-one-file resolve.
649 if (o->aggressive) {
650 int head_deleted = !head && !df_conflict_head;
651 int remote_deleted = !remote && !df_conflict_remote;
653 * Deleted in both.
654 * Deleted in one and unchanged in the other.
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;
666 * Added in both, identically.
668 if (no_anc_exists && head && remote && same(head, remote))
669 return merged_entry(head, index, o);
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.
677 if (index) {
678 verify_uptodate(index, o);
681 o->nontrivial_merge = 1;
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;
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]);
700 #endif
701 if (head) { count += keep_entry(head); }
702 if (remote) { count += keep_entry(remote); }
703 return count;
707 * Two-way merge.
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>.
715 int twoway_merge(struct cache_entry **src,
716 struct unpack_trees_options *o)
718 struct cache_entry *current = src[0];
719 struct cache_entry *oldtree = src[1], *newtree = src[2];
721 if (o->merge_size != 2)
722 return error("Cannot do a twoway merge of %d trees",
723 o->merge_size);
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);
736 else if (oldtree && !newtree && same(current, oldtree)) {
737 /* 10 or 11 */
738 return deleted_entry(oldtree, current, o);
740 else if (oldtree && newtree &&
741 same(current, oldtree) && !same(current, newtree)) {
742 /* 20 or 21 */
743 return merged_entry(newtree, current, o);
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;
756 else if (newtree)
757 return merged_entry(newtree, current, o);
758 else
759 return deleted_entry(oldtree, current, o);
763 * Bind merge.
765 * Keep the index entries at stage0, collapse stage1 but make sure
766 * stage0 does not have anything there.
768 int bind_merge(struct cache_entry **src,
769 struct unpack_trees_options *o)
771 struct cache_entry *old = src[0];
772 struct cache_entry *a = src[1];
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);
786 * One-way merge.
788 * The rule is:
789 * - take the stat information from stage0, take the data from stage1
791 int oneway_merge(struct cache_entry **src,
792 struct unpack_trees_options *o)
794 struct cache_entry *old = src[0];
795 struct cache_entry *a = src[1];
797 if (o->merge_size != 1)
798 return error("Cannot do a oneway merge of %d trees",
799 o->merge_size);
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);
810 return keep_entry(old);
812 return merged_entry(a, old, o);