1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
8#include "object.h"
9#include "tree.h"
10
11static int merge = 0;
12static int update = 0;
13
14static int head_idx = -1;
15static int merge_size = 0;
16
17static struct object_list *trees = NULL;
18
19static struct cache_entry df_conflict_entry = {
20};
21
22static struct tree_entry_list df_conflict_list = {
23 .name = NULL,
24 .next = &df_conflict_list
25};
26
27typedef int (*merge_fn_t)(struct cache_entry **src);
28
29static int entcmp(char *name1, int dir1, char *name2, int dir2)
30{
31 int len1 = strlen(name1);
32 int len2 = strlen(name2);
33 int len = len1 < len2 ? len1 : len2;
34 int ret = memcmp(name1, name2, len);
35 unsigned char c1, c2;
36 if (ret)
37 return ret;
38 c1 = name1[len];
39 c2 = name2[len];
40 if (!c1 && dir1)
41 c1 = '/';
42 if (!c2 && dir2)
43 c2 = '/';
44 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
45 if (c1 && c2 && !ret)
46 ret = len1 - len2;
47 return ret;
48}
49
50static int unpack_trees_rec(struct tree_entry_list **posns, int len,
51 const char *base, merge_fn_t fn, int *indpos)
52{
53 int baselen = strlen(base);
54 int src_size = len + 1;
55 do {
56 int i;
57 char *first;
58 int firstdir = 0;
59 int pathlen;
60 unsigned ce_size;
61 struct tree_entry_list **subposns;
62 struct cache_entry **src;
63 int any_files = 0;
64 int any_dirs = 0;
65 char *cache_name;
66 int ce_stage;
67
68 /* Find the first name in the input. */
69
70 first = NULL;
71 cache_name = NULL;
72
73 /* Check the cache */
74 if (merge && *indpos < active_nr) {
75 /* This is a bit tricky: */
76 /* If the index has a subdirectory (with
77 * contents) as the first name, it'll get a
78 * filename like "foo/bar". But that's after
79 * "foo", so the entry in trees will get
80 * handled first, at which point we'll go into
81 * "foo", and deal with "bar" from the index,
82 * because the base will be "foo/". The only
83 * way we can actually have "foo/bar" first of
84 * all the things is if the trees don't
85 * contain "foo" at all, in which case we'll
86 * handle "foo/bar" without going into the
87 * directory, but that's fine (and will return
88 * an error anyway, with the added unknown
89 * file case.
90 */
91
92 cache_name = active_cache[*indpos]->name;
93 if (strlen(cache_name) > baselen &&
94 !memcmp(cache_name, base, baselen)) {
95 cache_name += baselen;
96 first = cache_name;
97 } else {
98 cache_name = NULL;
99 }
100 }
101
102 if (first)
103 printf("index %s\n", first);
104
105 for (i = 0; i < len; i++) {
106 if (!posns[i] || posns[i] == &df_conflict_list)
107 continue;
108 printf("%d %s\n", i + 1, posns[i]->name);
109 if (!first || entcmp(first, firstdir,
110 posns[i]->name,
111 posns[i]->directory) > 0) {
112 first = posns[i]->name;
113 firstdir = posns[i]->directory;
114 }
115 }
116 /* No name means we're done */
117 if (!first)
118 return 0;
119
120 pathlen = strlen(first);
121 ce_size = cache_entry_size(baselen + pathlen);
122
123 src = xmalloc(sizeof(struct cache_entry *) * src_size);
124 memset(src, 0, sizeof(struct cache_entry *) * src_size);
125
126 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
127 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
128
129 if (cache_name && !strcmp(cache_name, first)) {
130 any_files = 1;
131 src[0] = active_cache[*indpos];
132 remove_cache_entry_at(*indpos);
133 }
134
135 for (i = 0; i < len; i++) {
136 struct cache_entry *ce;
137
138 if (!posns[i] ||
139 (posns[i] != &df_conflict_list &&
140 strcmp(first, posns[i]->name))) {
141 continue;
142 }
143
144 if (posns[i] == &df_conflict_list) {
145 src[i + merge] = &df_conflict_entry;
146 continue;
147 }
148
149 if (posns[i]->directory) {
150 any_dirs = 1;
151 parse_tree(posns[i]->item.tree);
152 subposns[i] = posns[i]->item.tree->entries;
153 posns[i] = posns[i]->next;
154 src[i + merge] = &df_conflict_entry;
155 continue;
156 }
157
158 if (!merge)
159 ce_stage = 0;
160 else if (i + 1 < head_idx)
161 ce_stage = 1;
162 else if (i + 1 > head_idx)
163 ce_stage = 3;
164 else
165 ce_stage = 2;
166
167 ce = xmalloc(ce_size);
168 memset(ce, 0, ce_size);
169 ce->ce_mode = create_ce_mode(posns[i]->mode);
170 ce->ce_flags = create_ce_flags(baselen + pathlen,
171 ce_stage);
172 memcpy(ce->name, base, baselen);
173 memcpy(ce->name + baselen, first, pathlen + 1);
174
175 any_files = 1;
176
177 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
178 src[i + merge] = ce;
179 subposns[i] = &df_conflict_list;
180 posns[i] = posns[i]->next;
181 }
182 if (any_files) {
183 if (merge) {
184 int ret;
185
186 printf("%s:\n", first);
187 for (i = 0; i < src_size; i++) {
188 printf(" %d ", i);
189 if (src[i])
190 printf("%s\n", sha1_to_hex(src[i]->sha1));
191 else
192 printf("\n");
193 }
194
195 ret = fn(src);
196
197 printf("Added %d entries\n", ret);
198
199 *indpos += ret;
200 } else {
201 for (i = 0; i < src_size; i++) {
202 if (src[i]) {
203 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
204 }
205 }
206 }
207 }
208 if (any_dirs) {
209 char *newbase = xmalloc(baselen + 2 + pathlen);
210 memcpy(newbase, base, baselen);
211 memcpy(newbase + baselen, first, pathlen);
212 newbase[baselen + pathlen] = '/';
213 newbase[baselen + pathlen + 1] = '\0';
214 if (unpack_trees_rec(subposns, len, newbase, fn,
215 indpos))
216 return -1;
217 }
218 free(subposns);
219 free(src);
220 } while (1);
221}
222
223static void reject_merge(struct cache_entry *ce)
224{
225 die("Entry '%s' would be overwritten by merge. Cannot merge.",
226 ce->name);
227}
228
229static void check_updates(struct cache_entry **src, int nr)
230{
231 static struct checkout state = {
232 .base_dir = "",
233 .force = 1,
234 .quiet = 1,
235 .refresh_cache = 1,
236 };
237 unsigned short mask = htons(CE_UPDATE);
238 while (nr--) {
239 struct cache_entry *ce = *src++;
240 if (!ce->ce_mode) {
241 if (update)
242 unlink(ce->name);
243 continue;
244 }
245 if (ce->ce_flags & mask) {
246 ce->ce_flags &= ~mask;
247 if (update)
248 checkout_entry(ce, &state);
249 }
250 }
251}
252
253static int unpack_trees(merge_fn_t fn)
254{
255 int indpos = 0;
256 unsigned len = object_list_length(trees);
257 struct tree_entry_list **posns =
258 xmalloc(len * sizeof(struct tree_entry_list *));
259 int i;
260 struct object_list *posn = trees;
261 merge_size = len;
262 for (i = 0; i < len; i++) {
263 posns[i] = ((struct tree *) posn->item)->entries;
264 posn = posn->next;
265 }
266 if (unpack_trees_rec(posns, len, "", fn, &indpos))
267 return -1;
268
269 check_updates(active_cache, active_nr);
270 return 0;
271}
272
273static int list_tree(unsigned char *sha1)
274{
275 struct tree *tree = parse_tree_indirect(sha1);
276 if (!tree)
277 return -1;
278 object_list_append(&tree->object, &trees);
279 return 0;
280}
281
282static int same(struct cache_entry *a, struct cache_entry *b)
283{
284 if (!!a != !!b)
285 return 0;
286 if (!a && !b)
287 return 1;
288 return a->ce_mode == b->ce_mode &&
289 !memcmp(a->sha1, b->sha1, 20);
290}
291
292
293/*
294 * When a CE gets turned into an unmerged entry, we
295 * want it to be up-to-date
296 */
297static void verify_uptodate(struct cache_entry *ce)
298{
299 struct stat st;
300
301 if (!lstat(ce->name, &st)) {
302 unsigned changed = ce_match_stat(ce, &st);
303 if (!changed)
304 return;
305 errno = 0;
306 }
307 if (errno == ENOENT)
308 return;
309 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
310}
311
312static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
313{
314 merge->ce_flags |= htons(CE_UPDATE);
315 if (old) {
316 /*
317 * See if we can re-use the old CE directly?
318 * That way we get the uptodate stat info.
319 *
320 * This also removes the UPDATE flag on
321 * a match.
322 */
323 if (same(old, merge)) {
324 *merge = *old;
325 } else {
326 verify_uptodate(old);
327 }
328 }
329 merge->ce_flags &= ~htons(CE_STAGEMASK);
330 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
331 return 1;
332}
333
334static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
335{
336 if (old)
337 verify_uptodate(old);
338 ce->ce_mode = 0;
339 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
340 return 1;
341}
342
343static int keep_entry(struct cache_entry *ce)
344{
345 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
346 return 1;
347}
348
349static int threeway_merge(struct cache_entry **stages)
350{
351 struct cache_entry *index;
352 struct cache_entry *head;
353 struct cache_entry *remote = stages[head_idx + 1];
354 int count;
355 int head_match = 0;
356 int remote_match = 0;
357
358 int df_conflict_head = 0;
359 int df_conflict_remote = 0;
360
361 int any_anc_missing = 0;
362 int i;
363
364 for (i = 1; i < head_idx; i++) {
365 if (!stages[i])
366 any_anc_missing = 1;
367 }
368
369 index = stages[0];
370 head = stages[head_idx];
371
372 if (head == &df_conflict_entry) {
373 df_conflict_head = 1;
374 head = NULL;
375 }
376
377 if (remote == &df_conflict_entry) {
378 df_conflict_remote = 1;
379 remote = NULL;
380 }
381
382 /* First, if there's a #16 situation, note that to prevent #13
383 * and #14.
384 */
385 if (!same(remote, head)) {
386 for (i = 1; i < head_idx; i++) {
387 if (same(stages[i], head)) {
388 head_match = 1;
389 }
390 if (same(stages[i], remote)) {
391 remote_match = 1;
392 }
393 }
394 }
395
396 /* We start with cases where the index is allowed to match
397 * something other than the head: #14(ALT) and #2ALT, where it
398 * is permitted to match the result instead.
399 */
400 /* #14, #14ALT, #2ALT */
401 if (remote && !df_conflict_head && head_match && !remote_match) {
402 if (index && !same(index, remote) && !same(index, head))
403 reject_merge(index);
404 return merged_entry(remote, index);
405 }
406 /*
407 * If we have an entry in the index cache, then we want to
408 * make sure that it matches head.
409 */
410 if (index && !same(index, head)) {
411 reject_merge(index);
412 }
413
414 if (head) {
415 /* #5ALT, #15 */
416 if (same(head, remote))
417 return merged_entry(head, index);
418 /* #13, #3ALT */
419 if (!df_conflict_remote && remote_match && !head_match)
420 return merged_entry(head, index);
421 }
422
423 /* #1 */
424 if (!head && !remote && any_anc_missing)
425 return 0;
426
427 /* Below are "no merge" cases, which require that the index be
428 * up-to-date to avoid the files getting overwritten with
429 * conflict resolution files.
430 */
431 if (index) {
432 verify_uptodate(index);
433 }
434
435 /* #2, #3, #4, #6, #7, #9, #11. */
436 count = 0;
437 if (!head_match || !remote_match) {
438 for (i = 1; i < head_idx; i++) {
439 if (stages[i]) {
440 keep_entry(stages[i]);
441 count++;
442 break;
443 }
444 }
445 }
446 if (head) { count += keep_entry(head); }
447 if (remote) { count += keep_entry(remote); }
448 return count;
449}
450
451/*
452 * Two-way merge.
453 *
454 * The rule is to "carry forward" what is in the index without losing
455 * information across a "fast forward", favoring a successful merge
456 * over a merge failure when it makes sense. For details of the
457 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
458 *
459 */
460static int twoway_merge(struct cache_entry **src)
461{
462 struct cache_entry *current = src[0];
463 struct cache_entry *oldtree = src[1], *newtree = src[2];
464
465 if (merge_size != 2)
466 return error("Cannot do a twoway merge of %d trees\n",
467 merge_size);
468
469 if (current) {
470 if ((!oldtree && !newtree) || /* 4 and 5 */
471 (!oldtree && newtree &&
472 same(current, newtree)) || /* 6 and 7 */
473 (oldtree && newtree &&
474 same(oldtree, newtree)) || /* 14 and 15 */
475 (oldtree && newtree &&
476 !same(oldtree, newtree) && /* 18 and 19*/
477 same(current, newtree))) {
478 return keep_entry(current);
479 }
480 else if (oldtree && !newtree && same(current, oldtree)) {
481 /* 10 or 11 */
482 return deleted_entry(oldtree, current);
483 }
484 else if (oldtree && newtree &&
485 same(current, oldtree) && !same(current, newtree)) {
486 /* 20 or 21 */
487 return merged_entry(newtree, current);
488 }
489 else {
490 /* all other failures */
491 if (oldtree)
492 reject_merge(oldtree);
493 if (current)
494 reject_merge(current);
495 if (newtree)
496 reject_merge(newtree);
497 return -1;
498 }
499 }
500 else if (newtree)
501 return merged_entry(newtree, current);
502 else
503 return deleted_entry(oldtree, current);
504}
505
506/*
507 * One-way merge.
508 *
509 * The rule is:
510 * - take the stat information from stage0, take the data from stage1
511 */
512static int oneway_merge(struct cache_entry **src)
513{
514 struct cache_entry *old = src[0];
515 struct cache_entry *a = src[1];
516
517 if (merge_size != 1)
518 return error("Cannot do a oneway merge of %d trees\n",
519 merge_size);
520
521 if (!a)
522 return 0;
523 if (old && same(old, a)) {
524 return keep_entry(old);
525 }
526 return merged_entry(a, NULL);
527}
528
529static int read_cache_unmerged(void)
530{
531 int i, deleted;
532 struct cache_entry **dst;
533
534 read_cache();
535 dst = active_cache;
536 deleted = 0;
537 for (i = 0; i < active_nr; i++) {
538 struct cache_entry *ce = active_cache[i];
539 if (ce_stage(ce)) {
540 deleted++;
541 continue;
542 }
543 if (deleted)
544 *dst = ce;
545 dst++;
546 }
547 active_nr -= deleted;
548 return deleted;
549}
550
551static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
552
553static struct cache_file cache_file;
554
555int main(int argc, char **argv)
556{
557 int i, newfd, reset, stage = 0;
558 unsigned char sha1[20];
559 merge_fn_t fn = NULL;
560
561 newfd = hold_index_file_for_update(&cache_file, get_index_file());
562 if (newfd < 0)
563 die("unable to create new cachefile");
564
565 merge = 0;
566 reset = 0;
567 for (i = 1; i < argc; i++) {
568 const char *arg = argv[i];
569
570 /* "-u" means "update", meaning that a merge will update the working directory */
571 if (!strcmp(arg, "-u")) {
572 update = 1;
573 continue;
574 }
575
576 /* This differs from "-m" in that we'll silently ignore unmerged entries */
577 if (!strcmp(arg, "--reset")) {
578 if (stage || merge)
579 usage(read_tree_usage);
580 reset = 1;
581 merge = 1;
582 stage = 1;
583 read_cache_unmerged();
584 continue;
585 }
586
587 if (!strcmp(arg, "--head")) {
588 head_idx = stage - 1;
589 fn = threeway_merge;
590 }
591
592 /* "-m" stands for "merge", meaning we start in stage 1 */
593 if (!strcmp(arg, "-m")) {
594 if (stage || merge)
595 usage(read_tree_usage);
596 if (read_cache_unmerged())
597 die("you need to resolve your current index first");
598 stage = 1;
599 merge = 1;
600 continue;
601 }
602
603 if (get_sha1(arg, sha1) < 0)
604 usage(read_tree_usage);
605 if (list_tree(sha1) < 0)
606 die("failed to unpack tree object %s", arg);
607 stage++;
608 }
609 if (update && !merge)
610 usage(read_tree_usage);
611 if (merge && !fn) {
612 if (stage < 2)
613 die("just how do you expect me to merge %d trees?", stage-1);
614 switch (stage - 1) {
615 case 1:
616 fn = oneway_merge;
617 break;
618 case 2:
619 fn = twoway_merge;
620 break;
621 case 3:
622 fn = threeway_merge;
623 break;
624 default:
625 fn = threeway_merge;
626 break;
627 }
628 }
629
630 if (head_idx < 0) {
631 if (stage - 1 >= 3)
632 head_idx = stage - 2;
633 else
634 head_idx = 1;
635 }
636
637 unpack_trees(fn);
638 if (write_cache(newfd, active_cache, active_nr) ||
639 commit_index_file(&cache_file))
640 die("unable to write new index file");
641 return 0;
642}