ls-files: honour per-directory ignore file from higher directories.
[git/jrn.git] / read-tree.c
blob5580f15ba2c89f9fd1ed35de0b0183487d024bf8
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"
13 static int merge = 0;
14 static int update = 0;
15 static int index_only = 0;
16 static int nontrivial_merge = 0;
17 static int trivial_merges_only = 0;
18 static int aggressive = 0;
20 static int head_idx = -1;
21 static int merge_size = 0;
23 static struct object_list *trees = NULL;
25 static struct cache_entry df_conflict_entry = {
28 static struct tree_entry_list df_conflict_list = {
29 .name = NULL,
30 .next = &df_conflict_list
33 typedef int (*merge_fn_t)(struct cache_entry **src);
35 static int entcmp(char *name1, int dir1, char *name2, int dir2)
37 int len1 = strlen(name1);
38 int len2 = strlen(name2);
39 int len = len1 < len2 ? len1 : len2;
40 int ret = memcmp(name1, name2, len);
41 unsigned char c1, c2;
42 if (ret)
43 return ret;
44 c1 = name1[len];
45 c2 = name2[len];
46 if (!c1 && dir1)
47 c1 = '/';
48 if (!c2 && dir2)
49 c2 = '/';
50 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
51 if (c1 && c2 && !ret)
52 ret = len1 - len2;
53 return ret;
56 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
57 const char *base, merge_fn_t fn, int *indpos)
59 int baselen = strlen(base);
60 int src_size = len + 1;
61 do {
62 int i;
63 char *first;
64 int firstdir = 0;
65 int pathlen;
66 unsigned ce_size;
67 struct tree_entry_list **subposns;
68 struct cache_entry **src;
69 int any_files = 0;
70 int any_dirs = 0;
71 char *cache_name;
72 int ce_stage;
74 /* Find the first name in the input. */
76 first = NULL;
77 cache_name = NULL;
79 /* Check the cache */
80 if (merge && *indpos < active_nr) {
81 /* This is a bit tricky: */
82 /* If the index has a subdirectory (with
83 * contents) as the first name, it'll get a
84 * filename like "foo/bar". But that's after
85 * "foo", so the entry in trees will get
86 * handled first, at which point we'll go into
87 * "foo", and deal with "bar" from the index,
88 * because the base will be "foo/". The only
89 * way we can actually have "foo/bar" first of
90 * all the things is if the trees don't
91 * contain "foo" at all, in which case we'll
92 * handle "foo/bar" without going into the
93 * directory, but that's fine (and will return
94 * an error anyway, with the added unknown
95 * file case.
98 cache_name = active_cache[*indpos]->name;
99 if (strlen(cache_name) > baselen &&
100 !memcmp(cache_name, base, baselen)) {
101 cache_name += baselen;
102 first = cache_name;
103 } else {
104 cache_name = NULL;
108 #if DBRT_DEBUG > 1
109 if (first)
110 printf("index %s\n", first);
111 #endif
112 for (i = 0; i < len; i++) {
113 if (!posns[i] || posns[i] == &df_conflict_list)
114 continue;
115 #if DBRT_DEBUG > 1
116 printf("%d %s\n", i + 1, posns[i]->name);
117 #endif
118 if (!first || entcmp(first, firstdir,
119 posns[i]->name,
120 posns[i]->directory) > 0) {
121 first = posns[i]->name;
122 firstdir = posns[i]->directory;
125 /* No name means we're done */
126 if (!first)
127 return 0;
129 pathlen = strlen(first);
130 ce_size = cache_entry_size(baselen + pathlen);
132 src = xmalloc(sizeof(struct cache_entry *) * src_size);
133 memset(src, 0, sizeof(struct cache_entry *) * src_size);
135 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
136 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
138 if (cache_name && !strcmp(cache_name, first)) {
139 any_files = 1;
140 src[0] = active_cache[*indpos];
141 remove_cache_entry_at(*indpos);
144 for (i = 0; i < len; i++) {
145 struct cache_entry *ce;
147 if (!posns[i] ||
148 (posns[i] != &df_conflict_list &&
149 strcmp(first, posns[i]->name))) {
150 continue;
153 if (posns[i] == &df_conflict_list) {
154 src[i + merge] = &df_conflict_entry;
155 continue;
158 if (posns[i]->directory) {
159 any_dirs = 1;
160 parse_tree(posns[i]->item.tree);
161 subposns[i] = posns[i]->item.tree->entries;
162 posns[i] = posns[i]->next;
163 src[i + merge] = &df_conflict_entry;
164 continue;
167 if (!merge)
168 ce_stage = 0;
169 else if (i + 1 < head_idx)
170 ce_stage = 1;
171 else if (i + 1 > head_idx)
172 ce_stage = 3;
173 else
174 ce_stage = 2;
176 ce = xmalloc(ce_size);
177 memset(ce, 0, ce_size);
178 ce->ce_mode = create_ce_mode(posns[i]->mode);
179 ce->ce_flags = create_ce_flags(baselen + pathlen,
180 ce_stage);
181 memcpy(ce->name, base, baselen);
182 memcpy(ce->name + baselen, first, pathlen + 1);
184 any_files = 1;
186 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
187 src[i + merge] = ce;
188 subposns[i] = &df_conflict_list;
189 posns[i] = posns[i]->next;
191 if (any_files) {
192 if (merge) {
193 int ret;
195 #if DBRT_DEBUG > 1
196 printf("%s:\n", first);
197 for (i = 0; i < src_size; i++) {
198 printf(" %d ", i);
199 if (src[i])
200 printf("%s\n", sha1_to_hex(src[i]->sha1));
201 else
202 printf("\n");
204 #endif
205 ret = fn(src);
207 #if DBRT_DEBUG > 1
208 printf("Added %d entries\n", ret);
209 #endif
210 *indpos += ret;
211 } else {
212 for (i = 0; i < src_size; i++) {
213 if (src[i]) {
214 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
219 if (any_dirs) {
220 char *newbase = xmalloc(baselen + 2 + pathlen);
221 memcpy(newbase, base, baselen);
222 memcpy(newbase + baselen, first, pathlen);
223 newbase[baselen + pathlen] = '/';
224 newbase[baselen + pathlen + 1] = '\0';
225 if (unpack_trees_rec(subposns, len, newbase, fn,
226 indpos))
227 return -1;
228 free(newbase);
230 free(subposns);
231 free(src);
232 } while (1);
235 static void reject_merge(struct cache_entry *ce)
237 die("Entry '%s' would be overwritten by merge. Cannot merge.",
238 ce->name);
241 /* Unlink the last component and attempt to remove leading
242 * directories, in case this unlink is the removal of the
243 * last entry in the directory -- empty directories are removed.
245 static void unlink_entry(char *name)
247 char *cp, *prev;
249 if (unlink(name))
250 return;
251 prev = NULL;
252 while (1) {
253 int status;
254 cp = strrchr(name, '/');
255 if (prev)
256 *prev = '/';
257 if (!cp)
258 break;
260 *cp = 0;
261 status = rmdir(name);
262 if (status) {
263 *cp = '/';
264 break;
266 prev = cp;
270 static void check_updates(struct cache_entry **src, int nr)
272 static struct checkout state = {
273 .base_dir = "",
274 .force = 1,
275 .quiet = 1,
276 .refresh_cache = 1,
278 unsigned short mask = htons(CE_UPDATE);
279 while (nr--) {
280 struct cache_entry *ce = *src++;
281 if (!ce->ce_mode) {
282 if (update)
283 unlink_entry(ce->name);
284 continue;
286 if (ce->ce_flags & mask) {
287 ce->ce_flags &= ~mask;
288 if (update)
289 checkout_entry(ce, &state);
294 static int unpack_trees(merge_fn_t fn)
296 int indpos = 0;
297 unsigned len = object_list_length(trees);
298 struct tree_entry_list **posns;
299 int i;
300 struct object_list *posn = trees;
301 merge_size = len;
303 if (len) {
304 posns = xmalloc(len * sizeof(struct tree_entry_list *));
305 for (i = 0; i < len; i++) {
306 posns[i] = ((struct tree *) posn->item)->entries;
307 posn = posn->next;
309 if (unpack_trees_rec(posns, len, "", fn, &indpos))
310 return -1;
313 if (trivial_merges_only && nontrivial_merge)
314 die("Merge requires file-level merging");
316 check_updates(active_cache, active_nr);
317 return 0;
320 static int list_tree(unsigned char *sha1)
322 struct tree *tree = parse_tree_indirect(sha1);
323 if (!tree)
324 return -1;
325 object_list_append(&tree->object, &trees);
326 return 0;
329 static int same(struct cache_entry *a, struct cache_entry *b)
331 if (!!a != !!b)
332 return 0;
333 if (!a && !b)
334 return 1;
335 return a->ce_mode == b->ce_mode &&
336 !memcmp(a->sha1, b->sha1, 20);
341 * When a CE gets turned into an unmerged entry, we
342 * want it to be up-to-date
344 static void verify_uptodate(struct cache_entry *ce)
346 struct stat st;
348 if (index_only)
349 return;
351 if (!lstat(ce->name, &st)) {
352 unsigned changed = ce_match_stat(ce, &st);
353 if (!changed)
354 return;
355 errno = 0;
357 if (errno == ENOENT)
358 return;
359 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
362 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
364 merge->ce_flags |= htons(CE_UPDATE);
365 if (old) {
367 * See if we can re-use the old CE directly?
368 * That way we get the uptodate stat info.
370 * This also removes the UPDATE flag on
371 * a match.
373 if (same(old, merge)) {
374 *merge = *old;
375 } else {
376 verify_uptodate(old);
379 merge->ce_flags &= ~htons(CE_STAGEMASK);
380 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
381 return 1;
384 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
386 if (old)
387 verify_uptodate(old);
388 ce->ce_mode = 0;
389 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
390 return 1;
393 static int keep_entry(struct cache_entry *ce)
395 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
396 return 1;
399 #if DBRT_DEBUG
400 static void show_stage_entry(FILE *o,
401 const char *label, const struct cache_entry *ce)
403 if (!ce)
404 fprintf(o, "%s (missing)\n", label);
405 else
406 fprintf(o, "%s%06o %s %d\t%s\n",
407 label,
408 ntohl(ce->ce_mode),
409 sha1_to_hex(ce->sha1),
410 ce_stage(ce),
411 ce->name);
413 #endif
415 static int threeway_merge(struct cache_entry **stages)
417 struct cache_entry *index;
418 struct cache_entry *head;
419 struct cache_entry *remote = stages[head_idx + 1];
420 int count;
421 int head_match = 0;
422 int remote_match = 0;
424 int df_conflict_head = 0;
425 int df_conflict_remote = 0;
427 int any_anc_missing = 0;
428 int no_anc_exists = 1;
429 int i;
431 for (i = 1; i < head_idx; i++) {
432 if (!stages[i])
433 any_anc_missing = 1;
434 else
435 no_anc_exists = 0;
438 index = stages[0];
439 head = stages[head_idx];
441 if (head == &df_conflict_entry) {
442 df_conflict_head = 1;
443 head = NULL;
446 if (remote == &df_conflict_entry) {
447 df_conflict_remote = 1;
448 remote = NULL;
451 /* First, if there's a #16 situation, note that to prevent #13
452 * and #14.
454 if (!same(remote, head)) {
455 for (i = 1; i < head_idx; i++) {
456 if (same(stages[i], head)) {
457 head_match = i;
459 if (same(stages[i], remote)) {
460 remote_match = i;
465 /* We start with cases where the index is allowed to match
466 * something other than the head: #14(ALT) and #2ALT, where it
467 * is permitted to match the result instead.
469 /* #14, #14ALT, #2ALT */
470 if (remote && !df_conflict_head && head_match && !remote_match) {
471 if (index && !same(index, remote) && !same(index, head))
472 reject_merge(index);
473 return merged_entry(remote, index);
476 * If we have an entry in the index cache, then we want to
477 * make sure that it matches head.
479 if (index && !same(index, head)) {
480 reject_merge(index);
483 if (head) {
484 /* #5ALT, #15 */
485 if (same(head, remote))
486 return merged_entry(head, index);
487 /* #13, #3ALT */
488 if (!df_conflict_remote && remote_match && !head_match)
489 return merged_entry(head, index);
492 /* #1 */
493 if (!head && !remote && any_anc_missing)
494 return 0;
496 /* Under the new "aggressive" rule, we resolve mostly trivial
497 * cases that we historically had git-merge-one-file resolve.
499 if (aggressive) {
500 int head_deleted = !head && !df_conflict_head;
501 int remote_deleted = !remote && !df_conflict_remote;
503 * Deleted in both.
504 * Deleted in one and unchanged in the other.
506 if ((head_deleted && remote_deleted) ||
507 (head_deleted && remote && remote_match) ||
508 (remote_deleted && head && head_match))
509 return 0;
512 * Added in both, identically.
514 if (no_anc_exists && head && remote && same(head, remote))
515 return merged_entry(head, index);
519 /* Below are "no merge" cases, which require that the index be
520 * up-to-date to avoid the files getting overwritten with
521 * conflict resolution files.
523 if (index) {
524 verify_uptodate(index);
527 nontrivial_merge = 1;
529 /* #2, #3, #4, #6, #7, #9, #11. */
530 count = 0;
531 if (!head_match || !remote_match) {
532 for (i = 1; i < head_idx; i++) {
533 if (stages[i]) {
534 keep_entry(stages[i]);
535 count++;
536 break;
540 #if DBRT_DEBUG
541 else {
542 fprintf(stderr, "read-tree: warning #16 detected\n");
543 show_stage_entry(stderr, "head ", stages[head_match]);
544 show_stage_entry(stderr, "remote ", stages[remote_match]);
546 #endif
547 if (head) { count += keep_entry(head); }
548 if (remote) { count += keep_entry(remote); }
549 return count;
553 * Two-way merge.
555 * The rule is to "carry forward" what is in the index without losing
556 * information across a "fast forward", favoring a successful merge
557 * over a merge failure when it makes sense. For details of the
558 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
561 static int twoway_merge(struct cache_entry **src)
563 struct cache_entry *current = src[0];
564 struct cache_entry *oldtree = src[1], *newtree = src[2];
566 if (merge_size != 2)
567 return error("Cannot do a twoway merge of %d trees\n",
568 merge_size);
570 if (current) {
571 if ((!oldtree && !newtree) || /* 4 and 5 */
572 (!oldtree && newtree &&
573 same(current, newtree)) || /* 6 and 7 */
574 (oldtree && newtree &&
575 same(oldtree, newtree)) || /* 14 and 15 */
576 (oldtree && newtree &&
577 !same(oldtree, newtree) && /* 18 and 19*/
578 same(current, newtree))) {
579 return keep_entry(current);
581 else if (oldtree && !newtree && same(current, oldtree)) {
582 /* 10 or 11 */
583 return deleted_entry(oldtree, current);
585 else if (oldtree && newtree &&
586 same(current, oldtree) && !same(current, newtree)) {
587 /* 20 or 21 */
588 return merged_entry(newtree, current);
590 else {
591 /* all other failures */
592 if (oldtree)
593 reject_merge(oldtree);
594 if (current)
595 reject_merge(current);
596 if (newtree)
597 reject_merge(newtree);
598 return -1;
601 else if (newtree)
602 return merged_entry(newtree, current);
603 else
604 return deleted_entry(oldtree, current);
608 * One-way merge.
610 * The rule is:
611 * - take the stat information from stage0, take the data from stage1
613 static int oneway_merge(struct cache_entry **src)
615 struct cache_entry *old = src[0];
616 struct cache_entry *a = src[1];
618 if (merge_size != 1)
619 return error("Cannot do a oneway merge of %d trees\n",
620 merge_size);
622 if (!a)
623 return 0;
624 if (old && same(old, a)) {
625 return keep_entry(old);
627 return merged_entry(a, NULL);
630 static int read_cache_unmerged(void)
632 int i, deleted;
633 struct cache_entry **dst;
635 read_cache();
636 dst = active_cache;
637 deleted = 0;
638 for (i = 0; i < active_nr; i++) {
639 struct cache_entry *ce = active_cache[i];
640 if (ce_stage(ce)) {
641 deleted++;
642 continue;
644 if (deleted)
645 *dst = ce;
646 dst++;
648 active_nr -= deleted;
649 return deleted;
652 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
654 static struct cache_file cache_file;
656 int main(int argc, char **argv)
658 int i, newfd, reset, stage = 0;
659 unsigned char sha1[20];
660 merge_fn_t fn = NULL;
662 setup_git_directory();
664 newfd = hold_index_file_for_update(&cache_file, get_index_file());
665 if (newfd < 0)
666 die("unable to create new cachefile");
668 git_config(git_default_config);
670 merge = 0;
671 reset = 0;
672 for (i = 1; i < argc; i++) {
673 const char *arg = argv[i];
675 /* "-u" means "update", meaning that a merge will update
676 * the working tree.
678 if (!strcmp(arg, "-u")) {
679 update = 1;
680 continue;
683 /* "-i" means "index only", meaning that a merge will
684 * not even look at the working tree.
686 if (!strcmp(arg, "-i")) {
687 index_only = 1;
688 continue;
691 /* This differs from "-m" in that we'll silently ignore unmerged entries */
692 if (!strcmp(arg, "--reset")) {
693 if (stage || merge)
694 usage(read_tree_usage);
695 reset = 1;
696 merge = 1;
697 stage = 1;
698 read_cache_unmerged();
699 continue;
702 if (!strcmp(arg, "--trivial")) {
703 trivial_merges_only = 1;
704 continue;
707 if (!strcmp(arg, "--aggressive")) {
708 aggressive = 1;
709 continue;
712 /* "-m" stands for "merge", meaning we start in stage 1 */
713 if (!strcmp(arg, "-m")) {
714 if (stage || merge)
715 usage(read_tree_usage);
716 if (read_cache_unmerged())
717 die("you need to resolve your current index first");
718 stage = 1;
719 merge = 1;
720 continue;
723 /* using -u and -i at the same time makes no sense */
724 if (1 < index_only + update)
725 usage(read_tree_usage);
727 if (get_sha1(arg, sha1) < 0)
728 usage(read_tree_usage);
729 if (list_tree(sha1) < 0)
730 die("failed to unpack tree object %s", arg);
731 stage++;
733 if ((update||index_only) && !merge)
734 usage(read_tree_usage);
736 if (merge) {
737 if (stage < 2)
738 die("just how do you expect me to merge %d trees?", stage-1);
739 switch (stage - 1) {
740 case 1:
741 fn = oneway_merge;
742 break;
743 case 2:
744 fn = twoway_merge;
745 break;
746 case 3:
747 fn = threeway_merge;
748 break;
749 default:
750 fn = threeway_merge;
751 break;
754 if (stage - 1 >= 3)
755 head_idx = stage - 2;
756 else
757 head_idx = 1;
760 unpack_trees(fn);
761 if (write_cache(newfd, active_cache, active_nr) ||
762 commit_index_file(&cache_file))
763 die("unable to write new index file");
764 return 0;