5 #include "cache-tree.h"
6 #include "unpack-trees.h"
10 struct tree_entry_list
{
11 struct tree_entry_list
*next
;
12 unsigned directory
: 1;
13 unsigned executable
: 1;
17 const unsigned char *sha1
;
20 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
22 struct tree_desc desc
;
23 struct name_entry one
;
24 struct tree_entry_list
*ret
= NULL
;
25 struct tree_entry_list
**list_p
= &ret
;
27 if (!tree
->object
.parsed
)
30 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
32 while (tree_entry(&desc
, &one
)) {
33 struct tree_entry_list
*entry
;
35 entry
= xmalloc(sizeof(struct tree_entry_list
));
36 entry
->name
= one
.path
;
37 entry
->sha1
= one
.sha1
;
38 entry
->mode
= one
.mode
;
39 entry
->directory
= S_ISDIR(one
.mode
) != 0;
40 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
41 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
45 list_p
= &entry
->next
;
50 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
52 int len1
= strlen(name1
);
53 int len2
= strlen(name2
);
54 int len
= len1
< len2
? len1
: len2
;
55 int ret
= memcmp(name1
, name2
, len
);
65 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
71 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
72 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;
82 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
90 struct tree_entry_list
**subposns
;
91 struct cache_entry
**src
;
97 /* Find the first name in the input. */
102 /* Check the cache */
103 if (o
->merge
&& *indpos
< 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
121 cache_name
= active_cache
[*indpos
]->name
;
122 if (strlen(cache_name
) > baselen
&&
123 !memcmp(cache_name
, base
, baselen
)) {
124 cache_name
+= baselen
;
133 printf("index %s\n", first
);
135 for (i
= 0; i
< len
; i
++) {
136 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
139 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
141 if (!first
|| entcmp(first
, firstdir
,
143 posns
[i
]->directory
) > 0) {
144 first
= posns
[i
]->name
;
145 firstdir
= posns
[i
]->directory
;
148 /* No name means we're done */
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
)) {
161 src
[0] = active_cache
[*indpos
];
162 remove_cache_entry_at(*indpos
);
165 for (i
= 0; i
< len
; i
++) {
166 struct cache_entry
*ce
;
169 (posns
[i
] != df_conflict_list
&&
170 strcmp(first
, posns
[i
]->name
))) {
174 if (posns
[i
] == df_conflict_list
) {
175 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
179 if (posns
[i
]->directory
) {
180 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
183 subposns
[i
] = create_tree_entry_list(tree
);
184 posns
[i
] = posns
[i
]->next
;
185 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
191 else if (i
+ 1 < o
->head_idx
)
193 else if (i
+ 1 > o
->head_idx
)
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
,
202 memcpy(ce
->name
, base
, baselen
);
203 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 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
;
217 printf("%s:\n", first
);
218 for (i
= 0; i
< src_size
; i
++) {
221 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
229 printf("Added %d entries\n", ret
);
233 for (i
= 0; i
< src_size
; i
++) {
235 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
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 indpos
, df_conflict_list
)) {
249 goto leave_directory
;
259 pop_exclude_per_directory(o
->dir
, i_stk
);
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
)
276 cp
= strrchr(name
, '/');
283 status
= rmdir(name
);
292 static volatile sig_atomic_t progress_update
;
294 static void progress_interval(int signum
)
299 static void setup_progress_signal(void)
304 memset(&sa
, 0, sizeof(sa
));
305 sa
.sa_handler
= progress_interval
;
306 sigemptyset(&sa
.sa_mask
);
307 sa
.sa_flags
= SA_RESTART
;
308 sigaction(SIGALRM
, &sa
, NULL
);
310 v
.it_interval
.tv_sec
= 1;
311 v
.it_interval
.tv_usec
= 0;
312 v
.it_value
= v
.it_interval
;
313 setitimer(ITIMER_REAL
, &v
, NULL
);
316 static struct checkout state
;
317 static void check_updates(struct cache_entry
**src
, int nr
,
318 struct unpack_trees_options
*o
)
320 unsigned short mask
= htons(CE_UPDATE
);
321 unsigned last_percent
= 200, cnt
= 0, total
= 0;
323 if (o
->update
&& o
->verbose_update
) {
324 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
325 struct cache_entry
*ce
= src
[cnt
];
326 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
330 /* Don't bother doing this for very small updates */
335 fprintf(stderr
, "Checking files out...\n");
336 setup_progress_signal();
343 struct cache_entry
*ce
= *src
++;
346 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
349 percent
= (cnt
* 100) / total
;
350 if (percent
!= last_percent
||
352 fprintf(stderr
, "%4u%% (%u/%u) done\r",
353 percent
, cnt
, total
);
354 last_percent
= percent
;
361 unlink_entry(ce
->name
);
364 if (ce
->ce_flags
& mask
) {
365 ce
->ce_flags
&= ~mask
;
367 checkout_entry(ce
, &state
, NULL
);
371 signal(SIGALRM
, SIG_IGN
);
376 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
379 unsigned len
= object_list_length(trees
);
380 struct tree_entry_list
**posns
;
382 struct object_list
*posn
= trees
;
383 struct tree_entry_list df_conflict_list
;
384 static struct cache_entry
*dfc
;
386 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
387 df_conflict_list
.next
= &df_conflict_list
;
388 memset(&state
, 0, sizeof(state
));
392 state
.refresh_cache
= 1;
397 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
398 o
->df_conflict_entry
= dfc
;
401 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
402 for (i
= 0; i
< len
; i
++) {
403 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
406 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
407 o
, &indpos
, &df_conflict_list
))
411 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
412 die("Merge requires file-level merging");
414 check_updates(active_cache
, active_nr
, o
);
418 /* Here come the merge functions */
420 static void reject_merge(struct cache_entry
*ce
)
422 die("Entry '%s' would be overwritten by merge. Cannot merge.",
426 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
432 return a
->ce_mode
== b
->ce_mode
&&
433 !hashcmp(a
->sha1
, b
->sha1
);
438 * When a CE gets turned into an unmerged entry, we
439 * want it to be up-to-date
441 static void verify_uptodate(struct cache_entry
*ce
,
442 struct unpack_trees_options
*o
)
446 if (o
->index_only
|| o
->reset
)
449 if (!lstat(ce
->name
, &st
)) {
450 unsigned changed
= ce_match_stat(ce
, &st
, 1);
456 ce
->ce_flags
|= htons(CE_UPDATE
);
461 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
464 static void invalidate_ce_path(struct cache_entry
*ce
)
467 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
471 * We do not want to remove or overwrite a working tree file that
472 * is not tracked, unless it is ignored.
474 static void verify_absent(const char *path
, const char *action
,
475 struct unpack_trees_options
*o
)
479 if (o
->index_only
|| o
->reset
|| !o
->update
)
481 if (!lstat(path
, &st
) && !(o
->dir
&& excluded(o
->dir
, path
)))
482 die("Untracked working tree file '%s' "
483 "would be %s by merge.", path
, action
);
486 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
487 struct unpack_trees_options
*o
)
489 merge
->ce_flags
|= htons(CE_UPDATE
);
492 * See if we can re-use the old CE directly?
493 * That way we get the uptodate stat info.
495 * This also removes the UPDATE flag on
498 if (same(old
, merge
)) {
501 verify_uptodate(old
, o
);
502 invalidate_ce_path(old
);
506 verify_absent(merge
->name
, "overwritten", o
);
507 invalidate_ce_path(merge
);
510 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
511 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
515 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
516 struct unpack_trees_options
*o
)
519 verify_uptodate(old
, o
);
521 verify_absent(ce
->name
, "removed", o
);
523 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
524 invalidate_ce_path(ce
);
528 static int keep_entry(struct cache_entry
*ce
)
530 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
535 static void show_stage_entry(FILE *o
,
536 const char *label
, const struct cache_entry
*ce
)
539 fprintf(o
, "%s (missing)\n", label
);
541 fprintf(o
, "%s%06o %s %d\t%s\n",
544 sha1_to_hex(ce
->sha1
),
550 int threeway_merge(struct cache_entry
**stages
,
551 struct unpack_trees_options
*o
)
553 struct cache_entry
*index
;
554 struct cache_entry
*head
;
555 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
558 int remote_match
= 0;
559 const char *path
= NULL
;
561 int df_conflict_head
= 0;
562 int df_conflict_remote
= 0;
564 int any_anc_missing
= 0;
565 int no_anc_exists
= 1;
568 for (i
= 1; i
< o
->head_idx
; i
++) {
573 path
= stages
[i
]->name
;
579 head
= stages
[o
->head_idx
];
581 if (head
== o
->df_conflict_entry
) {
582 df_conflict_head
= 1;
586 if (remote
== o
->df_conflict_entry
) {
587 df_conflict_remote
= 1;
598 /* First, if there's a #16 situation, note that to prevent #13
601 if (!same(remote
, head
)) {
602 for (i
= 1; i
< o
->head_idx
; i
++) {
603 if (same(stages
[i
], head
)) {
606 if (same(stages
[i
], remote
)) {
612 /* We start with cases where the index is allowed to match
613 * something other than the head: #14(ALT) and #2ALT, where it
614 * is permitted to match the result instead.
616 /* #14, #14ALT, #2ALT */
617 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
618 if (index
&& !same(index
, remote
) && !same(index
, head
))
620 return merged_entry(remote
, index
, o
);
623 * If we have an entry in the index cache, then we want to
624 * make sure that it matches head.
626 if (index
&& !same(index
, head
)) {
632 if (same(head
, remote
))
633 return merged_entry(head
, index
, o
);
635 if (!df_conflict_remote
&& remote_match
&& !head_match
)
636 return merged_entry(head
, index
, o
);
640 if (!head
&& !remote
&& any_anc_missing
)
643 /* Under the new "aggressive" rule, we resolve mostly trivial
644 * cases that we historically had git-merge-one-file resolve.
647 int head_deleted
= !head
&& !df_conflict_head
;
648 int remote_deleted
= !remote
&& !df_conflict_remote
;
651 * Deleted in one and unchanged in the other.
653 if ((head_deleted
&& remote_deleted
) ||
654 (head_deleted
&& remote
&& remote_match
) ||
655 (remote_deleted
&& head
&& head_match
)) {
657 return deleted_entry(index
, index
, o
);
658 else if (path
&& !head_deleted
)
659 verify_absent(path
, "removed", o
);
663 * Added in both, identically.
665 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
666 return merged_entry(head
, index
, o
);
670 /* Below are "no merge" cases, which require that the index be
671 * up-to-date to avoid the files getting overwritten with
672 * conflict resolution files.
675 verify_uptodate(index
, o
);
678 o
->nontrivial_merge
= 1;
680 /* #2, #3, #4, #6, #7, #9, #11. */
682 if (!head_match
|| !remote_match
) {
683 for (i
= 1; i
< o
->head_idx
; i
++) {
685 keep_entry(stages
[i
]);
693 fprintf(stderr
, "read-tree: warning #16 detected\n");
694 show_stage_entry(stderr
, "head ", stages
[head_match
]);
695 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
698 if (head
) { count
+= keep_entry(head
); }
699 if (remote
) { count
+= keep_entry(remote
); }
706 * The rule is to "carry forward" what is in the index without losing
707 * information across a "fast forward", favoring a successful merge
708 * over a merge failure when it makes sense. For details of the
709 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
712 int twoway_merge(struct cache_entry
**src
,
713 struct unpack_trees_options
*o
)
715 struct cache_entry
*current
= src
[0];
716 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
718 if (o
->merge_size
!= 2)
719 return error("Cannot do a twoway merge of %d trees",
723 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
724 (!oldtree
&& newtree
&&
725 same(current
, newtree
)) || /* 6 and 7 */
726 (oldtree
&& newtree
&&
727 same(oldtree
, newtree
)) || /* 14 and 15 */
728 (oldtree
&& newtree
&&
729 !same(oldtree
, newtree
) && /* 18 and 19*/
730 same(current
, newtree
))) {
731 return keep_entry(current
);
733 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
735 return deleted_entry(oldtree
, current
, o
);
737 else if (oldtree
&& newtree
&&
738 same(current
, oldtree
) && !same(current
, newtree
)) {
740 return merged_entry(newtree
, current
, o
);
743 /* all other failures */
745 reject_merge(oldtree
);
747 reject_merge(current
);
749 reject_merge(newtree
);
754 return merged_entry(newtree
, current
, o
);
756 return deleted_entry(oldtree
, current
, o
);
762 * Keep the index entries at stage0, collapse stage1 but make sure
763 * stage0 does not have anything there.
765 int bind_merge(struct cache_entry
**src
,
766 struct unpack_trees_options
*o
)
768 struct cache_entry
*old
= src
[0];
769 struct cache_entry
*a
= src
[1];
771 if (o
->merge_size
!= 1)
772 return error("Cannot do a bind merge of %d trees\n",
775 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
777 return keep_entry(old
);
779 return merged_entry(a
, NULL
, o
);
786 * - take the stat information from stage0, take the data from stage1
788 int oneway_merge(struct cache_entry
**src
,
789 struct unpack_trees_options
*o
)
791 struct cache_entry
*old
= src
[0];
792 struct cache_entry
*a
= src
[1];
794 if (o
->merge_size
!= 1)
795 return error("Cannot do a oneway merge of %d trees",
799 return deleted_entry(old
, old
, o
);
800 if (old
&& same(old
, a
)) {
803 if (lstat(old
->name
, &st
) ||
804 ce_match_stat(old
, &st
, 1))
805 old
->ce_flags
|= htons(CE_UPDATE
);
807 return keep_entry(old
);
809 return merged_entry(a
, old
, o
);