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 desc
.buf
= tree
->buffer
;
31 desc
.size
= 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;
46 list_p
= &entry
->next
;
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
);
66 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
72 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
73 const char *base
, struct unpack_trees_options
*o
,
75 struct tree_entry_list
*df_conflict_list
)
77 int baselen
= strlen(base
);
78 int src_size
= len
+ 1;
83 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
91 struct tree_entry_list
**subposns
;
92 struct cache_entry
**src
;
98 /* Find the first name in the input. */
103 /* Check the cache */
104 if (o
->merge
&& *indpos
< active_nr
) {
105 /* This is a bit tricky: */
106 /* If the index has a subdirectory (with
107 * contents) as the first name, it'll get a
108 * filename like "foo/bar". But that's after
109 * "foo", so the entry in trees will get
110 * handled first, at which point we'll go into
111 * "foo", and deal with "bar" from the index,
112 * because the base will be "foo/". The only
113 * way we can actually have "foo/bar" first of
114 * all the things is if the trees don't
115 * contain "foo" at all, in which case we'll
116 * handle "foo/bar" without going into the
117 * directory, but that's fine (and will return
118 * an error anyway, with the added unknown
122 cache_name
= active_cache
[*indpos
]->name
;
123 if (strlen(cache_name
) > baselen
&&
124 !memcmp(cache_name
, base
, baselen
)) {
125 cache_name
+= baselen
;
134 printf("index %s\n", first
);
136 for (i
= 0; i
< len
; i
++) {
137 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
140 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
142 if (!first
|| entcmp(first
, firstdir
,
144 posns
[i
]->directory
) > 0) {
145 first
= posns
[i
]->name
;
146 firstdir
= posns
[i
]->directory
;
149 /* No name means we're done */
151 goto leave_directory
;
153 pathlen
= strlen(first
);
154 ce_size
= cache_entry_size(baselen
+ pathlen
);
156 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
158 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
160 if (cache_name
&& !strcmp(cache_name
, first
)) {
162 src
[0] = active_cache
[*indpos
];
163 remove_cache_entry_at(*indpos
);
166 for (i
= 0; i
< len
; i
++) {
167 struct cache_entry
*ce
;
170 (posns
[i
] != df_conflict_list
&&
171 strcmp(first
, posns
[i
]->name
))) {
175 if (posns
[i
] == df_conflict_list
) {
176 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
180 if (posns
[i
]->directory
) {
181 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
184 subposns
[i
] = create_tree_entry_list(tree
);
185 posns
[i
] = posns
[i
]->next
;
186 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
192 else if (i
+ 1 < o
->head_idx
)
194 else if (i
+ 1 > o
->head_idx
)
199 ce
= xcalloc(1, ce_size
);
200 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
201 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
203 memcpy(ce
->name
, base
, baselen
);
204 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
208 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
209 src
[i
+ o
->merge
] = ce
;
210 subposns
[i
] = df_conflict_list
;
211 posns
[i
] = posns
[i
]->next
;
218 printf("%s:\n", first
);
219 for (i
= 0; i
< src_size
; i
++) {
222 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
230 printf("Added %d entries\n", ret
);
234 for (i
= 0; i
< src_size
; i
++) {
236 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
242 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
243 memcpy(newbase
, base
, baselen
);
244 memcpy(newbase
+ baselen
, first
, pathlen
);
245 newbase
[baselen
+ pathlen
] = '/';
246 newbase
[baselen
+ pathlen
+ 1] = '\0';
247 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
248 indpos
, df_conflict_list
)) {
250 goto leave_directory
;
260 pop_exclude_per_directory(o
->dir
, i_stk
);
264 /* Unlink the last component and attempt to remove leading
265 * directories, in case this unlink is the removal of the
266 * last entry in the directory -- empty directories are removed.
268 static void unlink_entry(char *name
)
277 cp
= strrchr(name
, '/');
284 status
= rmdir(name
);
293 static volatile sig_atomic_t progress_update
;
295 static void progress_interval(int signum
)
300 static void setup_progress_signal(void)
305 memset(&sa
, 0, sizeof(sa
));
306 sa
.sa_handler
= progress_interval
;
307 sigemptyset(&sa
.sa_mask
);
308 sa
.sa_flags
= SA_RESTART
;
309 sigaction(SIGALRM
, &sa
, NULL
);
311 v
.it_interval
.tv_sec
= 1;
312 v
.it_interval
.tv_usec
= 0;
313 v
.it_value
= v
.it_interval
;
314 setitimer(ITIMER_REAL
, &v
, NULL
);
317 static struct checkout state
;
318 static void check_updates(struct cache_entry
**src
, int nr
,
319 struct unpack_trees_options
*o
)
321 unsigned short mask
= htons(CE_UPDATE
);
322 unsigned last_percent
= 200, cnt
= 0, total
= 0;
324 if (o
->update
&& o
->verbose_update
) {
325 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
326 struct cache_entry
*ce
= src
[cnt
];
327 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
331 /* Don't bother doing this for very small updates */
336 fprintf(stderr
, "Checking files out...\n");
337 setup_progress_signal();
344 struct cache_entry
*ce
= *src
++;
347 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
350 percent
= (cnt
* 100) / total
;
351 if (percent
!= last_percent
||
353 fprintf(stderr
, "%4u%% (%u/%u) done\r",
354 percent
, cnt
, total
);
355 last_percent
= percent
;
362 unlink_entry(ce
->name
);
365 if (ce
->ce_flags
& mask
) {
366 ce
->ce_flags
&= ~mask
;
368 checkout_entry(ce
, &state
, NULL
);
372 signal(SIGALRM
, SIG_IGN
);
377 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
380 unsigned len
= object_list_length(trees
);
381 struct tree_entry_list
**posns
;
383 struct object_list
*posn
= trees
;
384 struct tree_entry_list df_conflict_list
;
385 static struct cache_entry
*dfc
;
387 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
388 df_conflict_list
.next
= &df_conflict_list
;
389 memset(&state
, 0, sizeof(state
));
393 state
.refresh_cache
= 1;
398 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
399 o
->df_conflict_entry
= dfc
;
402 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
403 for (i
= 0; i
< len
; i
++) {
404 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
407 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
408 o
, &indpos
, &df_conflict_list
))
412 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
413 die("Merge requires file-level merging");
415 check_updates(active_cache
, active_nr
, o
);
419 /* Here come the merge functions */
421 static void reject_merge(struct cache_entry
*ce
)
423 die("Entry '%s' would be overwritten by merge. Cannot merge.",
427 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
433 return a
->ce_mode
== b
->ce_mode
&&
434 !hashcmp(a
->sha1
, b
->sha1
);
439 * When a CE gets turned into an unmerged entry, we
440 * want it to be up-to-date
442 static void verify_uptodate(struct cache_entry
*ce
,
443 struct unpack_trees_options
*o
)
447 if (o
->index_only
|| o
->reset
)
450 if (!lstat(ce
->name
, &st
)) {
451 unsigned changed
= ce_match_stat(ce
, &st
, 1);
457 ce
->ce_flags
|= htons(CE_UPDATE
);
462 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
465 static void invalidate_ce_path(struct cache_entry
*ce
)
468 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
472 * We do not want to remove or overwrite a working tree file that
473 * is not tracked, unless it is ignored.
475 static void verify_absent(const char *path
, const char *action
,
476 struct unpack_trees_options
*o
)
480 if (o
->index_only
|| o
->reset
|| !o
->update
)
482 if (!lstat(path
, &st
) && !(o
->dir
&& excluded(o
->dir
, path
)))
483 die("Untracked working tree file '%s' "
484 "would be %s by merge.", path
, action
);
487 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
488 struct unpack_trees_options
*o
)
490 merge
->ce_flags
|= htons(CE_UPDATE
);
493 * See if we can re-use the old CE directly?
494 * That way we get the uptodate stat info.
496 * This also removes the UPDATE flag on
499 if (same(old
, merge
)) {
502 verify_uptodate(old
, o
);
503 invalidate_ce_path(old
);
507 verify_absent(merge
->name
, "overwritten", o
);
508 invalidate_ce_path(merge
);
511 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
512 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
516 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
517 struct unpack_trees_options
*o
)
520 verify_uptodate(old
, o
);
522 verify_absent(ce
->name
, "removed", o
);
524 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
525 invalidate_ce_path(ce
);
529 static int keep_entry(struct cache_entry
*ce
)
531 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
536 static void show_stage_entry(FILE *o
,
537 const char *label
, const struct cache_entry
*ce
)
540 fprintf(o
, "%s (missing)\n", label
);
542 fprintf(o
, "%s%06o %s %d\t%s\n",
545 sha1_to_hex(ce
->sha1
),
551 int threeway_merge(struct cache_entry
**stages
,
552 struct unpack_trees_options
*o
)
554 struct cache_entry
*index
;
555 struct cache_entry
*head
;
556 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
559 int remote_match
= 0;
560 const char *path
= NULL
;
562 int df_conflict_head
= 0;
563 int df_conflict_remote
= 0;
565 int any_anc_missing
= 0;
566 int no_anc_exists
= 1;
569 for (i
= 1; i
< o
->head_idx
; i
++) {
574 path
= stages
[i
]->name
;
580 head
= stages
[o
->head_idx
];
582 if (head
== o
->df_conflict_entry
) {
583 df_conflict_head
= 1;
587 if (remote
== o
->df_conflict_entry
) {
588 df_conflict_remote
= 1;
599 /* First, if there's a #16 situation, note that to prevent #13
602 if (!same(remote
, head
)) {
603 for (i
= 1; i
< o
->head_idx
; i
++) {
604 if (same(stages
[i
], head
)) {
607 if (same(stages
[i
], remote
)) {
613 /* We start with cases where the index is allowed to match
614 * something other than the head: #14(ALT) and #2ALT, where it
615 * is permitted to match the result instead.
617 /* #14, #14ALT, #2ALT */
618 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
619 if (index
&& !same(index
, remote
) && !same(index
, head
))
621 return merged_entry(remote
, index
, o
);
624 * If we have an entry in the index cache, then we want to
625 * make sure that it matches head.
627 if (index
&& !same(index
, head
)) {
633 if (same(head
, remote
))
634 return merged_entry(head
, index
, o
);
636 if (!df_conflict_remote
&& remote_match
&& !head_match
)
637 return merged_entry(head
, index
, o
);
641 if (!head
&& !remote
&& any_anc_missing
)
644 /* Under the new "aggressive" rule, we resolve mostly trivial
645 * cases that we historically had git-merge-one-file resolve.
648 int head_deleted
= !head
&& !df_conflict_head
;
649 int remote_deleted
= !remote
&& !df_conflict_remote
;
652 * Deleted in one and unchanged in the other.
654 if ((head_deleted
&& remote_deleted
) ||
655 (head_deleted
&& remote
&& remote_match
) ||
656 (remote_deleted
&& head
&& head_match
)) {
658 return deleted_entry(index
, index
, o
);
659 else if (path
&& !head_deleted
)
660 verify_absent(path
, "removed", o
);
664 * Added in both, identically.
666 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
667 return merged_entry(head
, index
, o
);
671 /* Below are "no merge" cases, which require that the index be
672 * up-to-date to avoid the files getting overwritten with
673 * conflict resolution files.
676 verify_uptodate(index
, o
);
679 o
->nontrivial_merge
= 1;
681 /* #2, #3, #4, #6, #7, #9, #11. */
683 if (!head_match
|| !remote_match
) {
684 for (i
= 1; i
< o
->head_idx
; i
++) {
686 keep_entry(stages
[i
]);
694 fprintf(stderr
, "read-tree: warning #16 detected\n");
695 show_stage_entry(stderr
, "head ", stages
[head_match
]);
696 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
699 if (head
) { count
+= keep_entry(head
); }
700 if (remote
) { count
+= keep_entry(remote
); }
707 * The rule is to "carry forward" what is in the index without losing
708 * information across a "fast forward", favoring a successful merge
709 * over a merge failure when it makes sense. For details of the
710 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
713 int twoway_merge(struct cache_entry
**src
,
714 struct unpack_trees_options
*o
)
716 struct cache_entry
*current
= src
[0];
717 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
719 if (o
->merge_size
!= 2)
720 return error("Cannot do a twoway merge of %d trees",
724 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
725 (!oldtree
&& newtree
&&
726 same(current
, newtree
)) || /* 6 and 7 */
727 (oldtree
&& newtree
&&
728 same(oldtree
, newtree
)) || /* 14 and 15 */
729 (oldtree
&& newtree
&&
730 !same(oldtree
, newtree
) && /* 18 and 19*/
731 same(current
, newtree
))) {
732 return keep_entry(current
);
734 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
736 return deleted_entry(oldtree
, current
, o
);
738 else if (oldtree
&& newtree
&&
739 same(current
, oldtree
) && !same(current
, newtree
)) {
741 return merged_entry(newtree
, current
, o
);
744 /* all other failures */
746 reject_merge(oldtree
);
748 reject_merge(current
);
750 reject_merge(newtree
);
755 return merged_entry(newtree
, current
, o
);
757 return deleted_entry(oldtree
, current
, o
);
763 * Keep the index entries at stage0, collapse stage1 but make sure
764 * stage0 does not have anything there.
766 int bind_merge(struct cache_entry
**src
,
767 struct unpack_trees_options
*o
)
769 struct cache_entry
*old
= src
[0];
770 struct cache_entry
*a
= src
[1];
772 if (o
->merge_size
!= 1)
773 return error("Cannot do a bind merge of %d trees\n",
776 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
778 return keep_entry(old
);
780 return merged_entry(a
, NULL
, o
);
787 * - take the stat information from stage0, take the data from stage1
789 int oneway_merge(struct cache_entry
**src
,
790 struct unpack_trees_options
*o
)
792 struct cache_entry
*old
= src
[0];
793 struct cache_entry
*a
= src
[1];
795 if (o
->merge_size
!= 1)
796 return error("Cannot do a oneway merge of %d trees",
800 return deleted_entry(old
, old
, o
);
801 if (old
&& same(old
, a
)) {
804 if (lstat(old
->name
, &st
) ||
805 ce_match_stat(old
, &st
, 1))
806 old
->ce_flags
|= htons(CE_UPDATE
);
808 return keep_entry(old
);
810 return merged_entry(a
, old
, o
);