6 #include "cache-tree.h"
7 #include "unpack-trees.h"
11 struct tree_entry_list
{
12 struct tree_entry_list
*next
;
13 unsigned directory
: 1;
14 unsigned executable
: 1;
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
)
31 desc
.buf
= tree
->buffer
;
32 desc
.size
= tree
->size
;
34 while (tree_entry(&desc
, &one
)) {
35 struct tree_entry_list
*entry
;
37 entry
= xmalloc(sizeof(struct tree_entry_list
));
38 entry
->name
= one
.path
;
39 entry
->sha1
= one
.sha1
;
40 entry
->mode
= one
.mode
;
41 entry
->directory
= S_ISDIR(one
.mode
) != 0;
42 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
43 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
47 list_p
= &entry
->next
;
52 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
54 int len1
= strlen(name1
);
55 int len2
= strlen(name2
);
56 int len
= len1
< len2
? len1
: len2
;
57 int ret
= memcmp(name1
, name2
, len
);
67 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
73 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
74 const char *base
, struct unpack_trees_options
*o
,
76 struct tree_entry_list
*df_conflict_list
)
78 int baselen
= strlen(base
);
79 int src_size
= len
+ 1;
86 struct tree_entry_list
**subposns
;
87 struct cache_entry
**src
;
93 /* Find the first name in the input. */
99 if (o
->merge
&& *indpos
< active_nr
) {
100 /* This is a bit tricky: */
101 /* If the index has a subdirectory (with
102 * contents) as the first name, it'll get a
103 * filename like "foo/bar". But that's after
104 * "foo", so the entry in trees will get
105 * handled first, at which point we'll go into
106 * "foo", and deal with "bar" from the index,
107 * because the base will be "foo/". The only
108 * way we can actually have "foo/bar" first of
109 * all the things is if the trees don't
110 * contain "foo" at all, in which case we'll
111 * handle "foo/bar" without going into the
112 * directory, but that's fine (and will return
113 * an error anyway, with the added unknown
117 cache_name
= active_cache
[*indpos
]->name
;
118 if (strlen(cache_name
) > baselen
&&
119 !memcmp(cache_name
, base
, baselen
)) {
120 cache_name
+= baselen
;
129 printf("index %s\n", first
);
131 for (i
= 0; i
< len
; i
++) {
132 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
135 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
137 if (!first
|| entcmp(first
, firstdir
,
139 posns
[i
]->directory
) > 0) {
140 first
= posns
[i
]->name
;
141 firstdir
= posns
[i
]->directory
;
144 /* No name means we're done */
148 pathlen
= strlen(first
);
149 ce_size
= cache_entry_size(baselen
+ pathlen
);
151 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
153 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
155 if (cache_name
&& !strcmp(cache_name
, first
)) {
157 src
[0] = active_cache
[*indpos
];
158 remove_cache_entry_at(*indpos
);
161 for (i
= 0; i
< len
; i
++) {
162 struct cache_entry
*ce
;
165 (posns
[i
] != df_conflict_list
&&
166 strcmp(first
, posns
[i
]->name
))) {
170 if (posns
[i
] == df_conflict_list
) {
171 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
175 if (posns
[i
]->directory
) {
176 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
179 subposns
[i
] = create_tree_entry_list(tree
);
180 posns
[i
] = posns
[i
]->next
;
181 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
187 else if (i
+ 1 < o
->head_idx
)
189 else if (i
+ 1 > o
->head_idx
)
194 ce
= xcalloc(1, ce_size
);
195 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
196 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
198 memcpy(ce
->name
, base
, baselen
);
199 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
203 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
204 src
[i
+ o
->merge
] = ce
;
205 subposns
[i
] = df_conflict_list
;
206 posns
[i
] = posns
[i
]->next
;
213 printf("%s:\n", first
);
214 for (i
= 0; i
< src_size
; i
++) {
217 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
225 printf("Added %d entries\n", ret
);
229 for (i
= 0; i
< src_size
; i
++) {
231 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
237 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
238 memcpy(newbase
, base
, baselen
);
239 memcpy(newbase
+ baselen
, first
, pathlen
);
240 newbase
[baselen
+ pathlen
] = '/';
241 newbase
[baselen
+ pathlen
+ 1] = '\0';
242 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
243 indpos
, df_conflict_list
))
252 /* Unlink the last component and attempt to remove leading
253 * directories, in case this unlink is the removal of the
254 * last entry in the directory -- empty directories are removed.
256 static void unlink_entry(char *name
)
265 cp
= strrchr(name
, '/');
272 status
= rmdir(name
);
281 static volatile sig_atomic_t progress_update
;
283 static void progress_interval(int signum
)
288 static void setup_progress_signal(void)
293 memset(&sa
, 0, sizeof(sa
));
294 sa
.sa_handler
= progress_interval
;
295 sigemptyset(&sa
.sa_mask
);
296 sa
.sa_flags
= SA_RESTART
;
297 sigaction(SIGALRM
, &sa
, NULL
);
299 v
.it_interval
.tv_sec
= 1;
300 v
.it_interval
.tv_usec
= 0;
301 v
.it_value
= v
.it_interval
;
302 setitimer(ITIMER_REAL
, &v
, NULL
);
305 static struct checkout state
;
306 static void check_updates(struct cache_entry
**src
, int nr
,
307 struct unpack_trees_options
*o
)
309 unsigned short mask
= htons(CE_UPDATE
);
310 unsigned last_percent
= 200, cnt
= 0, total
= 0;
312 if (o
->update
&& o
->verbose_update
) {
313 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
314 struct cache_entry
*ce
= src
[cnt
];
315 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
319 /* Don't bother doing this for very small updates */
324 fprintf(stderr
, "Checking files out...\n");
325 setup_progress_signal();
332 struct cache_entry
*ce
= *src
++;
335 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
338 percent
= (cnt
* 100) / total
;
339 if (percent
!= last_percent
||
341 fprintf(stderr
, "%4u%% (%u/%u) done\r",
342 percent
, cnt
, total
);
343 last_percent
= percent
;
350 unlink_entry(ce
->name
);
353 if (ce
->ce_flags
& mask
) {
354 ce
->ce_flags
&= ~mask
;
356 checkout_entry(ce
, &state
, NULL
);
360 signal(SIGALRM
, SIG_IGN
);
365 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
368 unsigned len
= object_list_length(trees
);
369 struct tree_entry_list
**posns
;
371 struct object_list
*posn
= trees
;
372 struct tree_entry_list df_conflict_list
;
373 struct cache_entry df_conflict_entry
;
375 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
376 df_conflict_list
.next
= &df_conflict_list
;
377 memset(&state
, 0, sizeof(state
));
381 state
.refresh_cache
= 1;
384 memset(&df_conflict_entry
, 0, sizeof(df_conflict_entry
));
385 o
->df_conflict_entry
= &df_conflict_entry
;
388 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
389 for (i
= 0; i
< len
; i
++) {
390 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
393 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
394 o
, &indpos
, &df_conflict_list
))
398 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
399 die("Merge requires file-level merging");
401 check_updates(active_cache
, active_nr
, o
);
405 /* Here come the merge functions */
407 static void reject_merge(struct cache_entry
*ce
)
409 die("Entry '%s' would be overwritten by merge. Cannot merge.",
413 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
419 return a
->ce_mode
== b
->ce_mode
&&
420 !hashcmp(a
->sha1
, b
->sha1
);
425 * When a CE gets turned into an unmerged entry, we
426 * want it to be up-to-date
428 static void verify_uptodate(struct cache_entry
*ce
,
429 struct unpack_trees_options
*o
)
433 if (o
->index_only
|| o
->reset
)
436 if (!lstat(ce
->name
, &st
)) {
437 unsigned changed
= ce_match_stat(ce
, &st
, 1);
443 ce
->ce_flags
|= htons(CE_UPDATE
);
448 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
451 static void invalidate_ce_path(struct cache_entry
*ce
)
454 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
458 * We do not want to remove or overwrite a working tree file that
461 static void verify_absent(const char *path
, const char *action
,
462 struct unpack_trees_options
*o
)
466 if (o
->index_only
|| o
->reset
|| !o
->update
)
468 if (!lstat(path
, &st
))
469 die("Untracked working tree file '%s' "
470 "would be %s by merge.", path
, action
);
473 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
474 struct unpack_trees_options
*o
)
476 merge
->ce_flags
|= htons(CE_UPDATE
);
479 * See if we can re-use the old CE directly?
480 * That way we get the uptodate stat info.
482 * This also removes the UPDATE flag on
485 if (same(old
, merge
)) {
488 verify_uptodate(old
, o
);
489 invalidate_ce_path(old
);
493 verify_absent(merge
->name
, "overwritten", o
);
494 invalidate_ce_path(merge
);
497 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
498 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
502 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
503 struct unpack_trees_options
*o
)
506 verify_uptodate(old
, o
);
508 verify_absent(ce
->name
, "removed", o
);
510 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
511 invalidate_ce_path(ce
);
515 static int keep_entry(struct cache_entry
*ce
)
517 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
522 static void show_stage_entry(FILE *o
,
523 const char *label
, const struct cache_entry
*ce
)
526 fprintf(o
, "%s (missing)\n", label
);
528 fprintf(o
, "%s%06o %s %d\t%s\n",
531 sha1_to_hex(ce
->sha1
),
537 int threeway_merge(struct cache_entry
**stages
,
538 struct unpack_trees_options
*o
)
540 struct cache_entry
*index
;
541 struct cache_entry
*head
;
542 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
545 int remote_match
= 0;
546 const char *path
= NULL
;
548 int df_conflict_head
= 0;
549 int df_conflict_remote
= 0;
551 int any_anc_missing
= 0;
552 int no_anc_exists
= 1;
555 for (i
= 1; i
< o
->head_idx
; i
++) {
560 path
= stages
[i
]->name
;
566 head
= stages
[o
->head_idx
];
568 if (head
== o
->df_conflict_entry
) {
569 df_conflict_head
= 1;
573 if (remote
== o
->df_conflict_entry
) {
574 df_conflict_remote
= 1;
585 /* First, if there's a #16 situation, note that to prevent #13
588 if (!same(remote
, head
)) {
589 for (i
= 1; i
< o
->head_idx
; i
++) {
590 if (same(stages
[i
], head
)) {
593 if (same(stages
[i
], remote
)) {
599 /* We start with cases where the index is allowed to match
600 * something other than the head: #14(ALT) and #2ALT, where it
601 * is permitted to match the result instead.
603 /* #14, #14ALT, #2ALT */
604 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
605 if (index
&& !same(index
, remote
) && !same(index
, head
))
607 return merged_entry(remote
, index
, o
);
610 * If we have an entry in the index cache, then we want to
611 * make sure that it matches head.
613 if (index
&& !same(index
, head
)) {
619 if (same(head
, remote
))
620 return merged_entry(head
, index
, o
);
622 if (!df_conflict_remote
&& remote_match
&& !head_match
)
623 return merged_entry(head
, index
, o
);
627 if (!head
&& !remote
&& any_anc_missing
)
630 /* Under the new "aggressive" rule, we resolve mostly trivial
631 * cases that we historically had git-merge-one-file resolve.
634 int head_deleted
= !head
&& !df_conflict_head
;
635 int remote_deleted
= !remote
&& !df_conflict_remote
;
638 * Deleted in one and unchanged in the other.
640 if ((head_deleted
&& remote_deleted
) ||
641 (head_deleted
&& remote
&& remote_match
) ||
642 (remote_deleted
&& head
&& head_match
)) {
644 return deleted_entry(index
, index
, o
);
645 else if (path
&& !head_deleted
)
646 verify_absent(path
, "removed", o
);
650 * Added in both, identically.
652 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
653 return merged_entry(head
, index
, o
);
657 /* Below are "no merge" cases, which require that the index be
658 * up-to-date to avoid the files getting overwritten with
659 * conflict resolution files.
662 verify_uptodate(index
, o
);
665 o
->nontrivial_merge
= 1;
667 /* #2, #3, #4, #6, #7, #9, #11. */
669 if (!head_match
|| !remote_match
) {
670 for (i
= 1; i
< o
->head_idx
; i
++) {
672 keep_entry(stages
[i
]);
680 fprintf(stderr
, "read-tree: warning #16 detected\n");
681 show_stage_entry(stderr
, "head ", stages
[head_match
]);
682 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
685 if (head
) { count
+= keep_entry(head
); }
686 if (remote
) { count
+= keep_entry(remote
); }
693 * The rule is to "carry forward" what is in the index without losing
694 * information across a "fast forward", favoring a successful merge
695 * over a merge failure when it makes sense. For details of the
696 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
699 int twoway_merge(struct cache_entry
**src
,
700 struct unpack_trees_options
*o
)
702 struct cache_entry
*current
= src
[0];
703 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
705 if (o
->merge_size
!= 2)
706 return error("Cannot do a twoway merge of %d trees",
710 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
711 (!oldtree
&& newtree
&&
712 same(current
, newtree
)) || /* 6 and 7 */
713 (oldtree
&& newtree
&&
714 same(oldtree
, newtree
)) || /* 14 and 15 */
715 (oldtree
&& newtree
&&
716 !same(oldtree
, newtree
) && /* 18 and 19*/
717 same(current
, newtree
))) {
718 return keep_entry(current
);
720 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
722 return deleted_entry(oldtree
, current
, o
);
724 else if (oldtree
&& newtree
&&
725 same(current
, oldtree
) && !same(current
, newtree
)) {
727 return merged_entry(newtree
, current
, o
);
730 /* all other failures */
732 reject_merge(oldtree
);
734 reject_merge(current
);
736 reject_merge(newtree
);
741 return merged_entry(newtree
, current
, o
);
743 return deleted_entry(oldtree
, current
, o
);
749 * Keep the index entries at stage0, collapse stage1 but make sure
750 * stage0 does not have anything there.
752 int bind_merge(struct cache_entry
**src
,
753 struct unpack_trees_options
*o
)
755 struct cache_entry
*old
= src
[0];
756 struct cache_entry
*a
= src
[1];
758 if (o
->merge_size
!= 1)
759 return error("Cannot do a bind merge of %d trees\n",
762 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
764 return keep_entry(old
);
766 return merged_entry(a
, NULL
, o
);
773 * - take the stat information from stage0, take the data from stage1
775 int oneway_merge(struct cache_entry
**src
,
776 struct unpack_trees_options
*o
)
778 struct cache_entry
*old
= src
[0];
779 struct cache_entry
*a
= src
[1];
781 if (o
->merge_size
!= 1)
782 return error("Cannot do a oneway merge of %d trees",
786 return deleted_entry(old
, old
, o
);
787 if (old
&& same(old
, a
)) {
790 if (lstat(old
->name
, &st
) ||
791 ce_match_stat(old
, &st
, 1))
792 old
->ce_flags
|= htons(CE_UPDATE
);
794 return keep_entry(old
);
796 return merged_entry(a
, old
, o
);