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