]> git.ipfire.org Git - thirdparty/git.git/blame - merge-ort.c
Fix various issues found in comments
[thirdparty/git.git] / merge-ort.c
CommitLineData
17e5574b
EN
1/*
2 * "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant
3 * as a drop-in replacement for the "recursive" merge strategy, allowing one
4 * to replace
5 *
6 * git merge [-s recursive]
7 *
8 * with
9 *
10 * git merge -s ort
11 *
12 * Note: git's parser allows the space between '-s' and its argument to be
13 * missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo",
14 * "cale", "peedy", or "ins" instead of "ort"?)
15 */
16
17#include "cache.h"
18#include "merge-ort.h"
19
4296d8f1 20#include "alloc.h"
ea305a68 21#include "attr.h"
67845745 22#include "blob.h"
ef2b3693 23#include "cache-tree.h"
4296d8f1 24#include "commit.h"
67845745 25#include "commit-reach.h"
e4171b1b
EN
26#include "diff.h"
27#include "diffcore.h"
6681ce5c 28#include "dir.h"
f591c472 29#include "ll-merge.h"
ee4012dc 30#include "object-store.h"
4204cd59 31#include "revision.h"
5b59c3db 32#include "strmap.h"
c73cda76 33#include "submodule.h"
231e2dd4 34#include "tree.h"
6681ce5c 35#include "unpack-trees.h"
c8017176 36#include "xdiff-interface.h"
5b59c3db 37
d2bc1994
EN
38/*
39 * We have many arrays of size 3. Whenever we have such an array, the
40 * indices refer to one of the sides of the three-way merge. This is so
41 * pervasive that the constants 0, 1, and 2 are used in many places in the
42 * code (especially in arithmetic operations to find the other side's index
43 * or to compute a relevant mask), but sometimes these enum names are used
44 * to aid code clarity.
45 *
46 * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
47 * referred to there is one of these three sides.
48 */
49enum merge_side {
50 MERGE_BASE = 0,
51 MERGE_SIDE1 = 1,
52 MERGE_SIDE2 = 2
53};
54
19ceb486
EN
55static unsigned RESULT_INITIALIZED = 0x1abe11ed; /* unlikely accidental value */
56
beb06145
EN
57struct traversal_callback_data {
58 unsigned long mask;
59 unsigned long dirmask;
60 struct name_entry names[3];
61};
62
864075ec 63struct rename_info {
c09376d5
EN
64 /*
65 * All variables that are arrays of size 3 correspond to data tracked
66 * for the sides in enum merge_side. Index 0 is almost always unused
67 * because we often only need to track information for MERGE_SIDE1 and
68 * MERGE_SIDE2 (MERGE_BASE can't have rename information since renames
69 * are determined relative to what changed since the MERGE_BASE).
70 */
71
864075ec
EN
72 /*
73 * pairs: pairing of filenames from diffcore_rename()
864075ec
EN
74 */
75 struct diff_queue_struct pairs[3];
76
c09376d5
EN
77 /*
78 * dirs_removed: directories removed on a given side of history.
fb52938e
EN
79 *
80 * The keys of dirs_removed[side] are the directories that were removed
81 * on the given side of history. The value of the strintmap for each
82 * directory is a value from enum dir_rename_relevance.
c09376d5 83 */
a49b55d5 84 struct strintmap dirs_removed[3];
c09376d5
EN
85
86 /*
87 * dir_rename_count: tracking where parts of a directory were renamed to
88 *
89 * When files in a directory are renamed, they may not all go to the
90 * same location. Each strmap here tracks:
91 * old_dir => {new_dir => int}
92 * That is, dir_rename_count[side] is a strmap to a strintmap.
93 */
94 struct strmap dir_rename_count[3];
95
96 /*
97 * dir_renames: computed directory renames
98 *
99 * This is a map of old_dir => new_dir and is derived in part from
100 * dir_rename_count.
101 */
102 struct strmap dir_renames[3];
103
32a56dfb 104 /*
ec59da60 105 * relevant_sources: deleted paths wanted in rename detection, and why
32a56dfb
EN
106 *
107 * relevant_sources is a set of deleted paths on each side of
108 * history for which we need rename detection. If a path is deleted
109 * on one side of history, we need to detect if it is part of a
110 * rename if either
32a56dfb 111 * * the file is modified/deleted on the other side of history
ec59da60 112 * * we need to detect renames for an ancestor directory
32a56dfb 113 * If neither of those are true, we can skip rename detection for
ec59da60
EN
114 * that path. The reason is stored as a value from enum
115 * file_rename_relevance, as the reason can inform the algorithm in
116 * diffcore_rename_extended().
32a56dfb 117 */
a49b55d5 118 struct strintmap relevant_sources[3];
32a56dfb 119
2fd9eda4
EN
120 /*
121 * dir_rename_mask:
122 * 0: optimization removing unmodified potential rename source okay
123 * 2 or 4: optimization okay, but must check for files added to dir
124 * 7: optimization forbidden; need rename source in case of dir rename
125 */
126 unsigned dir_rename_mask:3;
127
beb06145
EN
128 /*
129 * callback_data_*: supporting data structures for alternate traversal
130 *
131 * We sometimes need to be able to traverse through all the files
132 * in a given tree before all immediate subdirectories within that
133 * tree. Since traverse_trees() doesn't do that naturally, we have
134 * a traverse_trees_wrapper() that stores any immediate
135 * subdirectories while traversing files, then traverses the
136 * immediate subdirectories later. These callback_data* variables
137 * store the information for the subdirectories so that we can do
138 * that traversal order.
139 */
140 struct traversal_callback_data *callback_data;
141 int callback_data_nr, callback_data_alloc;
142 char *callback_data_traverse_path;
143
64aceb6d
EN
144 /*
145 * merge_trees: trees passed to the merge algorithm for the merge
146 *
147 * merge_trees records the trees passed to the merge algorithm. But,
148 * this data also is stored in merge_result->priv. If a sequence of
149 * merges are being done (such as when cherry-picking or rebasing),
150 * the next merge can look at this and re-use information from
151 * previous merges under certain circumstances.
152 *
153 * See also all the cached_* variables.
154 */
155 struct tree *merge_trees[3];
156
157 /*
158 * cached_pairs_valid_side: which side's cached info can be reused
159 *
160 * See the description for merge_trees. For repeated merges, at most
161 * only one side's cached information can be used. Valid values:
162 * MERGE_SIDE2: cached data from side2 can be reused
163 * MERGE_SIDE1: cached data from side1 can be reused
164 * 0: no cached data can be reused
165 */
166 int cached_pairs_valid_side;
167
d29bd6d7
EN
168 /*
169 * cached_pairs: Caching of renames and deletions.
170 *
171 * These are mappings recording renames and deletions of individual
172 * files (not directories). They are thus a map from an old
173 * filename to either NULL (for deletions) or a new filename (for
174 * renames).
175 */
176 struct strmap cached_pairs[3];
177
178 /*
179 * cached_target_names: just the destinations from cached_pairs
180 *
181 * We sometimes want a fast lookup to determine if a given filename
182 * is one of the destinations in cached_pairs. cached_target_names
183 * is thus duplicative information, but it provides a fast lookup.
184 */
185 struct strset cached_target_names[3];
186
187 /*
188 * cached_irrelevant: Caching of rename_sources that aren't relevant.
189 *
190 * If we try to detect a rename for a source path and succeed, it's
191 * part of a rename. If we try to detect a rename for a source path
192 * and fail, then it's a delete. If we do not try to detect a rename
193 * for a path, then we don't know if it's a rename or a delete. If
194 * merge-ort doesn't think the path is relevant, then we just won't
195 * cache anything for that path. But there's a slight problem in
196 * that merge-ort can think a path is RELEVANT_LOCATION, but due to
197 * commit 9bd342137e ("diffcore-rename: determine which
198 * relevant_sources are no longer relevant", 2021-03-13),
199 * diffcore-rename can downgrade the path to RELEVANT_NO_MORE. To
200 * avoid excessive calls to diffcore_rename_extended() we still need
201 * to cache such paths, though we cannot record them as either
202 * renames or deletes. So we cache them here as a "turned out to be
203 * irrelevant *for this commit*" as they are often also irrelevant
204 * for subsequent commits, though we will have to do some extra
205 * checking to see whether such paths become relevant for rename
206 * detection when cherry-picking/rebasing subsequent commits.
207 */
208 struct strset cached_irrelevant[3];
209
864075ec
EN
210 /*
211 * needed_limit: value needed for inexact rename detection to run
212 *
213 * If the current rename limit wasn't high enough for inexact
214 * rename detection to run, this records the limit needed. Otherwise,
215 * this value remains 0.
216 */
217 int needed_limit;
218};
219
5b59c3db
EN
220struct merge_options_internal {
221 /*
222 * paths: primary data structure in all of merge ort.
223 *
224 * The keys of paths:
225 * * are full relative paths from the toplevel of the repository
226 * (e.g. "drivers/firmware/raspberrypi.c").
227 * * store all relevant paths in the repo, both directories and
228 * files (e.g. drivers, drivers/firmware would also be included)
229 * * these keys serve to intern all the path strings, which allows
230 * us to do pointer comparison on directory names instead of
231 * strcmp; we just have to be careful to use the interned strings.
43c1dccb
EN
232 * (Technically paths_to_free may track some strings that were
233 * removed from froms paths.)
5b59c3db
EN
234 *
235 * The values of paths:
236 * * either a pointer to a merged_info, or a conflict_info struct
237 * * merged_info contains all relevant information for a
238 * non-conflicted entry.
239 * * conflict_info contains a merged_info, plus any additional
240 * information about a conflict such as the higher orders stages
241 * involved and the names of the paths those came from (handy
242 * once renames get involved).
243 * * a path may start "conflicted" (i.e. point to a conflict_info)
244 * and then a later step (e.g. three-way content merge) determines
245 * it can be cleanly merged, at which point it'll be marked clean
246 * and the algorithm will ignore any data outside the contained
247 * merged_info for that entry
248 * * If an entry remains conflicted, the merged_info portion of a
249 * conflict_info will later be filled with whatever version of
250 * the file should be placed in the working directory (e.g. an
251 * as-merged-as-possible variation that contains conflict markers).
252 */
253 struct strmap paths;
254
255 /*
256 * conflicted: a subset of keys->values from "paths"
257 *
258 * conflicted is basically an optimization between process_entries()
259 * and record_conflicted_index_entries(); the latter could loop over
260 * ALL the entries in paths AGAIN and look for the ones that are
261 * still conflicted, but since process_entries() has to loop over
262 * all of them, it saves the ones it couldn't resolve in this strmap
263 * so that record_conflicted_index_entries() can iterate just the
264 * relevant entries.
265 */
266 struct strmap conflicted;
267
43c1dccb
EN
268 /*
269 * paths_to_free: additional list of strings to free
270 *
271 * If keys are removed from "paths", they are added to paths_to_free
272 * to ensure they are later freed. We avoid free'ing immediately since
273 * other places (e.g. conflict_info.pathnames[]) may still be
274 * referencing these paths.
275 */
276 struct string_list paths_to_free;
277
c5a6f655
EN
278 /*
279 * output: special messages and conflict notices for various paths
280 *
281 * This is a map of pathnames (a subset of the keys in "paths" above)
282 * to strbufs. It gathers various warning/conflict/notice messages
283 * for later processing.
284 */
285 struct strmap output;
286
5b59c3db 287 /*
864075ec
EN
288 * renames: various data relating to rename detection
289 */
290 struct rename_info renames;
291
ea305a68
EN
292 /*
293 * attr_index: hacky minimal index used for renormalization
294 *
295 * renormalization code _requires_ an index, though it only needs to
296 * find a .gitattributes file within the index. So, when
297 * renormalization is important, we create a special index with just
298 * that one file.
299 */
300 struct index_state attr_index;
301
5b59c3db 302 /*
05b85c6e 303 * current_dir_name, toplevel_dir: temporary vars
5b59c3db 304 *
05b85c6e
EN
305 * These are used in collect_merge_info_callback(), and will set the
306 * various merged_info.directory_name for the various paths we get;
307 * see documentation for that variable and the requirements placed on
308 * that field.
5b59c3db
EN
309 */
310 const char *current_dir_name;
05b85c6e 311 const char *toplevel_dir;
5b59c3db
EN
312
313 /* call_depth: recursion level counter for merging merge bases */
314 int call_depth;
315};
316
317struct version_info {
318 struct object_id oid;
319 unsigned short mode;
320};
321
322struct merged_info {
323 /* if is_null, ignore result. otherwise result has oid & mode */
324 struct version_info result;
325 unsigned is_null:1;
326
327 /*
328 * clean: whether the path in question is cleanly merged.
329 *
330 * see conflict_info.merged for more details.
331 */
332 unsigned clean:1;
333
334 /*
335 * basename_offset: offset of basename of path.
336 *
337 * perf optimization to avoid recomputing offset of final '/'
338 * character in pathname (0 if no '/' in pathname).
339 */
340 size_t basename_offset;
341
342 /*
343 * directory_name: containing directory name.
344 *
345 * Note that we assume directory_name is constructed such that
346 * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
347 * i.e. string equality is equivalent to pointer equality. For this
348 * to hold, we have to be careful setting directory_name.
349 */
350 const char *directory_name;
351};
352
353struct conflict_info {
354 /*
355 * merged: the version of the path that will be written to working tree
356 *
357 * WARNING: It is critical to check merged.clean and ensure it is 0
358 * before reading any conflict_info fields outside of merged.
359 * Allocated merge_info structs will always have clean set to 1.
360 * Allocated conflict_info structs will have merged.clean set to 0
361 * initially. The merged.clean field is how we know if it is safe
362 * to access other parts of conflict_info besides merged; if a
363 * conflict_info's merged.clean is changed to 1, the rest of the
364 * algorithm is not allowed to look at anything outside of the
365 * merged member anymore.
366 */
367 struct merged_info merged;
368
369 /* oids & modes from each of the three trees for this path */
370 struct version_info stages[3];
371
372 /* pathnames for each stage; may differ due to rename detection */
373 const char *pathnames[3];
374
375 /* Whether this path is/was involved in a directory/file conflict */
376 unsigned df_conflict:1;
377
1c7873cd
EN
378 /*
379 * Whether this path is/was involved in a non-content conflict other
380 * than a directory/file conflict (e.g. rename/rename, rename/delete,
381 * file location based on possible directory rename).
382 */
383 unsigned path_conflict:1;
384
5b59c3db
EN
385 /*
386 * For filemask and dirmask, the ith bit corresponds to whether the
387 * ith entry is a file (filemask) or a directory (dirmask). Thus,
388 * filemask & dirmask is always zero, and filemask | dirmask is at
389 * most 7 but can be less when a path does not appear as either a
390 * file or a directory on at least one side of history.
391 *
392 * Note that these masks are related to enum merge_side, as the ith
393 * entry corresponds to side i.
394 *
395 * These values come from a traverse_trees() call; more info may be
396 * found looking at tree-walk.h's struct traverse_info,
397 * particularly the documentation above the "fn" member (note that
398 * filemask = mask & ~dirmask from that documentation).
399 */
400 unsigned filemask:3;
401 unsigned dirmask:3;
402
403 /*
404 * Optimization to track which stages match, to avoid the need to
405 * recompute it in multiple steps. Either 0 or at least 2 bits are
406 * set; if at least 2 bits are set, their corresponding stages match.
407 */
408 unsigned match_mask:3;
409};
410
04af1879
EN
411/*** Function Grouping: various utility functions ***/
412
98bf9841
EN
413/*
414 * For the next three macros, see warning for conflict_info.merged.
415 *
416 * In each of the below, mi is a struct merged_info*, and ci was defined
417 * as a struct conflict_info* (but we need to verify ci isn't actually
418 * pointed at a struct merged_info*).
419 *
420 * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
421 * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
422 * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
423 */
424#define INITIALIZE_CI(ci, mi) do { \
425 (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
426} while (0)
427#define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
428#define ASSIGN_AND_VERIFY_CI(ci, mi) do { \
429 (ci) = (struct conflict_info *)(mi); \
430 assert((ci) && !(mi)->clean); \
431} while (0)
432
89422d29
EN
433static void free_strmap_strings(struct strmap *map)
434{
435 struct hashmap_iter iter;
436 struct strmap_entry *entry;
437
438 strmap_for_each_entry(map, &iter, entry) {
439 free((char*)entry->key);
440 }
441}
442
43e9c4ee
EN
443static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
444 int reinitialize)
101bc5bc 445{
f5d9fbc2
EN
446 struct rename_info *renames = &opti->renames;
447 int i;
43e9c4ee
EN
448 void (*strmap_func)(struct strmap *, int) =
449 reinitialize ? strmap_partial_clear : strmap_clear;
a49b55d5
EN
450 void (*strintmap_func)(struct strintmap *) =
451 reinitialize ? strintmap_partial_clear : strintmap_clear;
d29bd6d7
EN
452 void (*strset_func)(struct strset *) =
453 reinitialize ? strset_partial_clear : strset_clear;
101bc5bc
EN
454
455 /*
456 * We marked opti->paths with strdup_strings = 0, so that we
457 * wouldn't have to make another copy of the fullpath created by
458 * make_traverse_path from setup_path_info(). But, now that we've
459 * used it and have no other references to these strings, it is time
460 * to deallocate them.
461 */
462 free_strmap_strings(&opti->paths);
43e9c4ee 463 strmap_func(&opti->paths, 1);
101bc5bc
EN
464
465 /*
466 * All keys and values in opti->conflicted are a subset of those in
467 * opti->paths. We don't want to deallocate anything twice, so we
468 * don't free the keys and we pass 0 for free_values.
469 */
43e9c4ee 470 strmap_func(&opti->conflicted, 0);
43c1dccb
EN
471
472 /*
473 * opti->paths_to_free is similar to opti->paths; we created it with
474 * strdup_strings = 0 to avoid making _another_ copy of the fullpath
475 * but now that we've used it and have no other references to these
476 * strings, it is time to deallocate them. We do so by temporarily
477 * setting strdup_strings to 1.
478 */
479 opti->paths_to_free.strdup_strings = 1;
480 string_list_clear(&opti->paths_to_free, 0);
481 opti->paths_to_free.strdup_strings = 0;
c5a6f655 482
1218b3ab 483 if (opti->attr_index.cache_nr) /* true iff opt->renormalize */
ea305a68
EN
484 discard_index(&opti->attr_index);
485
f5d9fbc2
EN
486 /* Free memory used by various renames maps */
487 for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) {
a49b55d5 488 strintmap_func(&renames->dirs_removed[i]);
f5d9fbc2 489 strmap_func(&renames->dir_renames[i], 0);
a49b55d5 490 strintmap_func(&renames->relevant_sources[i]);
d5098029
EN
491 if (!reinitialize)
492 assert(renames->cached_pairs_valid_side == 0);
493 if (i != renames->cached_pairs_valid_side) {
494 strset_func(&renames->cached_target_names[i]);
495 strmap_func(&renames->cached_pairs[i], 1);
496 strset_func(&renames->cached_irrelevant[i]);
497 partial_clear_dir_rename_count(&renames->dir_rename_count[i]);
498 if (!reinitialize)
499 strmap_clear(&renames->dir_rename_count[i], 1);
500 }
f5d9fbc2 501 }
64aceb6d
EN
502 renames->cached_pairs_valid_side = 0;
503 renames->dir_rename_mask = 0;
f5d9fbc2 504
c5a6f655
EN
505 if (!reinitialize) {
506 struct hashmap_iter iter;
507 struct strmap_entry *e;
508
509 /* Release and free each strbuf found in output */
510 strmap_for_each_entry(&opti->output, &iter, e) {
511 struct strbuf *sb = e->value;
512 strbuf_release(sb);
513 /*
514 * While strictly speaking we don't need to free(sb)
515 * here because we could pass free_values=1 when
516 * calling strmap_clear() on opti->output, that would
517 * require strmap_clear to do another
518 * strmap_for_each_entry() loop, so we just free it
519 * while we're iterating anyway.
520 */
521 free(sb);
522 }
523 strmap_clear(&opti->output, 0);
524 }
beb06145
EN
525
526 /* Clean out callback_data as well. */
527 FREE_AND_NULL(renames->callback_data);
528 renames->callback_data_nr = renames->callback_data_alloc = 0;
101bc5bc
EN
529}
530
0c0d705b
EN
531static int err(struct merge_options *opt, const char *err, ...)
532{
533 va_list params;
534 struct strbuf sb = STRBUF_INIT;
535
536 strbuf_addstr(&sb, "error: ");
537 va_start(params, err);
538 strbuf_vaddf(&sb, err, params);
539 va_end(params);
540
541 error("%s", sb.buf);
542 strbuf_release(&sb);
543
544 return -1;
545}
546
c73cda76
EN
547static void format_commit(struct strbuf *sb,
548 int indent,
549 struct commit *commit)
550{
70f19c7f
EN
551 struct merge_remote_desc *desc;
552 struct pretty_print_context ctx = {0};
553 ctx.abbrev = DEFAULT_ABBREV;
554
555 strbuf_addchars(sb, ' ', indent);
556 desc = merge_remote_util(commit);
557 if (desc) {
558 strbuf_addf(sb, "virtual %s\n", desc->name);
559 return;
560 }
561
562 format_commit_message(commit, "%h %s", sb, &ctx);
563 strbuf_addch(sb, '\n');
c73cda76
EN
564}
565
c5a6f655
EN
566__attribute__((format (printf, 4, 5)))
567static void path_msg(struct merge_options *opt,
568 const char *path,
569 int omittable_hint, /* skippable under --remerge-diff */
570 const char *fmt, ...)
571{
572 va_list ap;
573 struct strbuf *sb = strmap_get(&opt->priv->output, path);
574 if (!sb) {
575 sb = xmalloc(sizeof(*sb));
576 strbuf_init(sb, 0);
577 strmap_put(&opt->priv->output, path, sb);
578 }
579
580 va_start(ap, fmt);
581 strbuf_vaddf(sb, fmt, ap);
582 va_end(ap);
583
584 strbuf_addch(sb, '\n');
585}
586
5a1a1e8e
EN
587/* add a string to a strbuf, but converting "/" to "_" */
588static void add_flattened_path(struct strbuf *out, const char *s)
589{
590 size_t i = out->len;
591 strbuf_addstr(out, s);
592 for (; i < out->len; i++)
593 if (out->buf[i] == '/')
594 out->buf[i] = '_';
595}
596
23366d2a
EN
597static char *unique_path(struct strmap *existing_paths,
598 const char *path,
599 const char *branch)
600{
5a1a1e8e
EN
601 struct strbuf newpath = STRBUF_INIT;
602 int suffix = 0;
603 size_t base_len;
604
605 strbuf_addf(&newpath, "%s~", path);
606 add_flattened_path(&newpath, branch);
607
608 base_len = newpath.len;
609 while (strmap_contains(existing_paths, newpath.buf)) {
610 strbuf_setlen(&newpath, base_len);
611 strbuf_addf(&newpath, "_%d", suffix++);
612 }
613
614 return strbuf_detach(&newpath, NULL);
23366d2a
EN
615}
616
04af1879
EN
617/*** Function Grouping: functions related to collect_merge_info() ***/
618
a68e6cea
EN
619static int traverse_trees_wrapper_callback(int n,
620 unsigned long mask,
621 unsigned long dirmask,
622 struct name_entry *names,
623 struct traverse_info *info)
624{
625 struct merge_options *opt = info->data;
626 struct rename_info *renames = &opt->priv->renames;
2fd9eda4 627 unsigned filemask = mask & ~dirmask;
a68e6cea
EN
628
629 assert(n==3);
630
631 if (!renames->callback_data_traverse_path)
632 renames->callback_data_traverse_path = xstrdup(info->traverse_path);
633
2fd9eda4
EN
634 if (filemask && filemask == renames->dir_rename_mask)
635 renames->dir_rename_mask = 0x07;
636
a68e6cea
EN
637 ALLOC_GROW(renames->callback_data, renames->callback_data_nr + 1,
638 renames->callback_data_alloc);
639 renames->callback_data[renames->callback_data_nr].mask = mask;
640 renames->callback_data[renames->callback_data_nr].dirmask = dirmask;
641 COPY_ARRAY(renames->callback_data[renames->callback_data_nr].names,
642 names, 3);
643 renames->callback_data_nr++;
644
645 return mask;
646}
647
648/*
649 * Much like traverse_trees(), BUT:
650 * - read all the tree entries FIRST, saving them
651 * - note that the above step provides an opportunity to compute necessary
652 * additional details before the "real" traversal
653 * - loop through the saved entries and call the original callback on them
654 */
a68e6cea
EN
655static int traverse_trees_wrapper(struct index_state *istate,
656 int n,
657 struct tree_desc *t,
658 struct traverse_info *info)
659{
660 int ret, i, old_offset;
661 traverse_callback_t old_fn;
662 char *old_callback_data_traverse_path;
663 struct merge_options *opt = info->data;
664 struct rename_info *renames = &opt->priv->renames;
665
2fd9eda4
EN
666 assert(renames->dir_rename_mask == 2 || renames->dir_rename_mask == 4);
667
a68e6cea
EN
668 old_callback_data_traverse_path = renames->callback_data_traverse_path;
669 old_fn = info->fn;
670 old_offset = renames->callback_data_nr;
671
672 renames->callback_data_traverse_path = NULL;
673 info->fn = traverse_trees_wrapper_callback;
674 ret = traverse_trees(istate, n, t, info);
675 if (ret < 0)
676 return ret;
677
678 info->traverse_path = renames->callback_data_traverse_path;
679 info->fn = old_fn;
680 for (i = old_offset; i < renames->callback_data_nr; ++i) {
681 info->fn(n,
682 renames->callback_data[i].mask,
683 renames->callback_data[i].dirmask,
684 renames->callback_data[i].names,
685 info);
686 }
687
688 renames->callback_data_nr = old_offset;
689 free(renames->callback_data_traverse_path);
690 renames->callback_data_traverse_path = old_callback_data_traverse_path;
691 info->traverse_path = NULL;
692 return 0;
693}
694
98bf9841
EN
695static void setup_path_info(struct merge_options *opt,
696 struct string_list_item *result,
697 const char *current_dir_name,
698 int current_dir_name_len,
699 char *fullpath, /* we'll take over ownership */
700 struct name_entry *names,
701 struct name_entry *merged_version,
702 unsigned is_null, /* boolean */
703 unsigned df_conflict, /* boolean */
704 unsigned filemask,
705 unsigned dirmask,
706 int resolved /* boolean */)
707{
708 /* result->util is void*, so mi is a convenience typed variable */
709 struct merged_info *mi;
710
711 assert(!is_null || resolved);
712 assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
713 assert(resolved == (merged_version != NULL));
714
715 mi = xcalloc(1, resolved ? sizeof(struct merged_info) :
716 sizeof(struct conflict_info));
717 mi->directory_name = current_dir_name;
718 mi->basename_offset = current_dir_name_len;
719 mi->clean = !!resolved;
720 if (resolved) {
721 mi->result.mode = merged_version->mode;
722 oidcpy(&mi->result.oid, &merged_version->oid);
723 mi->is_null = !!is_null;
724 } else {
725 int i;
726 struct conflict_info *ci;
727
728 ASSIGN_AND_VERIFY_CI(ci, mi);
729 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
730 ci->pathnames[i] = fullpath;
731 ci->stages[i].mode = names[i].mode;
732 oidcpy(&ci->stages[i].oid, &names[i].oid);
733 }
734 ci->filemask = filemask;
735 ci->dirmask = dirmask;
736 ci->df_conflict = !!df_conflict;
737 if (dirmask)
738 /*
739 * Assume is_null for now, but if we have entries
740 * under the directory then when it is complete in
741 * write_completed_directory() it'll update this.
742 * Also, for D/F conflicts, we have to handle the
743 * directory first, then clear this bit and process
744 * the file to see how it is handled -- that occurs
745 * near the top of process_entry().
746 */
747 mi->is_null = 1;
748 }
749 strmap_put(&opt->priv->paths, fullpath, mi);
750 result->string = fullpath;
751 result->util = mi;
752}
753
f78cf976
EN
754static void add_pair(struct merge_options *opt,
755 struct name_entry *names,
756 const char *pathname,
757 unsigned side,
32a56dfb 758 unsigned is_add /* if false, is_delete */,
2fd9eda4
EN
759 unsigned match_mask,
760 unsigned dir_rename_mask)
f78cf976
EN
761{
762 struct diff_filespec *one, *two;
763 struct rename_info *renames = &opt->priv->renames;
764 int names_idx = is_add ? side : 0;
765
25e65b6d
EN
766 if (is_add) {
767 if (strset_contains(&renames->cached_target_names[side],
768 pathname))
769 return;
770 } else {
32a56dfb 771 unsigned content_relevant = (match_mask == 0);
2fd9eda4 772 unsigned location_relevant = (dir_rename_mask == 0x07);
32a56dfb 773
25e65b6d
EN
774 /*
775 * If pathname is found in cached_irrelevant[side] due to
776 * previous pick but for this commit content is relevant,
777 * then we need to remove it from cached_irrelevant.
778 */
779 if (content_relevant)
780 /* strset_remove is no-op if strset doesn't have key */
781 strset_remove(&renames->cached_irrelevant[side],
782 pathname);
783
784 /*
785 * We do not need to re-detect renames for paths that we already
786 * know the pairing, i.e. for cached_pairs (or
787 * cached_irrelevant). However, handle_deferred_entries() needs
788 * to loop over the union of keys from relevant_sources[side] and
789 * cached_pairs[side], so for simplicity we set relevant_sources
790 * for all the cached_pairs too and then strip them back out in
791 * prune_cached_from_relevant() at the beginning of
792 * detect_regular_renames().
793 */
ec59da60
EN
794 if (content_relevant || location_relevant) {
795 /* content_relevant trumps location_relevant */
796 strintmap_set(&renames->relevant_sources[side], pathname,
797 content_relevant ? RELEVANT_CONTENT : RELEVANT_LOCATION);
798 }
25e65b6d
EN
799
800 /*
801 * Avoid creating pair if we've already cached rename results.
802 * Note that we do this after setting relevant_sources[side]
803 * as noted in the comment above.
804 */
805 if (strmap_contains(&renames->cached_pairs[side], pathname) ||
806 strset_contains(&renames->cached_irrelevant[side], pathname))
807 return;
32a56dfb
EN
808 }
809
f78cf976
EN
810 one = alloc_filespec(pathname);
811 two = alloc_filespec(pathname);
812 fill_filespec(is_add ? two : one,
813 &names[names_idx].oid, 1, names[names_idx].mode);
814 diff_queue(&renames->pairs[side], one, two);
815}
816
eb3e3e1d
EN
817static void collect_rename_info(struct merge_options *opt,
818 struct name_entry *names,
819 const char *dirname,
820 const char *fullname,
821 unsigned filemask,
822 unsigned dirmask,
823 unsigned match_mask)
824{
825 struct rename_info *renames = &opt->priv->renames;
f78cf976 826 unsigned side;
eb3e3e1d 827
2fd9eda4
EN
828 /*
829 * Update dir_rename_mask (determines ignore-rename-source validity)
830 *
831 * dir_rename_mask helps us keep track of when directory rename
832 * detection may be relevant. Basically, whenver a directory is
833 * removed on one side of history, and a file is added to that
834 * directory on the other side of history, directory rename
835 * detection is relevant (meaning we have to detect renames for all
836 * files within that directory to deduce where the directory
837 * moved). Also, whenever a directory needs directory rename
838 * detection, due to the "majority rules" choice for where to move
839 * it (see t6423 testcase 1f), we also need to detect renames for
840 * all files within subdirectories of that directory as well.
841 *
842 * Here we haven't looked at files within the directory yet, we are
843 * just looking at the directory itself. So, if we aren't yet in
844 * a case where a parent directory needed directory rename detection
845 * (i.e. dir_rename_mask != 0x07), and if the directory was removed
846 * on one side of history, record the mask of the other side of
847 * history in dir_rename_mask.
848 */
849 if (renames->dir_rename_mask != 0x07 &&
850 (dirmask == 3 || dirmask == 5)) {
851 /* simple sanity check */
852 assert(renames->dir_rename_mask == 0 ||
853 renames->dir_rename_mask == (dirmask & ~1));
854 /* update dir_rename_mask; have it record mask of new side */
855 renames->dir_rename_mask = (dirmask & ~1);
856 }
857
eb3e3e1d
EN
858 /* Update dirs_removed, as needed */
859 if (dirmask == 1 || dirmask == 3 || dirmask == 5) {
860 /* absent_mask = 0x07 - dirmask; sides = absent_mask/2 */
861 unsigned sides = (0x07 - dirmask)/2;
fb52938e
EN
862 unsigned relevance = (renames->dir_rename_mask == 0x07) ?
863 RELEVANT_FOR_ANCESTOR : NOT_RELEVANT;
864 /*
865 * Record relevance of this directory. However, note that
866 * when collect_merge_info_callback() recurses into this
867 * directory and calls collect_rename_info() on paths
868 * within that directory, if we find a path that was added
869 * to this directory on the other side of history, we will
870 * upgrade this value to RELEVANT_FOR_SELF; see below.
871 */
eb3e3e1d 872 if (sides & 1)
fb52938e
EN
873 strintmap_set(&renames->dirs_removed[1], fullname,
874 relevance);
eb3e3e1d 875 if (sides & 2)
fb52938e
EN
876 strintmap_set(&renames->dirs_removed[2], fullname,
877 relevance);
878 }
879
880 /*
881 * Here's the block that potentially upgrades to RELEVANT_FOR_SELF.
882 * When we run across a file added to a directory. In such a case,
883 * find the directory of the file and upgrade its relevance.
884 */
885 if (renames->dir_rename_mask == 0x07 &&
886 (filemask == 2 || filemask == 4)) {
887 /*
888 * Need directory rename for parent directory on other side
889 * of history from added file. Thus
890 * side = (~filemask & 0x06) >> 1
891 * or
892 * side = 3 - (filemask/2).
893 */
894 unsigned side = 3 - (filemask >> 1);
895 strintmap_set(&renames->dirs_removed[side], dirname,
896 RELEVANT_FOR_SELF);
eb3e3e1d 897 }
f78cf976
EN
898
899 if (filemask == 0 || filemask == 7)
900 return;
901
902 for (side = MERGE_SIDE1; side <= MERGE_SIDE2; ++side) {
903 unsigned side_mask = (1 << side);
904
905 /* Check for deletion on side */
906 if ((filemask & 1) && !(filemask & side_mask))
32a56dfb 907 add_pair(opt, names, fullname, side, 0 /* delete */,
2fd9eda4
EN
908 match_mask & filemask,
909 renames->dir_rename_mask);
f78cf976
EN
910
911 /* Check for addition on side */
912 if (!(filemask & 1) && (filemask & side_mask))
32a56dfb 913 add_pair(opt, names, fullname, side, 1 /* add */,
2fd9eda4
EN
914 match_mask & filemask,
915 renames->dir_rename_mask);
f78cf976 916 }
eb3e3e1d
EN
917}
918
d2bc1994
EN
919static int collect_merge_info_callback(int n,
920 unsigned long mask,
921 unsigned long dirmask,
922 struct name_entry *names,
923 struct traverse_info *info)
924{
925 /*
926 * n is 3. Always.
927 * common ancestor (mbase) has mask 1, and stored in index 0 of names
928 * head of side 1 (side1) has mask 2, and stored in index 1 of names
929 * head of side 2 (side2) has mask 4, and stored in index 2 of names
930 */
931 struct merge_options *opt = info->data;
932 struct merge_options_internal *opti = opt->priv;
2fd9eda4 933 struct rename_info *renames = &opt->priv->renames;
98bf9841
EN
934 struct string_list_item pi; /* Path Info */
935 struct conflict_info *ci; /* typed alias to pi.util (which is void*) */
d2bc1994
EN
936 struct name_entry *p;
937 size_t len;
938 char *fullpath;
98bf9841 939 const char *dirname = opti->current_dir_name;
2fd9eda4 940 unsigned prev_dir_rename_mask = renames->dir_rename_mask;
d2bc1994 941 unsigned filemask = mask & ~dirmask;
34e557af 942 unsigned match_mask = 0; /* will be updated below */
d2bc1994
EN
943 unsigned mbase_null = !(mask & 1);
944 unsigned side1_null = !(mask & 2);
945 unsigned side2_null = !(mask & 4);
885f0063
EN
946 unsigned side1_matches_mbase = (!side1_null && !mbase_null &&
947 names[0].mode == names[1].mode &&
948 oideq(&names[0].oid, &names[1].oid));
949 unsigned side2_matches_mbase = (!side2_null && !mbase_null &&
950 names[0].mode == names[2].mode &&
951 oideq(&names[0].oid, &names[2].oid));
952 unsigned sides_match = (!side1_null && !side2_null &&
953 names[1].mode == names[2].mode &&
954 oideq(&names[1].oid, &names[2].oid));
d2bc1994 955
34e557af
EN
956 /*
957 * Note: When a path is a file on one side of history and a directory
958 * in another, we have a directory/file conflict. In such cases, if
959 * the conflict doesn't resolve from renames and deletions, then we
960 * always leave directories where they are and move files out of the
961 * way. Thus, while struct conflict_info has a df_conflict field to
962 * track such conflicts, we ignore that field for any directories at
963 * a path and only pay attention to it for files at the given path.
964 * The fact that we leave directories were they are also means that
965 * we do not need to worry about getting additional df_conflict
966 * information propagated from parent directories down to children
967 * (unlike, say traverse_trees_recursive() in unpack-trees.c, which
968 * sets a newinfo.df_conflicts field specifically to propagate it).
969 */
970 unsigned df_conflict = (filemask != 0) && (dirmask != 0);
971
d2bc1994
EN
972 /* n = 3 is a fundamental assumption. */
973 if (n != 3)
974 BUG("Called collect_merge_info_callback wrong");
975
976 /*
977 * A bunch of sanity checks verifying that traverse_trees() calls
978 * us the way I expect. Could just remove these at some point,
979 * though maybe they are helpful to future code readers.
980 */
981 assert(mbase_null == is_null_oid(&names[0].oid));
982 assert(side1_null == is_null_oid(&names[1].oid));
983 assert(side2_null == is_null_oid(&names[2].oid));
984 assert(!mbase_null || !side1_null || !side2_null);
985 assert(mask > 0 && mask < 8);
986
34e557af
EN
987 /* Determine match_mask */
988 if (side1_matches_mbase)
989 match_mask = (side2_matches_mbase ? 7 : 3);
990 else if (side2_matches_mbase)
991 match_mask = 5;
992 else if (sides_match)
993 match_mask = 6;
994
d2bc1994
EN
995 /*
996 * Get the name of the relevant filepath, which we'll pass to
997 * setup_path_info() for tracking.
998 */
999 p = names;
1000 while (!p->mode)
1001 p++;
1002 len = traverse_path_len(info, p->pathlen);
1003
1004 /* +1 in both of the following lines to include the NUL byte */
1005 fullpath = xmalloc(len + 1);
1006 make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen);
1007
291f29ca
EN
1008 /*
1009 * If mbase, side1, and side2 all match, we can resolve early. Even
1010 * if these are trees, there will be no renames or anything
1011 * underneath.
1012 */
1013 if (side1_matches_mbase && side2_matches_mbase) {
1014 /* mbase, side1, & side2 all match; use mbase as resolution */
1015 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
1016 names, names+0, mbase_null, 0,
1017 filemask, dirmask, 1);
1018 return mask;
1019 }
1020
eb3e3e1d
EN
1021 /*
1022 * Gather additional information used in rename detection.
1023 */
1024 collect_rename_info(opt, names, dirname, fullpath,
1025 filemask, dirmask, match_mask);
1026
d2bc1994 1027 /*
98bf9841
EN
1028 * Record information about the path so we can resolve later in
1029 * process_entries.
d2bc1994 1030 */
98bf9841
EN
1031 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
1032 names, NULL, 0, df_conflict, filemask, dirmask, 0);
1033
1034 ci = pi.util;
1035 VERIFY_CI(ci);
34e557af 1036 ci->match_mask = match_mask;
d2bc1994
EN
1037
1038 /* If dirmask, recurse into subdirectories */
1039 if (dirmask) {
1040 struct traverse_info newinfo;
1041 struct tree_desc t[3];
1042 void *buf[3] = {NULL, NULL, NULL};
1043 const char *original_dir_name;
1044 int i, ret;
1045
1046 ci->match_mask &= filemask;
1047 newinfo = *info;
1048 newinfo.prev = info;
1049 newinfo.name = p->path;
1050 newinfo.namelen = p->pathlen;
1051 newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1);
34e557af
EN
1052 /*
1053 * If this directory we are about to recurse into cared about
1054 * its parent directory (the current directory) having a D/F
1055 * conflict, then we'd propagate the masks in this way:
1056 * newinfo.df_conflicts |= (mask & ~dirmask);
1057 * But we don't worry about propagating D/F conflicts. (See
1058 * comment near setting of local df_conflict variable near
1059 * the beginning of this function).
1060 */
d2bc1994
EN
1061
1062 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
885f0063
EN
1063 if (i == 1 && side1_matches_mbase)
1064 t[1] = t[0];
1065 else if (i == 2 && side2_matches_mbase)
1066 t[2] = t[0];
1067 else if (i == 2 && sides_match)
1068 t[2] = t[1];
1069 else {
1070 const struct object_id *oid = NULL;
1071 if (dirmask & 1)
1072 oid = &names[i].oid;
1073 buf[i] = fill_tree_descriptor(opt->repo,
1074 t + i, oid);
1075 }
d2bc1994
EN
1076 dirmask >>= 1;
1077 }
1078
1079 original_dir_name = opti->current_dir_name;
98bf9841 1080 opti->current_dir_name = pi.string;
2fd9eda4
EN
1081 if (renames->dir_rename_mask == 0 ||
1082 renames->dir_rename_mask == 0x07)
1083 ret = traverse_trees(NULL, 3, t, &newinfo);
1084 else
1085 ret = traverse_trees_wrapper(NULL, 3, t, &newinfo);
d2bc1994 1086 opti->current_dir_name = original_dir_name;
2fd9eda4 1087 renames->dir_rename_mask = prev_dir_rename_mask;
d2bc1994
EN
1088
1089 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++)
1090 free(buf[i]);
1091
1092 if (ret < 0)
1093 return -1;
1094 }
1095
1096 return mask;
1097}
1098
231e2dd4
EN
1099static int collect_merge_info(struct merge_options *opt,
1100 struct tree *merge_base,
1101 struct tree *side1,
1102 struct tree *side2)
1103{
d2bc1994
EN
1104 int ret;
1105 struct tree_desc t[3];
1106 struct traverse_info info;
d2bc1994 1107
05b85c6e
EN
1108 opt->priv->toplevel_dir = "";
1109 opt->priv->current_dir_name = opt->priv->toplevel_dir;
1110 setup_traverse_info(&info, opt->priv->toplevel_dir);
d2bc1994
EN
1111 info.fn = collect_merge_info_callback;
1112 info.data = opt;
1113 info.show_all_errors = 1;
1114
1115 parse_tree(merge_base);
1116 parse_tree(side1);
1117 parse_tree(side2);
1118 init_tree_desc(t + 0, merge_base->buffer, merge_base->size);
1119 init_tree_desc(t + 1, side1->buffer, side1->size);
1120 init_tree_desc(t + 2, side2->buffer, side2->size);
1121
557ac035 1122 trace2_region_enter("merge", "traverse_trees", opt->repo);
d2bc1994 1123 ret = traverse_trees(NULL, 3, t, &info);
557ac035 1124 trace2_region_leave("merge", "traverse_trees", opt->repo);
d2bc1994
EN
1125
1126 return ret;
231e2dd4
EN
1127}
1128
04af1879
EN
1129/*** Function Grouping: functions related to threeway content merges ***/
1130
c73cda76
EN
1131static int find_first_merges(struct repository *repo,
1132 const char *path,
1133 struct commit *a,
1134 struct commit *b,
1135 struct object_array *result)
1136{
4204cd59
EN
1137 int i, j;
1138 struct object_array merges = OBJECT_ARRAY_INIT;
1139 struct commit *commit;
1140 int contains_another;
1141
1142 char merged_revision[GIT_MAX_HEXSZ + 2];
1143 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1144 "--all", merged_revision, NULL };
1145 struct rev_info revs;
1146 struct setup_revision_opt rev_opts;
1147
1148 memset(result, 0, sizeof(struct object_array));
1149 memset(&rev_opts, 0, sizeof(rev_opts));
1150
1151 /* get all revisions that merge commit a */
1152 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1153 oid_to_hex(&a->object.oid));
1154 repo_init_revisions(repo, &revs, NULL);
1155 rev_opts.submodule = path;
1156 /* FIXME: can't handle linked worktrees in submodules yet */
1157 revs.single_worktree = path != NULL;
1158 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1159
1160 /* save all revisions from the above list that contain b */
1161 if (prepare_revision_walk(&revs))
1162 die("revision walk setup failed");
1163 while ((commit = get_revision(&revs)) != NULL) {
1164 struct object *o = &(commit->object);
1165 if (in_merge_bases(b, commit))
1166 add_object_array(o, NULL, &merges);
1167 }
1168 reset_revision_walk();
1169
1170 /* Now we've got all merges that contain a and b. Prune all
1171 * merges that contain another found merge and save them in
1172 * result.
1173 */
1174 for (i = 0; i < merges.nr; i++) {
1175 struct commit *m1 = (struct commit *) merges.objects[i].item;
1176
1177 contains_another = 0;
1178 for (j = 0; j < merges.nr; j++) {
1179 struct commit *m2 = (struct commit *) merges.objects[j].item;
1180 if (i != j && in_merge_bases(m2, m1)) {
1181 contains_another = 1;
1182 break;
1183 }
1184 }
1185
1186 if (!contains_another)
1187 add_object_array(merges.objects[i].item, NULL, result);
1188 }
1189
1190 object_array_clear(&merges);
1191 return result->nr;
c73cda76
EN
1192}
1193
62fdec17
EN
1194static int merge_submodule(struct merge_options *opt,
1195 const char *path,
1196 const struct object_id *o,
1197 const struct object_id *a,
1198 const struct object_id *b,
1199 struct object_id *result)
1200{
c73cda76
EN
1201 struct commit *commit_o, *commit_a, *commit_b;
1202 int parent_count;
1203 struct object_array merges;
1204 struct strbuf sb = STRBUF_INIT;
1205
1206 int i;
1207 int search = !opt->priv->call_depth;
1208
1209 /* store fallback answer in result in case we fail */
1210 oidcpy(result, opt->priv->call_depth ? o : a);
1211
1212 /* we can not handle deletion conflicts */
1213 if (is_null_oid(o))
1214 return 0;
1215 if (is_null_oid(a))
1216 return 0;
1217 if (is_null_oid(b))
1218 return 0;
1219
1220 if (add_submodule_odb(path)) {
1221 path_msg(opt, path, 0,
1222 _("Failed to merge submodule %s (not checked out)"),
1223 path);
1224 return 0;
1225 }
1226
1227 if (!(commit_o = lookup_commit_reference(opt->repo, o)) ||
1228 !(commit_a = lookup_commit_reference(opt->repo, a)) ||
1229 !(commit_b = lookup_commit_reference(opt->repo, b))) {
1230 path_msg(opt, path, 0,
1231 _("Failed to merge submodule %s (commits not present)"),
1232 path);
1233 return 0;
1234 }
1235
1236 /* check whether both changes are forward */
1237 if (!in_merge_bases(commit_o, commit_a) ||
1238 !in_merge_bases(commit_o, commit_b)) {
1239 path_msg(opt, path, 0,
1240 _("Failed to merge submodule %s "
1241 "(commits don't follow merge-base)"),
1242 path);
1243 return 0;
1244 }
1245
1246 /* Case #1: a is contained in b or vice versa */
1247 if (in_merge_bases(commit_a, commit_b)) {
1248 oidcpy(result, b);
1249 path_msg(opt, path, 1,
1250 _("Note: Fast-forwarding submodule %s to %s"),
1251 path, oid_to_hex(b));
1252 return 1;
1253 }
1254 if (in_merge_bases(commit_b, commit_a)) {
1255 oidcpy(result, a);
1256 path_msg(opt, path, 1,
1257 _("Note: Fast-forwarding submodule %s to %s"),
1258 path, oid_to_hex(a));
1259 return 1;
1260 }
1261
1262 /*
1263 * Case #2: There are one or more merges that contain a and b in
1264 * the submodule. If there is only one, then present it as a
1265 * suggestion to the user, but leave it marked unmerged so the
1266 * user needs to confirm the resolution.
1267 */
1268
1269 /* Skip the search if makes no sense to the calling context. */
1270 if (!search)
1271 return 0;
1272
1273 /* find commit which merges them */
1274 parent_count = find_first_merges(opt->repo, path, commit_a, commit_b,
1275 &merges);
1276 switch (parent_count) {
1277 case 0:
1278 path_msg(opt, path, 0, _("Failed to merge submodule %s"), path);
1279 break;
1280
1281 case 1:
1282 format_commit(&sb, 4,
1283 (struct commit *)merges.objects[0].item);
1284 path_msg(opt, path, 0,
1285 _("Failed to merge submodule %s, but a possible merge "
1286 "resolution exists:\n%s\n"),
1287 path, sb.buf);
1288 path_msg(opt, path, 1,
1289 _("If this is correct simply add it to the index "
1290 "for example\n"
1291 "by using:\n\n"
1292 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1293 "which will accept this suggestion.\n"),
1294 oid_to_hex(&merges.objects[0].item->oid), path);
1295 strbuf_release(&sb);
1296 break;
1297 default:
1298 for (i = 0; i < merges.nr; i++)
1299 format_commit(&sb, 4,
1300 (struct commit *)merges.objects[i].item);
1301 path_msg(opt, path, 0,
1302 _("Failed to merge submodule %s, but multiple "
1303 "possible merges exist:\n%s"), path, sb.buf);
1304 strbuf_release(&sb);
1305 }
1306
1307 object_array_clear(&merges);
1308 return 0;
62fdec17
EN
1309}
1310
1218b3ab
EN
1311static void initialize_attr_index(struct merge_options *opt)
1312{
1313 /*
1314 * The renormalize_buffer() functions require attributes, and
1315 * annoyingly those can only be read from the working tree or from
1316 * an index_state. merge-ort doesn't have an index_state, so we
1317 * generate a fake one containing only attribute information.
1318 */
1319 struct merged_info *mi;
1320 struct index_state *attr_index = &opt->priv->attr_index;
1321 struct cache_entry *ce;
1322
1323 attr_index->initialized = 1;
1324
1325 if (!opt->renormalize)
1326 return;
1327
1328 mi = strmap_get(&opt->priv->paths, GITATTRIBUTES_FILE);
1329 if (!mi)
1330 return;
1331
1332 if (mi->clean) {
1333 int len = strlen(GITATTRIBUTES_FILE);
1334 ce = make_empty_cache_entry(attr_index, len);
1335 ce->ce_mode = create_ce_mode(mi->result.mode);
1336 ce->ce_flags = create_ce_flags(0);
1337 ce->ce_namelen = len;
1338 oidcpy(&ce->oid, &mi->result.oid);
1339 memcpy(ce->name, GITATTRIBUTES_FILE, len);
1340 add_index_entry(attr_index, ce,
1341 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
1342 get_stream_filter(attr_index, GITATTRIBUTES_FILE, &ce->oid);
1343 } else {
1344 int stage, len;
1345 struct conflict_info *ci;
1346
1347 ASSIGN_AND_VERIFY_CI(ci, mi);
1348 for (stage = 0; stage < 3; stage++) {
1349 unsigned stage_mask = (1 << stage);
1350
1351 if (!(ci->filemask & stage_mask))
1352 continue;
1353 len = strlen(GITATTRIBUTES_FILE);
1354 ce = make_empty_cache_entry(attr_index, len);
1355 ce->ce_mode = create_ce_mode(ci->stages[stage].mode);
1356 ce->ce_flags = create_ce_flags(stage);
1357 ce->ce_namelen = len;
1358 oidcpy(&ce->oid, &ci->stages[stage].oid);
1359 memcpy(ce->name, GITATTRIBUTES_FILE, len);
1360 add_index_entry(attr_index, ce,
1361 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
1362 get_stream_filter(attr_index, GITATTRIBUTES_FILE,
1363 &ce->oid);
1364 }
1365 }
1366}
1367
62fdec17
EN
1368static int merge_3way(struct merge_options *opt,
1369 const char *path,
1370 const struct object_id *o,
1371 const struct object_id *a,
1372 const struct object_id *b,
1373 const char *pathnames[3],
1374 const int extra_marker_size,
1375 mmbuffer_t *result_buf)
1376{
f591c472
EN
1377 mmfile_t orig, src1, src2;
1378 struct ll_merge_options ll_opts = {0};
1379 char *base, *name1, *name2;
1380 int merge_status;
1381
1218b3ab
EN
1382 if (!opt->priv->attr_index.initialized)
1383 initialize_attr_index(opt);
1384
f591c472
EN
1385 ll_opts.renormalize = opt->renormalize;
1386 ll_opts.extra_marker_size = extra_marker_size;
1387 ll_opts.xdl_opts = opt->xdl_opts;
1388
1389 if (opt->priv->call_depth) {
1390 ll_opts.virtual_ancestor = 1;
1391 ll_opts.variant = 0;
1392 } else {
1393 switch (opt->recursive_variant) {
1394 case MERGE_VARIANT_OURS:
1395 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
1396 break;
1397 case MERGE_VARIANT_THEIRS:
1398 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
1399 break;
1400 default:
1401 ll_opts.variant = 0;
1402 break;
1403 }
1404 }
1405
1406 assert(pathnames[0] && pathnames[1] && pathnames[2] && opt->ancestor);
1407 if (pathnames[0] == pathnames[1] && pathnames[1] == pathnames[2]) {
1408 base = mkpathdup("%s", opt->ancestor);
1409 name1 = mkpathdup("%s", opt->branch1);
1410 name2 = mkpathdup("%s", opt->branch2);
1411 } else {
1412 base = mkpathdup("%s:%s", opt->ancestor, pathnames[0]);
1413 name1 = mkpathdup("%s:%s", opt->branch1, pathnames[1]);
1414 name2 = mkpathdup("%s:%s", opt->branch2, pathnames[2]);
1415 }
1416
1417 read_mmblob(&orig, o);
1418 read_mmblob(&src1, a);
1419 read_mmblob(&src2, b);
1420
1421 merge_status = ll_merge(result_buf, path, &orig, base,
1422 &src1, name1, &src2, name2,
1218b3ab 1423 &opt->priv->attr_index, &ll_opts);
f591c472
EN
1424
1425 free(base);
1426 free(name1);
1427 free(name2);
1428 free(orig.ptr);
1429 free(src1.ptr);
1430 free(src2.ptr);
1431 return merge_status;
62fdec17
EN
1432}
1433
e2e9dc03
EN
1434static int handle_content_merge(struct merge_options *opt,
1435 const char *path,
1436 const struct version_info *o,
1437 const struct version_info *a,
1438 const struct version_info *b,
1439 const char *pathnames[3],
1440 const int extra_marker_size,
1441 struct version_info *result)
1442{
991bbdca 1443 /*
62fdec17
EN
1444 * path is the target location where we want to put the file, and
1445 * is used to determine any normalization rules in ll_merge.
1446 *
1447 * The normal case is that path and all entries in pathnames are
1448 * identical, though renames can affect which path we got one of
1449 * the three blobs to merge on various sides of history.
1450 *
1451 * extra_marker_size is the amount to extend conflict markers in
1452 * ll_merge; this is neeed if we have content merges of content
1453 * merges, which happens for example with rename/rename(2to1) and
1454 * rename/add conflicts.
1455 */
1456 unsigned clean = 1;
1457
1458 /*
1459 * handle_content_merge() needs both files to be of the same type, i.e.
1460 * both files OR both submodules OR both symlinks. Conflicting types
1461 * needs to be handled elsewhere.
991bbdca 1462 */
62fdec17
EN
1463 assert((S_IFMT & a->mode) == (S_IFMT & b->mode));
1464
1465 /* Merge modes */
1466 if (a->mode == b->mode || a->mode == o->mode)
1467 result->mode = b->mode;
1468 else {
1469 /* must be the 100644/100755 case */
1470 assert(S_ISREG(a->mode));
1471 result->mode = a->mode;
1472 clean = (b->mode == o->mode);
1473 /*
1474 * FIXME: If opt->priv->call_depth && !clean, then we really
1475 * should not make result->mode match either a->mode or
1476 * b->mode; that causes t6036 "check conflicting mode for
1477 * regular file" to fail. It would be best to use some other
1478 * mode, but we'll confuse all kinds of stuff if we use one
1479 * where S_ISREG(result->mode) isn't true, and if we use
1480 * something like 0100666, then tree-walk.c's calls to
1481 * canon_mode() will just normalize that to 100644 for us and
1482 * thus not solve anything.
1483 *
1484 * Figure out if there's some kind of way we can work around
1485 * this...
1486 */
1487 }
1488
1489 /*
1490 * Trivial oid merge.
1491 *
1492 * Note: While one might assume that the next four lines would
1493 * be unnecessary due to the fact that match_mask is often
1494 * setup and already handled, renames don't always take care
1495 * of that.
1496 */
1497 if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))
1498 oidcpy(&result->oid, &b->oid);
1499 else if (oideq(&b->oid, &o->oid))
1500 oidcpy(&result->oid, &a->oid);
1501
1502 /* Remaining rules depend on file vs. submodule vs. symlink. */
1503 else if (S_ISREG(a->mode)) {
1504 mmbuffer_t result_buf;
1505 int ret = 0, merge_status;
1506 int two_way;
1507
1508 /*
1509 * If 'o' is different type, treat it as null so we do a
1510 * two-way merge.
1511 */
1512 two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode));
1513
1514 merge_status = merge_3way(opt, path,
1515 two_way ? &null_oid : &o->oid,
1516 &a->oid, &b->oid,
1517 pathnames, extra_marker_size,
1518 &result_buf);
1519
1520 if ((merge_status < 0) || !result_buf.ptr)
1521 ret = err(opt, _("Failed to execute internal merge"));
1522
1523 if (!ret &&
1524 write_object_file(result_buf.ptr, result_buf.size,
1525 blob_type, &result->oid))
1526 ret = err(opt, _("Unable to add %s to database"),
1527 path);
1528
1529 free(result_buf.ptr);
1530 if (ret)
1531 return -1;
1532 clean &= (merge_status == 0);
1533 path_msg(opt, path, 1, _("Auto-merging %s"), path);
1534 } else if (S_ISGITLINK(a->mode)) {
1535 int two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode));
1536 clean = merge_submodule(opt, pathnames[0],
1537 two_way ? &null_oid : &o->oid,
1538 &a->oid, &b->oid, &result->oid);
1539 if (opt->priv->call_depth && two_way && !clean) {
1540 result->mode = o->mode;
1541 oidcpy(&result->oid, &o->oid);
1542 }
1543 } else if (S_ISLNK(a->mode)) {
1544 if (opt->priv->call_depth) {
1545 clean = 0;
1546 result->mode = o->mode;
1547 oidcpy(&result->oid, &o->oid);
1548 } else {
1549 switch (opt->recursive_variant) {
1550 case MERGE_VARIANT_NORMAL:
1551 clean = 0;
1552 oidcpy(&result->oid, &a->oid);
1553 break;
1554 case MERGE_VARIANT_OURS:
1555 oidcpy(&result->oid, &a->oid);
1556 break;
1557 case MERGE_VARIANT_THEIRS:
1558 oidcpy(&result->oid, &b->oid);
1559 break;
1560 }
1561 }
1562 } else
1563 BUG("unsupported object type in the tree: %06o for %s",
1564 a->mode, path);
1565
991bbdca 1566 return clean;
e2e9dc03
EN
1567}
1568
04af1879
EN
1569/*** Function Grouping: functions related to detect_and_process_renames(), ***
1570 *** which are split into directory and regular rename detection sections. ***/
1571
1572/*** Function Grouping: functions related to directory rename detection ***/
1573
fa5e06d6
EN
1574struct collision_info {
1575 struct string_list source_files;
1576 unsigned reported_already:1;
1577};
1578
d9d015df
EN
1579/*
1580 * Return a new string that replaces the beginning portion (which matches
1581 * rename_info->key), with rename_info->util.new_dir. In perl-speak:
1582 * new_path_name = (old_path =~ s/rename_info->key/rename_info->value/);
1583 * NOTE:
1584 * Caller must ensure that old_path starts with rename_info->key + '/'.
1585 */
1586static char *apply_dir_rename(struct strmap_entry *rename_info,
1587 const char *old_path)
1588{
fbcfc0cc
EN
1589 struct strbuf new_path = STRBUF_INIT;
1590 const char *old_dir = rename_info->key;
1591 const char *new_dir = rename_info->value;
1592 int oldlen, newlen, new_dir_len;
1593
1594 oldlen = strlen(old_dir);
1595 if (*new_dir == '\0')
1596 /*
1597 * If someone renamed/merged a subdirectory into the root
1598 * directory (e.g. 'some/subdir' -> ''), then we want to
1599 * avoid returning
1600 * '' + '/filename'
1601 * as the rename; we need to make old_path + oldlen advance
1602 * past the '/' character.
1603 */
1604 oldlen++;
1605 new_dir_len = strlen(new_dir);
1606 newlen = new_dir_len + (strlen(old_path) - oldlen) + 1;
1607 strbuf_grow(&new_path, newlen);
1608 strbuf_add(&new_path, new_dir, new_dir_len);
1609 strbuf_addstr(&new_path, &old_path[oldlen]);
1610
1611 return strbuf_detach(&new_path, NULL);
d9d015df
EN
1612}
1613
bea43365
EN
1614static int path_in_way(struct strmap *paths, const char *path, unsigned side_mask)
1615{
1616 struct merged_info *mi = strmap_get(paths, path);
1617 struct conflict_info *ci;
1618 if (!mi)
1619 return 0;
1620 INITIALIZE_CI(ci, mi);
1621 return mi->clean || (side_mask & (ci->filemask | ci->dirmask));
1622}
1623
47325e85
EN
1624/*
1625 * See if there is a directory rename for path, and if there are any file
1626 * level conflicts on the given side for the renamed location. If there is
1627 * a rename and there are no conflicts, return the new name. Otherwise,
1628 * return NULL.
1629 */
1630static char *handle_path_level_conflicts(struct merge_options *opt,
1631 const char *path,
1632 unsigned side_index,
1633 struct strmap_entry *rename_info,
1634 struct strmap *collisions)
1635{
bea43365
EN
1636 char *new_path = NULL;
1637 struct collision_info *c_info;
1638 int clean = 1;
1639 struct strbuf collision_paths = STRBUF_INIT;
1640
1641 /*
1642 * entry has the mapping of old directory name to new directory name
1643 * that we want to apply to path.
1644 */
1645 new_path = apply_dir_rename(rename_info, path);
1646 if (!new_path)
1647 BUG("Failed to apply directory rename!");
1648
1649 /*
1650 * The caller needs to have ensured that it has pre-populated
1651 * collisions with all paths that map to new_path. Do a quick check
1652 * to ensure that's the case.
1653 */
1654 c_info = strmap_get(collisions, new_path);
1655 if (c_info == NULL)
1656 BUG("c_info is NULL");
1657
1658 /*
1659 * Check for one-sided add/add/.../add conflicts, i.e.
1660 * where implicit renames from the other side doing
1661 * directory rename(s) can affect this side of history
1662 * to put multiple paths into the same location. Warn
1663 * and bail on directory renames for such paths.
1664 */
1665 if (c_info->reported_already) {
1666 clean = 0;
1667 } else if (path_in_way(&opt->priv->paths, new_path, 1 << side_index)) {
1668 c_info->reported_already = 1;
1669 strbuf_add_separated_string_list(&collision_paths, ", ",
1670 &c_info->source_files);
1671 path_msg(opt, new_path, 0,
1672 _("CONFLICT (implicit dir rename): Existing file/dir "
1673 "at %s in the way of implicit directory rename(s) "
1674 "putting the following path(s) there: %s."),
1675 new_path, collision_paths.buf);
1676 clean = 0;
1677 } else if (c_info->source_files.nr > 1) {
1678 c_info->reported_already = 1;
1679 strbuf_add_separated_string_list(&collision_paths, ", ",
1680 &c_info->source_files);
1681 path_msg(opt, new_path, 0,
1682 _("CONFLICT (implicit dir rename): Cannot map more "
1683 "than one path to %s; implicit directory renames "
1684 "tried to put these paths there: %s"),
1685 new_path, collision_paths.buf);
1686 clean = 0;
1687 }
1688
1689 /* Free memory we no longer need */
1690 strbuf_release(&collision_paths);
1691 if (!clean && new_path) {
1692 free(new_path);
1693 return NULL;
1694 }
1695
1696 return new_path;
47325e85
EN
1697}
1698
112e1112
EN
1699static void get_provisional_directory_renames(struct merge_options *opt,
1700 unsigned side,
1701 int *clean)
1702{
04264d40
EN
1703 struct hashmap_iter iter;
1704 struct strmap_entry *entry;
1705 struct rename_info *renames = &opt->priv->renames;
1706
04264d40
EN
1707 /*
1708 * Collapse
1709 * dir_rename_count: old_directory -> {new_directory -> count}
1710 * down to
1711 * dir_renames: old_directory -> best_new_directory
1712 * where best_new_directory is the one with the unique highest count.
1713 */
1714 strmap_for_each_entry(&renames->dir_rename_count[side], &iter, entry) {
1715 const char *source_dir = entry->key;
1716 struct strintmap *counts = entry->value;
1717 struct hashmap_iter count_iter;
1718 struct strmap_entry *count_entry;
1719 int max = 0;
1720 int bad_max = 0;
1721 const char *best = NULL;
1722
1723 strintmap_for_each_entry(counts, &count_iter, count_entry) {
1724 const char *target_dir = count_entry->key;
1725 intptr_t count = (intptr_t)count_entry->value;
1726
1727 if (count == max)
1728 bad_max = max;
1729 else if (count > max) {
1730 max = count;
1731 best = target_dir;
1732 }
1733 }
1734
bf238b71
EN
1735 if (max == 0)
1736 continue;
1737
04264d40
EN
1738 if (bad_max == max) {
1739 path_msg(opt, source_dir, 0,
1740 _("CONFLICT (directory rename split): "
1741 "Unclear where to rename %s to; it was "
1742 "renamed to multiple other directories, with "
1743 "no destination getting a majority of the "
1744 "files."),
1745 source_dir);
41376b58 1746 *clean = 0;
04264d40
EN
1747 } else {
1748 strmap_put(&renames->dir_renames[side],
1749 source_dir, (void*)best);
1750 }
1751 }
112e1112
EN
1752}
1753
1754static void handle_directory_level_conflicts(struct merge_options *opt)
1755{
98d0d081
EN
1756 struct hashmap_iter iter;
1757 struct strmap_entry *entry;
1758 struct string_list duplicated = STRING_LIST_INIT_NODUP;
1759 struct rename_info *renames = &opt->priv->renames;
1760 struct strmap *side1_dir_renames = &renames->dir_renames[MERGE_SIDE1];
1761 struct strmap *side2_dir_renames = &renames->dir_renames[MERGE_SIDE2];
1762 int i;
1763
1764 strmap_for_each_entry(side1_dir_renames, &iter, entry) {
1765 if (strmap_contains(side2_dir_renames, entry->key))
1766 string_list_append(&duplicated, entry->key);
1767 }
1768
1769 for (i = 0; i < duplicated.nr; i++) {
1770 strmap_remove(side1_dir_renames, duplicated.items[i].string, 0);
1771 strmap_remove(side2_dir_renames, duplicated.items[i].string, 0);
1772 }
1773 string_list_clear(&duplicated, 0);
112e1112
EN
1774}
1775
d9d015df
EN
1776static struct strmap_entry *check_dir_renamed(const char *path,
1777 struct strmap *dir_renames)
1778{
fbcfc0cc
EN
1779 char *temp = xstrdup(path);
1780 char *end;
1781 struct strmap_entry *e = NULL;
1782
1783 while ((end = strrchr(temp, '/'))) {
1784 *end = '\0';
1785 e = strmap_get_entry(dir_renames, temp);
1786 if (e)
1787 break;
1788 }
1789 free(temp);
1790 return e;
d9d015df
EN
1791}
1792
fa5e06d6
EN
1793static void compute_collisions(struct strmap *collisions,
1794 struct strmap *dir_renames,
1795 struct diff_queue_struct *pairs)
1796{
d9d015df
EN
1797 int i;
1798
1799 strmap_init_with_options(collisions, NULL, 0);
1800 if (strmap_empty(dir_renames))
1801 return;
1802
1803 /*
1804 * Multiple files can be mapped to the same path due to directory
1805 * renames done by the other side of history. Since that other
1806 * side of history could have merged multiple directories into one,
1807 * if our side of history added the same file basename to each of
1808 * those directories, then all N of them would get implicitly
1809 * renamed by the directory rename detection into the same path,
1810 * and we'd get an add/add/.../add conflict, and all those adds
1811 * from *this* side of history. This is not representable in the
1812 * index, and users aren't going to easily be able to make sense of
1813 * it. So we need to provide a good warning about what's
1814 * happening, and fall back to no-directory-rename detection
1815 * behavior for those paths.
1816 *
1817 * See testcases 9e and all of section 5 from t6043 for examples.
1818 */
1819 for (i = 0; i < pairs->nr; ++i) {
1820 struct strmap_entry *rename_info;
1821 struct collision_info *collision_info;
1822 char *new_path;
1823 struct diff_filepair *pair = pairs->queue[i];
1824
1825 if (pair->status != 'A' && pair->status != 'R')
1826 continue;
1827 rename_info = check_dir_renamed(pair->two->path, dir_renames);
1828 if (!rename_info)
1829 continue;
1830
1831 new_path = apply_dir_rename(rename_info, pair->two->path);
1832 assert(new_path);
1833 collision_info = strmap_get(collisions, new_path);
1834 if (collision_info) {
1835 free(new_path);
1836 } else {
1837 collision_info = xcalloc(1,
1838 sizeof(struct collision_info));
1839 string_list_init(&collision_info->source_files, 0);
1840 strmap_put(collisions, new_path, collision_info);
1841 }
1842 string_list_insert(&collision_info->source_files,
1843 pair->two->path);
1844 }
fa5e06d6
EN
1845}
1846
1847static char *check_for_directory_rename(struct merge_options *opt,
1848 const char *path,
1849 unsigned side_index,
1850 struct strmap *dir_renames,
1851 struct strmap *dir_rename_exclusions,
1852 struct strmap *collisions,
1853 int *clean_merge)
1854{
47325e85
EN
1855 char *new_path = NULL;
1856 struct strmap_entry *rename_info;
1857 struct strmap_entry *otherinfo = NULL;
1858 const char *new_dir;
1859
1860 if (strmap_empty(dir_renames))
1861 return new_path;
1862 rename_info = check_dir_renamed(path, dir_renames);
1863 if (!rename_info)
1864 return new_path;
1865 /* old_dir = rename_info->key; */
1866 new_dir = rename_info->value;
1867
1868 /*
1869 * This next part is a little weird. We do not want to do an
1870 * implicit rename into a directory we renamed on our side, because
1871 * that will result in a spurious rename/rename(1to2) conflict. An
1872 * example:
1873 * Base commit: dumbdir/afile, otherdir/bfile
1874 * Side 1: smrtdir/afile, otherdir/bfile
1875 * Side 2: dumbdir/afile, dumbdir/bfile
1876 * Here, while working on Side 1, we could notice that otherdir was
1877 * renamed/merged to dumbdir, and change the diff_filepair for
1878 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
1879 * 2 will notice the rename from dumbdir to smrtdir, and do the
1880 * transitive rename to move it from dumbdir/bfile to
1881 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
1882 * smrtdir, a rename/rename(1to2) conflict. We really just want
1883 * the file to end up in smrtdir. And the way to achieve that is
1884 * to not let Side1 do the rename to dumbdir, since we know that is
1885 * the source of one of our directory renames.
1886 *
1887 * That's why otherinfo and dir_rename_exclusions is here.
1888 *
1889 * As it turns out, this also prevents N-way transient rename
1890 * confusion; See testcases 9c and 9d of t6043.
1891 */
1892 otherinfo = strmap_get_entry(dir_rename_exclusions, new_dir);
1893 if (otherinfo) {
1894 path_msg(opt, rename_info->key, 1,
1895 _("WARNING: Avoiding applying %s -> %s rename "
1896 "to %s, because %s itself was renamed."),
1897 rename_info->key, new_dir, path, new_dir);
1898 return NULL;
1899 }
1900
1901 new_path = handle_path_level_conflicts(opt, path, side_index,
1902 rename_info, collisions);
1903 *clean_merge &= (new_path != NULL);
1904
1905 return new_path;
fa5e06d6
EN
1906}
1907
1908static void apply_directory_rename_modifications(struct merge_options *opt,
1909 struct diff_filepair *pair,
1910 char *new_path)
1911{
089d82bc
EN
1912 /*
1913 * The basic idea is to get the conflict_info from opt->priv->paths
1914 * at old path, and insert it into new_path; basically just this:
1915 * ci = strmap_get(&opt->priv->paths, old_path);
1916 * strmap_remove(&opt->priv->paths, old_path, 0);
1917 * strmap_put(&opt->priv->paths, new_path, ci);
1918 * However, there are some factors complicating this:
1919 * - opt->priv->paths may already have an entry at new_path
1920 * - Each ci tracks its containing directory, so we need to
1921 * update that
1922 * - If another ci has the same containing directory, then
1923 * the two char*'s MUST point to the same location. See the
1924 * comment in struct merged_info. strcmp equality is not
1925 * enough; we need pointer equality.
1926 * - opt->priv->paths must hold the parent directories of any
1927 * entries that are added. So, if this directory rename
1928 * causes entirely new directories, we must recursively add
1929 * parent directories.
1930 * - For each parent directory added to opt->priv->paths, we
1931 * also need to get its parent directory stored in its
1932 * conflict_info->merged.directory_name with all the same
1933 * requirements about pointer equality.
1934 */
1935 struct string_list dirs_to_insert = STRING_LIST_INIT_NODUP;
1936 struct conflict_info *ci, *new_ci;
1937 struct strmap_entry *entry;
1938 const char *branch_with_new_path, *branch_with_dir_rename;
1939 const char *old_path = pair->two->path;
1940 const char *parent_name;
1941 const char *cur_path;
1942 int i, len;
1943
1944 entry = strmap_get_entry(&opt->priv->paths, old_path);
1945 old_path = entry->key;
1946 ci = entry->value;
1947 VERIFY_CI(ci);
1948
1949 /* Find parent directories missing from opt->priv->paths */
1950 cur_path = new_path;
1951 while (1) {
1952 /* Find the parent directory of cur_path */
1953 char *last_slash = strrchr(cur_path, '/');
1954 if (last_slash) {
1955 parent_name = xstrndup(cur_path, last_slash - cur_path);
1956 } else {
1957 parent_name = opt->priv->toplevel_dir;
1958 break;
1959 }
1960
1961 /* Look it up in opt->priv->paths */
1962 entry = strmap_get_entry(&opt->priv->paths, parent_name);
1963 if (entry) {
1964 free((char*)parent_name);
1965 parent_name = entry->key; /* reuse known pointer */
1966 break;
1967 }
1968
1969 /* Record this is one of the directories we need to insert */
1970 string_list_append(&dirs_to_insert, parent_name);
1971 cur_path = parent_name;
1972 }
1973
1974 /* Traverse dirs_to_insert and insert them into opt->priv->paths */
1975 for (i = dirs_to_insert.nr-1; i >= 0; --i) {
1976 struct conflict_info *dir_ci;
1977 char *cur_dir = dirs_to_insert.items[i].string;
1978
1979 dir_ci = xcalloc(1, sizeof(*dir_ci));
1980
1981 dir_ci->merged.directory_name = parent_name;
1982 len = strlen(parent_name);
1983 /* len+1 because of trailing '/' character */
1984 dir_ci->merged.basename_offset = (len > 0 ? len+1 : len);
1985 dir_ci->dirmask = ci->filemask;
1986 strmap_put(&opt->priv->paths, cur_dir, dir_ci);
1987
1988 parent_name = cur_dir;
1989 }
1990
1991 /*
1992 * We are removing old_path from opt->priv->paths. old_path also will
1993 * eventually need to be freed, but it may still be used by e.g.
1994 * ci->pathnames. So, store it in another string-list for now.
1995 */
1996 string_list_append(&opt->priv->paths_to_free, old_path);
1997
1998 assert(ci->filemask == 2 || ci->filemask == 4);
1999 assert(ci->dirmask == 0);
2000 strmap_remove(&opt->priv->paths, old_path, 0);
2001
2002 branch_with_new_path = (ci->filemask == 2) ? opt->branch1 : opt->branch2;
2003 branch_with_dir_rename = (ci->filemask == 2) ? opt->branch2 : opt->branch1;
2004
2005 /* Now, finally update ci and stick it into opt->priv->paths */
2006 ci->merged.directory_name = parent_name;
2007 len = strlen(parent_name);
2008 ci->merged.basename_offset = (len > 0 ? len+1 : len);
2009 new_ci = strmap_get(&opt->priv->paths, new_path);
2010 if (!new_ci) {
2011 /* Place ci back into opt->priv->paths, but at new_path */
2012 strmap_put(&opt->priv->paths, new_path, ci);
2013 } else {
2014 int index;
2015
2016 /* A few sanity checks */
2017 VERIFY_CI(new_ci);
2018 assert(ci->filemask == 2 || ci->filemask == 4);
2019 assert((new_ci->filemask & ci->filemask) == 0);
2020 assert(!new_ci->merged.clean);
2021
2022 /* Copy stuff from ci into new_ci */
2023 new_ci->filemask |= ci->filemask;
2024 if (new_ci->dirmask)
2025 new_ci->df_conflict = 1;
2026 index = (ci->filemask >> 1);
2027 new_ci->pathnames[index] = ci->pathnames[index];
2028 new_ci->stages[index].mode = ci->stages[index].mode;
2029 oidcpy(&new_ci->stages[index].oid, &ci->stages[index].oid);
2030
2031 free(ci);
2032 ci = new_ci;
2033 }
2034
2035 if (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) {
2036 /* Notify user of updated path */
2037 if (pair->status == 'A')
2038 path_msg(opt, new_path, 1,
2039 _("Path updated: %s added in %s inside a "
2040 "directory that was renamed in %s; moving "
2041 "it to %s."),
2042 old_path, branch_with_new_path,
2043 branch_with_dir_rename, new_path);
2044 else
2045 path_msg(opt, new_path, 1,
2046 _("Path updated: %s renamed to %s in %s, "
2047 "inside a directory that was renamed in %s; "
2048 "moving it to %s."),
2049 pair->one->path, old_path, branch_with_new_path,
2050 branch_with_dir_rename, new_path);
2051 } else {
2052 /*
2053 * opt->detect_directory_renames has the value
2054 * MERGE_DIRECTORY_RENAMES_CONFLICT, so mark these as conflicts.
2055 */
2056 ci->path_conflict = 1;
2057 if (pair->status == 'A')
2058 path_msg(opt, new_path, 0,
2059 _("CONFLICT (file location): %s added in %s "
2060 "inside a directory that was renamed in %s, "
2061 "suggesting it should perhaps be moved to "
2062 "%s."),
2063 old_path, branch_with_new_path,
2064 branch_with_dir_rename, new_path);
2065 else
2066 path_msg(opt, new_path, 0,
2067 _("CONFLICT (file location): %s renamed to %s "
2068 "in %s, inside a directory that was renamed "
2069 "in %s, suggesting it should perhaps be "
2070 "moved to %s."),
2071 pair->one->path, old_path, branch_with_new_path,
2072 branch_with_dir_rename, new_path);
2073 }
2074
2075 /*
2076 * Finally, record the new location.
2077 */
2078 pair->two->path = new_path;
fa5e06d6
EN
2079}
2080
04af1879
EN
2081/*** Function Grouping: functions related to regular rename detection ***/
2082
e1a124e8
EN
2083static int process_renames(struct merge_options *opt,
2084 struct diff_queue_struct *renames)
2085{
c2d267df
EN
2086 int clean_merge = 1, i;
2087
2088 for (i = 0; i < renames->nr; ++i) {
2089 const char *oldpath = NULL, *newpath;
2090 struct diff_filepair *pair = renames->queue[i];
2091 struct conflict_info *oldinfo = NULL, *newinfo = NULL;
2092 struct strmap_entry *old_ent, *new_ent;
2093 unsigned int old_sidemask;
2094 int target_index, other_source_index;
2095 int source_deleted, collision, type_changed;
2e91ddd2 2096 const char *rename_branch = NULL, *delete_branch = NULL;
c2d267df
EN
2097
2098 old_ent = strmap_get_entry(&opt->priv->paths, pair->one->path);
c2d267df 2099 new_ent = strmap_get_entry(&opt->priv->paths, pair->two->path);
1b6b902d
EN
2100 if (old_ent) {
2101 oldpath = old_ent->key;
2102 oldinfo = old_ent->value;
2103 }
2104 newpath = pair->two->path;
2105 if (new_ent) {
2106 newpath = new_ent->key;
2107 newinfo = new_ent->value;
2108 }
2109
2110 /*
2111 * If pair->one->path isn't in opt->priv->paths, that means
2112 * that either directory rename detection removed that
2113 * path, or a parent directory of oldpath was resolved and
2114 * we don't even need the rename; in either case, we can
2115 * skip it. If oldinfo->merged.clean, then the other side
2116 * of history had no changes to oldpath and we don't need
2117 * the rename and can skip it.
2118 */
2119 if (!oldinfo || oldinfo->merged.clean)
2120 continue;
c2d267df
EN
2121
2122 /*
2123 * diff_filepairs have copies of pathnames, thus we have to
2124 * use standard 'strcmp()' (negated) instead of '=='.
2125 */
2126 if (i + 1 < renames->nr &&
2127 !strcmp(oldpath, renames->queue[i+1]->one->path)) {
2128 /* Handle rename/rename(1to2) or rename/rename(1to1) */
2129 const char *pathnames[3];
af1e56c4
EN
2130 struct version_info merged;
2131 struct conflict_info *base, *side1, *side2;
53e88a03 2132 unsigned was_binary_blob = 0;
c2d267df
EN
2133
2134 pathnames[0] = oldpath;
2135 pathnames[1] = newpath;
2136 pathnames[2] = renames->queue[i+1]->two->path;
2137
af1e56c4
EN
2138 base = strmap_get(&opt->priv->paths, pathnames[0]);
2139 side1 = strmap_get(&opt->priv->paths, pathnames[1]);
2140 side2 = strmap_get(&opt->priv->paths, pathnames[2]);
2141
2142 VERIFY_CI(base);
2143 VERIFY_CI(side1);
2144 VERIFY_CI(side2);
2145
c2d267df 2146 if (!strcmp(pathnames[1], pathnames[2])) {
cbdca289
EN
2147 struct rename_info *ri = &opt->priv->renames;
2148 int j;
2149
af1e56c4
EN
2150 /* Both sides renamed the same way */
2151 assert(side1 == side2);
2152 memcpy(&side1->stages[0], &base->stages[0],
2153 sizeof(merged));
2154 side1->filemask |= (1 << MERGE_BASE);
2155 /* Mark base as resolved by removal */
2156 base->merged.is_null = 1;
2157 base->merged.clean = 1;
c2d267df 2158
cbdca289
EN
2159 /*
2160 * Disable remembering renames optimization;
2161 * rename/rename(1to1) is incredibly rare, and
2162 * just disabling the optimization is easier
2163 * than purging cached_pairs,
2164 * cached_target_names, and dir_rename_counts.
2165 */
2166 for (j = 0; j < 3; j++)
2167 ri->merge_trees[j] = NULL;
2168
c2d267df
EN
2169 /* We handled both renames, i.e. i+1 handled */
2170 i++;
2171 /* Move to next rename */
2172 continue;
2173 }
2174
2175 /* This is a rename/rename(1to2) */
53e88a03
EN
2176 clean_merge = handle_content_merge(opt,
2177 pair->one->path,
2178 &base->stages[0],
2179 &side1->stages[1],
2180 &side2->stages[2],
2181 pathnames,
2182 1 + 2 * opt->priv->call_depth,
2183 &merged);
2184 if (!clean_merge &&
2185 merged.mode == side1->stages[1].mode &&
2186 oideq(&merged.oid, &side1->stages[1].oid))
2187 was_binary_blob = 1;
2188 memcpy(&side1->stages[1], &merged, sizeof(merged));
2189 if (was_binary_blob) {
2190 /*
2191 * Getting here means we were attempting to
2192 * merge a binary blob.
2193 *
2194 * Since we can't merge binaries,
2195 * handle_content_merge() just takes one
2196 * side. But we don't want to copy the
2197 * contents of one side to both paths. We
2198 * used the contents of side1 above for
2199 * side1->stages, let's use the contents of
2200 * side2 for side2->stages below.
2201 */
2202 oidcpy(&merged.oid, &side2->stages[2].oid);
2203 merged.mode = side2->stages[2].mode;
2204 }
2205 memcpy(&side2->stages[2], &merged, sizeof(merged));
2206
2207 side1->path_conflict = 1;
2208 side2->path_conflict = 1;
2209 /*
2210 * TODO: For renames we normally remove the path at the
2211 * old name. It would thus seem consistent to do the
2212 * same for rename/rename(1to2) cases, but we haven't
2213 * done so traditionally and a number of the regression
2214 * tests now encode an expectation that the file is
2215 * left there at stage 1. If we ever decide to change
2216 * this, add the following two lines here:
2217 * base->merged.is_null = 1;
2218 * base->merged.clean = 1;
2219 * and remove the setting of base->path_conflict to 1.
2220 */
2221 base->path_conflict = 1;
2222 path_msg(opt, oldpath, 0,
2223 _("CONFLICT (rename/rename): %s renamed to "
2224 "%s in %s and to %s in %s."),
2225 pathnames[0],
2226 pathnames[1], opt->branch1,
2227 pathnames[2], opt->branch2);
c2d267df
EN
2228
2229 i++; /* We handled both renames, i.e. i+1 handled */
2230 continue;
2231 }
2232
2233 VERIFY_CI(oldinfo);
2234 VERIFY_CI(newinfo);
2235 target_index = pair->score; /* from collect_renames() */
2236 assert(target_index == 1 || target_index == 2);
2237 other_source_index = 3 - target_index;
2238 old_sidemask = (1 << other_source_index); /* 2 or 4 */
2239 source_deleted = (oldinfo->filemask == 1);
2240 collision = ((newinfo->filemask & old_sidemask) != 0);
2241 type_changed = !source_deleted &&
2242 (S_ISREG(oldinfo->stages[other_source_index].mode) !=
2243 S_ISREG(newinfo->stages[target_index].mode));
2244 if (type_changed && collision) {
6fcccbd7
EN
2245 /*
2246 * special handling so later blocks can handle this...
2247 *
2248 * if type_changed && collision are both true, then this
2249 * was really a double rename, but one side wasn't
2250 * detected due to lack of break detection. I.e.
2251 * something like
2252 * orig: has normal file 'foo'
2253 * side1: renames 'foo' to 'bar', adds 'foo' symlink
2254 * side2: renames 'foo' to 'bar'
2255 * In this case, the foo->bar rename on side1 won't be
2256 * detected because the new symlink named 'foo' is
2257 * there and we don't do break detection. But we detect
2258 * this here because we don't want to merge the content
2259 * of the foo symlink with the foo->bar file, so we
2260 * have some logic to handle this special case. The
2261 * easiest way to do that is make 'bar' on side1 not
2262 * be considered a colliding file but the other part
2263 * of a normal rename. If the file is very different,
2264 * well we're going to get content merge conflicts
2265 * anyway so it doesn't hurt. And if the colliding
2266 * file also has a different type, that'll be handled
2267 * by the content merge logic in process_entry() too.
2268 *
2269 * See also t6430, 'rename vs. rename/symlink'
2270 */
2271 collision = 0;
c2d267df 2272 }
2e91ddd2
EN
2273 if (source_deleted) {
2274 if (target_index == 1) {
2275 rename_branch = opt->branch1;
2276 delete_branch = opt->branch2;
2277 } else {
2278 rename_branch = opt->branch2;
2279 delete_branch = opt->branch1;
2280 }
2281 }
c2d267df
EN
2282
2283 assert(source_deleted || oldinfo->filemask & old_sidemask);
2284
2285 /* Need to check for special types of rename conflicts... */
2286 if (collision && !source_deleted) {
2287 /* collision: rename/add or rename/rename(2to1) */
35e47e35
EN
2288 const char *pathnames[3];
2289 struct version_info merged;
2290
2291 struct conflict_info *base, *side1, *side2;
2292 unsigned clean;
2293
2294 pathnames[0] = oldpath;
2295 pathnames[other_source_index] = oldpath;
2296 pathnames[target_index] = newpath;
2297
2298 base = strmap_get(&opt->priv->paths, pathnames[0]);
2299 side1 = strmap_get(&opt->priv->paths, pathnames[1]);
2300 side2 = strmap_get(&opt->priv->paths, pathnames[2]);
2301
2302 VERIFY_CI(base);
2303 VERIFY_CI(side1);
2304 VERIFY_CI(side2);
2305
2306 clean = handle_content_merge(opt, pair->one->path,
2307 &base->stages[0],
2308 &side1->stages[1],
2309 &side2->stages[2],
2310 pathnames,
2311 1 + 2 * opt->priv->call_depth,
2312 &merged);
2313
2314 memcpy(&newinfo->stages[target_index], &merged,
2315 sizeof(merged));
2316 if (!clean) {
2317 path_msg(opt, newpath, 0,
2318 _("CONFLICT (rename involved in "
2319 "collision): rename of %s -> %s has "
2320 "content conflicts AND collides "
2321 "with another path; this may result "
2322 "in nested conflict markers."),
2323 oldpath, newpath);
2324 }
c2d267df 2325 } else if (collision && source_deleted) {
35e47e35
EN
2326 /*
2327 * rename/add/delete or rename/rename(2to1)/delete:
2328 * since oldpath was deleted on the side that didn't
2329 * do the rename, there's not much of a content merge
2330 * we can do for the rename. oldinfo->merged.is_null
2331 * was already set, so we just leave things as-is so
2332 * they look like an add/add conflict.
2333 */
2334
2335 newinfo->path_conflict = 1;
2336 path_msg(opt, newpath, 0,
2337 _("CONFLICT (rename/delete): %s renamed "
2338 "to %s in %s, but deleted in %s."),
2339 oldpath, newpath, rename_branch, delete_branch);
c2d267df 2340 } else {
2e91ddd2
EN
2341 /*
2342 * a few different cases...start by copying the
2343 * existing stage(s) from oldinfo over the newinfo
2344 * and update the pathname(s).
2345 */
2346 memcpy(&newinfo->stages[0], &oldinfo->stages[0],
2347 sizeof(newinfo->stages[0]));
2348 newinfo->filemask |= (1 << MERGE_BASE);
2349 newinfo->pathnames[0] = oldpath;
c2d267df
EN
2350 if (type_changed) {
2351 /* rename vs. typechange */
6fcccbd7
EN
2352 /* Mark the original as resolved by removal */
2353 memcpy(&oldinfo->stages[0].oid, &null_oid,
2354 sizeof(oldinfo->stages[0].oid));
2355 oldinfo->stages[0].mode = 0;
2356 oldinfo->filemask &= 0x06;
c2d267df
EN
2357 } else if (source_deleted) {
2358 /* rename/delete */
2e91ddd2
EN
2359 newinfo->path_conflict = 1;
2360 path_msg(opt, newpath, 0,
2361 _("CONFLICT (rename/delete): %s renamed"
2362 " to %s in %s, but deleted in %s."),
2363 oldpath, newpath,
2364 rename_branch, delete_branch);
c2d267df
EN
2365 } else {
2366 /* normal rename */
f1665e69
EN
2367 memcpy(&newinfo->stages[other_source_index],
2368 &oldinfo->stages[other_source_index],
2369 sizeof(newinfo->stages[0]));
2370 newinfo->filemask |= (1 << other_source_index);
2371 newinfo->pathnames[other_source_index] = oldpath;
c2d267df
EN
2372 }
2373 }
2374
2375 if (!type_changed) {
2376 /* Mark the original as resolved by removal */
2377 oldinfo->merged.is_null = 1;
2378 oldinfo->merged.clean = 1;
2379 }
2380
2381 }
2382
2383 return clean_merge;
e1a124e8
EN
2384}
2385
f89b4f2b
EN
2386static inline int possible_side_renames(struct rename_info *renames,
2387 unsigned side_index)
2388{
2389 return renames->pairs[side_index].nr > 0 &&
a49b55d5 2390 !strintmap_empty(&renames->relevant_sources[side_index]);
f89b4f2b
EN
2391}
2392
2393static inline int possible_renames(struct rename_info *renames)
2394{
2395 return possible_side_renames(renames, 1) ||
25e65b6d
EN
2396 possible_side_renames(renames, 2) ||
2397 !strmap_empty(&renames->cached_pairs[1]) ||
2398 !strmap_empty(&renames->cached_pairs[2]);
f89b4f2b
EN
2399}
2400
f78cf976
EN
2401static void resolve_diffpair_statuses(struct diff_queue_struct *q)
2402{
2403 /*
2404 * A simplified version of diff_resolve_rename_copy(); would probably
2405 * just use that function but it's static...
2406 */
2407 int i;
2408 struct diff_filepair *p;
2409
2410 for (i = 0; i < q->nr; ++i) {
2411 p = q->queue[i];
2412 p->status = 0; /* undecided */
2413 if (!DIFF_FILE_VALID(p->one))
2414 p->status = DIFF_STATUS_ADDED;
2415 else if (!DIFF_FILE_VALID(p->two))
2416 p->status = DIFF_STATUS_DELETED;
2417 else if (DIFF_PAIR_RENAME(p))
2418 p->status = DIFF_STATUS_RENAMED;
2419 }
2420}
2421
86b41b38
EN
2422static void prune_cached_from_relevant(struct rename_info *renames,
2423 unsigned side)
2424{
2425 /* Reason for this function described in add_pair() */
2426 struct hashmap_iter iter;
2427 struct strmap_entry *entry;
2428
2429 /* Remove from relevant_sources all entries in cached_pairs[side] */
2430 strmap_for_each_entry(&renames->cached_pairs[side], &iter, entry) {
2431 strintmap_remove(&renames->relevant_sources[side],
2432 entry->key);
2433 }
2434 /* Remove from relevant_sources all entries in cached_irrelevant[side] */
2435 strset_for_each_entry(&renames->cached_irrelevant[side], &iter, entry) {
2436 strintmap_remove(&renames->relevant_sources[side],
2437 entry->key);
2438 }
2439}
2440
86b41b38
EN
2441static void use_cached_pairs(struct merge_options *opt,
2442 struct strmap *cached_pairs,
2443 struct diff_queue_struct *pairs)
2444{
2445 struct hashmap_iter iter;
2446 struct strmap_entry *entry;
2447
2448 /*
2449 * Add to side_pairs all entries from renames->cached_pairs[side_index].
2450 * (Info in cached_irrelevant[side_index] is not relevant here.)
2451 */
2452 strmap_for_each_entry(cached_pairs, &iter, entry) {
2453 struct diff_filespec *one, *two;
2454 const char *old_name = entry->key;
2455 const char *new_name = entry->value;
2456 if (!new_name)
2457 new_name = old_name;
2458
2459 /* We don't care about oid/mode, only filenames and status */
2460 one = alloc_filespec(old_name);
2461 two = alloc_filespec(new_name);
2462 diff_queue(pairs, one, two);
2463 pairs->queue[pairs->nr-1]->status = entry->value ? 'R' : 'D';
2464 }
2465}
2466
2734f2e3
EN
2467static void cache_new_pair(struct rename_info *renames,
2468 int side,
2469 char *old_path,
2470 char *new_path,
2471 int free_old_value)
2472{
2473 char *old_value;
2474 new_path = xstrdup(new_path);
2475 old_value = strmap_put(&renames->cached_pairs[side],
2476 old_path, new_path);
2477 strset_add(&renames->cached_target_names[side], new_path);
2478 if (free_old_value)
2479 free(old_value);
2480 else
2481 assert(!old_value);
2482}
2483
2484static void possibly_cache_new_pair(struct rename_info *renames,
2485 struct diff_filepair *p,
2486 unsigned side,
2487 char *new_path)
2488{
2489 int dir_renamed_side = 0;
2490
2491 if (new_path) {
2492 /*
2493 * Directory renames happen on the other side of history from
2494 * the side that adds new files to the old directory.
2495 */
2496 dir_renamed_side = 3 - side;
2497 } else {
2498 int val = strintmap_get(&renames->relevant_sources[side],
2499 p->one->path);
2500 if (val == RELEVANT_NO_MORE) {
2501 assert(p->status == 'D');
2502 strset_add(&renames->cached_irrelevant[side],
2503 p->one->path);
2504 }
2505 if (val <= 0)
2506 return;
2507 }
2508
2509 if (p->status == 'D') {
2510 /*
2511 * If we already had this delete, we'll just set it's value
2512 * to NULL again, so no harm.
2513 */
2514 strmap_put(&renames->cached_pairs[side], p->one->path, NULL);
2515 } else if (p->status == 'R') {
2516 if (!new_path)
2517 new_path = p->two->path;
2518 else
2519 cache_new_pair(renames, dir_renamed_side,
2520 p->two->path, new_path, 0);
2521 cache_new_pair(renames, side, p->one->path, new_path, 1);
2522 } else if (p->status == 'A' && new_path) {
2523 cache_new_pair(renames, dir_renamed_side,
2524 p->two->path, new_path, 0);
2525 }
2526}
2527
e1a124e8
EN
2528static int compare_pairs(const void *a_, const void *b_)
2529{
965a7bc2
EN
2530 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
2531 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
2532
2533 return strcmp(a->one->path, b->one->path);
e1a124e8
EN
2534}
2535
356da0f9 2536/* Call diffcore_rename() to update deleted/added pairs into rename pairs */
e1a124e8 2537static void detect_regular_renames(struct merge_options *opt,
e1a124e8
EN
2538 unsigned side_index)
2539{
f39d05ca
EN
2540 struct diff_options diff_opts;
2541 struct rename_info *renames = &opt->priv->renames;
2542
25e65b6d 2543 prune_cached_from_relevant(renames, side_index);
f89b4f2b
EN
2544 if (!possible_side_renames(renames, side_index)) {
2545 /*
2546 * No rename detection needed for this side, but we still need
2547 * to make sure 'adds' are marked correctly in case the other
2548 * side had directory renames.
2549 */
2550 resolve_diffpair_statuses(&renames->pairs[side_index]);
2551 return;
2552 }
2553
d5098029 2554 partial_clear_dir_rename_count(&renames->dir_rename_count[side_index]);
f39d05ca
EN
2555 repo_diff_setup(opt->repo, &diff_opts);
2556 diff_opts.flags.recursive = 1;
2557 diff_opts.flags.rename_empty = 0;
2558 diff_opts.detect_rename = DIFF_DETECT_RENAME;
2559 diff_opts.rename_limit = opt->rename_limit;
2560 if (opt->rename_limit <= 0)
2561 diff_opts.rename_limit = 1000;
2562 diff_opts.rename_score = opt->rename_score;
2563 diff_opts.show_rename_progress = opt->show_rename_progress;
2564 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
2565 diff_setup_done(&diff_opts);
557ac035 2566
f78cf976 2567 diff_queued_diff = renames->pairs[side_index];
557ac035 2568 trace2_region_enter("diff", "diffcore_rename", opt->repo);
0c4fd732 2569 diffcore_rename_extended(&diff_opts,
174791f0 2570 &renames->relevant_sources[side_index],
0c4fd732 2571 &renames->dirs_removed[side_index],
25e65b6d
EN
2572 &renames->dir_rename_count[side_index],
2573 &renames->cached_pairs[side_index]);
557ac035 2574 trace2_region_leave("diff", "diffcore_rename", opt->repo);
f78cf976 2575 resolve_diffpair_statuses(&diff_queued_diff);
f39d05ca
EN
2576
2577 if (diff_opts.needed_rename_limit > renames->needed_limit)
2578 renames->needed_limit = diff_opts.needed_rename_limit;
2579
2580 renames->pairs[side_index] = diff_queued_diff;
2581
2582 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
2583 diff_queued_diff.nr = 0;
2584 diff_queued_diff.queue = NULL;
2585 diff_flush(&diff_opts);
e1a124e8
EN
2586}
2587
2588/*
356da0f9
EN
2589 * Get information of all renames which occurred in 'side_pairs', making use
2590 * of any implicit directory renames in side_dir_renames (also making use of
2591 * implicit directory renames rename_exclusions as needed by
2592 * check_for_directory_rename()). Add all (updated) renames into result.
e1a124e8
EN
2593 */
2594static int collect_renames(struct merge_options *opt,
2595 struct diff_queue_struct *result,
fa5e06d6
EN
2596 unsigned side_index,
2597 struct strmap *dir_renames_for_side,
2598 struct strmap *rename_exclusions)
e1a124e8 2599{
965a7bc2 2600 int i, clean = 1;
fa5e06d6 2601 struct strmap collisions;
965a7bc2 2602 struct diff_queue_struct *side_pairs;
fa5e06d6
EN
2603 struct hashmap_iter iter;
2604 struct strmap_entry *entry;
965a7bc2
EN
2605 struct rename_info *renames = &opt->priv->renames;
2606
2607 side_pairs = &renames->pairs[side_index];
fa5e06d6 2608 compute_collisions(&collisions, dir_renames_for_side, side_pairs);
965a7bc2
EN
2609
2610 for (i = 0; i < side_pairs->nr; ++i) {
2611 struct diff_filepair *p = side_pairs->queue[i];
fa5e06d6 2612 char *new_path; /* non-NULL only with directory renames */
965a7bc2 2613
fa5e06d6 2614 if (p->status != 'A' && p->status != 'R') {
2734f2e3 2615 possibly_cache_new_pair(renames, p, side_index, NULL);
965a7bc2
EN
2616 diff_free_filepair(p);
2617 continue;
2618 }
2619
fa5e06d6
EN
2620 new_path = check_for_directory_rename(opt, p->two->path,
2621 side_index,
2622 dir_renames_for_side,
2623 rename_exclusions,
2624 &collisions,
2625 &clean);
2626
2734f2e3 2627 possibly_cache_new_pair(renames, p, side_index, new_path);
fa5e06d6
EN
2628 if (p->status != 'R' && !new_path) {
2629 diff_free_filepair(p);
2630 continue;
2631 }
2632
2633 if (new_path)
2634 apply_directory_rename_modifications(opt, p, new_path);
2635
965a7bc2
EN
2636 /*
2637 * p->score comes back from diffcore_rename_extended() with
2638 * the similarity of the renamed file. The similarity is
2639 * was used to determine that the two files were related
2640 * and are a rename, which we have already used, but beyond
2641 * that we have no use for the similarity. So p->score is
2642 * now irrelevant. However, process_renames() will need to
2643 * know which side of the merge this rename was associated
2644 * with, so overwrite p->score with that value.
2645 */
2646 p->score = side_index;
2647 result->queue[result->nr++] = p;
2648 }
2649
fa5e06d6
EN
2650 /* Free each value in the collisions map */
2651 strmap_for_each_entry(&collisions, &iter, entry) {
2652 struct collision_info *info = entry->value;
2653 string_list_clear(&info->source_files, 0);
2654 }
2655 /*
2656 * In compute_collisions(), we set collisions.strdup_strings to 0
2657 * so that we wouldn't have to make another copy of the new_path
2658 * allocated by apply_dir_rename(). But now that we've used them
2659 * and have no other references to these strings, it is time to
2660 * deallocate them.
2661 */
2662 free_strmap_strings(&collisions);
2663 strmap_clear(&collisions, 1);
965a7bc2 2664 return clean;
e1a124e8
EN
2665}
2666
231e2dd4
EN
2667static int detect_and_process_renames(struct merge_options *opt,
2668 struct tree *merge_base,
2669 struct tree *side1,
2670 struct tree *side2)
2671{
e1a124e8
EN
2672 struct diff_queue_struct combined;
2673 struct rename_info *renames = &opt->priv->renames;
112e1112 2674 int need_dir_renames, s, clean = 1;
e1a124e8
EN
2675
2676 memset(&combined, 0, sizeof(combined));
f89b4f2b
EN
2677 if (!possible_renames(renames))
2678 goto cleanup;
e1a124e8 2679
557ac035 2680 trace2_region_enter("merge", "regular renames", opt->repo);
f78cf976
EN
2681 detect_regular_renames(opt, MERGE_SIDE1);
2682 detect_regular_renames(opt, MERGE_SIDE2);
25e65b6d
EN
2683 use_cached_pairs(opt, &renames->cached_pairs[1], &renames->pairs[1]);
2684 use_cached_pairs(opt, &renames->cached_pairs[2], &renames->pairs[2]);
557ac035 2685 trace2_region_leave("merge", "regular renames", opt->repo);
e1a124e8 2686
557ac035 2687 trace2_region_enter("merge", "directory renames", opt->repo);
112e1112
EN
2688 need_dir_renames =
2689 !opt->priv->call_depth &&
2690 (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE ||
2691 opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT);
2692
2693 if (need_dir_renames) {
2694 get_provisional_directory_renames(opt, MERGE_SIDE1, &clean);
2695 get_provisional_directory_renames(opt, MERGE_SIDE2, &clean);
2696 handle_directory_level_conflicts(opt);
2697 }
2698
e1a124e8
EN
2699 ALLOC_GROW(combined.queue,
2700 renames->pairs[1].nr + renames->pairs[2].nr,
2701 combined.alloc);
fa5e06d6
EN
2702 clean &= collect_renames(opt, &combined, MERGE_SIDE1,
2703 &renames->dir_renames[2],
2704 &renames->dir_renames[1]);
2705 clean &= collect_renames(opt, &combined, MERGE_SIDE2,
2706 &renames->dir_renames[1],
2707 &renames->dir_renames[2]);
72b30910 2708 STABLE_QSORT(combined.queue, combined.nr, compare_pairs);
557ac035 2709 trace2_region_leave("merge", "directory renames", opt->repo);
e1a124e8 2710
557ac035 2711 trace2_region_enter("merge", "process renames", opt->repo);
e1a124e8 2712 clean &= process_renames(opt, &combined);
557ac035 2713 trace2_region_leave("merge", "process renames", opt->repo);
e1a124e8 2714
f89b4f2b
EN
2715 goto simple_cleanup; /* collect_renames() handles some of cleanup */
2716
2717cleanup:
2718 /*
2719 * Free now unneeded filepairs, which would have been handled
2720 * in collect_renames() normally but we skipped that code.
2721 */
2722 for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) {
2723 struct diff_queue_struct *side_pairs;
2724 int i;
2725
2726 side_pairs = &renames->pairs[s];
2727 for (i = 0; i < side_pairs->nr; ++i) {
2728 struct diff_filepair *p = side_pairs->queue[i];
2729 diff_free_filepair(p);
2730 }
2731 }
2732
2733simple_cleanup:
e1a124e8
EN
2734 /* Free memory for renames->pairs[] and combined */
2735 for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) {
2736 free(renames->pairs[s].queue);
2737 DIFF_QUEUE_CLEAR(&renames->pairs[s]);
2738 }
2739 if (combined.nr) {
2740 int i;
2741 for (i = 0; i < combined.nr; i++)
2742 diff_free_filepair(combined.queue[i]);
2743 free(combined.queue);
2744 }
231e2dd4 2745
231e2dd4
EN
2746 return clean;
2747}
2748
04af1879
EN
2749/*** Function Grouping: functions related to process_entries() ***/
2750
5a3743da 2751static int sort_dirs_next_to_their_children(const char *one, const char *two)
8adffaa8 2752{
5a3743da
EN
2753 unsigned char c1, c2;
2754
2755 /*
2756 * Here we only care that entries for directories appear adjacent
2757 * to and before files underneath the directory. We can achieve
2758 * that by pretending to add a trailing slash to every file and
2759 * then sorting. In other words, we do not want the natural
2760 * sorting of
2761 * foo
2762 * foo.txt
2763 * foo/bar
2764 * Instead, we want "foo" to sort as though it were "foo/", so that
2765 * we instead get
2766 * foo.txt
2767 * foo
2768 * foo/bar
2769 * To achieve this, we basically implement our own strcmp, except that
2770 * if we get to the end of either string instead of comparing NUL to
2771 * another character, we compare '/' to it.
2772 *
2773 * If this unusual "sort as though '/' were appended" perplexes
2774 * you, perhaps it will help to note that this is not the final
2775 * sort. write_tree() will sort again without the trailing slash
2776 * magic, but just on paths immediately under a given tree.
8adffaa8 2777 *
5a3743da
EN
2778 * The reason to not use df_name_compare directly was that it was
2779 * just too expensive (we don't have the string lengths handy), so
2780 * it was reimplemented.
8adffaa8 2781 */
5a3743da 2782
8adffaa8 2783 /*
5a3743da
EN
2784 * NOTE: This function will never be called with two equal strings,
2785 * because it is used to sort the keys of a strmap, and strmaps have
2786 * unique keys by construction. That simplifies our c1==c2 handling
2787 * below.
8adffaa8 2788 */
5a3743da
EN
2789
2790 while (*one && (*one == *two)) {
2791 one++;
2792 two++;
2793 }
2794
2795 c1 = *one ? *one : '/';
2796 c2 = *two ? *two : '/';
2797
2798 if (c1 == c2) {
2799 /* Getting here means one is a leading directory of the other */
2800 return (*one) ? 1 : -1;
2801 } else
2802 return c1 - c2;
8adffaa8
EN
2803}
2804
3860220b
EN
2805static int read_oid_strbuf(struct merge_options *opt,
2806 const struct object_id *oid,
2807 struct strbuf *dst)
2808{
2809 void *buf;
2810 enum object_type type;
2811 unsigned long size;
2812 buf = read_object_file(oid, &type, &size);
2813 if (!buf)
2814 return err(opt, _("cannot read object %s"), oid_to_hex(oid));
2815 if (type != OBJ_BLOB) {
2816 free(buf);
2817 return err(opt, _("object %s is not a blob"), oid_to_hex(oid));
2818 }
2819 strbuf_attach(dst, buf, size, size + 1);
2820 return 0;
2821}
2822
2823static int blob_unchanged(struct merge_options *opt,
2824 const struct version_info *base,
2825 const struct version_info *side,
2826 const char *path)
2827{
2828 struct strbuf basebuf = STRBUF_INIT;
2829 struct strbuf sidebuf = STRBUF_INIT;
2830 int ret = 0; /* assume changed for safety */
2831 const struct index_state *idx = &opt->priv->attr_index;
2832
2833 if (!idx->initialized)
2834 initialize_attr_index(opt);
2835
2836 if (base->mode != side->mode)
2837 return 0;
2838 if (oideq(&base->oid, &side->oid))
2839 return 1;
2840
2841 if (read_oid_strbuf(opt, &base->oid, &basebuf) ||
2842 read_oid_strbuf(opt, &side->oid, &sidebuf))
2843 goto error_return;
2844 /*
2845 * Note: binary | is used so that both renormalizations are
2846 * performed. Comparison can be skipped if both files are
2847 * unchanged since their sha1s have already been compared.
2848 */
2849 if (renormalize_buffer(idx, path, basebuf.buf, basebuf.len, &basebuf) |
2850 renormalize_buffer(idx, path, sidebuf.buf, sidebuf.len, &sidebuf))
2851 ret = (basebuf.len == sidebuf.len &&
2852 !memcmp(basebuf.buf, sidebuf.buf, basebuf.len));
2853
2854error_return:
2855 strbuf_release(&basebuf);
2856 strbuf_release(&sidebuf);
2857 return ret;
2858}
2859
a9945bba 2860struct directory_versions {
bb470f4e
EN
2861 /*
2862 * versions: list of (basename -> version_info)
2863 *
2864 * The basenames are in reverse lexicographic order of full pathnames,
2865 * as processed in process_entries(). This puts all entries within
2866 * a directory together, and covers the directory itself after
2867 * everything within it, allowing us to write subtrees before needing
2868 * to record information for the tree itself.
2869 */
a9945bba 2870 struct string_list versions;
bb470f4e
EN
2871
2872 /*
2873 * offsets: list of (full relative path directories -> integer offsets)
2874 *
2875 * Since versions contains basenames from files in multiple different
2876 * directories, we need to know which entries in versions correspond
2877 * to which directories. Values of e.g.
2878 * "" 0
2879 * src 2
2880 * src/moduleA 5
2881 * Would mean that entries 0-1 of versions are files in the toplevel
2882 * directory, entries 2-4 are files under src/, and the remaining
2883 * entries starting at index 5 are files under src/moduleA/.
2884 */
2885 struct string_list offsets;
2886
2887 /*
2888 * last_directory: directory that previously processed file found in
2889 *
2890 * last_directory starts NULL, but records the directory in which the
2891 * previous file was found within. As soon as
2892 * directory(current_file) != last_directory
2893 * then we need to start updating accounting in versions & offsets.
2894 * Note that last_directory is always the last path in "offsets" (or
2895 * NULL if "offsets" is empty) so this exists just for quick access.
2896 */
2897 const char *last_directory;
2898
2899 /* last_directory_len: cached computation of strlen(last_directory) */
2900 unsigned last_directory_len;
a9945bba
EN
2901};
2902
ee4012dc
EN
2903static int tree_entry_order(const void *a_, const void *b_)
2904{
2905 const struct string_list_item *a = a_;
2906 const struct string_list_item *b = b_;
2907
2908 const struct merged_info *ami = a->util;
2909 const struct merged_info *bmi = b->util;
2910 return base_name_compare(a->string, strlen(a->string), ami->result.mode,
2911 b->string, strlen(b->string), bmi->result.mode);
2912}
2913
2914static void write_tree(struct object_id *result_oid,
2915 struct string_list *versions,
2916 unsigned int offset,
2917 size_t hash_size)
2918{
2919 size_t maxlen = 0, extra;
2920 unsigned int nr = versions->nr - offset;
2921 struct strbuf buf = STRBUF_INIT;
2922 struct string_list relevant_entries = STRING_LIST_INIT_NODUP;
2923 int i;
2924
2925 /*
2926 * We want to sort the last (versions->nr-offset) entries in versions.
2927 * Do so by abusing the string_list API a bit: make another string_list
2928 * that contains just those entries and then sort them.
2929 *
2930 * We won't use relevant_entries again and will let it just pop off the
2931 * stack, so there won't be allocation worries or anything.
2932 */
2933 relevant_entries.items = versions->items + offset;
2934 relevant_entries.nr = versions->nr - offset;
72b30910 2935 /* No need for STABLE_QSORT -- filenames must be unique */
ee4012dc
EN
2936 QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order);
2937
2938 /* Pre-allocate some space in buf */
2939 extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */
2940 for (i = 0; i < nr; i++) {
2941 maxlen += strlen(versions->items[offset+i].string) + extra;
2942 }
2943 strbuf_grow(&buf, maxlen);
2944
2945 /* Write each entry out to buf */
2946 for (i = 0; i < nr; i++) {
2947 struct merged_info *mi = versions->items[offset+i].util;
2948 struct version_info *ri = &mi->result;
2949 strbuf_addf(&buf, "%o %s%c",
2950 ri->mode,
2951 versions->items[offset+i].string, '\0');
2952 strbuf_add(&buf, ri->oid.hash, hash_size);
2953 }
2954
2955 /* Write this object file out, and record in result_oid */
2956 write_object_file(buf.buf, buf.len, tree_type, result_oid);
2957 strbuf_release(&buf);
2958}
2959
a9945bba
EN
2960static void record_entry_for_tree(struct directory_versions *dir_metadata,
2961 const char *path,
2962 struct merged_info *mi)
2963{
2964 const char *basename;
2965
2966 if (mi->is_null)
2967 /* nothing to record */
2968 return;
2969
2970 basename = path + mi->basename_offset;
2971 assert(strchr(basename, '/') == NULL);
2972 string_list_append(&dir_metadata->versions,
2973 basename)->util = &mi->result;
2974}
2975
bb470f4e
EN
2976static void write_completed_directory(struct merge_options *opt,
2977 const char *new_directory_name,
2978 struct directory_versions *info)
2979{
2980 const char *prev_dir;
2981 struct merged_info *dir_info = NULL;
2982 unsigned int offset;
2983
2984 /*
2985 * Some explanation of info->versions and info->offsets...
2986 *
2987 * process_entries() iterates over all relevant files AND
2988 * directories in reverse lexicographic order, and calls this
2989 * function. Thus, an example of the paths that process_entries()
2990 * could operate on (along with the directories for those paths
2991 * being shown) is:
2992 *
2993 * xtract.c ""
2994 * tokens.txt ""
2995 * src/moduleB/umm.c src/moduleB
2996 * src/moduleB/stuff.h src/moduleB
2997 * src/moduleB/baz.c src/moduleB
2998 * src/moduleB src
2999 * src/moduleA/foo.c src/moduleA
3000 * src/moduleA/bar.c src/moduleA
3001 * src/moduleA src
3002 * src ""
3003 * Makefile ""
3004 *
3005 * info->versions:
3006 *
3007 * always contains the unprocessed entries and their
3008 * version_info information. For example, after the first five
3009 * entries above, info->versions would be:
3010 *
3011 * xtract.c <xtract.c's version_info>
3012 * token.txt <token.txt's version_info>
3013 * umm.c <src/moduleB/umm.c's version_info>
3014 * stuff.h <src/moduleB/stuff.h's version_info>
3015 * baz.c <src/moduleB/baz.c's version_info>
3016 *
3017 * Once a subdirectory is completed we remove the entries in
3018 * that subdirectory from info->versions, writing it as a tree
3019 * (write_tree()). Thus, as soon as we get to src/moduleB,
3020 * info->versions would be updated to
3021 *
3022 * xtract.c <xtract.c's version_info>
3023 * token.txt <token.txt's version_info>
3024 * moduleB <src/moduleB's version_info>
3025 *
3026 * info->offsets:
3027 *
3028 * helps us track which entries in info->versions correspond to
3029 * which directories. When we are N directories deep (e.g. 4
3030 * for src/modA/submod/subdir/), we have up to N+1 unprocessed
3031 * directories (+1 because of toplevel dir). Corresponding to
3032 * the info->versions example above, after processing five entries
3033 * info->offsets will be:
3034 *
3035 * "" 0
3036 * src/moduleB 2
3037 *
3038 * which is used to know that xtract.c & token.txt are from the
3039 * toplevel dirctory, while umm.c & stuff.h & baz.c are from the
3040 * src/moduleB directory. Again, following the example above,
3041 * once we need to process src/moduleB, then info->offsets is
3042 * updated to
3043 *
3044 * "" 0
3045 * src 2
3046 *
3047 * which says that moduleB (and only moduleB so far) is in the
3048 * src directory.
3049 *
3050 * One unique thing to note about info->offsets here is that
3051 * "src" was not added to info->offsets until there was a path
3052 * (a file OR directory) immediately below src/ that got
3053 * processed.
3054 *
3055 * Since process_entry() just appends new entries to info->versions,
3056 * write_completed_directory() only needs to do work if the next path
3057 * is in a directory that is different than the last directory found
3058 * in info->offsets.
3059 */
3060
3061 /*
3062 * If we are working with the same directory as the last entry, there
3063 * is no work to do. (See comments above the directory_name member of
3064 * struct merged_info for why we can use pointer comparison instead of
3065 * strcmp here.)
3066 */
3067 if (new_directory_name == info->last_directory)
3068 return;
3069
3070 /*
3071 * If we are just starting (last_directory is NULL), or last_directory
3072 * is a prefix of the current directory, then we can just update
3073 * info->offsets to record the offset where we started this directory
3074 * and update last_directory to have quick access to it.
3075 */
3076 if (info->last_directory == NULL ||
3077 !strncmp(new_directory_name, info->last_directory,
3078 info->last_directory_len)) {
3079 uintptr_t offset = info->versions.nr;
3080
3081 info->last_directory = new_directory_name;
3082 info->last_directory_len = strlen(info->last_directory);
3083 /*
3084 * Record the offset into info->versions where we will
3085 * start recording basenames of paths found within
3086 * new_directory_name.
3087 */
3088 string_list_append(&info->offsets,
3089 info->last_directory)->util = (void*)offset;
3090 return;
3091 }
3092
3093 /*
3094 * The next entry that will be processed will be within
3095 * new_directory_name. Since at this point we know that
3096 * new_directory_name is within a different directory than
3097 * info->last_directory, we have all entries for info->last_directory
3098 * in info->versions and we need to create a tree object for them.
3099 */
3100 dir_info = strmap_get(&opt->priv->paths, info->last_directory);
3101 assert(dir_info);
3102 offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util;
3103 if (offset == info->versions.nr) {
3104 /*
3105 * Actually, we don't need to create a tree object in this
3106 * case. Whenever all files within a directory disappear
3107 * during the merge (e.g. unmodified on one side and
3108 * deleted on the other, or files were renamed elsewhere),
3109 * then we get here and the directory itself needs to be
3110 * omitted from its parent tree as well.
3111 */
3112 dir_info->is_null = 1;
3113 } else {
3114 /*
3115 * Write out the tree to the git object directory, and also
3116 * record the mode and oid in dir_info->result.
3117 */
3118 dir_info->is_null = 0;
3119 dir_info->result.mode = S_IFDIR;
3120 write_tree(&dir_info->result.oid, &info->versions, offset,
3121 opt->repo->hash_algo->rawsz);
3122 }
3123
3124 /*
3125 * We've now used several entries from info->versions and one entry
3126 * from info->offsets, so we get rid of those values.
3127 */
3128 info->offsets.nr--;
3129 info->versions.nr = offset;
3130
3131 /*
3132 * Now we've taken care of the completed directory, but we need to
3133 * prepare things since future entries will be in
3134 * new_directory_name. (In particular, process_entry() will be
3135 * appending new entries to info->versions.) So, we need to make
3136 * sure new_directory_name is the last entry in info->offsets.
3137 */
3138 prev_dir = info->offsets.nr == 0 ? NULL :
3139 info->offsets.items[info->offsets.nr-1].string;
3140 if (new_directory_name != prev_dir) {
3141 uintptr_t c = info->versions.nr;
3142 string_list_append(&info->offsets,
3143 new_directory_name)->util = (void*)c;
3144 }
3145
3146 /* And, of course, we need to update last_directory to match. */
3147 info->last_directory = new_directory_name;
3148 info->last_directory_len = strlen(info->last_directory);
3149}
3150
6a02dd90
EN
3151/* Per entry merge function */
3152static void process_entry(struct merge_options *opt,
3153 const char *path,
a9945bba
EN
3154 struct conflict_info *ci,
3155 struct directory_versions *dir_metadata)
6a02dd90 3156{
23366d2a
EN
3157 int df_file_index = 0;
3158
6a02dd90
EN
3159 VERIFY_CI(ci);
3160 assert(ci->filemask >= 0 && ci->filemask <= 7);
3161 /* ci->match_mask == 7 was handled in collect_merge_info_callback() */
3162 assert(ci->match_mask == 0 || ci->match_mask == 3 ||
3163 ci->match_mask == 5 || ci->match_mask == 6);
3164
a9945bba
EN
3165 if (ci->dirmask) {
3166 record_entry_for_tree(dir_metadata, path, &ci->merged);
3167 if (ci->filemask == 0)
3168 /* nothing else to handle */
3169 return;
3170 assert(ci->df_conflict);
3171 }
3172
0ccfa4e5
EN
3173 if (ci->df_conflict && ci->merged.result.mode == 0) {
3174 int i;
3175
3176 /*
3177 * directory no longer in the way, but we do have a file we
3178 * need to place here so we need to clean away the "directory
3179 * merges to nothing" result.
3180 */
3181 ci->df_conflict = 0;
3182 assert(ci->filemask != 0);
3183 ci->merged.clean = 0;
3184 ci->merged.is_null = 0;
3185 /* and we want to zero out any directory-related entries */
3186 ci->match_mask = (ci->match_mask & ~ci->dirmask);
3187 ci->dirmask = 0;
3188 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
3189 if (ci->filemask & (1 << i))
3190 continue;
3191 ci->stages[i].mode = 0;
3192 oidcpy(&ci->stages[i].oid, &null_oid);
3193 }
3194 } else if (ci->df_conflict && ci->merged.result.mode != 0) {
23366d2a
EN
3195 /*
3196 * This started out as a D/F conflict, and the entries in
3197 * the competing directory were not removed by the merge as
3198 * evidenced by write_completed_directory() writing a value
3199 * to ci->merged.result.mode.
3200 */
3201 struct conflict_info *new_ci;
3202 const char *branch;
3203 const char *old_path = path;
3204 int i;
3205
3206 assert(ci->merged.result.mode == S_IFDIR);
3207
3208 /*
3209 * If filemask is 1, we can just ignore the file as having
3210 * been deleted on both sides. We do not want to overwrite
3211 * ci->merged.result, since it stores the tree for all the
3212 * files under it.
3213 */
3214 if (ci->filemask == 1) {
3215 ci->filemask = 0;
3216 return;
3217 }
3218
3219 /*
3220 * This file still exists on at least one side, and we want
3221 * the directory to remain here, so we need to move this
3222 * path to some new location.
3223 */
3224 new_ci = xcalloc(1, sizeof(*new_ci));
3225 /* We don't really want new_ci->merged.result copied, but it'll
3226 * be overwritten below so it doesn't matter. We also don't
3227 * want any directory mode/oid values copied, but we'll zero
3228 * those out immediately. We do want the rest of ci copied.
3229 */
3230 memcpy(new_ci, ci, sizeof(*ci));
3231 new_ci->match_mask = (new_ci->match_mask & ~new_ci->dirmask);
3232 new_ci->dirmask = 0;
3233 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
3234 if (new_ci->filemask & (1 << i))
3235 continue;
3236 /* zero out any entries related to directories */
3237 new_ci->stages[i].mode = 0;
3238 oidcpy(&new_ci->stages[i].oid, &null_oid);
3239 }
3240
3241 /*
3242 * Find out which side this file came from; note that we
3243 * cannot just use ci->filemask, because renames could cause
3244 * the filemask to go back to 7. So we use dirmask, then
3245 * pick the opposite side's index.
3246 */
3247 df_file_index = (ci->dirmask & (1 << 1)) ? 2 : 1;
3248 branch = (df_file_index == 1) ? opt->branch1 : opt->branch2;
3249 path = unique_path(&opt->priv->paths, path, branch);
3250 strmap_put(&opt->priv->paths, path, new_ci);
3251
3252 path_msg(opt, path, 0,
3253 _("CONFLICT (file/directory): directory in the way "
3254 "of %s from %s; moving it to %s instead."),
3255 old_path, branch, path);
3256
3257 /*
3258 * Zero out the filemask for the old ci. At this point, ci
3259 * was just an entry for a directory, so we don't need to
3260 * do anything more with it.
3261 */
3262 ci->filemask = 0;
3263
3264 /*
3265 * Now note that we're working on the new entry (path was
3266 * updated above.
3267 */
3268 ci = new_ci;
6a02dd90
EN
3269 }
3270
3271 /*
3272 * NOTE: Below there is a long switch-like if-elseif-elseif... block
3273 * which the code goes through even for the df_conflict cases
23366d2a 3274 * above.
6a02dd90
EN
3275 */
3276 if (ci->match_mask) {
3277 ci->merged.clean = 1;
3278 if (ci->match_mask == 6) {
3279 /* stages[1] == stages[2] */
3280 ci->merged.result.mode = ci->stages[1].mode;
3281 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
3282 } else {
3283 /* determine the mask of the side that didn't match */
3284 unsigned int othermask = 7 & ~ci->match_mask;
3285 int side = (othermask == 4) ? 2 : 1;
3286
3287 ci->merged.result.mode = ci->stages[side].mode;
3288 ci->merged.is_null = !ci->merged.result.mode;
3289 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
3290
3291 assert(othermask == 2 || othermask == 4);
3292 assert(ci->merged.is_null ==
3293 (ci->filemask == ci->match_mask));
3294 }
3295 } else if (ci->filemask >= 6 &&
3296 (S_IFMT & ci->stages[1].mode) !=
3297 (S_IFMT & ci->stages[2].mode)) {
4ef88fc3
EN
3298 /* Two different items from (file/submodule/symlink) */
3299 if (opt->priv->call_depth) {
3300 /* Just use the version from the merge base */
3301 ci->merged.clean = 0;
3302 oidcpy(&ci->merged.result.oid, &ci->stages[0].oid);
3303 ci->merged.result.mode = ci->stages[0].mode;
3304 ci->merged.is_null = (ci->merged.result.mode == 0);
3305 } else {
3306 /* Handle by renaming one or both to separate paths. */
3307 unsigned o_mode = ci->stages[0].mode;
3308 unsigned a_mode = ci->stages[1].mode;
3309 unsigned b_mode = ci->stages[2].mode;
3310 struct conflict_info *new_ci;
3311 const char *a_path = NULL, *b_path = NULL;
3312 int rename_a = 0, rename_b = 0;
3313
3314 new_ci = xmalloc(sizeof(*new_ci));
3315
3316 if (S_ISREG(a_mode))
3317 rename_a = 1;
3318 else if (S_ISREG(b_mode))
3319 rename_b = 1;
3320 else {
3321 rename_a = 1;
3322 rename_b = 1;
3323 }
3324
3325 path_msg(opt, path, 0,
3326 _("CONFLICT (distinct types): %s had different "
3327 "types on each side; renamed %s of them so "
3328 "each can be recorded somewhere."),
3329 path,
3330 (rename_a && rename_b) ? _("both") : _("one"));
3331
3332 ci->merged.clean = 0;
3333 memcpy(new_ci, ci, sizeof(*new_ci));
3334
3335 /* Put b into new_ci, removing a from stages */
3336 new_ci->merged.result.mode = ci->stages[2].mode;
3337 oidcpy(&new_ci->merged.result.oid, &ci->stages[2].oid);
3338 new_ci->stages[1].mode = 0;
3339 oidcpy(&new_ci->stages[1].oid, &null_oid);
3340 new_ci->filemask = 5;
3341 if ((S_IFMT & b_mode) != (S_IFMT & o_mode)) {
3342 new_ci->stages[0].mode = 0;
3343 oidcpy(&new_ci->stages[0].oid, &null_oid);
3344 new_ci->filemask = 4;
3345 }
3346
3347 /* Leave only a in ci, fixing stages. */
3348 ci->merged.result.mode = ci->stages[1].mode;
3349 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
3350 ci->stages[2].mode = 0;
3351 oidcpy(&ci->stages[2].oid, &null_oid);
3352 ci->filemask = 3;
3353 if ((S_IFMT & a_mode) != (S_IFMT & o_mode)) {
3354 ci->stages[0].mode = 0;
3355 oidcpy(&ci->stages[0].oid, &null_oid);
3356 ci->filemask = 2;
3357 }
3358
3359 /* Insert entries into opt->priv_paths */
3360 assert(rename_a || rename_b);
3361 if (rename_a) {
3362 a_path = unique_path(&opt->priv->paths,
3363 path, opt->branch1);
3364 strmap_put(&opt->priv->paths, a_path, ci);
3365 }
3366
3367 if (rename_b)
3368 b_path = unique_path(&opt->priv->paths,
3369 path, opt->branch2);
3370 else
3371 b_path = path;
3372 strmap_put(&opt->priv->paths, b_path, new_ci);
3373
3374 if (rename_a && rename_b) {
3375 strmap_remove(&opt->priv->paths, path, 0);
3376 /*
3377 * We removed path from opt->priv->paths. path
3378 * will also eventually need to be freed, but
3379 * it may still be used by e.g. ci->pathnames.
3380 * So, store it in another string-list for now.
3381 */
3382 string_list_append(&opt->priv->paths_to_free,
3383 path);
3384 }
3385
3386 /*
3387 * Do special handling for b_path since process_entry()
3388 * won't be called on it specially.
3389 */
3390 strmap_put(&opt->priv->conflicted, b_path, new_ci);
3391 record_entry_for_tree(dir_metadata, b_path,
3392 &new_ci->merged);
3393
3394 /*
3395 * Remaining code for processing this entry should
3396 * think in terms of processing a_path.
3397 */
3398 if (a_path)
3399 path = a_path;
3400 }
6a02dd90 3401 } else if (ci->filemask >= 6) {
991bbdca
EN
3402 /* Need a two-way or three-way content merge */
3403 struct version_info merged_file;
3404 unsigned clean_merge;
3405 struct version_info *o = &ci->stages[0];
3406 struct version_info *a = &ci->stages[1];
3407 struct version_info *b = &ci->stages[2];
3408
3409 clean_merge = handle_content_merge(opt, path, o, a, b,
3410 ci->pathnames,
3411 opt->priv->call_depth * 2,
3412 &merged_file);
3413 ci->merged.clean = clean_merge &&
3414 !ci->df_conflict && !ci->path_conflict;
3415 ci->merged.result.mode = merged_file.mode;
3416 ci->merged.is_null = (merged_file.mode == 0);
3417 oidcpy(&ci->merged.result.oid, &merged_file.oid);
3418 if (clean_merge && ci->df_conflict) {
3419 assert(df_file_index == 1 || df_file_index == 2);
3420 ci->filemask = 1 << df_file_index;
3421 ci->stages[df_file_index].mode = merged_file.mode;
3422 oidcpy(&ci->stages[df_file_index].oid, &merged_file.oid);
3423 }
3424 if (!clean_merge) {
3425 const char *reason = _("content");
3426 if (ci->filemask == 6)
3427 reason = _("add/add");
3428 if (S_ISGITLINK(merged_file.mode))
3429 reason = _("submodule");
3430 path_msg(opt, path, 0,
3431 _("CONFLICT (%s): Merge conflict in %s"),
3432 reason, path);
3433 }
6a02dd90
EN
3434 } else if (ci->filemask == 3 || ci->filemask == 5) {
3435 /* Modify/delete */
c5a6f655
EN
3436 const char *modify_branch, *delete_branch;
3437 int side = (ci->filemask == 5) ? 2 : 1;
3438 int index = opt->priv->call_depth ? 0 : side;
3439
3440 ci->merged.result.mode = ci->stages[index].mode;
3441 oidcpy(&ci->merged.result.oid, &ci->stages[index].oid);
3442 ci->merged.clean = 0;
3443
3444 modify_branch = (side == 1) ? opt->branch1 : opt->branch2;
3445 delete_branch = (side == 1) ? opt->branch2 : opt->branch1;
3446
3860220b
EN
3447 if (opt->renormalize &&
3448 blob_unchanged(opt, &ci->stages[0], &ci->stages[side],
3449 path)) {
3450 ci->merged.is_null = 1;
3451 ci->merged.clean = 1;
3452 } else if (ci->path_conflict &&
3453 oideq(&ci->stages[0].oid, &ci->stages[side].oid)) {
2e91ddd2
EN
3454 /*
3455 * This came from a rename/delete; no action to take,
3456 * but avoid printing "modify/delete" conflict notice
3457 * since the contents were not modified.
3458 */
3459 } else {
3460 path_msg(opt, path, 0,
3461 _("CONFLICT (modify/delete): %s deleted in %s "
3462 "and modified in %s. Version %s of %s left "
3463 "in tree."),
3464 path, delete_branch, modify_branch,
3465 modify_branch, path);
3466 }
6a02dd90
EN
3467 } else if (ci->filemask == 2 || ci->filemask == 4) {
3468 /* Added on one side */
3469 int side = (ci->filemask == 4) ? 2 : 1;
3470 ci->merged.result.mode = ci->stages[side].mode;
3471 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
53e88a03 3472 ci->merged.clean = !ci->df_conflict && !ci->path_conflict;
6a02dd90
EN
3473 } else if (ci->filemask == 1) {
3474 /* Deleted on both sides */
3475 ci->merged.is_null = 1;
3476 ci->merged.result.mode = 0;
3477 oidcpy(&ci->merged.result.oid, &null_oid);
53e88a03 3478 ci->merged.clean = !ci->path_conflict;
6a02dd90
EN
3479 }
3480
3481 /*
3482 * If still conflicted, record it separately. This allows us to later
3483 * iterate over just conflicted entries when updating the index instead
3484 * of iterating over all entries.
3485 */
3486 if (!ci->merged.clean)
3487 strmap_put(&opt->priv->conflicted, path, ci);
a9945bba 3488 record_entry_for_tree(dir_metadata, path, &ci->merged);
6a02dd90
EN
3489}
3490
231e2dd4
EN
3491static void process_entries(struct merge_options *opt,
3492 struct object_id *result_oid)
3493{
6a02dd90
EN
3494 struct hashmap_iter iter;
3495 struct strmap_entry *e;
8adffaa8
EN
3496 struct string_list plist = STRING_LIST_INIT_NODUP;
3497 struct string_list_item *entry;
bb470f4e
EN
3498 struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP,
3499 STRING_LIST_INIT_NODUP,
3500 NULL, 0 };
6a02dd90 3501
557ac035 3502 trace2_region_enter("merge", "process_entries setup", opt->repo);
6a02dd90
EN
3503 if (strmap_empty(&opt->priv->paths)) {
3504 oidcpy(result_oid, opt->repo->hash_algo->empty_tree);
3505 return;
3506 }
3507
8adffaa8 3508 /* Hack to pre-allocate plist to the desired size */
557ac035 3509 trace2_region_enter("merge", "plist grow", opt->repo);
8adffaa8 3510 ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc);
557ac035 3511 trace2_region_leave("merge", "plist grow", opt->repo);
8adffaa8
EN
3512
3513 /* Put every entry from paths into plist, then sort */
557ac035 3514 trace2_region_enter("merge", "plist copy", opt->repo);
6a02dd90 3515 strmap_for_each_entry(&opt->priv->paths, &iter, e) {
8adffaa8
EN
3516 string_list_append(&plist, e->key)->util = e->value;
3517 }
557ac035
EN
3518 trace2_region_leave("merge", "plist copy", opt->repo);
3519
3520 trace2_region_enter("merge", "plist special sort", opt->repo);
5a3743da 3521 plist.cmp = sort_dirs_next_to_their_children;
8adffaa8 3522 string_list_sort(&plist);
557ac035
EN
3523 trace2_region_leave("merge", "plist special sort", opt->repo);
3524
3525 trace2_region_leave("merge", "process_entries setup", opt->repo);
8adffaa8
EN
3526
3527 /*
3528 * Iterate over the items in reverse order, so we can handle paths
3529 * below a directory before needing to handle the directory itself.
bb470f4e
EN
3530 *
3531 * This allows us to write subtrees before we need to write trees,
3532 * and it also enables sane handling of directory/file conflicts
3533 * (because it allows us to know whether the directory is still in
3534 * the way when it is time to process the file at the same path).
8adffaa8 3535 */
557ac035 3536 trace2_region_enter("merge", "processing", opt->repo);
8adffaa8
EN
3537 for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) {
3538 char *path = entry->string;
6a02dd90
EN
3539 /*
3540 * NOTE: mi may actually be a pointer to a conflict_info, but
3541 * we have to check mi->clean first to see if it's safe to
3542 * reassign to such a pointer type.
3543 */
8adffaa8 3544 struct merged_info *mi = entry->util;
6a02dd90 3545
bb470f4e
EN
3546 write_completed_directory(opt, mi->directory_name,
3547 &dir_metadata);
a9945bba
EN
3548 if (mi->clean)
3549 record_entry_for_tree(&dir_metadata, path, mi);
3550 else {
8adffaa8 3551 struct conflict_info *ci = (struct conflict_info *)mi;
a9945bba 3552 process_entry(opt, path, ci, &dir_metadata);
8adffaa8 3553 }
6a02dd90 3554 }
557ac035 3555 trace2_region_leave("merge", "processing", opt->repo);
6a02dd90 3556
557ac035 3557 trace2_region_enter("merge", "process_entries cleanup", opt->repo);
bb470f4e
EN
3558 if (dir_metadata.offsets.nr != 1 ||
3559 (uintptr_t)dir_metadata.offsets.items[0].util != 0) {
3560 printf("dir_metadata.offsets.nr = %d (should be 1)\n",
3561 dir_metadata.offsets.nr);
3562 printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n",
3563 (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util);
3564 fflush(stdout);
3565 BUG("dir_metadata accounting completely off; shouldn't happen");
3566 }
ee4012dc
EN
3567 write_tree(result_oid, &dir_metadata.versions, 0,
3568 opt->repo->hash_algo->rawsz);
8adffaa8 3569 string_list_clear(&plist, 0);
a9945bba 3570 string_list_clear(&dir_metadata.versions, 0);
bb470f4e 3571 string_list_clear(&dir_metadata.offsets, 0);
557ac035 3572 trace2_region_leave("merge", "process_entries cleanup", opt->repo);
231e2dd4
EN
3573}
3574
04af1879
EN
3575/*** Function Grouping: functions related to merge_switch_to_result() ***/
3576
9fefce68
EN
3577static int checkout(struct merge_options *opt,
3578 struct tree *prev,
3579 struct tree *next)
3580{
6681ce5c
EN
3581 /* Switch the index/working copy from old to new */
3582 int ret;
3583 struct tree_desc trees[2];
3584 struct unpack_trees_options unpack_opts;
3585
3586 memset(&unpack_opts, 0, sizeof(unpack_opts));
3587 unpack_opts.head_idx = -1;
3588 unpack_opts.src_index = opt->repo->index;
3589 unpack_opts.dst_index = opt->repo->index;
3590
3591 setup_unpack_trees_porcelain(&unpack_opts, "merge");
3592
3593 /*
3594 * NOTE: if this were just "git checkout" code, we would probably
3595 * read or refresh the cache and check for a conflicted index, but
3596 * builtin/merge.c or sequencer.c really needs to read the index
3597 * and check for conflicted entries before starting merging for a
3598 * good user experience (no sense waiting for merges/rebases before
3599 * erroring out), so there's no reason to duplicate that work here.
3600 */
3601
3602 /* 2-way merge to the new branch */
3603 unpack_opts.update = 1;
3604 unpack_opts.merge = 1;
3605 unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
3606 unpack_opts.verbose_update = (opt->verbosity > 2);
3607 unpack_opts.fn = twoway_merge;
3608 if (1/* FIXME: opts->overwrite_ignore*/) {
3609 unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
3610 unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
3611 setup_standard_excludes(unpack_opts.dir);
3612 }
3613 parse_tree(prev);
3614 init_tree_desc(&trees[0], prev->buffer, prev->size);
3615 parse_tree(next);
3616 init_tree_desc(&trees[1], next->buffer, next->size);
3617
3618 ret = unpack_trees(2, trees, &unpack_opts);
3619 clear_unpack_trees_porcelain(&unpack_opts);
3620 dir_clear(unpack_opts.dir);
3621 FREE_AND_NULL(unpack_opts.dir);
3622 return ret;
9fefce68
EN
3623}
3624
66b209b8 3625static int record_conflicted_index_entries(struct merge_options *opt)
9fefce68 3626{
ef2b3693
EN
3627 struct hashmap_iter iter;
3628 struct strmap_entry *e;
66b209b8
EN
3629 struct index_state *index = opt->repo->index;
3630 struct checkout state = CHECKOUT_INIT;
ef2b3693
EN
3631 int errs = 0;
3632 int original_cache_nr;
3633
66b209b8 3634 if (strmap_empty(&opt->priv->conflicted))
9fefce68
EN
3635 return 0;
3636
66b209b8
EN
3637 /* If any entries have skip_worktree set, we'll have to check 'em out */
3638 state.force = 1;
3639 state.quiet = 1;
3640 state.refresh_cache = 1;
3641 state.istate = index;
ef2b3693
EN
3642 original_cache_nr = index->cache_nr;
3643
3644 /* Put every entry from paths into plist, then sort */
66b209b8 3645 strmap_for_each_entry(&opt->priv->conflicted, &iter, e) {
ef2b3693
EN
3646 const char *path = e->key;
3647 struct conflict_info *ci = e->value;
3648 int pos;
3649 struct cache_entry *ce;
3650 int i;
3651
3652 VERIFY_CI(ci);
3653
3654 /*
3655 * The index will already have a stage=0 entry for this path,
3656 * because we created an as-merged-as-possible version of the
3657 * file and checkout() moved the working copy and index over
3658 * to that version.
3659 *
3660 * However, previous iterations through this loop will have
3661 * added unstaged entries to the end of the cache which
3662 * ignore the standard alphabetical ordering of cache
3663 * entries and break invariants needed for index_name_pos()
3664 * to work. However, we know the entry we want is before
3665 * those appended cache entries, so do a temporary swap on
3666 * cache_nr to only look through entries of interest.
3667 */
3668 SWAP(index->cache_nr, original_cache_nr);
3669 pos = index_name_pos(index, path, strlen(path));
3670 SWAP(index->cache_nr, original_cache_nr);
3671 if (pos < 0) {
3672 if (ci->filemask != 1)
3673 BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path);
3674 cache_tree_invalidate_path(index, path);
3675 } else {
3676 ce = index->cache[pos];
3677
3678 /*
3679 * Clean paths with CE_SKIP_WORKTREE set will not be
3680 * written to the working tree by the unpack_trees()
3681 * call in checkout(). Our conflicted entries would
3682 * have appeared clean to that code since we ignored
3683 * the higher order stages. Thus, we need override
3684 * the CE_SKIP_WORKTREE bit and manually write those
3685 * files to the working disk here.
ef2b3693 3686 */
66b209b8
EN
3687 if (ce_skip_worktree(ce)) {
3688 struct stat st;
3689
3690 if (!lstat(path, &st)) {
3691 char *new_name = unique_path(&opt->priv->paths,
3692 path,
3693 "cruft");
3694
3695 path_msg(opt, path, 1,
3696 _("Note: %s not up to date and in way of checking out conflicted version; old copy renamed to %s"),
3697 path, new_name);
3698 errs |= rename(path, new_name);
3699 free(new_name);
3700 }
3701 errs |= checkout_entry(ce, &state, NULL, NULL);
3702 }
ef2b3693
EN
3703
3704 /*
3705 * Mark this cache entry for removal and instead add
3706 * new stage>0 entries corresponding to the
3707 * conflicts. If there are many conflicted entries, we
3708 * want to avoid memmove'ing O(NM) entries by
3709 * inserting the new entries one at a time. So,
3710 * instead, we just add the new cache entries to the
3711 * end (ignoring normal index requirements on sort
3712 * order) and sort the index once we're all done.
3713 */
3714 ce->ce_flags |= CE_REMOVE;
3715 }
3716
3717 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
3718 struct version_info *vi;
3719 if (!(ci->filemask & (1ul << i)))
3720 continue;
3721 vi = &ci->stages[i];
3722 ce = make_cache_entry(index, vi->mode, &vi->oid,
3723 path, i+1, 0);
3724 add_index_entry(index, ce, ADD_CACHE_JUST_APPEND);
3725 }
3726 }
3727
3728 /*
3729 * Remove the unused cache entries (and invalidate the relevant
3730 * cache-trees), then sort the index entries to get the conflicted
3731 * entries we added to the end into their right locations.
3732 */
3733 remove_marked_cache_entries(index, 1);
72b30910
EN
3734 /*
3735 * No need for STABLE_QSORT -- cmp_cache_name_compare sorts primarily
3736 * on filename and secondarily on stage, and (name, stage #) are a
3737 * unique tuple.
3738 */
ef2b3693
EN
3739 QSORT(index->cache, index->cache_nr, cmp_cache_name_compare);
3740
3741 return errs;
9fefce68
EN
3742}
3743
17e5574b
EN
3744void merge_switch_to_result(struct merge_options *opt,
3745 struct tree *head,
3746 struct merge_result *result,
3747 int update_worktree_and_index,
3748 int display_update_msgs)
3749{
9fefce68
EN
3750 assert(opt->priv == NULL);
3751 if (result->clean >= 0 && update_worktree_and_index) {
5291828d
EN
3752 const char *filename;
3753 FILE *fp;
3754
557ac035 3755 trace2_region_enter("merge", "checkout", opt->repo);
9fefce68
EN
3756 if (checkout(opt, head, result->tree)) {
3757 /* failure to function */
3758 result->clean = -1;
3759 return;
3760 }
557ac035 3761 trace2_region_leave("merge", "checkout", opt->repo);
9fefce68 3762
557ac035 3763 trace2_region_enter("merge", "record_conflicted", opt->repo);
66b209b8
EN
3764 opt->priv = result->priv;
3765 if (record_conflicted_index_entries(opt)) {
9fefce68 3766 /* failure to function */
66b209b8 3767 opt->priv = NULL;
9fefce68
EN
3768 result->clean = -1;
3769 return;
3770 }
66b209b8 3771 opt->priv = NULL;
557ac035 3772 trace2_region_leave("merge", "record_conflicted", opt->repo);
5291828d
EN
3773
3774 trace2_region_enter("merge", "write_auto_merge", opt->repo);
3775 filename = git_path_auto_merge(opt->repo);
3776 fp = xfopen(filename, "w");
3777 fprintf(fp, "%s\n", oid_to_hex(&result->tree->object.oid));
3778 fclose(fp);
3779 trace2_region_leave("merge", "write_auto_merge", opt->repo);
9fefce68
EN
3780 }
3781
3782 if (display_update_msgs) {
c5a6f655
EN
3783 struct merge_options_internal *opti = result->priv;
3784 struct hashmap_iter iter;
3785 struct strmap_entry *e;
3786 struct string_list olist = STRING_LIST_INIT_NODUP;
3787 int i;
3788
557ac035
EN
3789 trace2_region_enter("merge", "display messages", opt->repo);
3790
c5a6f655
EN
3791 /* Hack to pre-allocate olist to the desired size */
3792 ALLOC_GROW(olist.items, strmap_get_size(&opti->output),
3793 olist.alloc);
3794
3795 /* Put every entry from output into olist, then sort */
3796 strmap_for_each_entry(&opti->output, &iter, e) {
3797 string_list_append(&olist, e->key)->util = e->value;
3798 }
3799 string_list_sort(&olist);
3800
3801 /* Iterate over the items, printing them */
3802 for (i = 0; i < olist.nr; ++i) {
3803 struct strbuf *sb = olist.items[i].util;
3804
3805 printf("%s", sb->buf);
3806 }
3807 string_list_clear(&olist, 0);
f39d05ca
EN
3808
3809 /* Also include needed rename limit adjustment now */
3810 diff_warn_rename_limit("merge.renamelimit",
3811 opti->renames.needed_limit, 0);
557ac035
EN
3812
3813 trace2_region_leave("merge", "display messages", opt->repo);
9fefce68
EN
3814 }
3815
17e5574b
EN
3816 merge_finalize(opt, result);
3817}
3818
3819void merge_finalize(struct merge_options *opt,
3820 struct merge_result *result)
3821{
89422d29
EN
3822 struct merge_options_internal *opti = result->priv;
3823
ea305a68
EN
3824 if (opt->renormalize)
3825 git_attr_set_direction(GIT_ATTR_CHECKIN);
89422d29
EN
3826 assert(opt->priv == NULL);
3827
43e9c4ee 3828 clear_or_reinit_internal_opts(opti, 0);
89422d29 3829 FREE_AND_NULL(opti);
17e5574b
EN
3830}
3831
04af1879
EN
3832/*** Function Grouping: helper functions for merge_incore_*() ***/
3833
3639dfb3
EN
3834static struct tree *shift_tree_object(struct repository *repo,
3835 struct tree *one, struct tree *two,
3836 const char *subtree_shift)
3837{
3838 struct object_id shifted;
3839
3840 if (!*subtree_shift) {
3841 shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);
3842 } else {
3843 shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,
3844 subtree_shift);
3845 }
3846 if (oideq(&two->object.oid, &shifted))
3847 return two;
3848 return lookup_tree(repo, &shifted);
3849}
3850
4296d8f1
EN
3851static inline void set_commit_tree(struct commit *c, struct tree *t)
3852{
3853 c->maybe_tree = t;
3854}
3855
4296d8f1
EN
3856static struct commit *make_virtual_commit(struct repository *repo,
3857 struct tree *tree,
3858 const char *comment)
3859{
3860 struct commit *commit = alloc_commit_node(repo);
3861
3862 set_merge_remote_desc(commit, comment, (struct object *)commit);
3863 set_commit_tree(commit, tree);
3864 commit->object.parsed = 1;
3865 return commit;
3866}
3867
231e2dd4
EN
3868static void merge_start(struct merge_options *opt, struct merge_result *result)
3869{
f5d9fbc2
EN
3870 struct rename_info *renames;
3871 int i;
3872
e4171b1b 3873 /* Sanity checks on opt */
557ac035 3874 trace2_region_enter("merge", "sanity checks", opt->repo);
e4171b1b
EN
3875 assert(opt->repo);
3876
3877 assert(opt->branch1 && opt->branch2);
3878
3879 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3880 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3881 assert(opt->rename_limit >= -1);
3882 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3883 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3884
3885 assert(opt->xdl_opts >= 0);
3886 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3887 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3888
3889 /*
3890 * detect_renames, verbosity, buffer_output, and obuf are ignored
3891 * fields that were used by "recursive" rather than "ort" -- but
3892 * sanity check them anyway.
3893 */
3894 assert(opt->detect_renames >= -1 &&
3895 opt->detect_renames <= DIFF_DETECT_COPY);
3896 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3897 assert(opt->buffer_output <= 2);
3898 assert(opt->obuf.len == 0);
3899
3900 assert(opt->priv == NULL);
19ceb486
EN
3901 if (result->_properly_initialized != 0 &&
3902 result->_properly_initialized != RESULT_INITIALIZED)
3903 BUG("struct merge_result passed to merge_incore_*recursive() must be zeroed or filled with values from a previous run");
3904 assert(!!result->priv == !!result->_properly_initialized);
cf8937ac
EN
3905 if (result->priv) {
3906 opt->priv = result->priv;
3907 result->priv = NULL;
3908 /*
3909 * opt->priv non-NULL means we had results from a previous
3910 * run; do a few sanity checks that user didn't mess with
3911 * it in an obvious fashion.
3912 */
3913 assert(opt->priv->call_depth == 0);
3914 assert(!opt->priv->toplevel_dir ||
3915 0 == strlen(opt->priv->toplevel_dir));
3916 }
557ac035 3917 trace2_region_leave("merge", "sanity checks", opt->repo);
e4171b1b 3918
c8017176
EN
3919 /* Default to histogram diff. Actually, just hardcode it...for now. */
3920 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
3921
ea305a68
EN
3922 /* Handle attr direction stuff for renormalization */
3923 if (opt->renormalize)
3924 git_attr_set_direction(GIT_ATTR_CHECKOUT);
3925
e4171b1b 3926 /* Initialization of opt->priv, our internal merge data */
557ac035 3927 trace2_region_enter("merge", "allocate/init", opt->repo);
cf8937ac
EN
3928 if (opt->priv) {
3929 clear_or_reinit_internal_opts(opt->priv, 1);
3930 trace2_region_leave("merge", "allocate/init", opt->repo);
3931 return;
3932 }
e4171b1b
EN
3933 opt->priv = xcalloc(1, sizeof(*opt->priv));
3934
f5d9fbc2
EN
3935 /* Initialization of various renames fields */
3936 renames = &opt->priv->renames;
3937 for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) {
a49b55d5 3938 strintmap_init_with_options(&renames->dirs_removed[i],
fb52938e 3939 NOT_RELEVANT, NULL, 0);
f5d9fbc2
EN
3940 strmap_init_with_options(&renames->dir_rename_count[i],
3941 NULL, 1);
3942 strmap_init_with_options(&renames->dir_renames[i],
3943 NULL, 0);
2734f2e3
EN
3944 /*
3945 * relevant_sources uses -1 for the default, because we need
3946 * to be able to distinguish not-in-strintmap from valid
3947 * relevant_source values from enum file_rename_relevance.
3948 * In particular, possibly_cache_new_pair() expects a negative
3949 * value for not-found entries.
3950 */
a49b55d5 3951 strintmap_init_with_options(&renames->relevant_sources[i],
2734f2e3
EN
3952 -1 /* explicitly invalid */,
3953 NULL, 0);
d29bd6d7
EN
3954 strmap_init_with_options(&renames->cached_pairs[i],
3955 NULL, 1);
3956 strset_init_with_options(&renames->cached_irrelevant[i],
3957 NULL, 1);
3958 strset_init_with_options(&renames->cached_target_names[i],
3959 NULL, 0);
f5d9fbc2
EN
3960 }
3961
e4171b1b
EN
3962 /*
3963 * Although we initialize opt->priv->paths with strdup_strings=0,
3964 * that's just to avoid making yet another copy of an allocated
3965 * string. Putting the entry into paths means we are taking
43c1dccb 3966 * ownership, so we will later free it. paths_to_free is similar.
e4171b1b
EN
3967 *
3968 * In contrast, conflicted just has a subset of keys from paths, so
3969 * we don't want to free those (it'd be a duplicate free).
3970 */
3971 strmap_init_with_options(&opt->priv->paths, NULL, 0);
3972 strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
43c1dccb 3973 string_list_init(&opt->priv->paths_to_free, 0);
c5a6f655
EN
3974
3975 /*
3976 * keys & strbufs in output will sometimes need to outlive "paths",
3977 * so it will have a copy of relevant keys. It's probably a small
3978 * subset of the overall paths that have special output.
3979 */
3980 strmap_init(&opt->priv->output);
557ac035
EN
3981
3982 trace2_region_leave("merge", "allocate/init", opt->repo);
231e2dd4
EN
3983}
3984
64aceb6d
EN
3985static void merge_check_renames_reusable(struct merge_options *opt,
3986 struct merge_result *result,
3987 struct tree *merge_base,
3988 struct tree *side1,
3989 struct tree *side2)
3990{
3991 struct rename_info *renames;
3992 struct tree **merge_trees;
3993 struct merge_options_internal *opti = result->priv;
3994
3995 if (!opti)
3996 return;
3997
3998 renames = &opti->renames;
3999 merge_trees = renames->merge_trees;
cbdca289
EN
4000
4001 /*
4002 * Handle case where previous merge operation did not want cache to
4003 * take effect, e.g. because rename/rename(1to1) makes it invalid.
4004 */
4005 if (!merge_trees[0]) {
4006 assert(!merge_trees[0] && !merge_trees[1] && !merge_trees[2]);
4007 renames->cached_pairs_valid_side = 0; /* neither side valid */
4008 return;
4009 }
4010
4011 /*
4012 * Handle other cases; note that merge_trees[0..2] will only
4013 * be NULL if opti is, or if all three were manually set to
4014 * NULL by e.g. rename/rename(1to1) handling.
4015 */
64aceb6d
EN
4016 assert(merge_trees[0] && merge_trees[1] && merge_trees[2]);
4017
4018 /* Check if we meet a condition for re-using cached_pairs */
4019 if (oideq(&merge_base->object.oid, &merge_trees[2]->object.oid) &&
4020 oideq(&side1->object.oid, &result->tree->object.oid))
4021 renames->cached_pairs_valid_side = MERGE_SIDE1;
4022 else if (oideq(&merge_base->object.oid, &merge_trees[1]->object.oid) &&
4023 oideq(&side2->object.oid, &result->tree->object.oid))
4024 renames->cached_pairs_valid_side = MERGE_SIDE2;
4025 else
4026 renames->cached_pairs_valid_side = 0; /* neither side valid */
4027}
4028
04af1879
EN
4029/*** Function Grouping: merge_incore_*() and their internal variants ***/
4030
231e2dd4
EN
4031/*
4032 * Originally from merge_trees_internal(); heavily adapted, though.
4033 */
4034static void merge_ort_nonrecursive_internal(struct merge_options *opt,
4035 struct tree *merge_base,
4036 struct tree *side1,
4037 struct tree *side2,
4038 struct merge_result *result)
4039{
4040 struct object_id working_tree_oid;
4041
3639dfb3
EN
4042 if (opt->subtree_shift) {
4043 side2 = shift_tree_object(opt->repo, side1, side2,
4044 opt->subtree_shift);
4045 merge_base = shift_tree_object(opt->repo, side1, merge_base,
4046 opt->subtree_shift);
4047 }
4048
557ac035 4049 trace2_region_enter("merge", "collect_merge_info", opt->repo);
0c0d705b
EN
4050 if (collect_merge_info(opt, merge_base, side1, side2) != 0) {
4051 /*
4052 * TRANSLATORS: The %s arguments are: 1) tree hash of a merge
4053 * base, and 2-3) the trees for the two trees we're merging.
4054 */
4055 err(opt, _("collecting merge info failed for trees %s, %s, %s"),
4056 oid_to_hex(&merge_base->object.oid),
4057 oid_to_hex(&side1->object.oid),
4058 oid_to_hex(&side2->object.oid));
4059 result->clean = -1;
4060 return;
4061 }
557ac035 4062 trace2_region_leave("merge", "collect_merge_info", opt->repo);
0c0d705b 4063
557ac035 4064 trace2_region_enter("merge", "renames", opt->repo);
231e2dd4
EN
4065 result->clean = detect_and_process_renames(opt, merge_base,
4066 side1, side2);
557ac035
EN
4067 trace2_region_leave("merge", "renames", opt->repo);
4068
4069 trace2_region_enter("merge", "process_entries", opt->repo);
231e2dd4 4070 process_entries(opt, &working_tree_oid);
557ac035 4071 trace2_region_leave("merge", "process_entries", opt->repo);
231e2dd4
EN
4072
4073 /* Set return values */
4074 result->tree = parse_tree_indirect(&working_tree_oid);
4075 /* existence of conflicted entries implies unclean */
4076 result->clean &= strmap_empty(&opt->priv->conflicted);
4077 if (!opt->priv->call_depth) {
4078 result->priv = opt->priv;
19ceb486 4079 result->_properly_initialized = RESULT_INITIALIZED;
231e2dd4
EN
4080 opt->priv = NULL;
4081 }
4082}
4083
8119214f
EN
4084/*
4085 * Originally from merge_recursive_internal(); somewhat adapted, though.
4086 */
4087static void merge_ort_internal(struct merge_options *opt,
4088 struct commit_list *merge_bases,
4089 struct commit *h1,
4090 struct commit *h2,
4091 struct merge_result *result)
4092{
4093 struct commit_list *iter;
4094 struct commit *merged_merge_bases;
4095 const char *ancestor_name;
4096 struct strbuf merge_base_abbrev = STRBUF_INIT;
4097
4098 if (!merge_bases) {
4099 merge_bases = get_merge_bases(h1, h2);
4100 /* See merge-ort.h:merge_incore_recursive() declaration NOTE */
4101 merge_bases = reverse_commit_list(merge_bases);
4102 }
4103
4104 merged_merge_bases = pop_commit(&merge_bases);
4105 if (merged_merge_bases == NULL) {
4106 /* if there is no common ancestor, use an empty tree */
4107 struct tree *tree;
4108
4109 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
4110 merged_merge_bases = make_virtual_commit(opt->repo, tree,
4111 "ancestor");
4112 ancestor_name = "empty tree";
4113 } else if (merge_bases) {
4114 ancestor_name = "merged common ancestors";
4115 } else {
4116 strbuf_add_unique_abbrev(&merge_base_abbrev,
4117 &merged_merge_bases->object.oid,
4118 DEFAULT_ABBREV);
4119 ancestor_name = merge_base_abbrev.buf;
4120 }
4121
4122 for (iter = merge_bases; iter; iter = iter->next) {
4123 const char *saved_b1, *saved_b2;
4124 struct commit *prev = merged_merge_bases;
4125
4126 opt->priv->call_depth++;
4127 /*
4128 * When the merge fails, the result contains files
4129 * with conflict markers. The cleanness flag is
4130 * ignored (unless indicating an error), it was never
4131 * actually used, as result of merge_trees has always
4132 * overwritten it: the committed "conflicts" were
4133 * already resolved.
4134 */
4135 saved_b1 = opt->branch1;
4136 saved_b2 = opt->branch2;
4137 opt->branch1 = "Temporary merge branch 1";
4138 opt->branch2 = "Temporary merge branch 2";
4139 merge_ort_internal(opt, NULL, prev, iter->item, result);
4140 if (result->clean < 0)
4141 return;
4142 opt->branch1 = saved_b1;
4143 opt->branch2 = saved_b2;
4144 opt->priv->call_depth--;
4145
4146 merged_merge_bases = make_virtual_commit(opt->repo,
4147 result->tree,
4148 "merged tree");
4149 commit_list_insert(prev, &merged_merge_bases->parents);
4150 commit_list_insert(iter->item,
4151 &merged_merge_bases->parents->next);
4152
4153 clear_or_reinit_internal_opts(opt->priv, 1);
4154 }
4155
4156 opt->ancestor = ancestor_name;
4157 merge_ort_nonrecursive_internal(opt,
4158 repo_get_commit_tree(opt->repo,
4159 merged_merge_bases),
4160 repo_get_commit_tree(opt->repo, h1),
4161 repo_get_commit_tree(opt->repo, h2),
4162 result);
4163 strbuf_release(&merge_base_abbrev);
4164 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
4165}
4166
17e5574b
EN
4167void merge_incore_nonrecursive(struct merge_options *opt,
4168 struct tree *merge_base,
4169 struct tree *side1,
4170 struct tree *side2,
4171 struct merge_result *result)
4172{
557ac035
EN
4173 trace2_region_enter("merge", "incore_nonrecursive", opt->repo);
4174
4175 trace2_region_enter("merge", "merge_start", opt->repo);
231e2dd4 4176 assert(opt->ancestor != NULL);
64aceb6d 4177 merge_check_renames_reusable(opt, result, merge_base, side1, side2);
231e2dd4 4178 merge_start(opt, result);
64aceb6d
EN
4179 /*
4180 * Record the trees used in this merge, so if there's a next merge in
4181 * a cherry-pick or rebase sequence it might be able to take advantage
4182 * of the cached_pairs in that next merge.
4183 */
4184 opt->priv->renames.merge_trees[0] = merge_base;
4185 opt->priv->renames.merge_trees[1] = side1;
4186 opt->priv->renames.merge_trees[2] = side2;
557ac035
EN
4187 trace2_region_leave("merge", "merge_start", opt->repo);
4188
231e2dd4 4189 merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
557ac035 4190 trace2_region_leave("merge", "incore_nonrecursive", opt->repo);
17e5574b
EN
4191}
4192
4193void merge_incore_recursive(struct merge_options *opt,
4194 struct commit_list *merge_bases,
4195 struct commit *side1,
4196 struct commit *side2,
4197 struct merge_result *result)
4198{
557ac035
EN
4199 trace2_region_enter("merge", "incore_recursive", opt->repo);
4200
8119214f
EN
4201 /* We set the ancestor label based on the merge_bases */
4202 assert(opt->ancestor == NULL);
4203
557ac035 4204 trace2_region_enter("merge", "merge_start", opt->repo);
8119214f 4205 merge_start(opt, result);
557ac035
EN
4206 trace2_region_leave("merge", "merge_start", opt->repo);
4207
8119214f 4208 merge_ort_internal(opt, merge_bases, side1, side2, result);
557ac035 4209 trace2_region_leave("merge", "incore_recursive", opt->repo);
17e5574b 4210}