]>
Commit | Line | Data |
---|---|---|
16da134b | 1 | #include "cache.h" |
f8a9d428 | 2 | #include "dir.h" |
16da134b JS |
3 | #include "tree.h" |
4 | #include "tree-walk.h" | |
076b0adc | 5 | #include "cache-tree.h" |
16da134b | 6 | #include "unpack-trees.h" |
96a02f8f | 7 | #include "progress.h" |
0cf73755 | 8 | #include "refs.h" |
16da134b | 9 | |
076b0adc JS |
10 | #define DBRT_DEBUG 1 |
11 | ||
16da134b JS |
12 | struct tree_entry_list { |
13 | struct tree_entry_list *next; | |
16da134b JS |
14 | unsigned int mode; |
15 | const char *name; | |
16 | const unsigned char *sha1; | |
17 | }; | |
18 | ||
933bf40a | 19 | static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc) |
16da134b | 20 | { |
16da134b JS |
21 | struct name_entry one; |
22 | struct tree_entry_list *ret = NULL; | |
23 | struct tree_entry_list **list_p = &ret; | |
24 | ||
933bf40a | 25 | while (tree_entry(desc, &one)) { |
16da134b JS |
26 | struct tree_entry_list *entry; |
27 | ||
28 | entry = xmalloc(sizeof(struct tree_entry_list)); | |
29 | entry->name = one.path; | |
30 | entry->sha1 = one.sha1; | |
31 | entry->mode = one.mode; | |
16da134b JS |
32 | entry->next = NULL; |
33 | ||
34 | *list_p = entry; | |
35 | list_p = &entry->next; | |
36 | } | |
37 | return ret; | |
38 | } | |
39 | ||
40 | static int entcmp(const char *name1, int dir1, const 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; | |
58 | return ret; | |
59 | } | |
60 | ||
b48d5a05 LT |
61 | static inline void remove_entry(int remove) |
62 | { | |
63 | if (remove >= 0) | |
64 | remove_cache_entry_at(remove); | |
65 | } | |
66 | ||
16da134b JS |
67 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, |
68 | const char *base, struct unpack_trees_options *o, | |
16da134b JS |
69 | struct tree_entry_list *df_conflict_list) |
70 | { | |
b48d5a05 | 71 | int remove; |
16da134b JS |
72 | int baselen = strlen(base); |
73 | int src_size = len + 1; | |
f8a9d428 JH |
74 | int retval = 0; |
75 | ||
16da134b JS |
76 | do { |
77 | int i; | |
78 | const char *first; | |
79 | int firstdir = 0; | |
80 | int pathlen; | |
81 | unsigned ce_size; | |
82 | struct tree_entry_list **subposns; | |
83 | struct cache_entry **src; | |
84 | int any_files = 0; | |
85 | int any_dirs = 0; | |
86 | char *cache_name; | |
87 | int ce_stage; | |
88 | ||
89 | /* Find the first name in the input. */ | |
90 | ||
91 | first = NULL; | |
92 | cache_name = NULL; | |
93 | ||
94 | /* Check the cache */ | |
9a4d8fdc | 95 | if (o->merge && o->pos < active_nr) { |
16da134b JS |
96 | /* This is a bit tricky: */ |
97 | /* If the index has a subdirectory (with | |
98 | * contents) as the first name, it'll get a | |
99 | * filename like "foo/bar". But that's after | |
100 | * "foo", so the entry in trees will get | |
101 | * handled first, at which point we'll go into | |
102 | * "foo", and deal with "bar" from the index, | |
103 | * because the base will be "foo/". The only | |
104 | * way we can actually have "foo/bar" first of | |
105 | * all the things is if the trees don't | |
106 | * contain "foo" at all, in which case we'll | |
107 | * handle "foo/bar" without going into the | |
108 | * directory, but that's fine (and will return | |
109 | * an error anyway, with the added unknown | |
110 | * file case. | |
111 | */ | |
112 | ||
9a4d8fdc | 113 | cache_name = active_cache[o->pos]->name; |
16da134b JS |
114 | if (strlen(cache_name) > baselen && |
115 | !memcmp(cache_name, base, baselen)) { | |
116 | cache_name += baselen; | |
117 | first = cache_name; | |
118 | } else { | |
119 | cache_name = NULL; | |
120 | } | |
121 | } | |
122 | ||
123 | #if DBRT_DEBUG > 1 | |
124 | if (first) | |
b05c6dff | 125 | fprintf(stderr, "index %s\n", first); |
16da134b JS |
126 | #endif |
127 | for (i = 0; i < len; i++) { | |
128 | if (!posns[i] || posns[i] == df_conflict_list) | |
129 | continue; | |
130 | #if DBRT_DEBUG > 1 | |
b05c6dff | 131 | fprintf(stderr, "%d %s\n", i + 1, posns[i]->name); |
16da134b JS |
132 | #endif |
133 | if (!first || entcmp(first, firstdir, | |
134 | posns[i]->name, | |
1843d8d5 | 135 | S_ISDIR(posns[i]->mode)) > 0) { |
16da134b | 136 | first = posns[i]->name; |
1843d8d5 | 137 | firstdir = S_ISDIR(posns[i]->mode); |
16da134b JS |
138 | } |
139 | } | |
140 | /* No name means we're done */ | |
141 | if (!first) | |
f8a9d428 | 142 | goto leave_directory; |
16da134b JS |
143 | |
144 | pathlen = strlen(first); | |
145 | ce_size = cache_entry_size(baselen + pathlen); | |
146 | ||
147 | src = xcalloc(src_size, sizeof(struct cache_entry *)); | |
148 | ||
149 | subposns = xcalloc(len, sizeof(struct tree_list_entry *)); | |
150 | ||
b48d5a05 | 151 | remove = -1; |
16da134b JS |
152 | if (cache_name && !strcmp(cache_name, first)) { |
153 | any_files = 1; | |
9a4d8fdc | 154 | src[0] = active_cache[o->pos]; |
b48d5a05 | 155 | remove = o->pos; |
16da134b JS |
156 | } |
157 | ||
158 | for (i = 0; i < len; i++) { | |
159 | struct cache_entry *ce; | |
160 | ||
161 | if (!posns[i] || | |
162 | (posns[i] != df_conflict_list && | |
163 | strcmp(first, posns[i]->name))) { | |
164 | continue; | |
165 | } | |
166 | ||
167 | if (posns[i] == df_conflict_list) { | |
168 | src[i + o->merge] = o->df_conflict_entry; | |
169 | continue; | |
170 | } | |
171 | ||
1843d8d5 | 172 | if (S_ISDIR(posns[i]->mode)) { |
16da134b | 173 | struct tree *tree = lookup_tree(posns[i]->sha1); |
933bf40a | 174 | struct tree_desc t; |
16da134b JS |
175 | any_dirs = 1; |
176 | parse_tree(tree); | |
933bf40a LT |
177 | init_tree_desc(&t, tree->buffer, tree->size); |
178 | subposns[i] = create_tree_entry_list(&t); | |
16da134b JS |
179 | posns[i] = posns[i]->next; |
180 | src[i + o->merge] = o->df_conflict_entry; | |
181 | continue; | |
182 | } | |
183 | ||
184 | if (!o->merge) | |
185 | ce_stage = 0; | |
186 | else if (i + 1 < o->head_idx) | |
187 | ce_stage = 1; | |
188 | else if (i + 1 > o->head_idx) | |
189 | ce_stage = 3; | |
190 | else | |
191 | ce_stage = 2; | |
192 | ||
193 | ce = xcalloc(1, ce_size); | |
194 | ce->ce_mode = create_ce_mode(posns[i]->mode); | |
195 | ce->ce_flags = create_ce_flags(baselen + pathlen, | |
196 | ce_stage); | |
197 | memcpy(ce->name, base, baselen); | |
198 | memcpy(ce->name + baselen, first, pathlen + 1); | |
199 | ||
200 | any_files = 1; | |
201 | ||
e702496e | 202 | hashcpy(ce->sha1, posns[i]->sha1); |
16da134b JS |
203 | src[i + o->merge] = ce; |
204 | subposns[i] = df_conflict_list; | |
205 | posns[i] = posns[i]->next; | |
206 | } | |
207 | if (any_files) { | |
208 | if (o->merge) { | |
209 | int ret; | |
210 | ||
211 | #if DBRT_DEBUG > 1 | |
b05c6dff | 212 | fprintf(stderr, "%s:\n", first); |
16da134b | 213 | for (i = 0; i < src_size; i++) { |
b05c6dff | 214 | fprintf(stderr, " %d ", i); |
16da134b | 215 | if (src[i]) |
b05c6dff | 216 | fprintf(stderr, "%06x %s\n", src[i]->ce_mode, sha1_to_hex(src[i]->sha1)); |
16da134b | 217 | else |
b05c6dff | 218 | fprintf(stderr, "\n"); |
16da134b JS |
219 | } |
220 | #endif | |
b48d5a05 | 221 | ret = o->fn(src, o, remove); |
203a2fe1 DB |
222 | if (ret < 0) |
223 | return ret; | |
16da134b JS |
224 | |
225 | #if DBRT_DEBUG > 1 | |
b05c6dff | 226 | fprintf(stderr, "Added %d entries\n", ret); |
16da134b | 227 | #endif |
9a4d8fdc | 228 | o->pos += ret; |
16da134b | 229 | } else { |
b48d5a05 | 230 | remove_entry(remove); |
16da134b JS |
231 | for (i = 0; i < src_size; i++) { |
232 | if (src[i]) { | |
233 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); | |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | if (any_dirs) { | |
239 | char *newbase = xmalloc(baselen + 2 + pathlen); | |
240 | memcpy(newbase, base, baselen); | |
241 | memcpy(newbase + baselen, first, pathlen); | |
242 | newbase[baselen + pathlen] = '/'; | |
243 | newbase[baselen + pathlen + 1] = '\0'; | |
244 | if (unpack_trees_rec(subposns, len, newbase, o, | |
9a4d8fdc | 245 | df_conflict_list)) { |
f8a9d428 JH |
246 | retval = -1; |
247 | goto leave_directory; | |
248 | } | |
16da134b JS |
249 | free(newbase); |
250 | } | |
251 | free(subposns); | |
252 | free(src); | |
253 | } while (1); | |
f8a9d428 JH |
254 | |
255 | leave_directory: | |
f8a9d428 | 256 | return retval; |
16da134b JS |
257 | } |
258 | ||
259 | /* Unlink the last component and attempt to remove leading | |
260 | * directories, in case this unlink is the removal of the | |
261 | * last entry in the directory -- empty directories are removed. | |
262 | */ | |
16a4c617 | 263 | static void unlink_entry(char *name, char *last_symlink) |
16da134b JS |
264 | { |
265 | char *cp, *prev; | |
266 | ||
16a4c617 JH |
267 | if (has_symlink_leading_path(name, last_symlink)) |
268 | return; | |
16da134b JS |
269 | if (unlink(name)) |
270 | return; | |
271 | prev = NULL; | |
272 | while (1) { | |
273 | int status; | |
274 | cp = strrchr(name, '/'); | |
275 | if (prev) | |
276 | *prev = '/'; | |
277 | if (!cp) | |
278 | break; | |
279 | ||
280 | *cp = 0; | |
281 | status = rmdir(name); | |
282 | if (status) { | |
283 | *cp = '/'; | |
284 | break; | |
285 | } | |
286 | prev = cp; | |
287 | } | |
288 | } | |
289 | ||
16da134b | 290 | static struct checkout state; |
33ecf7eb | 291 | static void check_updates(struct unpack_trees_options *o) |
16da134b | 292 | { |
96a02f8f | 293 | unsigned cnt = 0, total = 0; |
dc6a0757 | 294 | struct progress *progress = NULL; |
16a4c617 | 295 | char last_symlink[PATH_MAX]; |
33ecf7eb | 296 | int i; |
16da134b JS |
297 | |
298 | if (o->update && o->verbose_update) { | |
33ecf7eb DB |
299 | for (total = cnt = 0; cnt < active_nr; cnt++) { |
300 | struct cache_entry *ce = active_cache[cnt]; | |
7a51ed66 | 301 | if (ce->ce_flags & (CE_UPDATE | CE_REMOVE)) |
16da134b JS |
302 | total++; |
303 | } | |
304 | ||
dc6a0757 NP |
305 | progress = start_progress_delay("Checking out files", |
306 | total, 50, 2); | |
16da134b JS |
307 | cnt = 0; |
308 | } | |
309 | ||
16a4c617 | 310 | *last_symlink = '\0'; |
33ecf7eb DB |
311 | for (i = 0; i < active_nr; i++) { |
312 | struct cache_entry *ce = active_cache[i]; | |
16da134b | 313 | |
7a51ed66 | 314 | if (ce->ce_flags & (CE_UPDATE | CE_REMOVE)) |
4d4fcc54 | 315 | display_progress(progress, ++cnt); |
7a51ed66 | 316 | if (ce->ce_flags & CE_REMOVE) { |
16da134b | 317 | if (o->update) |
16a4c617 | 318 | unlink_entry(ce->name, last_symlink); |
33ecf7eb DB |
319 | remove_cache_entry_at(i); |
320 | i--; | |
16da134b JS |
321 | continue; |
322 | } | |
7a51ed66 LT |
323 | if (ce->ce_flags & CE_UPDATE) { |
324 | ce->ce_flags &= ~CE_UPDATE; | |
16a4c617 | 325 | if (o->update) { |
16da134b | 326 | checkout_entry(ce, &state, NULL); |
16a4c617 JH |
327 | *last_symlink = '\0'; |
328 | } | |
16da134b JS |
329 | } |
330 | } | |
4d4fcc54 | 331 | stop_progress(&progress); |
16da134b JS |
332 | } |
333 | ||
933bf40a | 334 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) |
16da134b | 335 | { |
16da134b JS |
336 | struct tree_entry_list **posns; |
337 | int i; | |
16da134b | 338 | struct tree_entry_list df_conflict_list; |
0fb1eaa8 | 339 | static struct cache_entry *dfc; |
16da134b JS |
340 | |
341 | memset(&df_conflict_list, 0, sizeof(df_conflict_list)); | |
342 | df_conflict_list.next = &df_conflict_list; | |
076b0adc | 343 | memset(&state, 0, sizeof(state)); |
16da134b JS |
344 | state.base_dir = ""; |
345 | state.force = 1; | |
346 | state.quiet = 1; | |
347 | state.refresh_cache = 1; | |
348 | ||
349 | o->merge_size = len; | |
0fb1eaa8 JH |
350 | |
351 | if (!dfc) | |
352 | dfc = xcalloc(1, sizeof(struct cache_entry) + 1); | |
353 | o->df_conflict_entry = dfc; | |
16da134b JS |
354 | |
355 | if (len) { | |
356 | posns = xmalloc(len * sizeof(struct tree_entry_list *)); | |
933bf40a LT |
357 | for (i = 0; i < len; i++) |
358 | posns[i] = create_tree_entry_list(t+i); | |
359 | ||
16da134b | 360 | if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "", |
17e46426 DB |
361 | o, &df_conflict_list)) { |
362 | if (o->gently) { | |
363 | discard_cache(); | |
364 | read_cache(); | |
365 | } | |
16da134b | 366 | return -1; |
17e46426 | 367 | } |
16da134b JS |
368 | } |
369 | ||
17e46426 DB |
370 | if (o->trivial_merges_only && o->nontrivial_merge) { |
371 | if (o->gently) { | |
372 | discard_cache(); | |
373 | read_cache(); | |
374 | } | |
375 | return o->gently ? -1 : | |
376 | error("Merge requires file-level merging"); | |
377 | } | |
16da134b | 378 | |
33ecf7eb | 379 | check_updates(o); |
16da134b JS |
380 | return 0; |
381 | } | |
076b0adc JS |
382 | |
383 | /* Here come the merge functions */ | |
384 | ||
203a2fe1 | 385 | static int reject_merge(struct cache_entry *ce) |
076b0adc | 386 | { |
203a2fe1 DB |
387 | return error("Entry '%s' would be overwritten by merge. Cannot merge.", |
388 | ce->name); | |
076b0adc JS |
389 | } |
390 | ||
391 | static int same(struct cache_entry *a, struct cache_entry *b) | |
392 | { | |
393 | if (!!a != !!b) | |
394 | return 0; | |
395 | if (!a && !b) | |
396 | return 1; | |
397 | return a->ce_mode == b->ce_mode && | |
a89fccd2 | 398 | !hashcmp(a->sha1, b->sha1); |
076b0adc JS |
399 | } |
400 | ||
401 | ||
402 | /* | |
403 | * When a CE gets turned into an unmerged entry, we | |
404 | * want it to be up-to-date | |
405 | */ | |
203a2fe1 | 406 | static int verify_uptodate(struct cache_entry *ce, |
076b0adc JS |
407 | struct unpack_trees_options *o) |
408 | { | |
409 | struct stat st; | |
410 | ||
411 | if (o->index_only || o->reset) | |
203a2fe1 | 412 | return 0; |
076b0adc JS |
413 | |
414 | if (!lstat(ce->name, &st)) { | |
4bd5b7da | 415 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID); |
076b0adc | 416 | if (!changed) |
203a2fe1 | 417 | return 0; |
936492d3 JH |
418 | /* |
419 | * NEEDSWORK: the current default policy is to allow | |
420 | * submodule to be out of sync wrt the supermodule | |
421 | * index. This needs to be tightened later for | |
422 | * submodules that are marked to be automatically | |
423 | * checked out. | |
424 | */ | |
7a51ed66 | 425 | if (S_ISGITLINK(ce->ce_mode)) |
203a2fe1 | 426 | return 0; |
076b0adc JS |
427 | errno = 0; |
428 | } | |
076b0adc | 429 | if (errno == ENOENT) |
203a2fe1 | 430 | return 0; |
17e46426 DB |
431 | return o->gently ? -1 : |
432 | error("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
076b0adc JS |
433 | } |
434 | ||
435 | static void invalidate_ce_path(struct cache_entry *ce) | |
436 | { | |
437 | if (ce) | |
438 | cache_tree_invalidate_path(active_cache_tree, ce->name); | |
439 | } | |
440 | ||
0cf73755 SV |
441 | /* |
442 | * Check that checking out ce->sha1 in subdir ce->name is not | |
443 | * going to overwrite any working files. | |
444 | * | |
445 | * Currently, git does not checkout subprojects during a superproject | |
446 | * checkout, so it is not going to overwrite anything. | |
447 | */ | |
448 | static int verify_clean_submodule(struct cache_entry *ce, const char *action, | |
449 | struct unpack_trees_options *o) | |
450 | { | |
451 | return 0; | |
452 | } | |
453 | ||
454 | static int verify_clean_subdirectory(struct cache_entry *ce, const char *action, | |
c8193534 JH |
455 | struct unpack_trees_options *o) |
456 | { | |
457 | /* | |
0cf73755 | 458 | * we are about to extract "ce->name"; we would not want to lose |
c8193534 JH |
459 | * anything in the existing directory there. |
460 | */ | |
461 | int namelen; | |
462 | int pos, i; | |
463 | struct dir_struct d; | |
464 | char *pathbuf; | |
465 | int cnt = 0; | |
0cf73755 SV |
466 | unsigned char sha1[20]; |
467 | ||
7a51ed66 | 468 | if (S_ISGITLINK(ce->ce_mode) && |
0cf73755 SV |
469 | resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) { |
470 | /* If we are not going to update the submodule, then | |
471 | * we don't care. | |
472 | */ | |
473 | if (!hashcmp(sha1, ce->sha1)) | |
474 | return 0; | |
475 | return verify_clean_submodule(ce, action, o); | |
476 | } | |
c8193534 JH |
477 | |
478 | /* | |
479 | * First let's make sure we do not have a local modification | |
480 | * in that directory. | |
481 | */ | |
0cf73755 SV |
482 | namelen = strlen(ce->name); |
483 | pos = cache_name_pos(ce->name, namelen); | |
c8193534 JH |
484 | if (0 <= pos) |
485 | return cnt; /* we have it as nondirectory */ | |
486 | pos = -pos - 1; | |
487 | for (i = pos; i < active_nr; i++) { | |
488 | struct cache_entry *ce = active_cache[i]; | |
489 | int len = ce_namelen(ce); | |
490 | if (len < namelen || | |
0cf73755 | 491 | strncmp(ce->name, ce->name, namelen) || |
c8193534 JH |
492 | ce->name[namelen] != '/') |
493 | break; | |
494 | /* | |
495 | * ce->name is an entry in the subdirectory. | |
496 | */ | |
497 | if (!ce_stage(ce)) { | |
203a2fe1 DB |
498 | if (verify_uptodate(ce, o)) |
499 | return -1; | |
7a51ed66 | 500 | ce->ce_flags |= CE_REMOVE; |
c8193534 JH |
501 | } |
502 | cnt++; | |
503 | } | |
504 | ||
505 | /* | |
506 | * Then we need to make sure that we do not lose a locally | |
507 | * present file that is not ignored. | |
508 | */ | |
509 | pathbuf = xmalloc(namelen + 2); | |
0cf73755 | 510 | memcpy(pathbuf, ce->name, namelen); |
c8193534 JH |
511 | strcpy(pathbuf+namelen, "/"); |
512 | ||
513 | memset(&d, 0, sizeof(d)); | |
514 | if (o->dir) | |
515 | d.exclude_per_dir = o->dir->exclude_per_dir; | |
0cf73755 | 516 | i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL); |
c8193534 | 517 | if (i) |
17e46426 DB |
518 | return o->gently ? -1 : |
519 | error("Updating '%s' would lose untracked files in it", | |
520 | ce->name); | |
c8193534 JH |
521 | free(pathbuf); |
522 | return cnt; | |
523 | } | |
524 | ||
076b0adc JS |
525 | /* |
526 | * We do not want to remove or overwrite a working tree file that | |
f8a9d428 | 527 | * is not tracked, unless it is ignored. |
076b0adc | 528 | */ |
203a2fe1 DB |
529 | static int verify_absent(struct cache_entry *ce, const char *action, |
530 | struct unpack_trees_options *o) | |
076b0adc JS |
531 | { |
532 | struct stat st; | |
533 | ||
534 | if (o->index_only || o->reset || !o->update) | |
203a2fe1 | 535 | return 0; |
c8193534 | 536 | |
0cf73755 | 537 | if (has_symlink_leading_path(ce->name, NULL)) |
203a2fe1 | 538 | return 0; |
ec0603e1 | 539 | |
0cf73755 | 540 | if (!lstat(ce->name, &st)) { |
c8193534 JH |
541 | int cnt; |
542 | ||
0cf73755 | 543 | if (o->dir && excluded(o->dir, ce->name)) |
c8193534 | 544 | /* |
0cf73755 | 545 | * ce->name is explicitly excluded, so it is Ok to |
c8193534 JH |
546 | * overwrite it. |
547 | */ | |
203a2fe1 | 548 | return 0; |
c8193534 JH |
549 | if (S_ISDIR(st.st_mode)) { |
550 | /* | |
551 | * We are checking out path "foo" and | |
552 | * found "foo/." in the working tree. | |
553 | * This is tricky -- if we have modified | |
554 | * files that are in "foo/" we would lose | |
555 | * it. | |
556 | */ | |
0cf73755 | 557 | cnt = verify_clean_subdirectory(ce, action, o); |
c8193534 JH |
558 | |
559 | /* | |
560 | * If this removed entries from the index, | |
561 | * what that means is: | |
562 | * | |
563 | * (1) the caller unpack_trees_rec() saw path/foo | |
564 | * in the index, and it has not removed it because | |
565 | * it thinks it is handling 'path' as blob with | |
566 | * D/F conflict; | |
567 | * (2) we will return "ok, we placed a merged entry | |
568 | * in the index" which would cause o->pos to be | |
569 | * incremented by one; | |
570 | * (3) however, original o->pos now has 'path/foo' | |
571 | * marked with "to be removed". | |
572 | * | |
573 | * We need to increment it by the number of | |
574 | * deleted entries here. | |
575 | */ | |
576 | o->pos += cnt; | |
203a2fe1 | 577 | return 0; |
c8193534 JH |
578 | } |
579 | ||
580 | /* | |
581 | * The previous round may already have decided to | |
582 | * delete this path, which is in a subdirectory that | |
583 | * is being replaced with a blob. | |
584 | */ | |
0cf73755 | 585 | cnt = cache_name_pos(ce->name, strlen(ce->name)); |
c8193534 JH |
586 | if (0 <= cnt) { |
587 | struct cache_entry *ce = active_cache[cnt]; | |
7a51ed66 | 588 | if (ce->ce_flags & CE_REMOVE) |
203a2fe1 | 589 | return 0; |
c8193534 JH |
590 | } |
591 | ||
17e46426 DB |
592 | return o->gently ? -1 : |
593 | error("Untracked working tree file '%s' " | |
594 | "would be %s by merge.", ce->name, action); | |
c8193534 | 595 | } |
203a2fe1 | 596 | return 0; |
076b0adc JS |
597 | } |
598 | ||
599 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, | |
600 | struct unpack_trees_options *o) | |
601 | { | |
7a51ed66 | 602 | merge->ce_flags |= CE_UPDATE; |
076b0adc JS |
603 | if (old) { |
604 | /* | |
605 | * See if we can re-use the old CE directly? | |
606 | * That way we get the uptodate stat info. | |
607 | * | |
608 | * This also removes the UPDATE flag on | |
609 | * a match. | |
610 | */ | |
611 | if (same(old, merge)) { | |
f2fdd10a | 612 | memcpy(merge, old, offsetof(struct cache_entry, name)); |
076b0adc | 613 | } else { |
203a2fe1 DB |
614 | if (verify_uptodate(old, o)) |
615 | return -1; | |
076b0adc JS |
616 | invalidate_ce_path(old); |
617 | } | |
618 | } | |
619 | else { | |
203a2fe1 DB |
620 | if (verify_absent(merge, "overwritten", o)) |
621 | return -1; | |
076b0adc JS |
622 | invalidate_ce_path(merge); |
623 | } | |
624 | ||
7a51ed66 | 625 | merge->ce_flags &= ~CE_STAGEMASK; |
076b0adc JS |
626 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
627 | return 1; | |
628 | } | |
629 | ||
630 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, | |
631 | struct unpack_trees_options *o) | |
632 | { | |
203a2fe1 DB |
633 | if (old) { |
634 | if (verify_uptodate(old, o)) | |
635 | return -1; | |
636 | } else | |
637 | if (verify_absent(ce, "removed", o)) | |
638 | return -1; | |
7a51ed66 | 639 | ce->ce_flags |= CE_REMOVE; |
076b0adc JS |
640 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
641 | invalidate_ce_path(ce); | |
642 | return 1; | |
643 | } | |
644 | ||
7f7932ab | 645 | static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc JS |
646 | { |
647 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); | |
648 | return 1; | |
649 | } | |
650 | ||
651 | #if DBRT_DEBUG | |
652 | static void show_stage_entry(FILE *o, | |
653 | const char *label, const struct cache_entry *ce) | |
654 | { | |
655 | if (!ce) | |
656 | fprintf(o, "%s (missing)\n", label); | |
657 | else | |
658 | fprintf(o, "%s%06o %s %d\t%s\n", | |
659 | label, | |
7a51ed66 | 660 | ce->ce_mode, |
076b0adc JS |
661 | sha1_to_hex(ce->sha1), |
662 | ce_stage(ce), | |
663 | ce->name); | |
664 | } | |
665 | #endif | |
666 | ||
667 | int threeway_merge(struct cache_entry **stages, | |
b48d5a05 LT |
668 | struct unpack_trees_options *o, |
669 | int remove) | |
076b0adc JS |
670 | { |
671 | struct cache_entry *index; | |
672 | struct cache_entry *head; | |
673 | struct cache_entry *remote = stages[o->head_idx + 1]; | |
674 | int count; | |
675 | int head_match = 0; | |
676 | int remote_match = 0; | |
076b0adc JS |
677 | |
678 | int df_conflict_head = 0; | |
679 | int df_conflict_remote = 0; | |
680 | ||
681 | int any_anc_missing = 0; | |
682 | int no_anc_exists = 1; | |
683 | int i; | |
684 | ||
685 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 686 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
076b0adc | 687 | any_anc_missing = 1; |
4c4caafc | 688 | else |
076b0adc | 689 | no_anc_exists = 0; |
076b0adc JS |
690 | } |
691 | ||
692 | index = stages[0]; | |
693 | head = stages[o->head_idx]; | |
694 | ||
695 | if (head == o->df_conflict_entry) { | |
696 | df_conflict_head = 1; | |
697 | head = NULL; | |
698 | } | |
699 | ||
700 | if (remote == o->df_conflict_entry) { | |
701 | df_conflict_remote = 1; | |
702 | remote = NULL; | |
703 | } | |
704 | ||
076b0adc JS |
705 | /* First, if there's a #16 situation, note that to prevent #13 |
706 | * and #14. | |
707 | */ | |
708 | if (!same(remote, head)) { | |
709 | for (i = 1; i < o->head_idx; i++) { | |
710 | if (same(stages[i], head)) { | |
711 | head_match = i; | |
712 | } | |
713 | if (same(stages[i], remote)) { | |
714 | remote_match = i; | |
715 | } | |
716 | } | |
717 | } | |
718 | ||
719 | /* We start with cases where the index is allowed to match | |
720 | * something other than the head: #14(ALT) and #2ALT, where it | |
721 | * is permitted to match the result instead. | |
722 | */ | |
723 | /* #14, #14ALT, #2ALT */ | |
724 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
725 | if (index && !same(index, remote) && !same(index, head)) | |
17e46426 | 726 | return o->gently ? -1 : reject_merge(index); |
076b0adc JS |
727 | return merged_entry(remote, index, o); |
728 | } | |
729 | /* | |
730 | * If we have an entry in the index cache, then we want to | |
731 | * make sure that it matches head. | |
732 | */ | |
733 | if (index && !same(index, head)) { | |
17e46426 | 734 | return o->gently ? -1 : reject_merge(index); |
076b0adc JS |
735 | } |
736 | ||
737 | if (head) { | |
738 | /* #5ALT, #15 */ | |
739 | if (same(head, remote)) | |
740 | return merged_entry(head, index, o); | |
741 | /* #13, #3ALT */ | |
742 | if (!df_conflict_remote && remote_match && !head_match) | |
743 | return merged_entry(head, index, o); | |
744 | } | |
745 | ||
746 | /* #1 */ | |
566b5c05 LT |
747 | if (!head && !remote && any_anc_missing) { |
748 | remove_entry(remove); | |
076b0adc | 749 | return 0; |
566b5c05 | 750 | } |
076b0adc JS |
751 | |
752 | /* Under the new "aggressive" rule, we resolve mostly trivial | |
753 | * cases that we historically had git-merge-one-file resolve. | |
754 | */ | |
755 | if (o->aggressive) { | |
756 | int head_deleted = !head && !df_conflict_head; | |
757 | int remote_deleted = !remote && !df_conflict_remote; | |
0cf73755 | 758 | struct cache_entry *ce = NULL; |
4c4caafc JH |
759 | |
760 | if (index) | |
0cf73755 | 761 | ce = index; |
4c4caafc | 762 | else if (head) |
0cf73755 | 763 | ce = head; |
4c4caafc | 764 | else if (remote) |
0cf73755 | 765 | ce = remote; |
4c4caafc JH |
766 | else { |
767 | for (i = 1; i < o->head_idx; i++) { | |
768 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
0cf73755 | 769 | ce = stages[i]; |
4c4caafc JH |
770 | break; |
771 | } | |
772 | } | |
773 | } | |
774 | ||
076b0adc JS |
775 | /* |
776 | * Deleted in both. | |
777 | * Deleted in one and unchanged in the other. | |
778 | */ | |
779 | if ((head_deleted && remote_deleted) || | |
780 | (head_deleted && remote && remote_match) || | |
781 | (remote_deleted && head && head_match)) { | |
566b5c05 | 782 | remove_entry(remove); |
076b0adc JS |
783 | if (index) |
784 | return deleted_entry(index, index, o); | |
203a2fe1 DB |
785 | else if (ce && !head_deleted) { |
786 | if (verify_absent(ce, "removed", o)) | |
787 | return -1; | |
788 | } | |
076b0adc JS |
789 | return 0; |
790 | } | |
791 | /* | |
792 | * Added in both, identically. | |
793 | */ | |
794 | if (no_anc_exists && head && remote && same(head, remote)) | |
795 | return merged_entry(head, index, o); | |
796 | ||
797 | } | |
798 | ||
799 | /* Below are "no merge" cases, which require that the index be | |
800 | * up-to-date to avoid the files getting overwritten with | |
801 | * conflict resolution files. | |
802 | */ | |
803 | if (index) { | |
203a2fe1 DB |
804 | if (verify_uptodate(index, o)) |
805 | return -1; | |
076b0adc | 806 | } |
076b0adc | 807 | |
566b5c05 | 808 | remove_entry(remove); |
076b0adc JS |
809 | o->nontrivial_merge = 1; |
810 | ||
ea4b52a8 | 811 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
076b0adc JS |
812 | count = 0; |
813 | if (!head_match || !remote_match) { | |
814 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 815 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
7f7932ab | 816 | keep_entry(stages[i], o); |
076b0adc JS |
817 | count++; |
818 | break; | |
819 | } | |
820 | } | |
821 | } | |
822 | #if DBRT_DEBUG | |
823 | else { | |
824 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
825 | show_stage_entry(stderr, "head ", stages[head_match]); | |
826 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
827 | } | |
828 | #endif | |
7f7932ab JH |
829 | if (head) { count += keep_entry(head, o); } |
830 | if (remote) { count += keep_entry(remote, o); } | |
076b0adc JS |
831 | return count; |
832 | } | |
833 | ||
834 | /* | |
835 | * Two-way merge. | |
836 | * | |
837 | * The rule is to "carry forward" what is in the index without losing | |
838 | * information across a "fast forward", favoring a successful merge | |
839 | * over a merge failure when it makes sense. For details of the | |
840 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
841 | * | |
842 | */ | |
843 | int twoway_merge(struct cache_entry **src, | |
b48d5a05 LT |
844 | struct unpack_trees_options *o, |
845 | int remove) | |
076b0adc JS |
846 | { |
847 | struct cache_entry *current = src[0]; | |
b8ba1535 JH |
848 | struct cache_entry *oldtree = src[1]; |
849 | struct cache_entry *newtree = src[2]; | |
076b0adc JS |
850 | |
851 | if (o->merge_size != 2) | |
852 | return error("Cannot do a twoway merge of %d trees", | |
853 | o->merge_size); | |
854 | ||
b8ba1535 JH |
855 | if (oldtree == o->df_conflict_entry) |
856 | oldtree = NULL; | |
857 | if (newtree == o->df_conflict_entry) | |
858 | newtree = NULL; | |
859 | ||
076b0adc JS |
860 | if (current) { |
861 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
862 | (!oldtree && newtree && | |
863 | same(current, newtree)) || /* 6 and 7 */ | |
864 | (oldtree && newtree && | |
865 | same(oldtree, newtree)) || /* 14 and 15 */ | |
866 | (oldtree && newtree && | |
b8ba1535 | 867 | !same(oldtree, newtree) && /* 18 and 19 */ |
076b0adc | 868 | same(current, newtree))) { |
7f7932ab | 869 | return keep_entry(current, o); |
076b0adc JS |
870 | } |
871 | else if (oldtree && !newtree && same(current, oldtree)) { | |
872 | /* 10 or 11 */ | |
d699676d | 873 | remove_entry(remove); |
076b0adc JS |
874 | return deleted_entry(oldtree, current, o); |
875 | } | |
876 | else if (oldtree && newtree && | |
877 | same(current, oldtree) && !same(current, newtree)) { | |
878 | /* 20 or 21 */ | |
879 | return merged_entry(newtree, current, o); | |
880 | } | |
881 | else { | |
882 | /* all other failures */ | |
d699676d | 883 | remove_entry(remove); |
076b0adc | 884 | if (oldtree) |
17e46426 | 885 | return o->gently ? -1 : reject_merge(oldtree); |
076b0adc | 886 | if (current) |
17e46426 | 887 | return o->gently ? -1 : reject_merge(current); |
076b0adc | 888 | if (newtree) |
17e46426 | 889 | return o->gently ? -1 : reject_merge(newtree); |
076b0adc JS |
890 | return -1; |
891 | } | |
892 | } | |
893 | else if (newtree) | |
894 | return merged_entry(newtree, current, o); | |
d699676d LT |
895 | remove_entry(remove); |
896 | return deleted_entry(oldtree, current, o); | |
076b0adc JS |
897 | } |
898 | ||
899 | /* | |
900 | * Bind merge. | |
901 | * | |
902 | * Keep the index entries at stage0, collapse stage1 but make sure | |
903 | * stage0 does not have anything there. | |
904 | */ | |
905 | int bind_merge(struct cache_entry **src, | |
b48d5a05 LT |
906 | struct unpack_trees_options *o, |
907 | int remove) | |
076b0adc JS |
908 | { |
909 | struct cache_entry *old = src[0]; | |
910 | struct cache_entry *a = src[1]; | |
911 | ||
912 | if (o->merge_size != 1) | |
913 | return error("Cannot do a bind merge of %d trees\n", | |
914 | o->merge_size); | |
915 | if (a && old) | |
17e46426 DB |
916 | return o->gently ? -1 : |
917 | error("Entry '%s' overlaps. Cannot bind.", a->name); | |
076b0adc | 918 | if (!a) |
7f7932ab | 919 | return keep_entry(old, o); |
076b0adc JS |
920 | else |
921 | return merged_entry(a, NULL, o); | |
922 | } | |
923 | ||
924 | /* | |
925 | * One-way merge. | |
926 | * | |
927 | * The rule is: | |
928 | * - take the stat information from stage0, take the data from stage1 | |
929 | */ | |
930 | int oneway_merge(struct cache_entry **src, | |
b48d5a05 LT |
931 | struct unpack_trees_options *o, |
932 | int remove) | |
076b0adc JS |
933 | { |
934 | struct cache_entry *old = src[0]; | |
935 | struct cache_entry *a = src[1]; | |
936 | ||
937 | if (o->merge_size != 1) | |
938 | return error("Cannot do a oneway merge of %d trees", | |
939 | o->merge_size); | |
940 | ||
288f072e LT |
941 | if (!a) { |
942 | remove_entry(remove); | |
076b0adc | 943 | return deleted_entry(old, old, o); |
288f072e | 944 | } |
076b0adc JS |
945 | if (old && same(old, a)) { |
946 | if (o->reset) { | |
947 | struct stat st; | |
948 | if (lstat(old->name, &st) || | |
4bd5b7da | 949 | ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID)) |
7a51ed66 | 950 | old->ce_flags |= CE_UPDATE; |
076b0adc | 951 | } |
7f7932ab | 952 | return keep_entry(old, o); |
076b0adc JS |
953 | } |
954 | return merged_entry(a, old, o); | |
955 | } |