]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
4d3fe0c5 JH |
6 | #define DBRT_DEBUG 1 |
7 | ||
e83c5163 LT |
8 | #include "cache.h" |
9 | ||
ee6566e8 DB |
10 | #include "object.h" |
11 | #include "tree.h" | |
744633cb JH |
12 | #include <sys/time.h> |
13 | #include <signal.h> | |
ee6566e8 | 14 | |
6d6776cb | 15 | static int reset = 0; |
ee6566e8 | 16 | static int merge = 0; |
220a0b52 | 17 | static int update = 0; |
720d150c | 18 | static int index_only = 0; |
23822a35 LT |
19 | static int nontrivial_merge = 0; |
20 | static int trivial_merges_only = 0; | |
1b1fdf8c | 21 | static int aggressive = 0; |
744633cb JH |
22 | static int verbose_update = 0; |
23 | static volatile int progress_update = 0; | |
d99082e0 | 24 | |
ee6566e8 DB |
25 | static int head_idx = -1; |
26 | static int merge_size = 0; | |
b12ec373 | 27 | |
ee6566e8 DB |
28 | static struct object_list *trees = NULL; |
29 | ||
30 | static struct cache_entry df_conflict_entry = { | |
31 | }; | |
32 | ||
33 | static struct tree_entry_list df_conflict_list = { | |
34 | .name = NULL, | |
35 | .next = &df_conflict_list | |
36 | }; | |
37 | ||
38 | typedef int (*merge_fn_t)(struct cache_entry **src); | |
39 | ||
40 | static int entcmp(char *name1, int dir1, char *name2, int dir2) | |
41 | { | |
42 | int len1 = strlen(name1); | |
43 | int len2 = strlen(name2); | |
44 | int len = len1 < len2 ? len1 : len2; | |
45 | int ret = memcmp(name1, name2, len); | |
46 | unsigned char c1, c2; | |
47 | if (ret) | |
48 | return ret; | |
49 | c1 = name1[len]; | |
50 | c2 = name2[len]; | |
51 | if (!c1 && dir1) | |
52 | c1 = '/'; | |
53 | if (!c2 && dir2) | |
54 | c2 = '/'; | |
55 | ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
56 | if (c1 && c2 && !ret) | |
57 | ret = len1 - len2; | |
14242464 | 58 | return ret; |
b12ec373 JH |
59 | } |
60 | ||
ee6566e8 DB |
61 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, |
62 | const char *base, merge_fn_t fn, int *indpos) | |
ca016f0e | 63 | { |
ee6566e8 DB |
64 | int baselen = strlen(base); |
65 | int src_size = len + 1; | |
66 | do { | |
67 | int i; | |
68 | char *first; | |
69 | int firstdir = 0; | |
70 | int pathlen; | |
71 | unsigned ce_size; | |
72 | struct tree_entry_list **subposns; | |
73 | struct cache_entry **src; | |
74 | int any_files = 0; | |
75 | int any_dirs = 0; | |
76 | char *cache_name; | |
77 | int ce_stage; | |
78 | ||
79 | /* Find the first name in the input. */ | |
80 | ||
81 | first = NULL; | |
82 | cache_name = NULL; | |
83 | ||
84 | /* Check the cache */ | |
85 | if (merge && *indpos < active_nr) { | |
86 | /* This is a bit tricky: */ | |
87 | /* If the index has a subdirectory (with | |
88 | * contents) as the first name, it'll get a | |
89 | * filename like "foo/bar". But that's after | |
90 | * "foo", so the entry in trees will get | |
91 | * handled first, at which point we'll go into | |
92 | * "foo", and deal with "bar" from the index, | |
93 | * because the base will be "foo/". The only | |
94 | * way we can actually have "foo/bar" first of | |
95 | * all the things is if the trees don't | |
96 | * contain "foo" at all, in which case we'll | |
97 | * handle "foo/bar" without going into the | |
98 | * directory, but that's fine (and will return | |
99 | * an error anyway, with the added unknown | |
100 | * file case. | |
101 | */ | |
102 | ||
103 | cache_name = active_cache[*indpos]->name; | |
104 | if (strlen(cache_name) > baselen && | |
105 | !memcmp(cache_name, base, baselen)) { | |
106 | cache_name += baselen; | |
107 | first = cache_name; | |
108 | } else { | |
109 | cache_name = NULL; | |
110 | } | |
111 | } | |
112 | ||
4d3fe0c5 | 113 | #if DBRT_DEBUG > 1 |
ee6566e8 DB |
114 | if (first) |
115 | printf("index %s\n", first); | |
2ab706a3 | 116 | #endif |
ee6566e8 DB |
117 | for (i = 0; i < len; i++) { |
118 | if (!posns[i] || posns[i] == &df_conflict_list) | |
119 | continue; | |
4d3fe0c5 | 120 | #if DBRT_DEBUG > 1 |
ee6566e8 | 121 | printf("%d %s\n", i + 1, posns[i]->name); |
2ab706a3 | 122 | #endif |
ee6566e8 DB |
123 | if (!first || entcmp(first, firstdir, |
124 | posns[i]->name, | |
125 | posns[i]->directory) > 0) { | |
126 | first = posns[i]->name; | |
127 | firstdir = posns[i]->directory; | |
128 | } | |
129 | } | |
130 | /* No name means we're done */ | |
131 | if (!first) | |
132 | return 0; | |
133 | ||
134 | pathlen = strlen(first); | |
135 | ce_size = cache_entry_size(baselen + pathlen); | |
136 | ||
90321c10 | 137 | src = xcalloc(src_size, sizeof(struct cache_entry *)); |
ee6566e8 | 138 | |
90321c10 | 139 | subposns = xcalloc(len, sizeof(struct tree_list_entry *)); |
ee6566e8 DB |
140 | |
141 | if (cache_name && !strcmp(cache_name, first)) { | |
142 | any_files = 1; | |
143 | src[0] = active_cache[*indpos]; | |
144 | remove_cache_entry_at(*indpos); | |
145 | } | |
146 | ||
147 | for (i = 0; i < len; i++) { | |
148 | struct cache_entry *ce; | |
149 | ||
150 | if (!posns[i] || | |
151 | (posns[i] != &df_conflict_list && | |
152 | strcmp(first, posns[i]->name))) { | |
153 | continue; | |
154 | } | |
155 | ||
156 | if (posns[i] == &df_conflict_list) { | |
157 | src[i + merge] = &df_conflict_entry; | |
158 | continue; | |
159 | } | |
160 | ||
161 | if (posns[i]->directory) { | |
162 | any_dirs = 1; | |
163 | parse_tree(posns[i]->item.tree); | |
164 | subposns[i] = posns[i]->item.tree->entries; | |
165 | posns[i] = posns[i]->next; | |
166 | src[i + merge] = &df_conflict_entry; | |
167 | continue; | |
168 | } | |
169 | ||
170 | if (!merge) | |
171 | ce_stage = 0; | |
172 | else if (i + 1 < head_idx) | |
173 | ce_stage = 1; | |
174 | else if (i + 1 > head_idx) | |
175 | ce_stage = 3; | |
176 | else | |
177 | ce_stage = 2; | |
178 | ||
90321c10 | 179 | ce = xcalloc(1, ce_size); |
ee6566e8 DB |
180 | ce->ce_mode = create_ce_mode(posns[i]->mode); |
181 | ce->ce_flags = create_ce_flags(baselen + pathlen, | |
182 | ce_stage); | |
183 | memcpy(ce->name, base, baselen); | |
184 | memcpy(ce->name + baselen, first, pathlen + 1); | |
185 | ||
186 | any_files = 1; | |
187 | ||
188 | memcpy(ce->sha1, posns[i]->item.any->sha1, 20); | |
189 | src[i + merge] = ce; | |
190 | subposns[i] = &df_conflict_list; | |
191 | posns[i] = posns[i]->next; | |
192 | } | |
193 | if (any_files) { | |
194 | if (merge) { | |
195 | int ret; | |
196 | ||
4d3fe0c5 | 197 | #if DBRT_DEBUG > 1 |
ee6566e8 DB |
198 | printf("%s:\n", first); |
199 | for (i = 0; i < src_size; i++) { | |
200 | printf(" %d ", i); | |
201 | if (src[i]) | |
202 | printf("%s\n", sha1_to_hex(src[i]->sha1)); | |
203 | else | |
204 | printf("\n"); | |
205 | } | |
2ab706a3 | 206 | #endif |
ee6566e8 DB |
207 | ret = fn(src); |
208 | ||
4d3fe0c5 | 209 | #if DBRT_DEBUG > 1 |
ee6566e8 | 210 | printf("Added %d entries\n", ret); |
2ab706a3 | 211 | #endif |
ee6566e8 DB |
212 | *indpos += ret; |
213 | } else { | |
214 | for (i = 0; i < src_size; i++) { | |
215 | if (src[i]) { | |
216 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | if (any_dirs) { | |
222 | char *newbase = xmalloc(baselen + 2 + pathlen); | |
223 | memcpy(newbase, base, baselen); | |
224 | memcpy(newbase + baselen, first, pathlen); | |
225 | newbase[baselen + pathlen] = '/'; | |
226 | newbase[baselen + pathlen + 1] = '\0'; | |
227 | if (unpack_trees_rec(subposns, len, newbase, fn, | |
228 | indpos)) | |
229 | return -1; | |
bb97a2a8 | 230 | free(newbase); |
ee6566e8 DB |
231 | } |
232 | free(subposns); | |
233 | free(src); | |
234 | } while (1); | |
ca016f0e LT |
235 | } |
236 | ||
ee6566e8 | 237 | static void reject_merge(struct cache_entry *ce) |
43f91266 | 238 | { |
ee6566e8 DB |
239 | die("Entry '%s' would be overwritten by merge. Cannot merge.", |
240 | ce->name); | |
43f91266 LT |
241 | } |
242 | ||
340e4f88 JH |
243 | /* Unlink the last component and attempt to remove leading |
244 | * directories, in case this unlink is the removal of the | |
245 | * last entry in the directory -- empty directories are removed. | |
246 | */ | |
247 | static void unlink_entry(char *name) | |
248 | { | |
249 | char *cp, *prev; | |
250 | ||
251 | if (unlink(name)) | |
252 | return; | |
253 | prev = NULL; | |
254 | while (1) { | |
255 | int status; | |
256 | cp = strrchr(name, '/'); | |
257 | if (prev) | |
258 | *prev = '/'; | |
259 | if (!cp) | |
260 | break; | |
261 | ||
262 | *cp = 0; | |
263 | status = rmdir(name); | |
264 | if (status) { | |
265 | *cp = '/'; | |
266 | break; | |
267 | } | |
268 | prev = cp; | |
269 | } | |
270 | } | |
271 | ||
744633cb JH |
272 | static void progress_interval(int signum) |
273 | { | |
744633cb JH |
274 | progress_update = 1; |
275 | } | |
276 | ||
72fdfb50 JR |
277 | static void setup_progress_signal(void) |
278 | { | |
279 | struct sigaction sa; | |
280 | struct itimerval v; | |
281 | ||
282 | memset(&sa, 0, sizeof(sa)); | |
283 | sa.sa_handler = progress_interval; | |
284 | sigemptyset(&sa.sa_mask); | |
285 | sa.sa_flags = SA_RESTART; | |
286 | sigaction(SIGALRM, &sa, NULL); | |
287 | ||
288 | v.it_interval.tv_sec = 1; | |
289 | v.it_interval.tv_usec = 0; | |
290 | v.it_value = v.it_interval; | |
291 | setitimer(ITIMER_REAL, &v, NULL); | |
292 | } | |
293 | ||
ee6566e8 DB |
294 | static void check_updates(struct cache_entry **src, int nr) |
295 | { | |
296 | static struct checkout state = { | |
297 | .base_dir = "", | |
298 | .force = 1, | |
299 | .quiet = 1, | |
300 | .refresh_cache = 1, | |
301 | }; | |
302 | unsigned short mask = htons(CE_UPDATE); | |
744633cb JH |
303 | unsigned last_percent = 200, cnt = 0, total = 0; |
304 | ||
305 | if (update && verbose_update) { | |
744633cb JH |
306 | for (total = cnt = 0; cnt < nr; cnt++) { |
307 | struct cache_entry *ce = src[cnt]; | |
308 | if (!ce->ce_mode || ce->ce_flags & mask) | |
309 | total++; | |
310 | } | |
311 | ||
312 | /* Don't bother doing this for very small updates */ | |
313 | if (total < 250) | |
314 | total = 0; | |
315 | ||
316 | if (total) { | |
744633cb | 317 | fprintf(stderr, "Checking files out...\n"); |
72fdfb50 | 318 | setup_progress_signal(); |
744633cb JH |
319 | progress_update = 1; |
320 | } | |
321 | cnt = 0; | |
322 | } | |
323 | ||
ee6566e8 DB |
324 | while (nr--) { |
325 | struct cache_entry *ce = *src++; | |
744633cb JH |
326 | |
327 | if (total) { | |
328 | if (!ce->ce_mode || ce->ce_flags & mask) { | |
329 | unsigned percent; | |
330 | cnt++; | |
331 | percent = (cnt * 100) / total; | |
332 | if (percent != last_percent || | |
333 | progress_update) { | |
334 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
335 | percent, cnt, total); | |
336 | last_percent = percent; | |
337 | } | |
338 | } | |
339 | } | |
ee6566e8 DB |
340 | if (!ce->ce_mode) { |
341 | if (update) | |
340e4f88 | 342 | unlink_entry(ce->name); |
ee6566e8 DB |
343 | continue; |
344 | } | |
345 | if (ce->ce_flags & mask) { | |
346 | ce->ce_flags &= ~mask; | |
347 | if (update) | |
de84f99c | 348 | checkout_entry(ce, &state, NULL); |
ee6566e8 DB |
349 | } |
350 | } | |
744633cb | 351 | if (total) { |
744633cb | 352 | signal(SIGALRM, SIG_IGN); |
72fdfb50 | 353 | fputc('\n', stderr); |
744633cb | 354 | } |
ee6566e8 | 355 | } |
43f91266 | 356 | |
ee6566e8 | 357 | static int unpack_trees(merge_fn_t fn) |
d99082e0 | 358 | { |
ee6566e8 DB |
359 | int indpos = 0; |
360 | unsigned len = object_list_length(trees); | |
7e4a2a84 | 361 | struct tree_entry_list **posns; |
ee6566e8 DB |
362 | int i; |
363 | struct object_list *posn = trees; | |
364 | merge_size = len; | |
7e4a2a84 JH |
365 | |
366 | if (len) { | |
367 | posns = xmalloc(len * sizeof(struct tree_entry_list *)); | |
368 | for (i = 0; i < len; i++) { | |
369 | posns[i] = ((struct tree *) posn->item)->entries; | |
370 | posn = posn->next; | |
371 | } | |
372 | if (unpack_trees_rec(posns, len, "", fn, &indpos)) | |
373 | return -1; | |
d723c690 | 374 | } |
ee6566e8 | 375 | |
23822a35 LT |
376 | if (trivial_merges_only && nontrivial_merge) |
377 | die("Merge requires file-level merging"); | |
378 | ||
ee6566e8 DB |
379 | check_updates(active_cache, active_nr); |
380 | return 0; | |
d99082e0 LT |
381 | } |
382 | ||
ee6566e8 DB |
383 | static int list_tree(unsigned char *sha1) |
384 | { | |
385 | struct tree *tree = parse_tree_indirect(sha1); | |
386 | if (!tree) | |
387 | return -1; | |
388 | object_list_append(&tree->object, &trees); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | static int same(struct cache_entry *a, struct cache_entry *b) | |
393 | { | |
394 | if (!!a != !!b) | |
395 | return 0; | |
396 | if (!a && !b) | |
397 | return 1; | |
398 | return a->ce_mode == b->ce_mode && | |
399 | !memcmp(a->sha1, b->sha1, 20); | |
400 | } | |
401 | ||
402 | ||
02ede67a LT |
403 | /* |
404 | * When a CE gets turned into an unmerged entry, we | |
405 | * want it to be up-to-date | |
406 | */ | |
407 | static void verify_uptodate(struct cache_entry *ce) | |
408 | { | |
409 | struct stat st; | |
410 | ||
720d150c JH |
411 | if (index_only) |
412 | return; | |
413 | ||
02ede67a | 414 | if (!lstat(ce->name, &st)) { |
5f73076c | 415 | unsigned changed = ce_match_stat(ce, &st, 1); |
02ede67a LT |
416 | if (!changed) |
417 | return; | |
418 | errno = 0; | |
419 | } | |
6d6776cb LT |
420 | if (reset) { |
421 | ce->ce_flags |= htons(CE_UPDATE); | |
422 | return; | |
423 | } | |
02ede67a LT |
424 | if (errno == ENOENT) |
425 | return; | |
426 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
427 | } | |
428 | ||
ee6566e8 | 429 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old) |
d99082e0 | 430 | { |
d723c690 LT |
431 | merge->ce_flags |= htons(CE_UPDATE); |
432 | if (old) { | |
02ede67a | 433 | /* |
d723c690 LT |
434 | * See if we can re-use the old CE directly? |
435 | * That way we get the uptodate stat info. | |
436 | * | |
437 | * This also removes the UPDATE flag on | |
438 | * a match. | |
02ede67a | 439 | */ |
d723c690 LT |
440 | if (same(old, merge)) { |
441 | *merge = *old; | |
ee6566e8 | 442 | } else { |
d723c690 LT |
443 | verify_uptodate(old); |
444 | } | |
a3a65234 | 445 | } |
d723c690 | 446 | merge->ce_flags &= ~htons(CE_STAGEMASK); |
ee6566e8 | 447 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD); |
d723c690 | 448 | return 1; |
a3a65234 LT |
449 | } |
450 | ||
ee6566e8 | 451 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old) |
aa16021e LT |
452 | { |
453 | if (old) | |
454 | verify_uptodate(old); | |
455 | ce->ce_mode = 0; | |
ee6566e8 | 456 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
aa16021e LT |
457 | return 1; |
458 | } | |
459 | ||
ee6566e8 | 460 | static int keep_entry(struct cache_entry *ce) |
32192e66 | 461 | { |
ee6566e8 DB |
462 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
463 | return 1; | |
32192e66 JH |
464 | } |
465 | ||
4d3fe0c5 JH |
466 | #if DBRT_DEBUG |
467 | static void show_stage_entry(FILE *o, | |
468 | const char *label, const struct cache_entry *ce) | |
469 | { | |
2ba6c47b JH |
470 | if (!ce) |
471 | fprintf(o, "%s (missing)\n", label); | |
472 | else | |
473 | fprintf(o, "%s%06o %s %d\t%s\n", | |
474 | label, | |
475 | ntohl(ce->ce_mode), | |
476 | sha1_to_hex(ce->sha1), | |
477 | ce_stage(ce), | |
478 | ce->name); | |
4d3fe0c5 JH |
479 | } |
480 | #endif | |
481 | ||
ee6566e8 | 482 | static int threeway_merge(struct cache_entry **stages) |
e6ee623b | 483 | { |
ee6566e8 DB |
484 | struct cache_entry *index; |
485 | struct cache_entry *head; | |
486 | struct cache_entry *remote = stages[head_idx + 1]; | |
d723c690 | 487 | int count; |
ee6566e8 DB |
488 | int head_match = 0; |
489 | int remote_match = 0; | |
d723c690 | 490 | |
ee6566e8 DB |
491 | int df_conflict_head = 0; |
492 | int df_conflict_remote = 0; | |
493 | ||
494 | int any_anc_missing = 0; | |
1b1fdf8c | 495 | int no_anc_exists = 1; |
ee6566e8 DB |
496 | int i; |
497 | ||
498 | for (i = 1; i < head_idx; i++) { | |
499 | if (!stages[i]) | |
500 | any_anc_missing = 1; | |
1b1fdf8c JH |
501 | else |
502 | no_anc_exists = 0; | |
e7f9bc41 | 503 | } |
ee6566e8 DB |
504 | |
505 | index = stages[0]; | |
506 | head = stages[head_idx]; | |
507 | ||
508 | if (head == &df_conflict_entry) { | |
509 | df_conflict_head = 1; | |
510 | head = NULL; | |
511 | } | |
512 | ||
513 | if (remote == &df_conflict_entry) { | |
514 | df_conflict_remote = 1; | |
515 | remote = NULL; | |
516 | } | |
517 | ||
518 | /* First, if there's a #16 situation, note that to prevent #13 | |
519 | * and #14. | |
520 | */ | |
521 | if (!same(remote, head)) { | |
522 | for (i = 1; i < head_idx; i++) { | |
523 | if (same(stages[i], head)) { | |
4d3fe0c5 | 524 | head_match = i; |
ee6566e8 DB |
525 | } |
526 | if (same(stages[i], remote)) { | |
4d3fe0c5 | 527 | remote_match = i; |
ee6566e8 | 528 | } |
32192e66 | 529 | } |
32192e66 | 530 | } |
ee6566e8 DB |
531 | |
532 | /* We start with cases where the index is allowed to match | |
533 | * something other than the head: #14(ALT) and #2ALT, where it | |
534 | * is permitted to match the result instead. | |
535 | */ | |
536 | /* #14, #14ALT, #2ALT */ | |
537 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
538 | if (index && !same(index, remote) && !same(index, head)) | |
539 | reject_merge(index); | |
540 | return merged_entry(remote, index); | |
036d51cc | 541 | } |
d723c690 | 542 | /* |
ee6566e8 DB |
543 | * If we have an entry in the index cache, then we want to |
544 | * make sure that it matches head. | |
d723c690 | 545 | */ |
ee6566e8 DB |
546 | if (index && !same(index, head)) { |
547 | reject_merge(index); | |
e6ee623b | 548 | } |
ee6566e8 DB |
549 | |
550 | if (head) { | |
551 | /* #5ALT, #15 */ | |
552 | if (same(head, remote)) | |
553 | return merged_entry(head, index); | |
554 | /* #13, #3ALT */ | |
555 | if (!df_conflict_remote && remote_match && !head_match) | |
556 | return merged_entry(head, index); | |
557 | } | |
558 | ||
559 | /* #1 */ | |
560 | if (!head && !remote && any_anc_missing) | |
561 | return 0; | |
562 | ||
1b1fdf8c JH |
563 | /* Under the new "aggressive" rule, we resolve mostly trivial |
564 | * cases that we historically had git-merge-one-file resolve. | |
565 | */ | |
566 | if (aggressive) { | |
567 | int head_deleted = !head && !df_conflict_head; | |
568 | int remote_deleted = !remote && !df_conflict_remote; | |
569 | /* | |
570 | * Deleted in both. | |
571 | * Deleted in one and unchanged in the other. | |
572 | */ | |
573 | if ((head_deleted && remote_deleted) || | |
574 | (head_deleted && remote && remote_match) || | |
11420380 JH |
575 | (remote_deleted && head && head_match)) { |
576 | if (index) | |
577 | return deleted_entry(index, index); | |
1b1fdf8c | 578 | return 0; |
11420380 | 579 | } |
1b1fdf8c JH |
580 | /* |
581 | * Added in both, identically. | |
582 | */ | |
583 | if (no_anc_exists && head && remote && same(head, remote)) | |
584 | return merged_entry(head, index); | |
585 | ||
586 | } | |
587 | ||
ee6566e8 DB |
588 | /* Below are "no merge" cases, which require that the index be |
589 | * up-to-date to avoid the files getting overwritten with | |
590 | * conflict resolution files. | |
591 | */ | |
592 | if (index) { | |
593 | verify_uptodate(index); | |
594 | } | |
595 | ||
23822a35 LT |
596 | nontrivial_merge = 1; |
597 | ||
ee6566e8 | 598 | /* #2, #3, #4, #6, #7, #9, #11. */ |
d723c690 | 599 | count = 0; |
ee6566e8 DB |
600 | if (!head_match || !remote_match) { |
601 | for (i = 1; i < head_idx; i++) { | |
602 | if (stages[i]) { | |
603 | keep_entry(stages[i]); | |
604 | count++; | |
605 | break; | |
606 | } | |
607 | } | |
608 | } | |
4d3fe0c5 JH |
609 | #if DBRT_DEBUG |
610 | else { | |
611 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
612 | show_stage_entry(stderr, "head ", stages[head_match]); | |
613 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
614 | } | |
615 | #endif | |
ee6566e8 DB |
616 | if (head) { count += keep_entry(head); } |
617 | if (remote) { count += keep_entry(remote); } | |
d723c690 | 618 | return count; |
e6ee623b LT |
619 | } |
620 | ||
220a0b52 LT |
621 | /* |
622 | * Two-way merge. | |
623 | * | |
c8596009 JH |
624 | * The rule is to "carry forward" what is in the index without losing |
625 | * information across a "fast forward", favoring a successful merge | |
626 | * over a merge failure when it makes sense. For details of the | |
627 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
628 | * | |
220a0b52 | 629 | */ |
ee6566e8 | 630 | static int twoway_merge(struct cache_entry **src) |
a3a65234 | 631 | { |
c8596009 JH |
632 | struct cache_entry *current = src[0]; |
633 | struct cache_entry *oldtree = src[1], *newtree = src[2]; | |
220a0b52 | 634 | |
ee6566e8 | 635 | if (merge_size != 2) |
bd2afde8 | 636 | return error("Cannot do a twoway merge of %d trees", |
ee6566e8 | 637 | merge_size); |
e6ee623b | 638 | |
c8596009 JH |
639 | if (current) { |
640 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
641 | (!oldtree && newtree && | |
642 | same(current, newtree)) || /* 6 and 7 */ | |
643 | (oldtree && newtree && | |
644 | same(oldtree, newtree)) || /* 14 and 15 */ | |
645 | (oldtree && newtree && | |
646 | !same(oldtree, newtree) && /* 18 and 19*/ | |
647 | same(current, newtree))) { | |
ee6566e8 | 648 | return keep_entry(current); |
c8596009 JH |
649 | } |
650 | else if (oldtree && !newtree && same(current, oldtree)) { | |
651 | /* 10 or 11 */ | |
ee6566e8 | 652 | return deleted_entry(oldtree, current); |
c8596009 JH |
653 | } |
654 | else if (oldtree && newtree && | |
655 | same(current, oldtree) && !same(current, newtree)) { | |
656 | /* 20 or 21 */ | |
ee6566e8 | 657 | return merged_entry(newtree, current); |
c8596009 | 658 | } |
ee6566e8 | 659 | else { |
c8596009 | 660 | /* all other failures */ |
ee6566e8 DB |
661 | if (oldtree) |
662 | reject_merge(oldtree); | |
663 | if (current) | |
664 | reject_merge(current); | |
665 | if (newtree) | |
666 | reject_merge(newtree); | |
d723c690 | 667 | return -1; |
ee6566e8 | 668 | } |
e6ee623b | 669 | } |
c8596009 | 670 | else if (newtree) |
ee6566e8 | 671 | return merged_entry(newtree, current); |
c8596009 | 672 | else |
ee6566e8 | 673 | return deleted_entry(oldtree, current); |
03efa6d9 JH |
674 | } |
675 | ||
d723c690 LT |
676 | /* |
677 | * One-way merge. | |
678 | * | |
679 | * The rule is: | |
680 | * - take the stat information from stage0, take the data from stage1 | |
681 | */ | |
ee6566e8 | 682 | static int oneway_merge(struct cache_entry **src) |
220a0b52 | 683 | { |
d723c690 LT |
684 | struct cache_entry *old = src[0]; |
685 | struct cache_entry *a = src[1]; | |
a3a65234 | 686 | |
ee6566e8 | 687 | if (merge_size != 1) |
bd2afde8 | 688 | return error("Cannot do a oneway merge of %d trees", |
ee6566e8 | 689 | merge_size); |
a3a65234 | 690 | |
d723c690 | 691 | if (!a) |
76b99b81 | 692 | return deleted_entry(old, NULL); |
b5b42507 | 693 | if (old && same(old, a)) { |
6d6776cb LT |
694 | if (reset) { |
695 | struct stat st; | |
696 | if (lstat(old->name, &st) || | |
697 | ce_match_stat(old, &st, 1)) | |
698 | old->ce_flags |= htons(CE_UPDATE); | |
699 | } | |
ee6566e8 | 700 | return keep_entry(old); |
b5b42507 | 701 | } |
ee6566e8 | 702 | return merged_entry(a, NULL); |
d723c690 LT |
703 | } |
704 | ||
438195cc LT |
705 | static int read_cache_unmerged(void) |
706 | { | |
707 | int i, deleted; | |
708 | struct cache_entry **dst; | |
709 | ||
710 | read_cache(); | |
711 | dst = active_cache; | |
712 | deleted = 0; | |
713 | for (i = 0; i < active_nr; i++) { | |
714 | struct cache_entry *ce = active_cache[i]; | |
715 | if (ce_stage(ce)) { | |
716 | deleted++; | |
717 | continue; | |
718 | } | |
719 | if (deleted) | |
720 | *dst = ce; | |
721 | dst++; | |
722 | } | |
723 | active_nr -= deleted; | |
724 | return deleted; | |
725 | } | |
726 | ||
afaa8d66 | 727 | static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])"; |
c5bac17a | 728 | |
96cd5429 JH |
729 | static struct cache_file cache_file; |
730 | ||
e83c5163 LT |
731 | int main(int argc, char **argv) |
732 | { | |
6d6776cb | 733 | int i, newfd, stage = 0; |
e83c5163 | 734 | unsigned char sha1[20]; |
ee6566e8 | 735 | merge_fn_t fn = NULL; |
bb233d69 | 736 | |
53228a5f | 737 | setup_git_directory(); |
84a9b58c | 738 | git_config(git_default_config); |
53228a5f | 739 | |
96cd5429 | 740 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
83adac3c | 741 | if (newfd < 0) |
2de381f9 | 742 | die("unable to create new cachefile"); |
83adac3c | 743 | |
39b4ac99 AR |
744 | git_config(git_default_config); |
745 | ||
ca016f0e | 746 | merge = 0; |
438195cc | 747 | reset = 0; |
83adac3c LT |
748 | for (i = 1; i < argc; i++) { |
749 | const char *arg = argv[i]; | |
750 | ||
720d150c JH |
751 | /* "-u" means "update", meaning that a merge will update |
752 | * the working tree. | |
753 | */ | |
220a0b52 LT |
754 | if (!strcmp(arg, "-u")) { |
755 | update = 1; | |
756 | continue; | |
757 | } | |
758 | ||
744633cb JH |
759 | if (!strcmp(arg, "-v")) { |
760 | verbose_update = 1; | |
761 | continue; | |
762 | } | |
763 | ||
720d150c JH |
764 | /* "-i" means "index only", meaning that a merge will |
765 | * not even look at the working tree. | |
766 | */ | |
767 | if (!strcmp(arg, "-i")) { | |
768 | index_only = 1; | |
769 | continue; | |
770 | } | |
771 | ||
438195cc LT |
772 | /* This differs from "-m" in that we'll silently ignore unmerged entries */ |
773 | if (!strcmp(arg, "--reset")) { | |
ee6566e8 | 774 | if (stage || merge) |
438195cc LT |
775 | usage(read_tree_usage); |
776 | reset = 1; | |
777 | merge = 1; | |
778 | stage = 1; | |
779 | read_cache_unmerged(); | |
7875b50d | 780 | continue; |
438195cc LT |
781 | } |
782 | ||
23822a35 LT |
783 | if (!strcmp(arg, "--trivial")) { |
784 | trivial_merges_only = 1; | |
785 | continue; | |
786 | } | |
787 | ||
1b1fdf8c JH |
788 | if (!strcmp(arg, "--aggressive")) { |
789 | aggressive = 1; | |
790 | continue; | |
791 | } | |
792 | ||
d99082e0 | 793 | /* "-m" stands for "merge", meaning we start in stage 1 */ |
83adac3c | 794 | if (!strcmp(arg, "-m")) { |
ee6566e8 | 795 | if (stage || merge) |
438195cc LT |
796 | usage(read_tree_usage); |
797 | if (read_cache_unmerged()) | |
798 | die("you need to resolve your current index first"); | |
d99082e0 | 799 | stage = 1; |
ca016f0e | 800 | merge = 1; |
83adac3c LT |
801 | continue; |
802 | } | |
03efa6d9 | 803 | |
720d150c JH |
804 | /* using -u and -i at the same time makes no sense */ |
805 | if (1 < index_only + update) | |
806 | usage(read_tree_usage); | |
807 | ||
31fff305 DL |
808 | if (get_sha1(arg, sha1)) |
809 | die("Not a valid object name %s", arg); | |
ee6566e8 | 810 | if (list_tree(sha1) < 0) |
2de381f9 | 811 | die("failed to unpack tree object %s", arg); |
d99082e0 | 812 | stage++; |
83adac3c | 813 | } |
f318dd22 | 814 | if ((update||index_only) && !merge) |
a57f0b58 | 815 | usage(read_tree_usage); |
7dd43575 JH |
816 | |
817 | if (merge) { | |
ee6566e8 | 818 | if (stage < 2) |
a3a65234 | 819 | die("just how do you expect me to merge %d trees?", stage-1); |
ee6566e8 DB |
820 | switch (stage - 1) { |
821 | case 1: | |
822 | fn = oneway_merge; | |
823 | break; | |
824 | case 2: | |
825 | fn = twoway_merge; | |
826 | break; | |
827 | case 3: | |
828 | fn = threeway_merge; | |
829 | break; | |
830 | default: | |
831 | fn = threeway_merge; | |
832 | break; | |
03efa6d9 | 833 | } |
ee6566e8 | 834 | |
ee6566e8 DB |
835 | if (stage - 1 >= 3) |
836 | head_idx = stage - 2; | |
837 | else | |
838 | head_idx = 1; | |
839 | } | |
840 | ||
841 | unpack_trees(fn); | |
96cd5429 JH |
842 | if (write_cache(newfd, active_cache, active_nr) || |
843 | commit_index_file(&cache_file)) | |
2de381f9 | 844 | die("unable to write new index file"); |
9614b8dc | 845 | return 0; |
e83c5163 | 846 | } |