7 #include "cache-tree.h"
8 #include "unpack-trees.h"
12 struct tree_entry_list
{
13 struct tree_entry_list
*next
;
14 unsigned directory
: 1;
15 unsigned executable
: 1;
19 const unsigned char *sha1
;
22 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
24 struct tree_desc desc
;
25 struct name_entry one
;
26 struct tree_entry_list
*ret
= NULL
;
27 struct tree_entry_list
**list_p
= &ret
;
29 if (!tree
->object
.parsed
)
32 desc
.buf
= tree
->buffer
;
33 desc
.size
= tree
->size
;
35 while (tree_entry(&desc
, &one
)) {
36 struct tree_entry_list
*entry
;
38 entry
= xmalloc(sizeof(struct tree_entry_list
));
39 entry
->name
= one
.path
;
40 entry
->sha1
= one
.sha1
;
41 entry
->mode
= one
.mode
;
42 entry
->directory
= S_ISDIR(one
.mode
) != 0;
43 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
44 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
48 list_p
= &entry
->next
;
53 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
55 int len1
= strlen(name1
);
56 int len2
= strlen(name2
);
57 int len
= len1
< len2
? len1
: len2
;
58 int ret
= memcmp(name1
, name2
, len
);
68 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
74 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
75 const char *base
, struct unpack_trees_options
*o
,
77 struct tree_entry_list
*df_conflict_list
)
79 int baselen
= strlen(base
);
80 int src_size
= len
+ 1;
85 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
93 struct tree_entry_list
**subposns
;
94 struct cache_entry
**src
;
100 /* Find the first name in the input. */
105 /* Check the cache */
106 if (o
->merge
&& *indpos
< active_nr
) {
107 /* This is a bit tricky: */
108 /* If the index has a subdirectory (with
109 * contents) as the first name, it'll get a
110 * filename like "foo/bar". But that's after
111 * "foo", so the entry in trees will get
112 * handled first, at which point we'll go into
113 * "foo", and deal with "bar" from the index,
114 * because the base will be "foo/". The only
115 * way we can actually have "foo/bar" first of
116 * all the things is if the trees don't
117 * contain "foo" at all, in which case we'll
118 * handle "foo/bar" without going into the
119 * directory, but that's fine (and will return
120 * an error anyway, with the added unknown
124 cache_name
= active_cache
[*indpos
]->name
;
125 if (strlen(cache_name
) > baselen
&&
126 !memcmp(cache_name
, base
, baselen
)) {
127 cache_name
+= baselen
;
136 printf("index %s\n", first
);
138 for (i
= 0; i
< len
; i
++) {
139 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
142 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
144 if (!first
|| entcmp(first
, firstdir
,
146 posns
[i
]->directory
) > 0) {
147 first
= posns
[i
]->name
;
148 firstdir
= posns
[i
]->directory
;
151 /* No name means we're done */
153 goto leave_directory
;
155 pathlen
= strlen(first
);
156 ce_size
= cache_entry_size(baselen
+ pathlen
);
158 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
160 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
162 if (cache_name
&& !strcmp(cache_name
, first
)) {
164 src
[0] = active_cache
[*indpos
];
165 remove_cache_entry_at(*indpos
);
168 for (i
= 0; i
< len
; i
++) {
169 struct cache_entry
*ce
;
172 (posns
[i
] != df_conflict_list
&&
173 strcmp(first
, posns
[i
]->name
))) {
177 if (posns
[i
] == df_conflict_list
) {
178 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
182 if (posns
[i
]->directory
) {
183 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
186 subposns
[i
] = create_tree_entry_list(tree
);
187 posns
[i
] = posns
[i
]->next
;
188 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
194 else if (i
+ 1 < o
->head_idx
)
196 else if (i
+ 1 > o
->head_idx
)
201 ce
= xcalloc(1, ce_size
);
202 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
203 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
205 memcpy(ce
->name
, base
, baselen
);
206 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
210 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
211 src
[i
+ o
->merge
] = ce
;
212 subposns
[i
] = df_conflict_list
;
213 posns
[i
] = posns
[i
]->next
;
220 printf("%s:\n", first
);
221 for (i
= 0; i
< src_size
; i
++) {
224 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
232 printf("Added %d entries\n", ret
);
236 for (i
= 0; i
< src_size
; i
++) {
238 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
244 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
245 memcpy(newbase
, base
, baselen
);
246 memcpy(newbase
+ baselen
, first
, pathlen
);
247 newbase
[baselen
+ pathlen
] = '/';
248 newbase
[baselen
+ pathlen
+ 1] = '\0';
249 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
250 indpos
, df_conflict_list
)) {
252 goto leave_directory
;
262 pop_exclude_per_directory(o
->dir
, i_stk
);
266 /* Unlink the last component and attempt to remove leading
267 * directories, in case this unlink is the removal of the
268 * last entry in the directory -- empty directories are removed.
270 static void unlink_entry(char *name
)
279 cp
= strrchr(name
, '/');
286 status
= rmdir(name
);
295 static volatile sig_atomic_t progress_update
;
297 static void progress_interval(int signum
)
302 static void setup_progress_signal(void)
307 memset(&sa
, 0, sizeof(sa
));
308 sa
.sa_handler
= progress_interval
;
309 sigemptyset(&sa
.sa_mask
);
310 sa
.sa_flags
= SA_RESTART
;
311 sigaction(SIGALRM
, &sa
, NULL
);
313 v
.it_interval
.tv_sec
= 1;
314 v
.it_interval
.tv_usec
= 0;
315 v
.it_value
= v
.it_interval
;
316 setitimer(ITIMER_REAL
, &v
, NULL
);
319 static struct checkout state
;
320 static void check_updates(struct cache_entry
**src
, int nr
,
321 struct unpack_trees_options
*o
)
323 unsigned short mask
= htons(CE_UPDATE
);
324 unsigned last_percent
= 200, cnt
= 0, total
= 0;
326 if (o
->update
&& o
->verbose_update
) {
327 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
328 struct cache_entry
*ce
= src
[cnt
];
329 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
333 /* Don't bother doing this for very small updates */
338 fprintf(stderr
, "Checking files out...\n");
339 setup_progress_signal();
346 struct cache_entry
*ce
= *src
++;
349 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
352 percent
= (cnt
* 100) / total
;
353 if (percent
!= last_percent
||
355 fprintf(stderr
, "%4u%% (%u/%u) done\r",
356 percent
, cnt
, total
);
357 last_percent
= percent
;
364 unlink_entry(ce
->name
);
367 if (ce
->ce_flags
& mask
) {
368 ce
->ce_flags
&= ~mask
;
370 checkout_entry(ce
, &state
, NULL
);
374 signal(SIGALRM
, SIG_IGN
);
379 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
382 unsigned len
= object_list_length(trees
);
383 struct tree_entry_list
**posns
;
385 struct object_list
*posn
= trees
;
386 struct tree_entry_list df_conflict_list
;
387 static struct cache_entry
*dfc
;
389 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
390 df_conflict_list
.next
= &df_conflict_list
;
391 memset(&state
, 0, sizeof(state
));
395 state
.refresh_cache
= 1;
400 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
401 o
->df_conflict_entry
= dfc
;
404 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
405 for (i
= 0; i
< len
; i
++) {
406 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
409 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
410 o
, &indpos
, &df_conflict_list
))
414 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
415 die("Merge requires file-level merging");
417 check_updates(active_cache
, active_nr
, o
);
421 /* Here come the merge functions */
423 static void reject_merge(struct cache_entry
*ce
)
425 die("Entry '%s' would be overwritten by merge. Cannot merge.",
429 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
435 return a
->ce_mode
== b
->ce_mode
&&
436 !hashcmp(a
->sha1
, b
->sha1
);
441 * When a CE gets turned into an unmerged entry, we
442 * want it to be up-to-date
444 static void verify_uptodate(struct cache_entry
*ce
,
445 struct unpack_trees_options
*o
)
449 if (o
->index_only
|| o
->reset
)
452 if (!lstat(ce
->name
, &st
)) {
453 unsigned changed
= ce_match_stat(ce
, &st
, 1);
459 ce
->ce_flags
|= htons(CE_UPDATE
);
464 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
467 static void invalidate_ce_path(struct cache_entry
*ce
)
470 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
474 * We do not want to remove or overwrite a working tree file that
475 * is not tracked, unless it is ignored.
477 static void verify_absent(const char *path
, const char *action
,
478 struct unpack_trees_options
*o
)
482 if (o
->index_only
|| o
->reset
|| !o
->update
)
484 if (!lstat(path
, &st
) && !(o
->dir
&& excluded(o
->dir
, path
)))
485 die("Untracked working tree file '%s' "
486 "would be %s by merge.", path
, action
);
489 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
490 struct unpack_trees_options
*o
)
492 merge
->ce_flags
|= htons(CE_UPDATE
);
495 * See if we can re-use the old CE directly?
496 * That way we get the uptodate stat info.
498 * This also removes the UPDATE flag on
501 if (same(old
, merge
)) {
504 verify_uptodate(old
, o
);
505 invalidate_ce_path(old
);
509 verify_absent(merge
->name
, "overwritten", o
);
510 invalidate_ce_path(merge
);
513 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
514 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
518 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
519 struct unpack_trees_options
*o
)
522 verify_uptodate(old
, o
);
524 verify_absent(ce
->name
, "removed", o
);
526 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
527 invalidate_ce_path(ce
);
531 static int keep_entry(struct cache_entry
*ce
)
533 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
538 static void show_stage_entry(FILE *o
,
539 const char *label
, const struct cache_entry
*ce
)
542 fprintf(o
, "%s (missing)\n", label
);
544 fprintf(o
, "%s%06o %s %d\t%s\n",
547 sha1_to_hex(ce
->sha1
),
553 int threeway_merge(struct cache_entry
**stages
,
554 struct unpack_trees_options
*o
)
556 struct cache_entry
*index
;
557 struct cache_entry
*head
;
558 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
561 int remote_match
= 0;
562 const char *path
= NULL
;
564 int df_conflict_head
= 0;
565 int df_conflict_remote
= 0;
567 int any_anc_missing
= 0;
568 int no_anc_exists
= 1;
571 for (i
= 1; i
< o
->head_idx
; i
++) {
576 path
= stages
[i
]->name
;
582 head
= stages
[o
->head_idx
];
584 if (head
== o
->df_conflict_entry
) {
585 df_conflict_head
= 1;
589 if (remote
== o
->df_conflict_entry
) {
590 df_conflict_remote
= 1;
601 /* First, if there's a #16 situation, note that to prevent #13
604 if (!same(remote
, head
)) {
605 for (i
= 1; i
< o
->head_idx
; i
++) {
606 if (same(stages
[i
], head
)) {
609 if (same(stages
[i
], remote
)) {
615 /* We start with cases where the index is allowed to match
616 * something other than the head: #14(ALT) and #2ALT, where it
617 * is permitted to match the result instead.
619 /* #14, #14ALT, #2ALT */
620 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
621 if (index
&& !same(index
, remote
) && !same(index
, head
))
623 return merged_entry(remote
, index
, o
);
626 * If we have an entry in the index cache, then we want to
627 * make sure that it matches head.
629 if (index
&& !same(index
, head
)) {
635 if (same(head
, remote
))
636 return merged_entry(head
, index
, o
);
638 if (!df_conflict_remote
&& remote_match
&& !head_match
)
639 return merged_entry(head
, index
, o
);
643 if (!head
&& !remote
&& any_anc_missing
)
646 /* Under the new "aggressive" rule, we resolve mostly trivial
647 * cases that we historically had git-merge-one-file resolve.
650 int head_deleted
= !head
&& !df_conflict_head
;
651 int remote_deleted
= !remote
&& !df_conflict_remote
;
654 * Deleted in one and unchanged in the other.
656 if ((head_deleted
&& remote_deleted
) ||
657 (head_deleted
&& remote
&& remote_match
) ||
658 (remote_deleted
&& head
&& head_match
)) {
660 return deleted_entry(index
, index
, o
);
661 else if (path
&& !head_deleted
)
662 verify_absent(path
, "removed", o
);
666 * Added in both, identically.
668 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
669 return merged_entry(head
, index
, o
);
673 /* Below are "no merge" cases, which require that the index be
674 * up-to-date to avoid the files getting overwritten with
675 * conflict resolution files.
678 verify_uptodate(index
, o
);
681 o
->nontrivial_merge
= 1;
683 /* #2, #3, #4, #6, #7, #9, #11. */
685 if (!head_match
|| !remote_match
) {
686 for (i
= 1; i
< o
->head_idx
; i
++) {
688 keep_entry(stages
[i
]);
696 fprintf(stderr
, "read-tree: warning #16 detected\n");
697 show_stage_entry(stderr
, "head ", stages
[head_match
]);
698 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
701 if (head
) { count
+= keep_entry(head
); }
702 if (remote
) { count
+= keep_entry(remote
); }
709 * The rule is to "carry forward" what is in the index without losing
710 * information across a "fast forward", favoring a successful merge
711 * over a merge failure when it makes sense. For details of the
712 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
715 int twoway_merge(struct cache_entry
**src
,
716 struct unpack_trees_options
*o
)
718 struct cache_entry
*current
= src
[0];
719 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
721 if (o
->merge_size
!= 2)
722 return error("Cannot do a twoway merge of %d trees",
726 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
727 (!oldtree
&& newtree
&&
728 same(current
, newtree
)) || /* 6 and 7 */
729 (oldtree
&& newtree
&&
730 same(oldtree
, newtree
)) || /* 14 and 15 */
731 (oldtree
&& newtree
&&
732 !same(oldtree
, newtree
) && /* 18 and 19*/
733 same(current
, newtree
))) {
734 return keep_entry(current
);
736 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
738 return deleted_entry(oldtree
, current
, o
);
740 else if (oldtree
&& newtree
&&
741 same(current
, oldtree
) && !same(current
, newtree
)) {
743 return merged_entry(newtree
, current
, o
);
746 /* all other failures */
748 reject_merge(oldtree
);
750 reject_merge(current
);
752 reject_merge(newtree
);
757 return merged_entry(newtree
, current
, o
);
759 return deleted_entry(oldtree
, current
, o
);
765 * Keep the index entries at stage0, collapse stage1 but make sure
766 * stage0 does not have anything there.
768 int bind_merge(struct cache_entry
**src
,
769 struct unpack_trees_options
*o
)
771 struct cache_entry
*old
= src
[0];
772 struct cache_entry
*a
= src
[1];
774 if (o
->merge_size
!= 1)
775 return error("Cannot do a bind merge of %d trees\n",
778 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
780 return keep_entry(old
);
782 return merged_entry(a
, NULL
, o
);
789 * - take the stat information from stage0, take the data from stage1
791 int oneway_merge(struct cache_entry
**src
,
792 struct unpack_trees_options
*o
)
794 struct cache_entry
*old
= src
[0];
795 struct cache_entry
*a
= src
[1];
797 if (o
->merge_size
!= 1)
798 return error("Cannot do a oneway merge of %d trees",
802 return deleted_entry(old
, old
, o
);
803 if (old
&& same(old
, a
)) {
806 if (lstat(old
->name
, &st
) ||
807 ce_match_stat(old
, &st
, 1))
808 old
->ce_flags
|= htons(CE_UPDATE
);
810 return keep_entry(old
);
812 return merged_entry(a
, old
, o
);