t5302: avoid using tail -c
[tgit.git] / unpack-trees.c
blob675a9998dcbab528faf2cf34ab1a66f884a36300
1 #include "cache.h"
2 #include "dir.h"
3 #include "tree.h"
4 #include "tree-walk.h"
5 #include "cache-tree.h"
6 #include "unpack-trees.h"
7 #include "progress.h"
9 #define DBRT_DEBUG 1
11 struct tree_entry_list {
12 struct tree_entry_list *next;
13 unsigned directory : 1;
14 unsigned executable : 1;
15 unsigned symlink : 1;
16 unsigned int mode;
17 const char *name;
18 const unsigned char *sha1;
21 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
23 struct tree_desc desc;
24 struct name_entry one;
25 struct tree_entry_list *ret = NULL;
26 struct tree_entry_list **list_p = &ret;
28 if (!tree->object.parsed)
29 parse_tree(tree);
31 init_tree_desc(&desc, tree->buffer, tree->size);
33 while (tree_entry(&desc, &one)) {
34 struct tree_entry_list *entry;
36 entry = xmalloc(sizeof(struct tree_entry_list));
37 entry->name = one.path;
38 entry->sha1 = one.sha1;
39 entry->mode = one.mode;
40 entry->directory = S_ISDIR(one.mode) != 0;
41 entry->executable = (one.mode & S_IXUSR) != 0;
42 entry->symlink = S_ISLNK(one.mode) != 0;
43 entry->next = NULL;
45 *list_p = entry;
46 list_p = &entry->next;
48 return ret;
51 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
53 int len1 = strlen(name1);
54 int len2 = strlen(name2);
55 int len = len1 < len2 ? len1 : len2;
56 int ret = memcmp(name1, name2, len);
57 unsigned char c1, c2;
58 if (ret)
59 return ret;
60 c1 = name1[len];
61 c2 = name2[len];
62 if (!c1 && dir1)
63 c1 = '/';
64 if (!c2 && dir2)
65 c2 = '/';
66 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
67 if (c1 && c2 && !ret)
68 ret = len1 - len2;
69 return ret;
72 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
73 const char *base, struct unpack_trees_options *o,
74 struct tree_entry_list *df_conflict_list)
76 int baselen = strlen(base);
77 int src_size = len + 1;
78 int i_stk = i_stk;
79 int retval = 0;
81 if (o->dir)
82 i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
84 do {
85 int i;
86 const char *first;
87 int firstdir = 0;
88 int pathlen;
89 unsigned ce_size;
90 struct tree_entry_list **subposns;
91 struct cache_entry **src;
92 int any_files = 0;
93 int any_dirs = 0;
94 char *cache_name;
95 int ce_stage;
97 /* Find the first name in the input. */
99 first = NULL;
100 cache_name = NULL;
102 /* Check the cache */
103 if (o->merge && o->pos < active_nr) {
104 /* This is a bit tricky: */
105 /* If the index has a subdirectory (with
106 * contents) as the first name, it'll get a
107 * filename like "foo/bar". But that's after
108 * "foo", so the entry in trees will get
109 * handled first, at which point we'll go into
110 * "foo", and deal with "bar" from the index,
111 * because the base will be "foo/". The only
112 * way we can actually have "foo/bar" first of
113 * all the things is if the trees don't
114 * contain "foo" at all, in which case we'll
115 * handle "foo/bar" without going into the
116 * directory, but that's fine (and will return
117 * an error anyway, with the added unknown
118 * file case.
121 cache_name = active_cache[o->pos]->name;
122 if (strlen(cache_name) > baselen &&
123 !memcmp(cache_name, base, baselen)) {
124 cache_name += baselen;
125 first = cache_name;
126 } else {
127 cache_name = NULL;
131 #if DBRT_DEBUG > 1
132 if (first)
133 printf("index %s\n", first);
134 #endif
135 for (i = 0; i < len; i++) {
136 if (!posns[i] || posns[i] == df_conflict_list)
137 continue;
138 #if DBRT_DEBUG > 1
139 printf("%d %s\n", i + 1, posns[i]->name);
140 #endif
141 if (!first || entcmp(first, firstdir,
142 posns[i]->name,
143 posns[i]->directory) > 0) {
144 first = posns[i]->name;
145 firstdir = posns[i]->directory;
148 /* No name means we're done */
149 if (!first)
150 goto leave_directory;
152 pathlen = strlen(first);
153 ce_size = cache_entry_size(baselen + pathlen);
155 src = xcalloc(src_size, sizeof(struct cache_entry *));
157 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
159 if (cache_name && !strcmp(cache_name, first)) {
160 any_files = 1;
161 src[0] = active_cache[o->pos];
162 remove_cache_entry_at(o->pos);
165 for (i = 0; i < len; i++) {
166 struct cache_entry *ce;
168 if (!posns[i] ||
169 (posns[i] != df_conflict_list &&
170 strcmp(first, posns[i]->name))) {
171 continue;
174 if (posns[i] == df_conflict_list) {
175 src[i + o->merge] = o->df_conflict_entry;
176 continue;
179 if (posns[i]->directory) {
180 struct tree *tree = lookup_tree(posns[i]->sha1);
181 any_dirs = 1;
182 parse_tree(tree);
183 subposns[i] = create_tree_entry_list(tree);
184 posns[i] = posns[i]->next;
185 src[i + o->merge] = o->df_conflict_entry;
186 continue;
189 if (!o->merge)
190 ce_stage = 0;
191 else if (i + 1 < o->head_idx)
192 ce_stage = 1;
193 else if (i + 1 > o->head_idx)
194 ce_stage = 3;
195 else
196 ce_stage = 2;
198 ce = xcalloc(1, ce_size);
199 ce->ce_mode = create_ce_mode(posns[i]->mode);
200 ce->ce_flags = create_ce_flags(baselen + pathlen,
201 ce_stage);
202 memcpy(ce->name, base, baselen);
203 memcpy(ce->name + baselen, first, pathlen + 1);
205 any_files = 1;
207 hashcpy(ce->sha1, posns[i]->sha1);
208 src[i + o->merge] = ce;
209 subposns[i] = df_conflict_list;
210 posns[i] = posns[i]->next;
212 if (any_files) {
213 if (o->merge) {
214 int ret;
216 #if DBRT_DEBUG > 1
217 printf("%s:\n", first);
218 for (i = 0; i < src_size; i++) {
219 printf(" %d ", i);
220 if (src[i])
221 printf("%s\n", sha1_to_hex(src[i]->sha1));
222 else
223 printf("\n");
225 #endif
226 ret = o->fn(src, o);
228 #if DBRT_DEBUG > 1
229 printf("Added %d entries\n", ret);
230 #endif
231 o->pos += ret;
232 } else {
233 for (i = 0; i < src_size; i++) {
234 if (src[i]) {
235 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
240 if (any_dirs) {
241 char *newbase = xmalloc(baselen + 2 + pathlen);
242 memcpy(newbase, base, baselen);
243 memcpy(newbase + baselen, first, pathlen);
244 newbase[baselen + pathlen] = '/';
245 newbase[baselen + pathlen + 1] = '\0';
246 if (unpack_trees_rec(subposns, len, newbase, o,
247 df_conflict_list)) {
248 retval = -1;
249 goto leave_directory;
251 free(newbase);
253 free(subposns);
254 free(src);
255 } while (1);
257 leave_directory:
258 if (o->dir)
259 pop_exclude_per_directory(o->dir, i_stk);
260 return retval;
263 /* Unlink the last component and attempt to remove leading
264 * directories, in case this unlink is the removal of the
265 * last entry in the directory -- empty directories are removed.
267 static void unlink_entry(char *name)
269 char *cp, *prev;
271 if (unlink(name))
272 return;
273 prev = NULL;
274 while (1) {
275 int status;
276 cp = strrchr(name, '/');
277 if (prev)
278 *prev = '/';
279 if (!cp)
280 break;
282 *cp = 0;
283 status = rmdir(name);
284 if (status) {
285 *cp = '/';
286 break;
288 prev = cp;
292 static struct checkout state;
293 static void check_updates(struct cache_entry **src, int nr,
294 struct unpack_trees_options *o)
296 unsigned short mask = htons(CE_UPDATE);
297 unsigned cnt = 0, total = 0;
298 struct progress progress;
300 if (o->update && o->verbose_update) {
301 for (total = cnt = 0; cnt < nr; cnt++) {
302 struct cache_entry *ce = src[cnt];
303 if (!ce->ce_mode || ce->ce_flags & mask)
304 total++;
307 start_progress_delay(&progress, "Checking %u files out...",
308 "", total, 50, 2);
309 cnt = 0;
312 while (nr--) {
313 struct cache_entry *ce = *src++;
315 if (total)
316 if (!ce->ce_mode || ce->ce_flags & mask)
317 display_progress(&progress, ++cnt);
318 if (!ce->ce_mode) {
319 if (o->update)
320 unlink_entry(ce->name);
321 continue;
323 if (ce->ce_flags & mask) {
324 ce->ce_flags &= ~mask;
325 if (o->update)
326 checkout_entry(ce, &state, NULL);
329 if (total)
330 stop_progress(&progress);;
333 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
335 unsigned len = object_list_length(trees);
336 struct tree_entry_list **posns;
337 int i;
338 struct object_list *posn = trees;
339 struct tree_entry_list df_conflict_list;
340 static struct cache_entry *dfc;
342 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
343 df_conflict_list.next = &df_conflict_list;
344 memset(&state, 0, sizeof(state));
345 state.base_dir = "";
346 state.force = 1;
347 state.quiet = 1;
348 state.refresh_cache = 1;
350 o->merge_size = len;
352 if (!dfc)
353 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
354 o->df_conflict_entry = dfc;
356 if (len) {
357 posns = xmalloc(len * sizeof(struct tree_entry_list *));
358 for (i = 0; i < len; i++) {
359 posns[i] = create_tree_entry_list((struct tree *) posn->item);
360 posn = posn->next;
362 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
363 o, &df_conflict_list))
364 return -1;
367 if (o->trivial_merges_only && o->nontrivial_merge)
368 die("Merge requires file-level merging");
370 check_updates(active_cache, active_nr, o);
371 return 0;
374 /* Here come the merge functions */
376 static void reject_merge(struct cache_entry *ce)
378 die("Entry '%s' would be overwritten by merge. Cannot merge.",
379 ce->name);
382 static int same(struct cache_entry *a, struct cache_entry *b)
384 if (!!a != !!b)
385 return 0;
386 if (!a && !b)
387 return 1;
388 return a->ce_mode == b->ce_mode &&
389 !hashcmp(a->sha1, b->sha1);
394 * When a CE gets turned into an unmerged entry, we
395 * want it to be up-to-date
397 static void verify_uptodate(struct cache_entry *ce,
398 struct unpack_trees_options *o)
400 struct stat st;
402 if (o->index_only || o->reset)
403 return;
405 if (!lstat(ce->name, &st)) {
406 unsigned changed = ce_match_stat(ce, &st, 1);
407 if (!changed)
408 return;
409 errno = 0;
411 if (o->reset) {
412 ce->ce_flags |= htons(CE_UPDATE);
413 return;
415 if (errno == ENOENT)
416 return;
417 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
420 static void invalidate_ce_path(struct cache_entry *ce)
422 if (ce)
423 cache_tree_invalidate_path(active_cache_tree, ce->name);
426 static int verify_clean_subdirectory(const char *path, const char *action,
427 struct unpack_trees_options *o)
430 * we are about to extract "path"; we would not want to lose
431 * anything in the existing directory there.
433 int namelen;
434 int pos, i;
435 struct dir_struct d;
436 char *pathbuf;
437 int cnt = 0;
440 * First let's make sure we do not have a local modification
441 * in that directory.
443 namelen = strlen(path);
444 pos = cache_name_pos(path, namelen);
445 if (0 <= pos)
446 return cnt; /* we have it as nondirectory */
447 pos = -pos - 1;
448 for (i = pos; i < active_nr; i++) {
449 struct cache_entry *ce = active_cache[i];
450 int len = ce_namelen(ce);
451 if (len < namelen ||
452 strncmp(path, ce->name, namelen) ||
453 ce->name[namelen] != '/')
454 break;
456 * ce->name is an entry in the subdirectory.
458 if (!ce_stage(ce)) {
459 verify_uptodate(ce, o);
460 ce->ce_mode = 0;
462 cnt++;
466 * Then we need to make sure that we do not lose a locally
467 * present file that is not ignored.
469 pathbuf = xmalloc(namelen + 2);
470 memcpy(pathbuf, path, namelen);
471 strcpy(pathbuf+namelen, "/");
473 memset(&d, 0, sizeof(d));
474 if (o->dir)
475 d.exclude_per_dir = o->dir->exclude_per_dir;
476 i = read_directory(&d, path, pathbuf, namelen+1, NULL);
477 if (i)
478 die("Updating '%s' would lose untracked files in it",
479 path);
480 free(pathbuf);
481 return cnt;
485 * We do not want to remove or overwrite a working tree file that
486 * is not tracked, unless it is ignored.
488 static void verify_absent(const char *path, const char *action,
489 struct unpack_trees_options *o)
491 struct stat st;
493 if (o->index_only || o->reset || !o->update)
494 return;
496 if (!lstat(path, &st)) {
497 int cnt;
499 if (o->dir && excluded(o->dir, path))
501 * path is explicitly excluded, so it is Ok to
502 * overwrite it.
504 return;
505 if (S_ISDIR(st.st_mode)) {
507 * We are checking out path "foo" and
508 * found "foo/." in the working tree.
509 * This is tricky -- if we have modified
510 * files that are in "foo/" we would lose
511 * it.
513 cnt = verify_clean_subdirectory(path, action, o);
516 * If this removed entries from the index,
517 * what that means is:
519 * (1) the caller unpack_trees_rec() saw path/foo
520 * in the index, and it has not removed it because
521 * it thinks it is handling 'path' as blob with
522 * D/F conflict;
523 * (2) we will return "ok, we placed a merged entry
524 * in the index" which would cause o->pos to be
525 * incremented by one;
526 * (3) however, original o->pos now has 'path/foo'
527 * marked with "to be removed".
529 * We need to increment it by the number of
530 * deleted entries here.
532 o->pos += cnt;
533 return;
537 * The previous round may already have decided to
538 * delete this path, which is in a subdirectory that
539 * is being replaced with a blob.
541 cnt = cache_name_pos(path, strlen(path));
542 if (0 <= cnt) {
543 struct cache_entry *ce = active_cache[cnt];
544 if (!ce_stage(ce) && !ce->ce_mode)
545 return;
548 die("Untracked working tree file '%s' "
549 "would be %s by merge.", path, action);
553 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
554 struct unpack_trees_options *o)
556 merge->ce_flags |= htons(CE_UPDATE);
557 if (old) {
559 * See if we can re-use the old CE directly?
560 * That way we get the uptodate stat info.
562 * This also removes the UPDATE flag on
563 * a match.
565 if (same(old, merge)) {
566 *merge = *old;
567 } else {
568 verify_uptodate(old, o);
569 invalidate_ce_path(old);
572 else {
573 verify_absent(merge->name, "overwritten", o);
574 invalidate_ce_path(merge);
577 merge->ce_flags &= ~htons(CE_STAGEMASK);
578 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
579 return 1;
582 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
583 struct unpack_trees_options *o)
585 if (old)
586 verify_uptodate(old, o);
587 else
588 verify_absent(ce->name, "removed", o);
589 ce->ce_mode = 0;
590 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
591 invalidate_ce_path(ce);
592 return 1;
595 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
597 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
598 return 1;
601 #if DBRT_DEBUG
602 static void show_stage_entry(FILE *o,
603 const char *label, const struct cache_entry *ce)
605 if (!ce)
606 fprintf(o, "%s (missing)\n", label);
607 else
608 fprintf(o, "%s%06o %s %d\t%s\n",
609 label,
610 ntohl(ce->ce_mode),
611 sha1_to_hex(ce->sha1),
612 ce_stage(ce),
613 ce->name);
615 #endif
617 int threeway_merge(struct cache_entry **stages,
618 struct unpack_trees_options *o)
620 struct cache_entry *index;
621 struct cache_entry *head;
622 struct cache_entry *remote = stages[o->head_idx + 1];
623 int count;
624 int head_match = 0;
625 int remote_match = 0;
627 int df_conflict_head = 0;
628 int df_conflict_remote = 0;
630 int any_anc_missing = 0;
631 int no_anc_exists = 1;
632 int i;
634 for (i = 1; i < o->head_idx; i++) {
635 if (!stages[i] || stages[i] == o->df_conflict_entry)
636 any_anc_missing = 1;
637 else
638 no_anc_exists = 0;
641 index = stages[0];
642 head = stages[o->head_idx];
644 if (head == o->df_conflict_entry) {
645 df_conflict_head = 1;
646 head = NULL;
649 if (remote == o->df_conflict_entry) {
650 df_conflict_remote = 1;
651 remote = NULL;
654 /* First, if there's a #16 situation, note that to prevent #13
655 * and #14.
657 if (!same(remote, head)) {
658 for (i = 1; i < o->head_idx; i++) {
659 if (same(stages[i], head)) {
660 head_match = i;
662 if (same(stages[i], remote)) {
663 remote_match = i;
668 /* We start with cases where the index is allowed to match
669 * something other than the head: #14(ALT) and #2ALT, where it
670 * is permitted to match the result instead.
672 /* #14, #14ALT, #2ALT */
673 if (remote && !df_conflict_head && head_match && !remote_match) {
674 if (index && !same(index, remote) && !same(index, head))
675 reject_merge(index);
676 return merged_entry(remote, index, o);
679 * If we have an entry in the index cache, then we want to
680 * make sure that it matches head.
682 if (index && !same(index, head)) {
683 reject_merge(index);
686 if (head) {
687 /* #5ALT, #15 */
688 if (same(head, remote))
689 return merged_entry(head, index, o);
690 /* #13, #3ALT */
691 if (!df_conflict_remote && remote_match && !head_match)
692 return merged_entry(head, index, o);
695 /* #1 */
696 if (!head && !remote && any_anc_missing)
697 return 0;
699 /* Under the new "aggressive" rule, we resolve mostly trivial
700 * cases that we historically had git-merge-one-file resolve.
702 if (o->aggressive) {
703 int head_deleted = !head && !df_conflict_head;
704 int remote_deleted = !remote && !df_conflict_remote;
705 const char *path = NULL;
707 if (index)
708 path = index->name;
709 else if (head)
710 path = head->name;
711 else if (remote)
712 path = remote->name;
713 else {
714 for (i = 1; i < o->head_idx; i++) {
715 if (stages[i] && stages[i] != o->df_conflict_entry) {
716 path = stages[i]->name;
717 break;
723 * Deleted in both.
724 * Deleted in one and unchanged in the other.
726 if ((head_deleted && remote_deleted) ||
727 (head_deleted && remote && remote_match) ||
728 (remote_deleted && head && head_match)) {
729 if (index)
730 return deleted_entry(index, index, o);
731 else if (path && !head_deleted)
732 verify_absent(path, "removed", o);
733 return 0;
736 * Added in both, identically.
738 if (no_anc_exists && head && remote && same(head, remote))
739 return merged_entry(head, index, o);
743 /* Below are "no merge" cases, which require that the index be
744 * up-to-date to avoid the files getting overwritten with
745 * conflict resolution files.
747 if (index) {
748 verify_uptodate(index, o);
751 o->nontrivial_merge = 1;
753 /* #2, #3, #4, #6, #7, #9, #10, #11. */
754 count = 0;
755 if (!head_match || !remote_match) {
756 for (i = 1; i < o->head_idx; i++) {
757 if (stages[i] && stages[i] != o->df_conflict_entry) {
758 keep_entry(stages[i], o);
759 count++;
760 break;
764 #if DBRT_DEBUG
765 else {
766 fprintf(stderr, "read-tree: warning #16 detected\n");
767 show_stage_entry(stderr, "head ", stages[head_match]);
768 show_stage_entry(stderr, "remote ", stages[remote_match]);
770 #endif
771 if (head) { count += keep_entry(head, o); }
772 if (remote) { count += keep_entry(remote, o); }
773 return count;
777 * Two-way merge.
779 * The rule is to "carry forward" what is in the index without losing
780 * information across a "fast forward", favoring a successful merge
781 * over a merge failure when it makes sense. For details of the
782 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
785 int twoway_merge(struct cache_entry **src,
786 struct unpack_trees_options *o)
788 struct cache_entry *current = src[0];
789 struct cache_entry *oldtree = src[1];
790 struct cache_entry *newtree = src[2];
792 if (o->merge_size != 2)
793 return error("Cannot do a twoway merge of %d trees",
794 o->merge_size);
796 if (oldtree == o->df_conflict_entry)
797 oldtree = NULL;
798 if (newtree == o->df_conflict_entry)
799 newtree = NULL;
801 if (current) {
802 if ((!oldtree && !newtree) || /* 4 and 5 */
803 (!oldtree && newtree &&
804 same(current, newtree)) || /* 6 and 7 */
805 (oldtree && newtree &&
806 same(oldtree, newtree)) || /* 14 and 15 */
807 (oldtree && newtree &&
808 !same(oldtree, newtree) && /* 18 and 19 */
809 same(current, newtree))) {
810 return keep_entry(current, o);
812 else if (oldtree && !newtree && same(current, oldtree)) {
813 /* 10 or 11 */
814 return deleted_entry(oldtree, current, o);
816 else if (oldtree && newtree &&
817 same(current, oldtree) && !same(current, newtree)) {
818 /* 20 or 21 */
819 return merged_entry(newtree, current, o);
821 else {
822 /* all other failures */
823 if (oldtree)
824 reject_merge(oldtree);
825 if (current)
826 reject_merge(current);
827 if (newtree)
828 reject_merge(newtree);
829 return -1;
832 else if (newtree)
833 return merged_entry(newtree, current, o);
834 else
835 return deleted_entry(oldtree, current, o);
839 * Bind merge.
841 * Keep the index entries at stage0, collapse stage1 but make sure
842 * stage0 does not have anything there.
844 int bind_merge(struct cache_entry **src,
845 struct unpack_trees_options *o)
847 struct cache_entry *old = src[0];
848 struct cache_entry *a = src[1];
850 if (o->merge_size != 1)
851 return error("Cannot do a bind merge of %d trees\n",
852 o->merge_size);
853 if (a && old)
854 die("Entry '%s' overlaps. Cannot bind.", a->name);
855 if (!a)
856 return keep_entry(old, o);
857 else
858 return merged_entry(a, NULL, o);
862 * One-way merge.
864 * The rule is:
865 * - take the stat information from stage0, take the data from stage1
867 int oneway_merge(struct cache_entry **src,
868 struct unpack_trees_options *o)
870 struct cache_entry *old = src[0];
871 struct cache_entry *a = src[1];
873 if (o->merge_size != 1)
874 return error("Cannot do a oneway merge of %d trees",
875 o->merge_size);
877 if (!a)
878 return deleted_entry(old, old, o);
879 if (old && same(old, a)) {
880 if (o->reset) {
881 struct stat st;
882 if (lstat(old->name, &st) ||
883 ce_match_stat(old, &st, 1))
884 old->ce_flags |= htons(CE_UPDATE);
886 return keep_entry(old, o);
888 return merged_entry(a, old, o);