git-write-tree writes garbage on sparc64
[git/debian.git] / read-tree.c
blob49436bf96fa4df29e4e12c587c2de4f9c68e7e21
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define DBRT_DEBUG 1
8 #include "cache.h"
10 #include "object.h"
11 #include "tree.h"
12 #include "cache-tree.h"
13 #include <sys/time.h>
14 #include <signal.h>
16 static int merge = 0;
17 static int update = 0;
18 static int index_only = 0;
19 static int nontrivial_merge = 0;
20 static int trivial_merges_only = 0;
21 static int aggressive = 0;
22 static int verbose_update = 0;
23 static volatile int progress_update = 0;
25 static int head_idx = -1;
26 static int merge_size = 0;
28 static struct object_list *trees = NULL;
30 static struct cache_entry df_conflict_entry = {
33 static struct tree_entry_list df_conflict_list = {
34 .name = NULL,
35 .next = &df_conflict_list
38 typedef int (*merge_fn_t)(struct cache_entry **src);
40 static int entcmp(char *name1, int dir1, char *name2, int dir2)
42 int len1 = strlen(name1);
43 int len2 = strlen(name2);
44 int len = len1 < len2 ? len1 : len2;
45 int ret = memcmp(name1, name2, len);
46 unsigned char c1, c2;
47 if (ret)
48 return ret;
49 c1 = name1[len];
50 c2 = name2[len];
51 if (!c1 && dir1)
52 c1 = '/';
53 if (!c2 && dir2)
54 c2 = '/';
55 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
56 if (c1 && c2 && !ret)
57 ret = len1 - len2;
58 return ret;
61 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
62 const char *base, merge_fn_t fn, int *indpos)
64 int baselen = strlen(base);
65 int src_size = len + 1;
66 do {
67 int i;
68 char *first;
69 int firstdir = 0;
70 int pathlen;
71 unsigned ce_size;
72 struct tree_entry_list **subposns;
73 struct cache_entry **src;
74 int any_files = 0;
75 int any_dirs = 0;
76 char *cache_name;
77 int ce_stage;
79 /* Find the first name in the input. */
81 first = NULL;
82 cache_name = NULL;
84 /* Check the cache */
85 if (merge && *indpos < active_nr) {
86 /* This is a bit tricky: */
87 /* If the index has a subdirectory (with
88 * contents) as the first name, it'll get a
89 * filename like "foo/bar". But that's after
90 * "foo", so the entry in trees will get
91 * handled first, at which point we'll go into
92 * "foo", and deal with "bar" from the index,
93 * because the base will be "foo/". The only
94 * way we can actually have "foo/bar" first of
95 * all the things is if the trees don't
96 * contain "foo" at all, in which case we'll
97 * handle "foo/bar" without going into the
98 * directory, but that's fine (and will return
99 * an error anyway, with the added unknown
100 * file case.
103 cache_name = active_cache[*indpos]->name;
104 if (strlen(cache_name) > baselen &&
105 !memcmp(cache_name, base, baselen)) {
106 cache_name += baselen;
107 first = cache_name;
108 } else {
109 cache_name = NULL;
113 #if DBRT_DEBUG > 1
114 if (first)
115 printf("index %s\n", first);
116 #endif
117 for (i = 0; i < len; i++) {
118 if (!posns[i] || posns[i] == &df_conflict_list)
119 continue;
120 #if DBRT_DEBUG > 1
121 printf("%d %s\n", i + 1, posns[i]->name);
122 #endif
123 if (!first || entcmp(first, firstdir,
124 posns[i]->name,
125 posns[i]->directory) > 0) {
126 first = posns[i]->name;
127 firstdir = posns[i]->directory;
130 /* No name means we're done */
131 if (!first)
132 return 0;
134 pathlen = strlen(first);
135 ce_size = cache_entry_size(baselen + pathlen);
137 src = xcalloc(src_size, sizeof(struct cache_entry *));
139 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
141 if (cache_name && !strcmp(cache_name, first)) {
142 any_files = 1;
143 src[0] = active_cache[*indpos];
144 remove_cache_entry_at(*indpos);
147 for (i = 0; i < len; i++) {
148 struct cache_entry *ce;
150 if (!posns[i] ||
151 (posns[i] != &df_conflict_list &&
152 strcmp(first, posns[i]->name))) {
153 continue;
156 if (posns[i] == &df_conflict_list) {
157 src[i + merge] = &df_conflict_entry;
158 continue;
161 if (posns[i]->directory) {
162 any_dirs = 1;
163 parse_tree(posns[i]->item.tree);
164 subposns[i] = posns[i]->item.tree->entries;
165 posns[i] = posns[i]->next;
166 src[i + merge] = &df_conflict_entry;
167 continue;
170 if (!merge)
171 ce_stage = 0;
172 else if (i + 1 < head_idx)
173 ce_stage = 1;
174 else if (i + 1 > head_idx)
175 ce_stage = 3;
176 else
177 ce_stage = 2;
179 ce = xcalloc(1, ce_size);
180 ce->ce_mode = create_ce_mode(posns[i]->mode);
181 ce->ce_flags = create_ce_flags(baselen + pathlen,
182 ce_stage);
183 memcpy(ce->name, base, baselen);
184 memcpy(ce->name + baselen, first, pathlen + 1);
186 any_files = 1;
188 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
189 src[i + merge] = ce;
190 subposns[i] = &df_conflict_list;
191 posns[i] = posns[i]->next;
193 if (any_files) {
194 if (merge) {
195 int ret;
197 #if DBRT_DEBUG > 1
198 printf("%s:\n", first);
199 for (i = 0; i < src_size; i++) {
200 printf(" %d ", i);
201 if (src[i])
202 printf("%s\n", sha1_to_hex(src[i]->sha1));
203 else
204 printf("\n");
206 #endif
207 ret = fn(src);
209 #if DBRT_DEBUG > 1
210 printf("Added %d entries\n", ret);
211 #endif
212 *indpos += ret;
213 } else {
214 for (i = 0; i < src_size; i++) {
215 if (src[i]) {
216 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
221 if (any_dirs) {
222 char *newbase = xmalloc(baselen + 2 + pathlen);
223 memcpy(newbase, base, baselen);
224 memcpy(newbase + baselen, first, pathlen);
225 newbase[baselen + pathlen] = '/';
226 newbase[baselen + pathlen + 1] = '\0';
227 if (unpack_trees_rec(subposns, len, newbase, fn,
228 indpos))
229 return -1;
230 free(newbase);
232 free(subposns);
233 free(src);
234 } while (1);
237 static void reject_merge(struct cache_entry *ce)
239 die("Entry '%s' would be overwritten by merge. Cannot merge.",
240 ce->name);
243 /* Unlink the last component and attempt to remove leading
244 * directories, in case this unlink is the removal of the
245 * last entry in the directory -- empty directories are removed.
247 static void unlink_entry(char *name)
249 char *cp, *prev;
251 if (unlink(name))
252 return;
253 prev = NULL;
254 while (1) {
255 int status;
256 cp = strrchr(name, '/');
257 if (prev)
258 *prev = '/';
259 if (!cp)
260 break;
262 *cp = 0;
263 status = rmdir(name);
264 if (status) {
265 *cp = '/';
266 break;
268 prev = cp;
272 static void progress_interval(int signum)
274 progress_update = 1;
277 static void setup_progress_signal(void)
279 struct sigaction sa;
280 struct itimerval v;
282 memset(&sa, 0, sizeof(sa));
283 sa.sa_handler = progress_interval;
284 sigemptyset(&sa.sa_mask);
285 sa.sa_flags = SA_RESTART;
286 sigaction(SIGALRM, &sa, NULL);
288 v.it_interval.tv_sec = 1;
289 v.it_interval.tv_usec = 0;
290 v.it_value = v.it_interval;
291 setitimer(ITIMER_REAL, &v, NULL);
294 static void check_updates(struct cache_entry **src, int nr)
296 static struct checkout state = {
297 .base_dir = "",
298 .force = 1,
299 .quiet = 1,
300 .refresh_cache = 1,
302 unsigned short mask = htons(CE_UPDATE);
303 unsigned last_percent = 200, cnt = 0, total = 0;
305 if (update && verbose_update) {
306 for (total = cnt = 0; cnt < nr; cnt++) {
307 struct cache_entry *ce = src[cnt];
308 if (!ce->ce_mode || ce->ce_flags & mask)
309 total++;
312 /* Don't bother doing this for very small updates */
313 if (total < 250)
314 total = 0;
316 if (total) {
317 fprintf(stderr, "Checking files out...\n");
318 setup_progress_signal();
319 progress_update = 1;
321 cnt = 0;
324 while (nr--) {
325 struct cache_entry *ce = *src++;
327 if (total) {
328 if (!ce->ce_mode || ce->ce_flags & mask) {
329 unsigned percent;
330 cnt++;
331 percent = (cnt * 100) / total;
332 if (percent != last_percent ||
333 progress_update) {
334 fprintf(stderr, "%4u%% (%u/%u) done\r",
335 percent, cnt, total);
336 last_percent = percent;
340 if (!ce->ce_mode) {
341 if (update)
342 unlink_entry(ce->name);
343 continue;
345 if (ce->ce_flags & mask) {
346 ce->ce_flags &= ~mask;
347 if (update)
348 checkout_entry(ce, &state, NULL);
351 if (total) {
352 signal(SIGALRM, SIG_IGN);
353 fputc('\n', stderr);
357 static int unpack_trees(merge_fn_t fn)
359 int indpos = 0;
360 unsigned len = object_list_length(trees);
361 struct tree_entry_list **posns;
362 int i;
363 struct object_list *posn = trees;
364 merge_size = len;
366 if (len) {
367 posns = xmalloc(len * sizeof(struct tree_entry_list *));
368 for (i = 0; i < len; i++) {
369 posns[i] = ((struct tree *) posn->item)->entries;
370 posn = posn->next;
372 if (unpack_trees_rec(posns, len, "", fn, &indpos))
373 return -1;
376 if (trivial_merges_only && nontrivial_merge)
377 die("Merge requires file-level merging");
379 check_updates(active_cache, active_nr);
380 return 0;
383 static int list_tree(unsigned char *sha1)
385 struct tree *tree = parse_tree_indirect(sha1);
386 if (!tree)
387 return -1;
388 object_list_append(&tree->object, &trees);
389 return 0;
392 static int same(struct cache_entry *a, struct cache_entry *b)
394 if (!!a != !!b)
395 return 0;
396 if (!a && !b)
397 return 1;
398 return a->ce_mode == b->ce_mode &&
399 !memcmp(a->sha1, b->sha1, 20);
404 * When a CE gets turned into an unmerged entry, we
405 * want it to be up-to-date
407 static void verify_uptodate(struct cache_entry *ce)
409 struct stat st;
411 if (index_only)
412 return;
414 if (!lstat(ce->name, &st)) {
415 unsigned changed = ce_match_stat(ce, &st, 1);
416 if (!changed)
417 return;
418 errno = 0;
420 if (errno == ENOENT)
421 return;
422 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
425 static void invalidate_ce_path(struct cache_entry *ce)
427 if (ce)
428 cache_tree_invalidate_path(active_cache_tree, ce->name);
431 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
433 merge->ce_flags |= htons(CE_UPDATE);
434 if (old) {
436 * See if we can re-use the old CE directly?
437 * That way we get the uptodate stat info.
439 * This also removes the UPDATE flag on
440 * a match.
442 if (same(old, merge)) {
443 *merge = *old;
444 } else {
445 verify_uptodate(old);
446 invalidate_ce_path(old);
449 else
450 invalidate_ce_path(merge);
451 merge->ce_flags &= ~htons(CE_STAGEMASK);
452 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
453 return 1;
456 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
458 if (old)
459 verify_uptodate(old);
460 ce->ce_mode = 0;
461 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
462 invalidate_ce_path(ce);
463 return 1;
466 static int keep_entry(struct cache_entry *ce)
468 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
469 return 1;
472 #if DBRT_DEBUG
473 static void show_stage_entry(FILE *o,
474 const char *label, const struct cache_entry *ce)
476 if (!ce)
477 fprintf(o, "%s (missing)\n", label);
478 else
479 fprintf(o, "%s%06o %s %d\t%s\n",
480 label,
481 ntohl(ce->ce_mode),
482 sha1_to_hex(ce->sha1),
483 ce_stage(ce),
484 ce->name);
486 #endif
488 static int threeway_merge(struct cache_entry **stages)
490 struct cache_entry *index;
491 struct cache_entry *head;
492 struct cache_entry *remote = stages[head_idx + 1];
493 int count;
494 int head_match = 0;
495 int remote_match = 0;
497 int df_conflict_head = 0;
498 int df_conflict_remote = 0;
500 int any_anc_missing = 0;
501 int no_anc_exists = 1;
502 int i;
504 for (i = 1; i < head_idx; i++) {
505 if (!stages[i])
506 any_anc_missing = 1;
507 else
508 no_anc_exists = 0;
511 index = stages[0];
512 head = stages[head_idx];
514 if (head == &df_conflict_entry) {
515 df_conflict_head = 1;
516 head = NULL;
519 if (remote == &df_conflict_entry) {
520 df_conflict_remote = 1;
521 remote = NULL;
524 /* First, if there's a #16 situation, note that to prevent #13
525 * and #14.
527 if (!same(remote, head)) {
528 for (i = 1; i < head_idx; i++) {
529 if (same(stages[i], head)) {
530 head_match = i;
532 if (same(stages[i], remote)) {
533 remote_match = i;
538 /* We start with cases where the index is allowed to match
539 * something other than the head: #14(ALT) and #2ALT, where it
540 * is permitted to match the result instead.
542 /* #14, #14ALT, #2ALT */
543 if (remote && !df_conflict_head && head_match && !remote_match) {
544 if (index && !same(index, remote) && !same(index, head))
545 reject_merge(index);
546 return merged_entry(remote, index);
549 * If we have an entry in the index cache, then we want to
550 * make sure that it matches head.
552 if (index && !same(index, head)) {
553 reject_merge(index);
556 if (head) {
557 /* #5ALT, #15 */
558 if (same(head, remote))
559 return merged_entry(head, index);
560 /* #13, #3ALT */
561 if (!df_conflict_remote && remote_match && !head_match)
562 return merged_entry(head, index);
565 /* #1 */
566 if (!head && !remote && any_anc_missing)
567 return 0;
569 /* Under the new "aggressive" rule, we resolve mostly trivial
570 * cases that we historically had git-merge-one-file resolve.
572 if (aggressive) {
573 int head_deleted = !head && !df_conflict_head;
574 int remote_deleted = !remote && !df_conflict_remote;
576 * Deleted in both.
577 * Deleted in one and unchanged in the other.
579 if ((head_deleted && remote_deleted) ||
580 (head_deleted && remote && remote_match) ||
581 (remote_deleted && head && head_match)) {
582 if (index)
583 return deleted_entry(index, index);
584 return 0;
587 * Added in both, identically.
589 if (no_anc_exists && head && remote && same(head, remote))
590 return merged_entry(head, index);
594 /* Below are "no merge" cases, which require that the index be
595 * up-to-date to avoid the files getting overwritten with
596 * conflict resolution files.
598 if (index) {
599 verify_uptodate(index);
602 nontrivial_merge = 1;
604 /* #2, #3, #4, #6, #7, #9, #11. */
605 count = 0;
606 if (!head_match || !remote_match) {
607 for (i = 1; i < head_idx; i++) {
608 if (stages[i]) {
609 keep_entry(stages[i]);
610 count++;
611 break;
615 #if DBRT_DEBUG
616 else {
617 fprintf(stderr, "read-tree: warning #16 detected\n");
618 show_stage_entry(stderr, "head ", stages[head_match]);
619 show_stage_entry(stderr, "remote ", stages[remote_match]);
621 #endif
622 if (head) { count += keep_entry(head); }
623 if (remote) { count += keep_entry(remote); }
624 return count;
628 * Two-way merge.
630 * The rule is to "carry forward" what is in the index without losing
631 * information across a "fast forward", favoring a successful merge
632 * over a merge failure when it makes sense. For details of the
633 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
636 static int twoway_merge(struct cache_entry **src)
638 struct cache_entry *current = src[0];
639 struct cache_entry *oldtree = src[1], *newtree = src[2];
641 if (merge_size != 2)
642 return error("Cannot do a twoway merge of %d trees",
643 merge_size);
645 if (current) {
646 if ((!oldtree && !newtree) || /* 4 and 5 */
647 (!oldtree && newtree &&
648 same(current, newtree)) || /* 6 and 7 */
649 (oldtree && newtree &&
650 same(oldtree, newtree)) || /* 14 and 15 */
651 (oldtree && newtree &&
652 !same(oldtree, newtree) && /* 18 and 19*/
653 same(current, newtree))) {
654 return keep_entry(current);
656 else if (oldtree && !newtree && same(current, oldtree)) {
657 /* 10 or 11 */
658 return deleted_entry(oldtree, current);
660 else if (oldtree && newtree &&
661 same(current, oldtree) && !same(current, newtree)) {
662 /* 20 or 21 */
663 return merged_entry(newtree, current);
665 else {
666 /* all other failures */
667 if (oldtree)
668 reject_merge(oldtree);
669 if (current)
670 reject_merge(current);
671 if (newtree)
672 reject_merge(newtree);
673 return -1;
676 else if (newtree)
677 return merged_entry(newtree, current);
678 else
679 return deleted_entry(oldtree, current);
683 * One-way merge.
685 * The rule is:
686 * - take the stat information from stage0, take the data from stage1
688 static int oneway_merge(struct cache_entry **src)
690 struct cache_entry *old = src[0];
691 struct cache_entry *a = src[1];
693 if (merge_size != 1)
694 return error("Cannot do a oneway merge of %d trees",
695 merge_size);
697 if (!a) {
698 invalidate_ce_path(old);
699 return 0;
701 if (old && same(old, a)) {
702 return keep_entry(old);
704 return merged_entry(a, NULL);
707 static int read_cache_unmerged(void)
709 int i, deleted;
710 struct cache_entry **dst;
712 read_cache();
713 dst = active_cache;
714 deleted = 0;
715 for (i = 0; i < active_nr; i++) {
716 struct cache_entry *ce = active_cache[i];
717 if (ce_stage(ce)) {
718 deleted++;
719 invalidate_ce_path(ce);
720 continue;
722 if (deleted)
723 *dst = ce;
724 dst++;
726 active_nr -= deleted;
727 return deleted;
730 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
732 struct tree_entry_list *ent;
733 int cnt;
735 memcpy(it->sha1, tree->object.sha1, 20);
736 for (cnt = 0, ent = tree->entries; ent; ent = ent->next) {
737 if (!ent->directory)
738 cnt++;
739 else {
740 struct cache_tree_sub *sub;
741 struct tree *subtree = (struct tree *)ent->item.tree;
742 if (!subtree->object.parsed)
743 parse_tree(subtree);
744 sub = cache_tree_sub(it, ent->name);
745 sub->cache_tree = cache_tree();
746 prime_cache_tree_rec(sub->cache_tree, subtree);
747 cnt += sub->cache_tree->entry_count;
750 it->entry_count = cnt;
753 static void prime_cache_tree(void)
755 struct tree *tree = (struct tree *)trees->item;
756 if (!tree)
757 return;
758 active_cache_tree = cache_tree();
759 prime_cache_tree_rec(active_cache_tree, tree);
763 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
765 static struct cache_file cache_file;
767 int main(int argc, char **argv)
769 int i, newfd, reset, stage = 0;
770 unsigned char sha1[20];
771 merge_fn_t fn = NULL;
773 setup_git_directory();
774 git_config(git_default_config);
776 newfd = hold_index_file_for_update(&cache_file, get_index_file());
777 if (newfd < 0)
778 die("unable to create new cachefile");
780 git_config(git_default_config);
782 merge = 0;
783 reset = 0;
784 for (i = 1; i < argc; i++) {
785 const char *arg = argv[i];
787 /* "-u" means "update", meaning that a merge will update
788 * the working tree.
790 if (!strcmp(arg, "-u")) {
791 update = 1;
792 continue;
795 if (!strcmp(arg, "-v")) {
796 verbose_update = 1;
797 continue;
800 /* "-i" means "index only", meaning that a merge will
801 * not even look at the working tree.
803 if (!strcmp(arg, "-i")) {
804 index_only = 1;
805 continue;
808 /* This differs from "-m" in that we'll silently ignore unmerged entries */
809 if (!strcmp(arg, "--reset")) {
810 if (stage || merge)
811 usage(read_tree_usage);
812 reset = 1;
813 merge = 1;
814 stage = 1;
815 read_cache_unmerged();
816 continue;
819 if (!strcmp(arg, "--trivial")) {
820 trivial_merges_only = 1;
821 continue;
824 if (!strcmp(arg, "--aggressive")) {
825 aggressive = 1;
826 continue;
829 /* "-m" stands for "merge", meaning we start in stage 1 */
830 if (!strcmp(arg, "-m")) {
831 if (stage || merge)
832 usage(read_tree_usage);
833 if (read_cache_unmerged())
834 die("you need to resolve your current index first");
835 stage = 1;
836 merge = 1;
837 continue;
840 /* using -u and -i at the same time makes no sense */
841 if (1 < index_only + update)
842 usage(read_tree_usage);
844 if (get_sha1(arg, sha1) < 0)
845 usage(read_tree_usage);
846 if (list_tree(sha1) < 0)
847 die("failed to unpack tree object %s", arg);
848 stage++;
850 if ((update||index_only) && !merge)
851 usage(read_tree_usage);
853 if (merge) {
854 if (stage < 2)
855 die("just how do you expect me to merge %d trees?", stage-1);
856 switch (stage - 1) {
857 case 1:
858 fn = oneway_merge;
859 break;
860 case 2:
861 fn = twoway_merge;
862 break;
863 case 3:
864 default:
865 fn = threeway_merge;
866 cache_tree_free(&active_cache_tree);
867 break;
870 if (stage - 1 >= 3)
871 head_idx = stage - 2;
872 else
873 head_idx = 1;
876 unpack_trees(fn);
879 * When reading only one tree (either the most basic form,
880 * "-m ent" or "--reset ent" form), we can obtain a fully
881 * valid cache-tree because the index must match exactly
882 * what came from the tree.
884 if (trees && trees->item && (!merge || (stage == 2))) {
885 cache_tree_free(&active_cache_tree);
886 prime_cache_tree();
889 if (write_cache(newfd, active_cache, active_nr) ||
890 commit_index_file(&cache_file))
891 die("unable to write new index file");
892 return 0;