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
6 * git merge [-s recursive]
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"?)
18 #include "merge-ort.h"
23 #include "cache-tree.h"
25 #include "commit-reach.h"
30 #include "object-store.h"
33 #include "submodule.h"
35 #include "unpack-trees.h"
36 #include "xdiff-interface.h"
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.
46 * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
47 * referred to there is one of these three sides.
55 struct traversal_callback_data
{
57 unsigned long dirmask
;
58 struct name_entry names
[3];
63 * All variables that are arrays of size 3 correspond to data tracked
64 * for the sides in enum merge_side. Index 0 is almost always unused
65 * because we often only need to track information for MERGE_SIDE1 and
66 * MERGE_SIDE2 (MERGE_BASE can't have rename information since renames
67 * are determined relative to what changed since the MERGE_BASE).
71 * pairs: pairing of filenames from diffcore_rename()
73 struct diff_queue_struct pairs
[3];
76 * dirs_removed: directories removed on a given side of history.
78 * The keys of dirs_removed[side] are the directories that were removed
79 * on the given side of history. The value of the strintmap for each
80 * directory is a value from enum dir_rename_relevance.
82 struct strintmap dirs_removed
[3];
85 * dir_rename_count: tracking where parts of a directory were renamed to
87 * When files in a directory are renamed, they may not all go to the
88 * same location. Each strmap here tracks:
89 * old_dir => {new_dir => int}
90 * That is, dir_rename_count[side] is a strmap to a strintmap.
92 struct strmap dir_rename_count
[3];
95 * dir_renames: computed directory renames
97 * This is a map of old_dir => new_dir and is derived in part from
100 struct strmap dir_renames
[3];
103 * relevant_sources: deleted paths wanted in rename detection, and why
105 * relevant_sources is a set of deleted paths on each side of
106 * history for which we need rename detection. If a path is deleted
107 * on one side of history, we need to detect if it is part of a
109 * * the file is modified/deleted on the other side of history
110 * * we need to detect renames for an ancestor directory
111 * If neither of those are true, we can skip rename detection for
112 * that path. The reason is stored as a value from enum
113 * file_rename_relevance, as the reason can inform the algorithm in
114 * diffcore_rename_extended().
116 struct strintmap relevant_sources
[3];
120 * 0: optimization removing unmodified potential rename source okay
121 * 2 or 4: optimization okay, but must check for files added to dir
122 * 7: optimization forbidden; need rename source in case of dir rename
124 unsigned dir_rename_mask
:3;
127 * callback_data_*: supporting data structures for alternate traversal
129 * We sometimes need to be able to traverse through all the files
130 * in a given tree before all immediate subdirectories within that
131 * tree. Since traverse_trees() doesn't do that naturally, we have
132 * a traverse_trees_wrapper() that stores any immediate
133 * subdirectories while traversing files, then traverses the
134 * immediate subdirectories later. These callback_data* variables
135 * store the information for the subdirectories so that we can do
136 * that traversal order.
138 struct traversal_callback_data
*callback_data
;
139 int callback_data_nr
, callback_data_alloc
;
140 char *callback_data_traverse_path
;
143 * needed_limit: value needed for inexact rename detection to run
145 * If the current rename limit wasn't high enough for inexact
146 * rename detection to run, this records the limit needed. Otherwise,
147 * this value remains 0.
152 struct merge_options_internal
{
154 * paths: primary data structure in all of merge ort.
157 * * are full relative paths from the toplevel of the repository
158 * (e.g. "drivers/firmware/raspberrypi.c").
159 * * store all relevant paths in the repo, both directories and
160 * files (e.g. drivers, drivers/firmware would also be included)
161 * * these keys serve to intern all the path strings, which allows
162 * us to do pointer comparison on directory names instead of
163 * strcmp; we just have to be careful to use the interned strings.
164 * (Technically paths_to_free may track some strings that were
165 * removed from froms paths.)
167 * The values of paths:
168 * * either a pointer to a merged_info, or a conflict_info struct
169 * * merged_info contains all relevant information for a
170 * non-conflicted entry.
171 * * conflict_info contains a merged_info, plus any additional
172 * information about a conflict such as the higher orders stages
173 * involved and the names of the paths those came from (handy
174 * once renames get involved).
175 * * a path may start "conflicted" (i.e. point to a conflict_info)
176 * and then a later step (e.g. three-way content merge) determines
177 * it can be cleanly merged, at which point it'll be marked clean
178 * and the algorithm will ignore any data outside the contained
179 * merged_info for that entry
180 * * If an entry remains conflicted, the merged_info portion of a
181 * conflict_info will later be filled with whatever version of
182 * the file should be placed in the working directory (e.g. an
183 * as-merged-as-possible variation that contains conflict markers).
188 * conflicted: a subset of keys->values from "paths"
190 * conflicted is basically an optimization between process_entries()
191 * and record_conflicted_index_entries(); the latter could loop over
192 * ALL the entries in paths AGAIN and look for the ones that are
193 * still conflicted, but since process_entries() has to loop over
194 * all of them, it saves the ones it couldn't resolve in this strmap
195 * so that record_conflicted_index_entries() can iterate just the
198 struct strmap conflicted
;
201 * paths_to_free: additional list of strings to free
203 * If keys are removed from "paths", they are added to paths_to_free
204 * to ensure they are later freed. We avoid free'ing immediately since
205 * other places (e.g. conflict_info.pathnames[]) may still be
206 * referencing these paths.
208 struct string_list paths_to_free
;
211 * output: special messages and conflict notices for various paths
213 * This is a map of pathnames (a subset of the keys in "paths" above)
214 * to strbufs. It gathers various warning/conflict/notice messages
215 * for later processing.
217 struct strmap output
;
220 * renames: various data relating to rename detection
222 struct rename_info renames
;
225 * attr_index: hacky minimal index used for renormalization
227 * renormalization code _requires_ an index, though it only needs to
228 * find a .gitattributes file within the index. So, when
229 * renormalization is important, we create a special index with just
232 struct index_state attr_index
;
235 * current_dir_name, toplevel_dir: temporary vars
237 * These are used in collect_merge_info_callback(), and will set the
238 * various merged_info.directory_name for the various paths we get;
239 * see documentation for that variable and the requirements placed on
242 const char *current_dir_name
;
243 const char *toplevel_dir
;
245 /* call_depth: recursion level counter for merging merge bases */
249 struct version_info
{
250 struct object_id oid
;
255 /* if is_null, ignore result. otherwise result has oid & mode */
256 struct version_info result
;
260 * clean: whether the path in question is cleanly merged.
262 * see conflict_info.merged for more details.
267 * basename_offset: offset of basename of path.
269 * perf optimization to avoid recomputing offset of final '/'
270 * character in pathname (0 if no '/' in pathname).
272 size_t basename_offset
;
275 * directory_name: containing directory name.
277 * Note that we assume directory_name is constructed such that
278 * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
279 * i.e. string equality is equivalent to pointer equality. For this
280 * to hold, we have to be careful setting directory_name.
282 const char *directory_name
;
285 struct conflict_info
{
287 * merged: the version of the path that will be written to working tree
289 * WARNING: It is critical to check merged.clean and ensure it is 0
290 * before reading any conflict_info fields outside of merged.
291 * Allocated merge_info structs will always have clean set to 1.
292 * Allocated conflict_info structs will have merged.clean set to 0
293 * initially. The merged.clean field is how we know if it is safe
294 * to access other parts of conflict_info besides merged; if a
295 * conflict_info's merged.clean is changed to 1, the rest of the
296 * algorithm is not allowed to look at anything outside of the
297 * merged member anymore.
299 struct merged_info merged
;
301 /* oids & modes from each of the three trees for this path */
302 struct version_info stages
[3];
304 /* pathnames for each stage; may differ due to rename detection */
305 const char *pathnames
[3];
307 /* Whether this path is/was involved in a directory/file conflict */
308 unsigned df_conflict
:1;
311 * Whether this path is/was involved in a non-content conflict other
312 * than a directory/file conflict (e.g. rename/rename, rename/delete,
313 * file location based on possible directory rename).
315 unsigned path_conflict
:1;
318 * For filemask and dirmask, the ith bit corresponds to whether the
319 * ith entry is a file (filemask) or a directory (dirmask). Thus,
320 * filemask & dirmask is always zero, and filemask | dirmask is at
321 * most 7 but can be less when a path does not appear as either a
322 * file or a directory on at least one side of history.
324 * Note that these masks are related to enum merge_side, as the ith
325 * entry corresponds to side i.
327 * These values come from a traverse_trees() call; more info may be
328 * found looking at tree-walk.h's struct traverse_info,
329 * particularly the documentation above the "fn" member (note that
330 * filemask = mask & ~dirmask from that documentation).
336 * Optimization to track which stages match, to avoid the need to
337 * recompute it in multiple steps. Either 0 or at least 2 bits are
338 * set; if at least 2 bits are set, their corresponding stages match.
340 unsigned match_mask
:3;
343 /*** Function Grouping: various utility functions ***/
346 * For the next three macros, see warning for conflict_info.merged.
348 * In each of the below, mi is a struct merged_info*, and ci was defined
349 * as a struct conflict_info* (but we need to verify ci isn't actually
350 * pointed at a struct merged_info*).
352 * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
353 * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
354 * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
356 #define INITIALIZE_CI(ci, mi) do { \
357 (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
359 #define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
360 #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \
361 (ci) = (struct conflict_info *)(mi); \
362 assert((ci) && !(mi)->clean); \
365 static void free_strmap_strings(struct strmap
*map
)
367 struct hashmap_iter iter
;
368 struct strmap_entry
*entry
;
370 strmap_for_each_entry(map
, &iter
, entry
) {
371 free((char*)entry
->key
);
375 static void clear_or_reinit_internal_opts(struct merge_options_internal
*opti
,
378 struct rename_info
*renames
= &opti
->renames
;
380 void (*strmap_func
)(struct strmap
*, int) =
381 reinitialize
? strmap_partial_clear
: strmap_clear
;
382 void (*strintmap_func
)(struct strintmap
*) =
383 reinitialize
? strintmap_partial_clear
: strintmap_clear
;
386 * We marked opti->paths with strdup_strings = 0, so that we
387 * wouldn't have to make another copy of the fullpath created by
388 * make_traverse_path from setup_path_info(). But, now that we've
389 * used it and have no other references to these strings, it is time
390 * to deallocate them.
392 free_strmap_strings(&opti
->paths
);
393 strmap_func(&opti
->paths
, 1);
396 * All keys and values in opti->conflicted are a subset of those in
397 * opti->paths. We don't want to deallocate anything twice, so we
398 * don't free the keys and we pass 0 for free_values.
400 strmap_func(&opti
->conflicted
, 0);
403 * opti->paths_to_free is similar to opti->paths; we created it with
404 * strdup_strings = 0 to avoid making _another_ copy of the fullpath
405 * but now that we've used it and have no other references to these
406 * strings, it is time to deallocate them. We do so by temporarily
407 * setting strdup_strings to 1.
409 opti
->paths_to_free
.strdup_strings
= 1;
410 string_list_clear(&opti
->paths_to_free
, 0);
411 opti
->paths_to_free
.strdup_strings
= 0;
413 if (opti
->attr_index
.cache_nr
)
414 discard_index(&opti
->attr_index
);
416 /* Free memory used by various renames maps */
417 for (i
= MERGE_SIDE1
; i
<= MERGE_SIDE2
; ++i
) {
418 strintmap_func(&renames
->dirs_removed
[i
]);
420 partial_clear_dir_rename_count(&renames
->dir_rename_count
[i
]);
422 strmap_clear(&renames
->dir_rename_count
[i
], 1);
424 strmap_func(&renames
->dir_renames
[i
], 0);
426 strintmap_func(&renames
->relevant_sources
[i
]);
430 struct hashmap_iter iter
;
431 struct strmap_entry
*e
;
433 /* Release and free each strbuf found in output */
434 strmap_for_each_entry(&opti
->output
, &iter
, e
) {
435 struct strbuf
*sb
= e
->value
;
438 * While strictly speaking we don't need to free(sb)
439 * here because we could pass free_values=1 when
440 * calling strmap_clear() on opti->output, that would
441 * require strmap_clear to do another
442 * strmap_for_each_entry() loop, so we just free it
443 * while we're iterating anyway.
447 strmap_clear(&opti
->output
, 0);
450 renames
->dir_rename_mask
= 0;
452 /* Clean out callback_data as well. */
453 FREE_AND_NULL(renames
->callback_data
);
454 renames
->callback_data_nr
= renames
->callback_data_alloc
= 0;
457 static int err(struct merge_options
*opt
, const char *err
, ...)
460 struct strbuf sb
= STRBUF_INIT
;
462 strbuf_addstr(&sb
, "error: ");
463 va_start(params
, err
);
464 strbuf_vaddf(&sb
, err
, params
);
473 static void format_commit(struct strbuf
*sb
,
475 struct commit
*commit
)
477 struct merge_remote_desc
*desc
;
478 struct pretty_print_context ctx
= {0};
479 ctx
.abbrev
= DEFAULT_ABBREV
;
481 strbuf_addchars(sb
, ' ', indent
);
482 desc
= merge_remote_util(commit
);
484 strbuf_addf(sb
, "virtual %s\n", desc
->name
);
488 format_commit_message(commit
, "%h %s", sb
, &ctx
);
489 strbuf_addch(sb
, '\n');
492 __attribute__((format (printf
, 4, 5)))
493 static void path_msg(struct merge_options
*opt
,
495 int omittable_hint
, /* skippable under --remerge-diff */
496 const char *fmt
, ...)
499 struct strbuf
*sb
= strmap_get(&opt
->priv
->output
, path
);
501 sb
= xmalloc(sizeof(*sb
));
503 strmap_put(&opt
->priv
->output
, path
, sb
);
507 strbuf_vaddf(sb
, fmt
, ap
);
510 strbuf_addch(sb
, '\n');
513 /* add a string to a strbuf, but converting "/" to "_" */
514 static void add_flattened_path(struct strbuf
*out
, const char *s
)
517 strbuf_addstr(out
, s
);
518 for (; i
< out
->len
; i
++)
519 if (out
->buf
[i
] == '/')
523 static char *unique_path(struct strmap
*existing_paths
,
527 struct strbuf newpath
= STRBUF_INIT
;
531 strbuf_addf(&newpath
, "%s~", path
);
532 add_flattened_path(&newpath
, branch
);
534 base_len
= newpath
.len
;
535 while (strmap_contains(existing_paths
, newpath
.buf
)) {
536 strbuf_setlen(&newpath
, base_len
);
537 strbuf_addf(&newpath
, "_%d", suffix
++);
540 return strbuf_detach(&newpath
, NULL
);
543 /*** Function Grouping: functions related to collect_merge_info() ***/
545 static int traverse_trees_wrapper_callback(int n
,
547 unsigned long dirmask
,
548 struct name_entry
*names
,
549 struct traverse_info
*info
)
551 struct merge_options
*opt
= info
->data
;
552 struct rename_info
*renames
= &opt
->priv
->renames
;
553 unsigned filemask
= mask
& ~dirmask
;
557 if (!renames
->callback_data_traverse_path
)
558 renames
->callback_data_traverse_path
= xstrdup(info
->traverse_path
);
560 if (filemask
&& filemask
== renames
->dir_rename_mask
)
561 renames
->dir_rename_mask
= 0x07;
563 ALLOC_GROW(renames
->callback_data
, renames
->callback_data_nr
+ 1,
564 renames
->callback_data_alloc
);
565 renames
->callback_data
[renames
->callback_data_nr
].mask
= mask
;
566 renames
->callback_data
[renames
->callback_data_nr
].dirmask
= dirmask
;
567 COPY_ARRAY(renames
->callback_data
[renames
->callback_data_nr
].names
,
569 renames
->callback_data_nr
++;
575 * Much like traverse_trees(), BUT:
576 * - read all the tree entries FIRST, saving them
577 * - note that the above step provides an opportunity to compute necessary
578 * additional details before the "real" traversal
579 * - loop through the saved entries and call the original callback on them
581 static int traverse_trees_wrapper(struct index_state
*istate
,
584 struct traverse_info
*info
)
586 int ret
, i
, old_offset
;
587 traverse_callback_t old_fn
;
588 char *old_callback_data_traverse_path
;
589 struct merge_options
*opt
= info
->data
;
590 struct rename_info
*renames
= &opt
->priv
->renames
;
592 assert(renames
->dir_rename_mask
== 2 || renames
->dir_rename_mask
== 4);
594 old_callback_data_traverse_path
= renames
->callback_data_traverse_path
;
596 old_offset
= renames
->callback_data_nr
;
598 renames
->callback_data_traverse_path
= NULL
;
599 info
->fn
= traverse_trees_wrapper_callback
;
600 ret
= traverse_trees(istate
, n
, t
, info
);
604 info
->traverse_path
= renames
->callback_data_traverse_path
;
606 for (i
= old_offset
; i
< renames
->callback_data_nr
; ++i
) {
608 renames
->callback_data
[i
].mask
,
609 renames
->callback_data
[i
].dirmask
,
610 renames
->callback_data
[i
].names
,
614 renames
->callback_data_nr
= old_offset
;
615 free(renames
->callback_data_traverse_path
);
616 renames
->callback_data_traverse_path
= old_callback_data_traverse_path
;
617 info
->traverse_path
= NULL
;
621 static void setup_path_info(struct merge_options
*opt
,
622 struct string_list_item
*result
,
623 const char *current_dir_name
,
624 int current_dir_name_len
,
625 char *fullpath
, /* we'll take over ownership */
626 struct name_entry
*names
,
627 struct name_entry
*merged_version
,
628 unsigned is_null
, /* boolean */
629 unsigned df_conflict
, /* boolean */
632 int resolved
/* boolean */)
634 /* result->util is void*, so mi is a convenience typed variable */
635 struct merged_info
*mi
;
637 assert(!is_null
|| resolved
);
638 assert(!df_conflict
|| !resolved
); /* df_conflict implies !resolved */
639 assert(resolved
== (merged_version
!= NULL
));
641 mi
= xcalloc(1, resolved
? sizeof(struct merged_info
) :
642 sizeof(struct conflict_info
));
643 mi
->directory_name
= current_dir_name
;
644 mi
->basename_offset
= current_dir_name_len
;
645 mi
->clean
= !!resolved
;
647 mi
->result
.mode
= merged_version
->mode
;
648 oidcpy(&mi
->result
.oid
, &merged_version
->oid
);
649 mi
->is_null
= !!is_null
;
652 struct conflict_info
*ci
;
654 ASSIGN_AND_VERIFY_CI(ci
, mi
);
655 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++) {
656 ci
->pathnames
[i
] = fullpath
;
657 ci
->stages
[i
].mode
= names
[i
].mode
;
658 oidcpy(&ci
->stages
[i
].oid
, &names
[i
].oid
);
660 ci
->filemask
= filemask
;
661 ci
->dirmask
= dirmask
;
662 ci
->df_conflict
= !!df_conflict
;
665 * Assume is_null for now, but if we have entries
666 * under the directory then when it is complete in
667 * write_completed_directory() it'll update this.
668 * Also, for D/F conflicts, we have to handle the
669 * directory first, then clear this bit and process
670 * the file to see how it is handled -- that occurs
671 * near the top of process_entry().
675 strmap_put(&opt
->priv
->paths
, fullpath
, mi
);
676 result
->string
= fullpath
;
680 static void add_pair(struct merge_options
*opt
,
681 struct name_entry
*names
,
682 const char *pathname
,
684 unsigned is_add
/* if false, is_delete */,
686 unsigned dir_rename_mask
)
688 struct diff_filespec
*one
, *two
;
689 struct rename_info
*renames
= &opt
->priv
->renames
;
690 int names_idx
= is_add
? side
: 0;
693 unsigned content_relevant
= (match_mask
== 0);
694 unsigned location_relevant
= (dir_rename_mask
== 0x07);
696 if (content_relevant
|| location_relevant
) {
697 /* content_relevant trumps location_relevant */
698 strintmap_set(&renames
->relevant_sources
[side
], pathname
,
699 content_relevant
? RELEVANT_CONTENT
: RELEVANT_LOCATION
);
703 one
= alloc_filespec(pathname
);
704 two
= alloc_filespec(pathname
);
705 fill_filespec(is_add
? two
: one
,
706 &names
[names_idx
].oid
, 1, names
[names_idx
].mode
);
707 diff_queue(&renames
->pairs
[side
], one
, two
);
710 static void collect_rename_info(struct merge_options
*opt
,
711 struct name_entry
*names
,
713 const char *fullname
,
718 struct rename_info
*renames
= &opt
->priv
->renames
;
722 * Update dir_rename_mask (determines ignore-rename-source validity)
724 * dir_rename_mask helps us keep track of when directory rename
725 * detection may be relevant. Basically, whenver a directory is
726 * removed on one side of history, and a file is added to that
727 * directory on the other side of history, directory rename
728 * detection is relevant (meaning we have to detect renames for all
729 * files within that directory to deduce where the directory
730 * moved). Also, whenever a directory needs directory rename
731 * detection, due to the "majority rules" choice for where to move
732 * it (see t6423 testcase 1f), we also need to detect renames for
733 * all files within subdirectories of that directory as well.
735 * Here we haven't looked at files within the directory yet, we are
736 * just looking at the directory itself. So, if we aren't yet in
737 * a case where a parent directory needed directory rename detection
738 * (i.e. dir_rename_mask != 0x07), and if the directory was removed
739 * on one side of history, record the mask of the other side of
740 * history in dir_rename_mask.
742 if (renames
->dir_rename_mask
!= 0x07 &&
743 (dirmask
== 3 || dirmask
== 5)) {
744 /* simple sanity check */
745 assert(renames
->dir_rename_mask
== 0 ||
746 renames
->dir_rename_mask
== (dirmask
& ~1));
747 /* update dir_rename_mask; have it record mask of new side */
748 renames
->dir_rename_mask
= (dirmask
& ~1);
751 /* Update dirs_removed, as needed */
752 if (dirmask
== 1 || dirmask
== 3 || dirmask
== 5) {
753 /* absent_mask = 0x07 - dirmask; sides = absent_mask/2 */
754 unsigned sides
= (0x07 - dirmask
)/2;
755 unsigned relevance
= (renames
->dir_rename_mask
== 0x07) ?
756 RELEVANT_FOR_ANCESTOR
: NOT_RELEVANT
;
758 * Record relevance of this directory. However, note that
759 * when collect_merge_info_callback() recurses into this
760 * directory and calls collect_rename_info() on paths
761 * within that directory, if we find a path that was added
762 * to this directory on the other side of history, we will
763 * upgrade this value to RELEVANT_FOR_SELF; see below.
766 strintmap_set(&renames
->dirs_removed
[1], fullname
,
769 strintmap_set(&renames
->dirs_removed
[2], fullname
,
774 * Here's the block that potentially upgrades to RELEVANT_FOR_SELF.
775 * When we run across a file added to a directory. In such a case,
776 * find the directory of the file and upgrade its relevance.
778 if (renames
->dir_rename_mask
== 0x07 &&
779 (filemask
== 2 || filemask
== 4)) {
781 * Need directory rename for parent directory on other side
782 * of history from added file. Thus
783 * side = (~filemask & 0x06) >> 1
785 * side = 3 - (filemask/2).
787 unsigned side
= 3 - (filemask
>> 1);
788 strintmap_set(&renames
->dirs_removed
[side
], dirname
,
792 if (filemask
== 0 || filemask
== 7)
795 for (side
= MERGE_SIDE1
; side
<= MERGE_SIDE2
; ++side
) {
796 unsigned side_mask
= (1 << side
);
798 /* Check for deletion on side */
799 if ((filemask
& 1) && !(filemask
& side_mask
))
800 add_pair(opt
, names
, fullname
, side
, 0 /* delete */,
801 match_mask
& filemask
,
802 renames
->dir_rename_mask
);
804 /* Check for addition on side */
805 if (!(filemask
& 1) && (filemask
& side_mask
))
806 add_pair(opt
, names
, fullname
, side
, 1 /* add */,
807 match_mask
& filemask
,
808 renames
->dir_rename_mask
);
812 static int collect_merge_info_callback(int n
,
814 unsigned long dirmask
,
815 struct name_entry
*names
,
816 struct traverse_info
*info
)
820 * common ancestor (mbase) has mask 1, and stored in index 0 of names
821 * head of side 1 (side1) has mask 2, and stored in index 1 of names
822 * head of side 2 (side2) has mask 4, and stored in index 2 of names
824 struct merge_options
*opt
= info
->data
;
825 struct merge_options_internal
*opti
= opt
->priv
;
826 struct rename_info
*renames
= &opt
->priv
->renames
;
827 struct string_list_item pi
; /* Path Info */
828 struct conflict_info
*ci
; /* typed alias to pi.util (which is void*) */
829 struct name_entry
*p
;
832 const char *dirname
= opti
->current_dir_name
;
833 unsigned prev_dir_rename_mask
= renames
->dir_rename_mask
;
834 unsigned filemask
= mask
& ~dirmask
;
835 unsigned match_mask
= 0; /* will be updated below */
836 unsigned mbase_null
= !(mask
& 1);
837 unsigned side1_null
= !(mask
& 2);
838 unsigned side2_null
= !(mask
& 4);
839 unsigned side1_matches_mbase
= (!side1_null
&& !mbase_null
&&
840 names
[0].mode
== names
[1].mode
&&
841 oideq(&names
[0].oid
, &names
[1].oid
));
842 unsigned side2_matches_mbase
= (!side2_null
&& !mbase_null
&&
843 names
[0].mode
== names
[2].mode
&&
844 oideq(&names
[0].oid
, &names
[2].oid
));
845 unsigned sides_match
= (!side1_null
&& !side2_null
&&
846 names
[1].mode
== names
[2].mode
&&
847 oideq(&names
[1].oid
, &names
[2].oid
));
850 * Note: When a path is a file on one side of history and a directory
851 * in another, we have a directory/file conflict. In such cases, if
852 * the conflict doesn't resolve from renames and deletions, then we
853 * always leave directories where they are and move files out of the
854 * way. Thus, while struct conflict_info has a df_conflict field to
855 * track such conflicts, we ignore that field for any directories at
856 * a path and only pay attention to it for files at the given path.
857 * The fact that we leave directories were they are also means that
858 * we do not need to worry about getting additional df_conflict
859 * information propagated from parent directories down to children
860 * (unlike, say traverse_trees_recursive() in unpack-trees.c, which
861 * sets a newinfo.df_conflicts field specifically to propagate it).
863 unsigned df_conflict
= (filemask
!= 0) && (dirmask
!= 0);
865 /* n = 3 is a fundamental assumption. */
867 BUG("Called collect_merge_info_callback wrong");
870 * A bunch of sanity checks verifying that traverse_trees() calls
871 * us the way I expect. Could just remove these at some point,
872 * though maybe they are helpful to future code readers.
874 assert(mbase_null
== is_null_oid(&names
[0].oid
));
875 assert(side1_null
== is_null_oid(&names
[1].oid
));
876 assert(side2_null
== is_null_oid(&names
[2].oid
));
877 assert(!mbase_null
|| !side1_null
|| !side2_null
);
878 assert(mask
> 0 && mask
< 8);
880 /* Determine match_mask */
881 if (side1_matches_mbase
)
882 match_mask
= (side2_matches_mbase
? 7 : 3);
883 else if (side2_matches_mbase
)
885 else if (sides_match
)
889 * Get the name of the relevant filepath, which we'll pass to
890 * setup_path_info() for tracking.
895 len
= traverse_path_len(info
, p
->pathlen
);
897 /* +1 in both of the following lines to include the NUL byte */
898 fullpath
= xmalloc(len
+ 1);
899 make_traverse_path(fullpath
, len
+ 1, info
, p
->path
, p
->pathlen
);
902 * If mbase, side1, and side2 all match, we can resolve early. Even
903 * if these are trees, there will be no renames or anything
906 if (side1_matches_mbase
&& side2_matches_mbase
) {
907 /* mbase, side1, & side2 all match; use mbase as resolution */
908 setup_path_info(opt
, &pi
, dirname
, info
->pathlen
, fullpath
,
909 names
, names
+0, mbase_null
, 0,
910 filemask
, dirmask
, 1);
915 * Gather additional information used in rename detection.
917 collect_rename_info(opt
, names
, dirname
, fullpath
,
918 filemask
, dirmask
, match_mask
);
921 * Record information about the path so we can resolve later in
924 setup_path_info(opt
, &pi
, dirname
, info
->pathlen
, fullpath
,
925 names
, NULL
, 0, df_conflict
, filemask
, dirmask
, 0);
929 ci
->match_mask
= match_mask
;
931 /* If dirmask, recurse into subdirectories */
933 struct traverse_info newinfo
;
934 struct tree_desc t
[3];
935 void *buf
[3] = {NULL
, NULL
, NULL
};
936 const char *original_dir_name
;
939 ci
->match_mask
&= filemask
;
942 newinfo
.name
= p
->path
;
943 newinfo
.namelen
= p
->pathlen
;
944 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, p
->pathlen
, 1);
946 * If this directory we are about to recurse into cared about
947 * its parent directory (the current directory) having a D/F
948 * conflict, then we'd propagate the masks in this way:
949 * newinfo.df_conflicts |= (mask & ~dirmask);
950 * But we don't worry about propagating D/F conflicts. (See
951 * comment near setting of local df_conflict variable near
952 * the beginning of this function).
955 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++) {
956 if (i
== 1 && side1_matches_mbase
)
958 else if (i
== 2 && side2_matches_mbase
)
960 else if (i
== 2 && sides_match
)
963 const struct object_id
*oid
= NULL
;
966 buf
[i
] = fill_tree_descriptor(opt
->repo
,
972 original_dir_name
= opti
->current_dir_name
;
973 opti
->current_dir_name
= pi
.string
;
974 if (renames
->dir_rename_mask
== 0 ||
975 renames
->dir_rename_mask
== 0x07)
976 ret
= traverse_trees(NULL
, 3, t
, &newinfo
);
978 ret
= traverse_trees_wrapper(NULL
, 3, t
, &newinfo
);
979 opti
->current_dir_name
= original_dir_name
;
980 renames
->dir_rename_mask
= prev_dir_rename_mask
;
982 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++)
992 static int collect_merge_info(struct merge_options
*opt
,
993 struct tree
*merge_base
,
998 struct tree_desc t
[3];
999 struct traverse_info info
;
1001 opt
->priv
->toplevel_dir
= "";
1002 opt
->priv
->current_dir_name
= opt
->priv
->toplevel_dir
;
1003 setup_traverse_info(&info
, opt
->priv
->toplevel_dir
);
1004 info
.fn
= collect_merge_info_callback
;
1006 info
.show_all_errors
= 1;
1008 parse_tree(merge_base
);
1011 init_tree_desc(t
+ 0, merge_base
->buffer
, merge_base
->size
);
1012 init_tree_desc(t
+ 1, side1
->buffer
, side1
->size
);
1013 init_tree_desc(t
+ 2, side2
->buffer
, side2
->size
);
1015 trace2_region_enter("merge", "traverse_trees", opt
->repo
);
1016 ret
= traverse_trees(NULL
, 3, t
, &info
);
1017 trace2_region_leave("merge", "traverse_trees", opt
->repo
);
1022 /*** Function Grouping: functions related to threeway content merges ***/
1024 static int find_first_merges(struct repository
*repo
,
1028 struct object_array
*result
)
1031 struct object_array merges
= OBJECT_ARRAY_INIT
;
1032 struct commit
*commit
;
1033 int contains_another
;
1035 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1036 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1037 "--all", merged_revision
, NULL
};
1038 struct rev_info revs
;
1039 struct setup_revision_opt rev_opts
;
1041 memset(result
, 0, sizeof(struct object_array
));
1042 memset(&rev_opts
, 0, sizeof(rev_opts
));
1044 /* get all revisions that merge commit a */
1045 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1046 oid_to_hex(&a
->object
.oid
));
1047 repo_init_revisions(repo
, &revs
, NULL
);
1048 rev_opts
.submodule
= path
;
1049 /* FIXME: can't handle linked worktrees in submodules yet */
1050 revs
.single_worktree
= path
!= NULL
;
1051 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1053 /* save all revisions from the above list that contain b */
1054 if (prepare_revision_walk(&revs
))
1055 die("revision walk setup failed");
1056 while ((commit
= get_revision(&revs
)) != NULL
) {
1057 struct object
*o
= &(commit
->object
);
1058 if (in_merge_bases(b
, commit
))
1059 add_object_array(o
, NULL
, &merges
);
1061 reset_revision_walk();
1063 /* Now we've got all merges that contain a and b. Prune all
1064 * merges that contain another found merge and save them in
1067 for (i
= 0; i
< merges
.nr
; i
++) {
1068 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1070 contains_another
= 0;
1071 for (j
= 0; j
< merges
.nr
; j
++) {
1072 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1073 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1074 contains_another
= 1;
1079 if (!contains_another
)
1080 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1083 object_array_clear(&merges
);
1087 static int merge_submodule(struct merge_options
*opt
,
1089 const struct object_id
*o
,
1090 const struct object_id
*a
,
1091 const struct object_id
*b
,
1092 struct object_id
*result
)
1094 struct commit
*commit_o
, *commit_a
, *commit_b
;
1096 struct object_array merges
;
1097 struct strbuf sb
= STRBUF_INIT
;
1100 int search
= !opt
->priv
->call_depth
;
1102 /* store fallback answer in result in case we fail */
1103 oidcpy(result
, opt
->priv
->call_depth
? o
: a
);
1105 /* we can not handle deletion conflicts */
1113 if (add_submodule_odb(path
)) {
1114 path_msg(opt
, path
, 0,
1115 _("Failed to merge submodule %s (not checked out)"),
1120 if (!(commit_o
= lookup_commit_reference(opt
->repo
, o
)) ||
1121 !(commit_a
= lookup_commit_reference(opt
->repo
, a
)) ||
1122 !(commit_b
= lookup_commit_reference(opt
->repo
, b
))) {
1123 path_msg(opt
, path
, 0,
1124 _("Failed to merge submodule %s (commits not present)"),
1129 /* check whether both changes are forward */
1130 if (!in_merge_bases(commit_o
, commit_a
) ||
1131 !in_merge_bases(commit_o
, commit_b
)) {
1132 path_msg(opt
, path
, 0,
1133 _("Failed to merge submodule %s "
1134 "(commits don't follow merge-base)"),
1139 /* Case #1: a is contained in b or vice versa */
1140 if (in_merge_bases(commit_a
, commit_b
)) {
1142 path_msg(opt
, path
, 1,
1143 _("Note: Fast-forwarding submodule %s to %s"),
1144 path
, oid_to_hex(b
));
1147 if (in_merge_bases(commit_b
, commit_a
)) {
1149 path_msg(opt
, path
, 1,
1150 _("Note: Fast-forwarding submodule %s to %s"),
1151 path
, oid_to_hex(a
));
1156 * Case #2: There are one or more merges that contain a and b in
1157 * the submodule. If there is only one, then present it as a
1158 * suggestion to the user, but leave it marked unmerged so the
1159 * user needs to confirm the resolution.
1162 /* Skip the search if makes no sense to the calling context. */
1166 /* find commit which merges them */
1167 parent_count
= find_first_merges(opt
->repo
, path
, commit_a
, commit_b
,
1169 switch (parent_count
) {
1171 path_msg(opt
, path
, 0, _("Failed to merge submodule %s"), path
);
1175 format_commit(&sb
, 4,
1176 (struct commit
*)merges
.objects
[0].item
);
1177 path_msg(opt
, path
, 0,
1178 _("Failed to merge submodule %s, but a possible merge "
1179 "resolution exists:\n%s\n"),
1181 path_msg(opt
, path
, 1,
1182 _("If this is correct simply add it to the index "
1185 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1186 "which will accept this suggestion.\n"),
1187 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1188 strbuf_release(&sb
);
1191 for (i
= 0; i
< merges
.nr
; i
++)
1192 format_commit(&sb
, 4,
1193 (struct commit
*)merges
.objects
[i
].item
);
1194 path_msg(opt
, path
, 0,
1195 _("Failed to merge submodule %s, but multiple "
1196 "possible merges exist:\n%s"), path
, sb
.buf
);
1197 strbuf_release(&sb
);
1200 object_array_clear(&merges
);
1204 static int merge_3way(struct merge_options
*opt
,
1206 const struct object_id
*o
,
1207 const struct object_id
*a
,
1208 const struct object_id
*b
,
1209 const char *pathnames
[3],
1210 const int extra_marker_size
,
1211 mmbuffer_t
*result_buf
)
1213 mmfile_t orig
, src1
, src2
;
1214 struct ll_merge_options ll_opts
= {0};
1215 char *base
, *name1
, *name2
;
1218 ll_opts
.renormalize
= opt
->renormalize
;
1219 ll_opts
.extra_marker_size
= extra_marker_size
;
1220 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1222 if (opt
->priv
->call_depth
) {
1223 ll_opts
.virtual_ancestor
= 1;
1224 ll_opts
.variant
= 0;
1226 switch (opt
->recursive_variant
) {
1227 case MERGE_VARIANT_OURS
:
1228 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1230 case MERGE_VARIANT_THEIRS
:
1231 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1234 ll_opts
.variant
= 0;
1239 assert(pathnames
[0] && pathnames
[1] && pathnames
[2] && opt
->ancestor
);
1240 if (pathnames
[0] == pathnames
[1] && pathnames
[1] == pathnames
[2]) {
1241 base
= mkpathdup("%s", opt
->ancestor
);
1242 name1
= mkpathdup("%s", opt
->branch1
);
1243 name2
= mkpathdup("%s", opt
->branch2
);
1245 base
= mkpathdup("%s:%s", opt
->ancestor
, pathnames
[0]);
1246 name1
= mkpathdup("%s:%s", opt
->branch1
, pathnames
[1]);
1247 name2
= mkpathdup("%s:%s", opt
->branch2
, pathnames
[2]);
1250 read_mmblob(&orig
, o
);
1251 read_mmblob(&src1
, a
);
1252 read_mmblob(&src2
, b
);
1254 merge_status
= ll_merge(result_buf
, path
, &orig
, base
,
1255 &src1
, name1
, &src2
, name2
,
1256 opt
->repo
->index
, &ll_opts
);
1264 return merge_status
;
1267 static int handle_content_merge(struct merge_options
*opt
,
1269 const struct version_info
*o
,
1270 const struct version_info
*a
,
1271 const struct version_info
*b
,
1272 const char *pathnames
[3],
1273 const int extra_marker_size
,
1274 struct version_info
*result
)
1277 * path is the target location where we want to put the file, and
1278 * is used to determine any normalization rules in ll_merge.
1280 * The normal case is that path and all entries in pathnames are
1281 * identical, though renames can affect which path we got one of
1282 * the three blobs to merge on various sides of history.
1284 * extra_marker_size is the amount to extend conflict markers in
1285 * ll_merge; this is neeed if we have content merges of content
1286 * merges, which happens for example with rename/rename(2to1) and
1287 * rename/add conflicts.
1292 * handle_content_merge() needs both files to be of the same type, i.e.
1293 * both files OR both submodules OR both symlinks. Conflicting types
1294 * needs to be handled elsewhere.
1296 assert((S_IFMT
& a
->mode
) == (S_IFMT
& b
->mode
));
1299 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1300 result
->mode
= b
->mode
;
1302 /* must be the 100644/100755 case */
1303 assert(S_ISREG(a
->mode
));
1304 result
->mode
= a
->mode
;
1305 clean
= (b
->mode
== o
->mode
);
1307 * FIXME: If opt->priv->call_depth && !clean, then we really
1308 * should not make result->mode match either a->mode or
1309 * b->mode; that causes t6036 "check conflicting mode for
1310 * regular file" to fail. It would be best to use some other
1311 * mode, but we'll confuse all kinds of stuff if we use one
1312 * where S_ISREG(result->mode) isn't true, and if we use
1313 * something like 0100666, then tree-walk.c's calls to
1314 * canon_mode() will just normalize that to 100644 for us and
1315 * thus not solve anything.
1317 * Figure out if there's some kind of way we can work around
1323 * Trivial oid merge.
1325 * Note: While one might assume that the next four lines would
1326 * be unnecessary due to the fact that match_mask is often
1327 * setup and already handled, renames don't always take care
1330 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1331 oidcpy(&result
->oid
, &b
->oid
);
1332 else if (oideq(&b
->oid
, &o
->oid
))
1333 oidcpy(&result
->oid
, &a
->oid
);
1335 /* Remaining rules depend on file vs. submodule vs. symlink. */
1336 else if (S_ISREG(a
->mode
)) {
1337 mmbuffer_t result_buf
;
1338 int ret
= 0, merge_status
;
1342 * If 'o' is different type, treat it as null so we do a
1345 two_way
= ((S_IFMT
& o
->mode
) != (S_IFMT
& a
->mode
));
1347 merge_status
= merge_3way(opt
, path
,
1348 two_way
? &null_oid
: &o
->oid
,
1350 pathnames
, extra_marker_size
,
1353 if ((merge_status
< 0) || !result_buf
.ptr
)
1354 ret
= err(opt
, _("Failed to execute internal merge"));
1357 write_object_file(result_buf
.ptr
, result_buf
.size
,
1358 blob_type
, &result
->oid
))
1359 ret
= err(opt
, _("Unable to add %s to database"),
1362 free(result_buf
.ptr
);
1365 clean
&= (merge_status
== 0);
1366 path_msg(opt
, path
, 1, _("Auto-merging %s"), path
);
1367 } else if (S_ISGITLINK(a
->mode
)) {
1368 int two_way
= ((S_IFMT
& o
->mode
) != (S_IFMT
& a
->mode
));
1369 clean
= merge_submodule(opt
, pathnames
[0],
1370 two_way
? &null_oid
: &o
->oid
,
1371 &a
->oid
, &b
->oid
, &result
->oid
);
1372 if (opt
->priv
->call_depth
&& two_way
&& !clean
) {
1373 result
->mode
= o
->mode
;
1374 oidcpy(&result
->oid
, &o
->oid
);
1376 } else if (S_ISLNK(a
->mode
)) {
1377 if (opt
->priv
->call_depth
) {
1379 result
->mode
= o
->mode
;
1380 oidcpy(&result
->oid
, &o
->oid
);
1382 switch (opt
->recursive_variant
) {
1383 case MERGE_VARIANT_NORMAL
:
1385 oidcpy(&result
->oid
, &a
->oid
);
1387 case MERGE_VARIANT_OURS
:
1388 oidcpy(&result
->oid
, &a
->oid
);
1390 case MERGE_VARIANT_THEIRS
:
1391 oidcpy(&result
->oid
, &b
->oid
);
1396 BUG("unsupported object type in the tree: %06o for %s",
1402 /*** Function Grouping: functions related to detect_and_process_renames(), ***
1403 *** which are split into directory and regular rename detection sections. ***/
1405 /*** Function Grouping: functions related to directory rename detection ***/
1407 struct collision_info
{
1408 struct string_list source_files
;
1409 unsigned reported_already
:1;
1413 * Return a new string that replaces the beginning portion (which matches
1414 * rename_info->key), with rename_info->util.new_dir. In perl-speak:
1415 * new_path_name = (old_path =~ s/rename_info->key/rename_info->value/);
1417 * Caller must ensure that old_path starts with rename_info->key + '/'.
1419 static char *apply_dir_rename(struct strmap_entry
*rename_info
,
1420 const char *old_path
)
1422 struct strbuf new_path
= STRBUF_INIT
;
1423 const char *old_dir
= rename_info
->key
;
1424 const char *new_dir
= rename_info
->value
;
1425 int oldlen
, newlen
, new_dir_len
;
1427 oldlen
= strlen(old_dir
);
1428 if (*new_dir
== '\0')
1430 * If someone renamed/merged a subdirectory into the root
1431 * directory (e.g. 'some/subdir' -> ''), then we want to
1434 * as the rename; we need to make old_path + oldlen advance
1435 * past the '/' character.
1438 new_dir_len
= strlen(new_dir
);
1439 newlen
= new_dir_len
+ (strlen(old_path
) - oldlen
) + 1;
1440 strbuf_grow(&new_path
, newlen
);
1441 strbuf_add(&new_path
, new_dir
, new_dir_len
);
1442 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
1444 return strbuf_detach(&new_path
, NULL
);
1447 static int path_in_way(struct strmap
*paths
, const char *path
, unsigned side_mask
)
1449 struct merged_info
*mi
= strmap_get(paths
, path
);
1450 struct conflict_info
*ci
;
1453 INITIALIZE_CI(ci
, mi
);
1454 return mi
->clean
|| (side_mask
& (ci
->filemask
| ci
->dirmask
));
1458 * See if there is a directory rename for path, and if there are any file
1459 * level conflicts on the given side for the renamed location. If there is
1460 * a rename and there are no conflicts, return the new name. Otherwise,
1463 static char *handle_path_level_conflicts(struct merge_options
*opt
,
1465 unsigned side_index
,
1466 struct strmap_entry
*rename_info
,
1467 struct strmap
*collisions
)
1469 char *new_path
= NULL
;
1470 struct collision_info
*c_info
;
1472 struct strbuf collision_paths
= STRBUF_INIT
;
1475 * entry has the mapping of old directory name to new directory name
1476 * that we want to apply to path.
1478 new_path
= apply_dir_rename(rename_info
, path
);
1480 BUG("Failed to apply directory rename!");
1483 * The caller needs to have ensured that it has pre-populated
1484 * collisions with all paths that map to new_path. Do a quick check
1485 * to ensure that's the case.
1487 c_info
= strmap_get(collisions
, new_path
);
1489 BUG("c_info is NULL");
1492 * Check for one-sided add/add/.../add conflicts, i.e.
1493 * where implicit renames from the other side doing
1494 * directory rename(s) can affect this side of history
1495 * to put multiple paths into the same location. Warn
1496 * and bail on directory renames for such paths.
1498 if (c_info
->reported_already
) {
1500 } else if (path_in_way(&opt
->priv
->paths
, new_path
, 1 << side_index
)) {
1501 c_info
->reported_already
= 1;
1502 strbuf_add_separated_string_list(&collision_paths
, ", ",
1503 &c_info
->source_files
);
1504 path_msg(opt
, new_path
, 0,
1505 _("CONFLICT (implicit dir rename): Existing file/dir "
1506 "at %s in the way of implicit directory rename(s) "
1507 "putting the following path(s) there: %s."),
1508 new_path
, collision_paths
.buf
);
1510 } else if (c_info
->source_files
.nr
> 1) {
1511 c_info
->reported_already
= 1;
1512 strbuf_add_separated_string_list(&collision_paths
, ", ",
1513 &c_info
->source_files
);
1514 path_msg(opt
, new_path
, 0,
1515 _("CONFLICT (implicit dir rename): Cannot map more "
1516 "than one path to %s; implicit directory renames "
1517 "tried to put these paths there: %s"),
1518 new_path
, collision_paths
.buf
);
1522 /* Free memory we no longer need */
1523 strbuf_release(&collision_paths
);
1524 if (!clean
&& new_path
) {
1532 static void get_provisional_directory_renames(struct merge_options
*opt
,
1536 struct hashmap_iter iter
;
1537 struct strmap_entry
*entry
;
1538 struct rename_info
*renames
= &opt
->priv
->renames
;
1542 * dir_rename_count: old_directory -> {new_directory -> count}
1544 * dir_renames: old_directory -> best_new_directory
1545 * where best_new_directory is the one with the unique highest count.
1547 strmap_for_each_entry(&renames
->dir_rename_count
[side
], &iter
, entry
) {
1548 const char *source_dir
= entry
->key
;
1549 struct strintmap
*counts
= entry
->value
;
1550 struct hashmap_iter count_iter
;
1551 struct strmap_entry
*count_entry
;
1554 const char *best
= NULL
;
1556 strintmap_for_each_entry(counts
, &count_iter
, count_entry
) {
1557 const char *target_dir
= count_entry
->key
;
1558 intptr_t count
= (intptr_t)count_entry
->value
;
1562 else if (count
> max
) {
1571 if (bad_max
== max
) {
1572 path_msg(opt
, source_dir
, 0,
1573 _("CONFLICT (directory rename split): "
1574 "Unclear where to rename %s to; it was "
1575 "renamed to multiple other directories, with "
1576 "no destination getting a majority of the "
1580 * We should mark this as unclean IF something attempts
1581 * to use this rename. We do not yet have the logic
1582 * in place to detect if this directory rename is being
1583 * used, and optimizations that reduce the number of
1584 * renames cause this to falsely trigger. For now,
1585 * just disable it, causing t6423 testcase 2a to break.
1586 * We'll later fix the detection, and when we do we
1587 * will re-enable setting *clean to 0 (and thereby fix
1588 * t6423 testcase 2a).
1592 strmap_put(&renames
->dir_renames
[side
],
1593 source_dir
, (void*)best
);
1598 static void handle_directory_level_conflicts(struct merge_options
*opt
)
1600 struct hashmap_iter iter
;
1601 struct strmap_entry
*entry
;
1602 struct string_list duplicated
= STRING_LIST_INIT_NODUP
;
1603 struct rename_info
*renames
= &opt
->priv
->renames
;
1604 struct strmap
*side1_dir_renames
= &renames
->dir_renames
[MERGE_SIDE1
];
1605 struct strmap
*side2_dir_renames
= &renames
->dir_renames
[MERGE_SIDE2
];
1608 strmap_for_each_entry(side1_dir_renames
, &iter
, entry
) {
1609 if (strmap_contains(side2_dir_renames
, entry
->key
))
1610 string_list_append(&duplicated
, entry
->key
);
1613 for (i
= 0; i
< duplicated
.nr
; i
++) {
1614 strmap_remove(side1_dir_renames
, duplicated
.items
[i
].string
, 0);
1615 strmap_remove(side2_dir_renames
, duplicated
.items
[i
].string
, 0);
1617 string_list_clear(&duplicated
, 0);
1620 static struct strmap_entry
*check_dir_renamed(const char *path
,
1621 struct strmap
*dir_renames
)
1623 char *temp
= xstrdup(path
);
1625 struct strmap_entry
*e
= NULL
;
1627 while ((end
= strrchr(temp
, '/'))) {
1629 e
= strmap_get_entry(dir_renames
, temp
);
1637 static void compute_collisions(struct strmap
*collisions
,
1638 struct strmap
*dir_renames
,
1639 struct diff_queue_struct
*pairs
)
1643 strmap_init_with_options(collisions
, NULL
, 0);
1644 if (strmap_empty(dir_renames
))
1648 * Multiple files can be mapped to the same path due to directory
1649 * renames done by the other side of history. Since that other
1650 * side of history could have merged multiple directories into one,
1651 * if our side of history added the same file basename to each of
1652 * those directories, then all N of them would get implicitly
1653 * renamed by the directory rename detection into the same path,
1654 * and we'd get an add/add/.../add conflict, and all those adds
1655 * from *this* side of history. This is not representable in the
1656 * index, and users aren't going to easily be able to make sense of
1657 * it. So we need to provide a good warning about what's
1658 * happening, and fall back to no-directory-rename detection
1659 * behavior for those paths.
1661 * See testcases 9e and all of section 5 from t6043 for examples.
1663 for (i
= 0; i
< pairs
->nr
; ++i
) {
1664 struct strmap_entry
*rename_info
;
1665 struct collision_info
*collision_info
;
1667 struct diff_filepair
*pair
= pairs
->queue
[i
];
1669 if (pair
->status
!= 'A' && pair
->status
!= 'R')
1671 rename_info
= check_dir_renamed(pair
->two
->path
, dir_renames
);
1675 new_path
= apply_dir_rename(rename_info
, pair
->two
->path
);
1677 collision_info
= strmap_get(collisions
, new_path
);
1678 if (collision_info
) {
1681 collision_info
= xcalloc(1,
1682 sizeof(struct collision_info
));
1683 string_list_init(&collision_info
->source_files
, 0);
1684 strmap_put(collisions
, new_path
, collision_info
);
1686 string_list_insert(&collision_info
->source_files
,
1691 static char *check_for_directory_rename(struct merge_options
*opt
,
1693 unsigned side_index
,
1694 struct strmap
*dir_renames
,
1695 struct strmap
*dir_rename_exclusions
,
1696 struct strmap
*collisions
,
1699 char *new_path
= NULL
;
1700 struct strmap_entry
*rename_info
;
1701 struct strmap_entry
*otherinfo
= NULL
;
1702 const char *new_dir
;
1704 if (strmap_empty(dir_renames
))
1706 rename_info
= check_dir_renamed(path
, dir_renames
);
1709 /* old_dir = rename_info->key; */
1710 new_dir
= rename_info
->value
;
1713 * This next part is a little weird. We do not want to do an
1714 * implicit rename into a directory we renamed on our side, because
1715 * that will result in a spurious rename/rename(1to2) conflict. An
1717 * Base commit: dumbdir/afile, otherdir/bfile
1718 * Side 1: smrtdir/afile, otherdir/bfile
1719 * Side 2: dumbdir/afile, dumbdir/bfile
1720 * Here, while working on Side 1, we could notice that otherdir was
1721 * renamed/merged to dumbdir, and change the diff_filepair for
1722 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
1723 * 2 will notice the rename from dumbdir to smrtdir, and do the
1724 * transitive rename to move it from dumbdir/bfile to
1725 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
1726 * smrtdir, a rename/rename(1to2) conflict. We really just want
1727 * the file to end up in smrtdir. And the way to achieve that is
1728 * to not let Side1 do the rename to dumbdir, since we know that is
1729 * the source of one of our directory renames.
1731 * That's why otherinfo and dir_rename_exclusions is here.
1733 * As it turns out, this also prevents N-way transient rename
1734 * confusion; See testcases 9c and 9d of t6043.
1736 otherinfo
= strmap_get_entry(dir_rename_exclusions
, new_dir
);
1738 path_msg(opt
, rename_info
->key
, 1,
1739 _("WARNING: Avoiding applying %s -> %s rename "
1740 "to %s, because %s itself was renamed."),
1741 rename_info
->key
, new_dir
, path
, new_dir
);
1745 new_path
= handle_path_level_conflicts(opt
, path
, side_index
,
1746 rename_info
, collisions
);
1747 *clean_merge
&= (new_path
!= NULL
);
1752 static void apply_directory_rename_modifications(struct merge_options
*opt
,
1753 struct diff_filepair
*pair
,
1757 * The basic idea is to get the conflict_info from opt->priv->paths
1758 * at old path, and insert it into new_path; basically just this:
1759 * ci = strmap_get(&opt->priv->paths, old_path);
1760 * strmap_remove(&opt->priv->paths, old_path, 0);
1761 * strmap_put(&opt->priv->paths, new_path, ci);
1762 * However, there are some factors complicating this:
1763 * - opt->priv->paths may already have an entry at new_path
1764 * - Each ci tracks its containing directory, so we need to
1766 * - If another ci has the same containing directory, then
1767 * the two char*'s MUST point to the same location. See the
1768 * comment in struct merged_info. strcmp equality is not
1769 * enough; we need pointer equality.
1770 * - opt->priv->paths must hold the parent directories of any
1771 * entries that are added. So, if this directory rename
1772 * causes entirely new directories, we must recursively add
1773 * parent directories.
1774 * - For each parent directory added to opt->priv->paths, we
1775 * also need to get its parent directory stored in its
1776 * conflict_info->merged.directory_name with all the same
1777 * requirements about pointer equality.
1779 struct string_list dirs_to_insert
= STRING_LIST_INIT_NODUP
;
1780 struct conflict_info
*ci
, *new_ci
;
1781 struct strmap_entry
*entry
;
1782 const char *branch_with_new_path
, *branch_with_dir_rename
;
1783 const char *old_path
= pair
->two
->path
;
1784 const char *parent_name
;
1785 const char *cur_path
;
1788 entry
= strmap_get_entry(&opt
->priv
->paths
, old_path
);
1789 old_path
= entry
->key
;
1793 /* Find parent directories missing from opt->priv->paths */
1794 cur_path
= new_path
;
1796 /* Find the parent directory of cur_path */
1797 char *last_slash
= strrchr(cur_path
, '/');
1799 parent_name
= xstrndup(cur_path
, last_slash
- cur_path
);
1801 parent_name
= opt
->priv
->toplevel_dir
;
1805 /* Look it up in opt->priv->paths */
1806 entry
= strmap_get_entry(&opt
->priv
->paths
, parent_name
);
1808 free((char*)parent_name
);
1809 parent_name
= entry
->key
; /* reuse known pointer */
1813 /* Record this is one of the directories we need to insert */
1814 string_list_append(&dirs_to_insert
, parent_name
);
1815 cur_path
= parent_name
;
1818 /* Traverse dirs_to_insert and insert them into opt->priv->paths */
1819 for (i
= dirs_to_insert
.nr
-1; i
>= 0; --i
) {
1820 struct conflict_info
*dir_ci
;
1821 char *cur_dir
= dirs_to_insert
.items
[i
].string
;
1823 dir_ci
= xcalloc(1, sizeof(*dir_ci
));
1825 dir_ci
->merged
.directory_name
= parent_name
;
1826 len
= strlen(parent_name
);
1827 /* len+1 because of trailing '/' character */
1828 dir_ci
->merged
.basename_offset
= (len
> 0 ? len
+1 : len
);
1829 dir_ci
->dirmask
= ci
->filemask
;
1830 strmap_put(&opt
->priv
->paths
, cur_dir
, dir_ci
);
1832 parent_name
= cur_dir
;
1836 * We are removing old_path from opt->priv->paths. old_path also will
1837 * eventually need to be freed, but it may still be used by e.g.
1838 * ci->pathnames. So, store it in another string-list for now.
1840 string_list_append(&opt
->priv
->paths_to_free
, old_path
);
1842 assert(ci
->filemask
== 2 || ci
->filemask
== 4);
1843 assert(ci
->dirmask
== 0);
1844 strmap_remove(&opt
->priv
->paths
, old_path
, 0);
1846 branch_with_new_path
= (ci
->filemask
== 2) ? opt
->branch1
: opt
->branch2
;
1847 branch_with_dir_rename
= (ci
->filemask
== 2) ? opt
->branch2
: opt
->branch1
;
1849 /* Now, finally update ci and stick it into opt->priv->paths */
1850 ci
->merged
.directory_name
= parent_name
;
1851 len
= strlen(parent_name
);
1852 ci
->merged
.basename_offset
= (len
> 0 ? len
+1 : len
);
1853 new_ci
= strmap_get(&opt
->priv
->paths
, new_path
);
1855 /* Place ci back into opt->priv->paths, but at new_path */
1856 strmap_put(&opt
->priv
->paths
, new_path
, ci
);
1860 /* A few sanity checks */
1862 assert(ci
->filemask
== 2 || ci
->filemask
== 4);
1863 assert((new_ci
->filemask
& ci
->filemask
) == 0);
1864 assert(!new_ci
->merged
.clean
);
1866 /* Copy stuff from ci into new_ci */
1867 new_ci
->filemask
|= ci
->filemask
;
1868 if (new_ci
->dirmask
)
1869 new_ci
->df_conflict
= 1;
1870 index
= (ci
->filemask
>> 1);
1871 new_ci
->pathnames
[index
] = ci
->pathnames
[index
];
1872 new_ci
->stages
[index
].mode
= ci
->stages
[index
].mode
;
1873 oidcpy(&new_ci
->stages
[index
].oid
, &ci
->stages
[index
].oid
);
1879 if (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) {
1880 /* Notify user of updated path */
1881 if (pair
->status
== 'A')
1882 path_msg(opt
, new_path
, 1,
1883 _("Path updated: %s added in %s inside a "
1884 "directory that was renamed in %s; moving "
1886 old_path
, branch_with_new_path
,
1887 branch_with_dir_rename
, new_path
);
1889 path_msg(opt
, new_path
, 1,
1890 _("Path updated: %s renamed to %s in %s, "
1891 "inside a directory that was renamed in %s; "
1892 "moving it to %s."),
1893 pair
->one
->path
, old_path
, branch_with_new_path
,
1894 branch_with_dir_rename
, new_path
);
1897 * opt->detect_directory_renames has the value
1898 * MERGE_DIRECTORY_RENAMES_CONFLICT, so mark these as conflicts.
1900 ci
->path_conflict
= 1;
1901 if (pair
->status
== 'A')
1902 path_msg(opt
, new_path
, 0,
1903 _("CONFLICT (file location): %s added in %s "
1904 "inside a directory that was renamed in %s, "
1905 "suggesting it should perhaps be moved to "
1907 old_path
, branch_with_new_path
,
1908 branch_with_dir_rename
, new_path
);
1910 path_msg(opt
, new_path
, 0,
1911 _("CONFLICT (file location): %s renamed to %s "
1912 "in %s, inside a directory that was renamed "
1913 "in %s, suggesting it should perhaps be "
1915 pair
->one
->path
, old_path
, branch_with_new_path
,
1916 branch_with_dir_rename
, new_path
);
1920 * Finally, record the new location.
1922 pair
->two
->path
= new_path
;
1925 /*** Function Grouping: functions related to regular rename detection ***/
1927 static int process_renames(struct merge_options
*opt
,
1928 struct diff_queue_struct
*renames
)
1930 int clean_merge
= 1, i
;
1932 for (i
= 0; i
< renames
->nr
; ++i
) {
1933 const char *oldpath
= NULL
, *newpath
;
1934 struct diff_filepair
*pair
= renames
->queue
[i
];
1935 struct conflict_info
*oldinfo
= NULL
, *newinfo
= NULL
;
1936 struct strmap_entry
*old_ent
, *new_ent
;
1937 unsigned int old_sidemask
;
1938 int target_index
, other_source_index
;
1939 int source_deleted
, collision
, type_changed
;
1940 const char *rename_branch
= NULL
, *delete_branch
= NULL
;
1942 old_ent
= strmap_get_entry(&opt
->priv
->paths
, pair
->one
->path
);
1943 new_ent
= strmap_get_entry(&opt
->priv
->paths
, pair
->two
->path
);
1945 oldpath
= old_ent
->key
;
1946 oldinfo
= old_ent
->value
;
1948 newpath
= pair
->two
->path
;
1950 newpath
= new_ent
->key
;
1951 newinfo
= new_ent
->value
;
1955 * If pair->one->path isn't in opt->priv->paths, that means
1956 * that either directory rename detection removed that
1957 * path, or a parent directory of oldpath was resolved and
1958 * we don't even need the rename; in either case, we can
1959 * skip it. If oldinfo->merged.clean, then the other side
1960 * of history had no changes to oldpath and we don't need
1961 * the rename and can skip it.
1963 if (!oldinfo
|| oldinfo
->merged
.clean
)
1967 * diff_filepairs have copies of pathnames, thus we have to
1968 * use standard 'strcmp()' (negated) instead of '=='.
1970 if (i
+ 1 < renames
->nr
&&
1971 !strcmp(oldpath
, renames
->queue
[i
+1]->one
->path
)) {
1972 /* Handle rename/rename(1to2) or rename/rename(1to1) */
1973 const char *pathnames
[3];
1974 struct version_info merged
;
1975 struct conflict_info
*base
, *side1
, *side2
;
1976 unsigned was_binary_blob
= 0;
1978 pathnames
[0] = oldpath
;
1979 pathnames
[1] = newpath
;
1980 pathnames
[2] = renames
->queue
[i
+1]->two
->path
;
1982 base
= strmap_get(&opt
->priv
->paths
, pathnames
[0]);
1983 side1
= strmap_get(&opt
->priv
->paths
, pathnames
[1]);
1984 side2
= strmap_get(&opt
->priv
->paths
, pathnames
[2]);
1990 if (!strcmp(pathnames
[1], pathnames
[2])) {
1991 /* Both sides renamed the same way */
1992 assert(side1
== side2
);
1993 memcpy(&side1
->stages
[0], &base
->stages
[0],
1995 side1
->filemask
|= (1 << MERGE_BASE
);
1996 /* Mark base as resolved by removal */
1997 base
->merged
.is_null
= 1;
1998 base
->merged
.clean
= 1;
2000 /* We handled both renames, i.e. i+1 handled */
2002 /* Move to next rename */
2006 /* This is a rename/rename(1to2) */
2007 clean_merge
= handle_content_merge(opt
,
2013 1 + 2 * opt
->priv
->call_depth
,
2016 merged
.mode
== side1
->stages
[1].mode
&&
2017 oideq(&merged
.oid
, &side1
->stages
[1].oid
))
2018 was_binary_blob
= 1;
2019 memcpy(&side1
->stages
[1], &merged
, sizeof(merged
));
2020 if (was_binary_blob
) {
2022 * Getting here means we were attempting to
2023 * merge a binary blob.
2025 * Since we can't merge binaries,
2026 * handle_content_merge() just takes one
2027 * side. But we don't want to copy the
2028 * contents of one side to both paths. We
2029 * used the contents of side1 above for
2030 * side1->stages, let's use the contents of
2031 * side2 for side2->stages below.
2033 oidcpy(&merged
.oid
, &side2
->stages
[2].oid
);
2034 merged
.mode
= side2
->stages
[2].mode
;
2036 memcpy(&side2
->stages
[2], &merged
, sizeof(merged
));
2038 side1
->path_conflict
= 1;
2039 side2
->path_conflict
= 1;
2041 * TODO: For renames we normally remove the path at the
2042 * old name. It would thus seem consistent to do the
2043 * same for rename/rename(1to2) cases, but we haven't
2044 * done so traditionally and a number of the regression
2045 * tests now encode an expectation that the file is
2046 * left there at stage 1. If we ever decide to change
2047 * this, add the following two lines here:
2048 * base->merged.is_null = 1;
2049 * base->merged.clean = 1;
2050 * and remove the setting of base->path_conflict to 1.
2052 base
->path_conflict
= 1;
2053 path_msg(opt
, oldpath
, 0,
2054 _("CONFLICT (rename/rename): %s renamed to "
2055 "%s in %s and to %s in %s."),
2057 pathnames
[1], opt
->branch1
,
2058 pathnames
[2], opt
->branch2
);
2060 i
++; /* We handled both renames, i.e. i+1 handled */
2066 target_index
= pair
->score
; /* from collect_renames() */
2067 assert(target_index
== 1 || target_index
== 2);
2068 other_source_index
= 3 - target_index
;
2069 old_sidemask
= (1 << other_source_index
); /* 2 or 4 */
2070 source_deleted
= (oldinfo
->filemask
== 1);
2071 collision
= ((newinfo
->filemask
& old_sidemask
) != 0);
2072 type_changed
= !source_deleted
&&
2073 (S_ISREG(oldinfo
->stages
[other_source_index
].mode
) !=
2074 S_ISREG(newinfo
->stages
[target_index
].mode
));
2075 if (type_changed
&& collision
) {
2077 * special handling so later blocks can handle this...
2079 * if type_changed && collision are both true, then this
2080 * was really a double rename, but one side wasn't
2081 * detected due to lack of break detection. I.e.
2083 * orig: has normal file 'foo'
2084 * side1: renames 'foo' to 'bar', adds 'foo' symlink
2085 * side2: renames 'foo' to 'bar'
2086 * In this case, the foo->bar rename on side1 won't be
2087 * detected because the new symlink named 'foo' is
2088 * there and we don't do break detection. But we detect
2089 * this here because we don't want to merge the content
2090 * of the foo symlink with the foo->bar file, so we
2091 * have some logic to handle this special case. The
2092 * easiest way to do that is make 'bar' on side1 not
2093 * be considered a colliding file but the other part
2094 * of a normal rename. If the file is very different,
2095 * well we're going to get content merge conflicts
2096 * anyway so it doesn't hurt. And if the colliding
2097 * file also has a different type, that'll be handled
2098 * by the content merge logic in process_entry() too.
2100 * See also t6430, 'rename vs. rename/symlink'
2104 if (source_deleted
) {
2105 if (target_index
== 1) {
2106 rename_branch
= opt
->branch1
;
2107 delete_branch
= opt
->branch2
;
2109 rename_branch
= opt
->branch2
;
2110 delete_branch
= opt
->branch1
;
2114 assert(source_deleted
|| oldinfo
->filemask
& old_sidemask
);
2116 /* Need to check for special types of rename conflicts... */
2117 if (collision
&& !source_deleted
) {
2118 /* collision: rename/add or rename/rename(2to1) */
2119 const char *pathnames
[3];
2120 struct version_info merged
;
2122 struct conflict_info
*base
, *side1
, *side2
;
2125 pathnames
[0] = oldpath
;
2126 pathnames
[other_source_index
] = oldpath
;
2127 pathnames
[target_index
] = newpath
;
2129 base
= strmap_get(&opt
->priv
->paths
, pathnames
[0]);
2130 side1
= strmap_get(&opt
->priv
->paths
, pathnames
[1]);
2131 side2
= strmap_get(&opt
->priv
->paths
, pathnames
[2]);
2137 clean
= handle_content_merge(opt
, pair
->one
->path
,
2142 1 + 2 * opt
->priv
->call_depth
,
2145 memcpy(&newinfo
->stages
[target_index
], &merged
,
2148 path_msg(opt
, newpath
, 0,
2149 _("CONFLICT (rename involved in "
2150 "collision): rename of %s -> %s has "
2151 "content conflicts AND collides "
2152 "with another path; this may result "
2153 "in nested conflict markers."),
2156 } else if (collision
&& source_deleted
) {
2158 * rename/add/delete or rename/rename(2to1)/delete:
2159 * since oldpath was deleted on the side that didn't
2160 * do the rename, there's not much of a content merge
2161 * we can do for the rename. oldinfo->merged.is_null
2162 * was already set, so we just leave things as-is so
2163 * they look like an add/add conflict.
2166 newinfo
->path_conflict
= 1;
2167 path_msg(opt
, newpath
, 0,
2168 _("CONFLICT (rename/delete): %s renamed "
2169 "to %s in %s, but deleted in %s."),
2170 oldpath
, newpath
, rename_branch
, delete_branch
);
2173 * a few different cases...start by copying the
2174 * existing stage(s) from oldinfo over the newinfo
2175 * and update the pathname(s).
2177 memcpy(&newinfo
->stages
[0], &oldinfo
->stages
[0],
2178 sizeof(newinfo
->stages
[0]));
2179 newinfo
->filemask
|= (1 << MERGE_BASE
);
2180 newinfo
->pathnames
[0] = oldpath
;
2182 /* rename vs. typechange */
2183 /* Mark the original as resolved by removal */
2184 memcpy(&oldinfo
->stages
[0].oid
, &null_oid
,
2185 sizeof(oldinfo
->stages
[0].oid
));
2186 oldinfo
->stages
[0].mode
= 0;
2187 oldinfo
->filemask
&= 0x06;
2188 } else if (source_deleted
) {
2190 newinfo
->path_conflict
= 1;
2191 path_msg(opt
, newpath
, 0,
2192 _("CONFLICT (rename/delete): %s renamed"
2193 " to %s in %s, but deleted in %s."),
2195 rename_branch
, delete_branch
);
2198 memcpy(&newinfo
->stages
[other_source_index
],
2199 &oldinfo
->stages
[other_source_index
],
2200 sizeof(newinfo
->stages
[0]));
2201 newinfo
->filemask
|= (1 << other_source_index
);
2202 newinfo
->pathnames
[other_source_index
] = oldpath
;
2206 if (!type_changed
) {
2207 /* Mark the original as resolved by removal */
2208 oldinfo
->merged
.is_null
= 1;
2209 oldinfo
->merged
.clean
= 1;
2217 static inline int possible_side_renames(struct rename_info
*renames
,
2218 unsigned side_index
)
2220 return renames
->pairs
[side_index
].nr
> 0 &&
2221 !strintmap_empty(&renames
->relevant_sources
[side_index
]);
2224 static inline int possible_renames(struct rename_info
*renames
)
2226 return possible_side_renames(renames
, 1) ||
2227 possible_side_renames(renames
, 2);
2230 static void resolve_diffpair_statuses(struct diff_queue_struct
*q
)
2233 * A simplified version of diff_resolve_rename_copy(); would probably
2234 * just use that function but it's static...
2237 struct diff_filepair
*p
;
2239 for (i
= 0; i
< q
->nr
; ++i
) {
2241 p
->status
= 0; /* undecided */
2242 if (!DIFF_FILE_VALID(p
->one
))
2243 p
->status
= DIFF_STATUS_ADDED
;
2244 else if (!DIFF_FILE_VALID(p
->two
))
2245 p
->status
= DIFF_STATUS_DELETED
;
2246 else if (DIFF_PAIR_RENAME(p
))
2247 p
->status
= DIFF_STATUS_RENAMED
;
2251 static int compare_pairs(const void *a_
, const void *b_
)
2253 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
2254 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
2256 return strcmp(a
->one
->path
, b
->one
->path
);
2259 /* Call diffcore_rename() to compute which files have changed on given side */
2260 static void detect_regular_renames(struct merge_options
*opt
,
2261 unsigned side_index
)
2263 struct diff_options diff_opts
;
2264 struct rename_info
*renames
= &opt
->priv
->renames
;
2266 if (!possible_side_renames(renames
, side_index
)) {
2268 * No rename detection needed for this side, but we still need
2269 * to make sure 'adds' are marked correctly in case the other
2270 * side had directory renames.
2272 resolve_diffpair_statuses(&renames
->pairs
[side_index
]);
2276 repo_diff_setup(opt
->repo
, &diff_opts
);
2277 diff_opts
.flags
.recursive
= 1;
2278 diff_opts
.flags
.rename_empty
= 0;
2279 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
2280 diff_opts
.rename_limit
= opt
->rename_limit
;
2281 if (opt
->rename_limit
<= 0)
2282 diff_opts
.rename_limit
= 1000;
2283 diff_opts
.rename_score
= opt
->rename_score
;
2284 diff_opts
.show_rename_progress
= opt
->show_rename_progress
;
2285 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
2286 diff_setup_done(&diff_opts
);
2288 diff_queued_diff
= renames
->pairs
[side_index
];
2289 trace2_region_enter("diff", "diffcore_rename", opt
->repo
);
2290 diffcore_rename_extended(&diff_opts
,
2291 &renames
->relevant_sources
[side_index
],
2292 &renames
->dirs_removed
[side_index
],
2293 &renames
->dir_rename_count
[side_index
]);
2294 trace2_region_leave("diff", "diffcore_rename", opt
->repo
);
2295 resolve_diffpair_statuses(&diff_queued_diff
);
2297 if (diff_opts
.needed_rename_limit
> renames
->needed_limit
)
2298 renames
->needed_limit
= diff_opts
.needed_rename_limit
;
2300 renames
->pairs
[side_index
] = diff_queued_diff
;
2302 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
2303 diff_queued_diff
.nr
= 0;
2304 diff_queued_diff
.queue
= NULL
;
2305 diff_flush(&diff_opts
);
2309 * Get information of all renames which occurred in 'side_pairs', discarding
2312 static int collect_renames(struct merge_options
*opt
,
2313 struct diff_queue_struct
*result
,
2314 unsigned side_index
,
2315 struct strmap
*dir_renames_for_side
,
2316 struct strmap
*rename_exclusions
)
2319 struct strmap collisions
;
2320 struct diff_queue_struct
*side_pairs
;
2321 struct hashmap_iter iter
;
2322 struct strmap_entry
*entry
;
2323 struct rename_info
*renames
= &opt
->priv
->renames
;
2325 side_pairs
= &renames
->pairs
[side_index
];
2326 compute_collisions(&collisions
, dir_renames_for_side
, side_pairs
);
2328 for (i
= 0; i
< side_pairs
->nr
; ++i
) {
2329 struct diff_filepair
*p
= side_pairs
->queue
[i
];
2330 char *new_path
; /* non-NULL only with directory renames */
2332 if (p
->status
!= 'A' && p
->status
!= 'R') {
2333 diff_free_filepair(p
);
2337 new_path
= check_for_directory_rename(opt
, p
->two
->path
,
2339 dir_renames_for_side
,
2344 if (p
->status
!= 'R' && !new_path
) {
2345 diff_free_filepair(p
);
2350 apply_directory_rename_modifications(opt
, p
, new_path
);
2353 * p->score comes back from diffcore_rename_extended() with
2354 * the similarity of the renamed file. The similarity is
2355 * was used to determine that the two files were related
2356 * and are a rename, which we have already used, but beyond
2357 * that we have no use for the similarity. So p->score is
2358 * now irrelevant. However, process_renames() will need to
2359 * know which side of the merge this rename was associated
2360 * with, so overwrite p->score with that value.
2362 p
->score
= side_index
;
2363 result
->queue
[result
->nr
++] = p
;
2366 /* Free each value in the collisions map */
2367 strmap_for_each_entry(&collisions
, &iter
, entry
) {
2368 struct collision_info
*info
= entry
->value
;
2369 string_list_clear(&info
->source_files
, 0);
2372 * In compute_collisions(), we set collisions.strdup_strings to 0
2373 * so that we wouldn't have to make another copy of the new_path
2374 * allocated by apply_dir_rename(). But now that we've used them
2375 * and have no other references to these strings, it is time to
2378 free_strmap_strings(&collisions
);
2379 strmap_clear(&collisions
, 1);
2383 static int detect_and_process_renames(struct merge_options
*opt
,
2384 struct tree
*merge_base
,
2388 struct diff_queue_struct combined
;
2389 struct rename_info
*renames
= &opt
->priv
->renames
;
2390 int need_dir_renames
, s
, clean
= 1;
2392 memset(&combined
, 0, sizeof(combined
));
2393 if (!possible_renames(renames
))
2396 trace2_region_enter("merge", "regular renames", opt
->repo
);
2397 detect_regular_renames(opt
, MERGE_SIDE1
);
2398 detect_regular_renames(opt
, MERGE_SIDE2
);
2399 trace2_region_leave("merge", "regular renames", opt
->repo
);
2401 trace2_region_enter("merge", "directory renames", opt
->repo
);
2403 !opt
->priv
->call_depth
&&
2404 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
||
2405 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
);
2407 if (need_dir_renames
) {
2408 get_provisional_directory_renames(opt
, MERGE_SIDE1
, &clean
);
2409 get_provisional_directory_renames(opt
, MERGE_SIDE2
, &clean
);
2410 handle_directory_level_conflicts(opt
);
2413 ALLOC_GROW(combined
.queue
,
2414 renames
->pairs
[1].nr
+ renames
->pairs
[2].nr
,
2416 clean
&= collect_renames(opt
, &combined
, MERGE_SIDE1
,
2417 &renames
->dir_renames
[2],
2418 &renames
->dir_renames
[1]);
2419 clean
&= collect_renames(opt
, &combined
, MERGE_SIDE2
,
2420 &renames
->dir_renames
[1],
2421 &renames
->dir_renames
[2]);
2422 STABLE_QSORT(combined
.queue
, combined
.nr
, compare_pairs
);
2423 trace2_region_leave("merge", "directory renames", opt
->repo
);
2425 trace2_region_enter("merge", "process renames", opt
->repo
);
2426 clean
&= process_renames(opt
, &combined
);
2427 trace2_region_leave("merge", "process renames", opt
->repo
);
2429 goto simple_cleanup
; /* collect_renames() handles some of cleanup */
2433 * Free now unneeded filepairs, which would have been handled
2434 * in collect_renames() normally but we skipped that code.
2436 for (s
= MERGE_SIDE1
; s
<= MERGE_SIDE2
; s
++) {
2437 struct diff_queue_struct
*side_pairs
;
2440 side_pairs
= &renames
->pairs
[s
];
2441 for (i
= 0; i
< side_pairs
->nr
; ++i
) {
2442 struct diff_filepair
*p
= side_pairs
->queue
[i
];
2443 diff_free_filepair(p
);
2448 /* Free memory for renames->pairs[] and combined */
2449 for (s
= MERGE_SIDE1
; s
<= MERGE_SIDE2
; s
++) {
2450 free(renames
->pairs
[s
].queue
);
2451 DIFF_QUEUE_CLEAR(&renames
->pairs
[s
]);
2455 for (i
= 0; i
< combined
.nr
; i
++)
2456 diff_free_filepair(combined
.queue
[i
]);
2457 free(combined
.queue
);
2463 /*** Function Grouping: functions related to process_entries() ***/
2465 static int string_list_df_name_compare(const char *one
, const char *two
)
2467 int onelen
= strlen(one
);
2468 int twolen
= strlen(two
);
2470 * Here we only care that entries for D/F conflicts are
2471 * adjacent, in particular with the file of the D/F conflict
2472 * appearing before files below the corresponding directory.
2473 * The order of the rest of the list is irrelevant for us.
2475 * To achieve this, we sort with df_name_compare and provide
2476 * the mode S_IFDIR so that D/F conflicts will sort correctly.
2477 * We use the mode S_IFDIR for everything else for simplicity,
2478 * since in other cases any changes in their order due to
2479 * sorting cause no problems for us.
2481 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
2482 two
, twolen
, S_IFDIR
);
2484 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
2485 * that 'foo' comes before 'foo/bar'.
2489 return onelen
- twolen
;
2492 struct directory_versions
{
2494 * versions: list of (basename -> version_info)
2496 * The basenames are in reverse lexicographic order of full pathnames,
2497 * as processed in process_entries(). This puts all entries within
2498 * a directory together, and covers the directory itself after
2499 * everything within it, allowing us to write subtrees before needing
2500 * to record information for the tree itself.
2502 struct string_list versions
;
2505 * offsets: list of (full relative path directories -> integer offsets)
2507 * Since versions contains basenames from files in multiple different
2508 * directories, we need to know which entries in versions correspond
2509 * to which directories. Values of e.g.
2513 * Would mean that entries 0-1 of versions are files in the toplevel
2514 * directory, entries 2-4 are files under src/, and the remaining
2515 * entries starting at index 5 are files under src/moduleA/.
2517 struct string_list offsets
;
2520 * last_directory: directory that previously processed file found in
2522 * last_directory starts NULL, but records the directory in which the
2523 * previous file was found within. As soon as
2524 * directory(current_file) != last_directory
2525 * then we need to start updating accounting in versions & offsets.
2526 * Note that last_directory is always the last path in "offsets" (or
2527 * NULL if "offsets" is empty) so this exists just for quick access.
2529 const char *last_directory
;
2531 /* last_directory_len: cached computation of strlen(last_directory) */
2532 unsigned last_directory_len
;
2535 static int tree_entry_order(const void *a_
, const void *b_
)
2537 const struct string_list_item
*a
= a_
;
2538 const struct string_list_item
*b
= b_
;
2540 const struct merged_info
*ami
= a
->util
;
2541 const struct merged_info
*bmi
= b
->util
;
2542 return base_name_compare(a
->string
, strlen(a
->string
), ami
->result
.mode
,
2543 b
->string
, strlen(b
->string
), bmi
->result
.mode
);
2546 static void write_tree(struct object_id
*result_oid
,
2547 struct string_list
*versions
,
2548 unsigned int offset
,
2551 size_t maxlen
= 0, extra
;
2552 unsigned int nr
= versions
->nr
- offset
;
2553 struct strbuf buf
= STRBUF_INIT
;
2554 struct string_list relevant_entries
= STRING_LIST_INIT_NODUP
;
2558 * We want to sort the last (versions->nr-offset) entries in versions.
2559 * Do so by abusing the string_list API a bit: make another string_list
2560 * that contains just those entries and then sort them.
2562 * We won't use relevant_entries again and will let it just pop off the
2563 * stack, so there won't be allocation worries or anything.
2565 relevant_entries
.items
= versions
->items
+ offset
;
2566 relevant_entries
.nr
= versions
->nr
- offset
;
2567 /* No need for STABLE_QSORT -- filenames must be unique */
2568 QSORT(relevant_entries
.items
, relevant_entries
.nr
, tree_entry_order
);
2570 /* Pre-allocate some space in buf */
2571 extra
= hash_size
+ 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */
2572 for (i
= 0; i
< nr
; i
++) {
2573 maxlen
+= strlen(versions
->items
[offset
+i
].string
) + extra
;
2575 strbuf_grow(&buf
, maxlen
);
2577 /* Write each entry out to buf */
2578 for (i
= 0; i
< nr
; i
++) {
2579 struct merged_info
*mi
= versions
->items
[offset
+i
].util
;
2580 struct version_info
*ri
= &mi
->result
;
2581 strbuf_addf(&buf
, "%o %s%c",
2583 versions
->items
[offset
+i
].string
, '\0');
2584 strbuf_add(&buf
, ri
->oid
.hash
, hash_size
);
2587 /* Write this object file out, and record in result_oid */
2588 write_object_file(buf
.buf
, buf
.len
, tree_type
, result_oid
);
2589 strbuf_release(&buf
);
2592 static void record_entry_for_tree(struct directory_versions
*dir_metadata
,
2594 struct merged_info
*mi
)
2596 const char *basename
;
2599 /* nothing to record */
2602 basename
= path
+ mi
->basename_offset
;
2603 assert(strchr(basename
, '/') == NULL
);
2604 string_list_append(&dir_metadata
->versions
,
2605 basename
)->util
= &mi
->result
;
2608 static void write_completed_directory(struct merge_options
*opt
,
2609 const char *new_directory_name
,
2610 struct directory_versions
*info
)
2612 const char *prev_dir
;
2613 struct merged_info
*dir_info
= NULL
;
2614 unsigned int offset
;
2617 * Some explanation of info->versions and info->offsets...
2619 * process_entries() iterates over all relevant files AND
2620 * directories in reverse lexicographic order, and calls this
2621 * function. Thus, an example of the paths that process_entries()
2622 * could operate on (along with the directories for those paths
2627 * src/moduleB/umm.c src/moduleB
2628 * src/moduleB/stuff.h src/moduleB
2629 * src/moduleB/baz.c src/moduleB
2631 * src/moduleA/foo.c src/moduleA
2632 * src/moduleA/bar.c src/moduleA
2639 * always contains the unprocessed entries and their
2640 * version_info information. For example, after the first five
2641 * entries above, info->versions would be:
2643 * xtract.c <xtract.c's version_info>
2644 * token.txt <token.txt's version_info>
2645 * umm.c <src/moduleB/umm.c's version_info>
2646 * stuff.h <src/moduleB/stuff.h's version_info>
2647 * baz.c <src/moduleB/baz.c's version_info>
2649 * Once a subdirectory is completed we remove the entries in
2650 * that subdirectory from info->versions, writing it as a tree
2651 * (write_tree()). Thus, as soon as we get to src/moduleB,
2652 * info->versions would be updated to
2654 * xtract.c <xtract.c's version_info>
2655 * token.txt <token.txt's version_info>
2656 * moduleB <src/moduleB's version_info>
2660 * helps us track which entries in info->versions correspond to
2661 * which directories. When we are N directories deep (e.g. 4
2662 * for src/modA/submod/subdir/), we have up to N+1 unprocessed
2663 * directories (+1 because of toplevel dir). Corresponding to
2664 * the info->versions example above, after processing five entries
2665 * info->offsets will be:
2670 * which is used to know that xtract.c & token.txt are from the
2671 * toplevel dirctory, while umm.c & stuff.h & baz.c are from the
2672 * src/moduleB directory. Again, following the example above,
2673 * once we need to process src/moduleB, then info->offsets is
2679 * which says that moduleB (and only moduleB so far) is in the
2682 * One unique thing to note about info->offsets here is that
2683 * "src" was not added to info->offsets until there was a path
2684 * (a file OR directory) immediately below src/ that got
2687 * Since process_entry() just appends new entries to info->versions,
2688 * write_completed_directory() only needs to do work if the next path
2689 * is in a directory that is different than the last directory found
2694 * If we are working with the same directory as the last entry, there
2695 * is no work to do. (See comments above the directory_name member of
2696 * struct merged_info for why we can use pointer comparison instead of
2699 if (new_directory_name
== info
->last_directory
)
2703 * If we are just starting (last_directory is NULL), or last_directory
2704 * is a prefix of the current directory, then we can just update
2705 * info->offsets to record the offset where we started this directory
2706 * and update last_directory to have quick access to it.
2708 if (info
->last_directory
== NULL
||
2709 !strncmp(new_directory_name
, info
->last_directory
,
2710 info
->last_directory_len
)) {
2711 uintptr_t offset
= info
->versions
.nr
;
2713 info
->last_directory
= new_directory_name
;
2714 info
->last_directory_len
= strlen(info
->last_directory
);
2716 * Record the offset into info->versions where we will
2717 * start recording basenames of paths found within
2718 * new_directory_name.
2720 string_list_append(&info
->offsets
,
2721 info
->last_directory
)->util
= (void*)offset
;
2726 * The next entry that will be processed will be within
2727 * new_directory_name. Since at this point we know that
2728 * new_directory_name is within a different directory than
2729 * info->last_directory, we have all entries for info->last_directory
2730 * in info->versions and we need to create a tree object for them.
2732 dir_info
= strmap_get(&opt
->priv
->paths
, info
->last_directory
);
2734 offset
= (uintptr_t)info
->offsets
.items
[info
->offsets
.nr
-1].util
;
2735 if (offset
== info
->versions
.nr
) {
2737 * Actually, we don't need to create a tree object in this
2738 * case. Whenever all files within a directory disappear
2739 * during the merge (e.g. unmodified on one side and
2740 * deleted on the other, or files were renamed elsewhere),
2741 * then we get here and the directory itself needs to be
2742 * omitted from its parent tree as well.
2744 dir_info
->is_null
= 1;
2747 * Write out the tree to the git object directory, and also
2748 * record the mode and oid in dir_info->result.
2750 dir_info
->is_null
= 0;
2751 dir_info
->result
.mode
= S_IFDIR
;
2752 write_tree(&dir_info
->result
.oid
, &info
->versions
, offset
,
2753 opt
->repo
->hash_algo
->rawsz
);
2757 * We've now used several entries from info->versions and one entry
2758 * from info->offsets, so we get rid of those values.
2761 info
->versions
.nr
= offset
;
2764 * Now we've taken care of the completed directory, but we need to
2765 * prepare things since future entries will be in
2766 * new_directory_name. (In particular, process_entry() will be
2767 * appending new entries to info->versions.) So, we need to make
2768 * sure new_directory_name is the last entry in info->offsets.
2770 prev_dir
= info
->offsets
.nr
== 0 ? NULL
:
2771 info
->offsets
.items
[info
->offsets
.nr
-1].string
;
2772 if (new_directory_name
!= prev_dir
) {
2773 uintptr_t c
= info
->versions
.nr
;
2774 string_list_append(&info
->offsets
,
2775 new_directory_name
)->util
= (void*)c
;
2778 /* And, of course, we need to update last_directory to match. */
2779 info
->last_directory
= new_directory_name
;
2780 info
->last_directory_len
= strlen(info
->last_directory
);
2783 /* Per entry merge function */
2784 static void process_entry(struct merge_options
*opt
,
2786 struct conflict_info
*ci
,
2787 struct directory_versions
*dir_metadata
)
2789 int df_file_index
= 0;
2792 assert(ci
->filemask
>= 0 && ci
->filemask
<= 7);
2793 /* ci->match_mask == 7 was handled in collect_merge_info_callback() */
2794 assert(ci
->match_mask
== 0 || ci
->match_mask
== 3 ||
2795 ci
->match_mask
== 5 || ci
->match_mask
== 6);
2798 record_entry_for_tree(dir_metadata
, path
, &ci
->merged
);
2799 if (ci
->filemask
== 0)
2800 /* nothing else to handle */
2802 assert(ci
->df_conflict
);
2805 if (ci
->df_conflict
&& ci
->merged
.result
.mode
== 0) {
2809 * directory no longer in the way, but we do have a file we
2810 * need to place here so we need to clean away the "directory
2811 * merges to nothing" result.
2813 ci
->df_conflict
= 0;
2814 assert(ci
->filemask
!= 0);
2815 ci
->merged
.clean
= 0;
2816 ci
->merged
.is_null
= 0;
2817 /* and we want to zero out any directory-related entries */
2818 ci
->match_mask
= (ci
->match_mask
& ~ci
->dirmask
);
2820 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++) {
2821 if (ci
->filemask
& (1 << i
))
2823 ci
->stages
[i
].mode
= 0;
2824 oidcpy(&ci
->stages
[i
].oid
, &null_oid
);
2826 } else if (ci
->df_conflict
&& ci
->merged
.result
.mode
!= 0) {
2828 * This started out as a D/F conflict, and the entries in
2829 * the competing directory were not removed by the merge as
2830 * evidenced by write_completed_directory() writing a value
2831 * to ci->merged.result.mode.
2833 struct conflict_info
*new_ci
;
2835 const char *old_path
= path
;
2838 assert(ci
->merged
.result
.mode
== S_IFDIR
);
2841 * If filemask is 1, we can just ignore the file as having
2842 * been deleted on both sides. We do not want to overwrite
2843 * ci->merged.result, since it stores the tree for all the
2846 if (ci
->filemask
== 1) {
2852 * This file still exists on at least one side, and we want
2853 * the directory to remain here, so we need to move this
2854 * path to some new location.
2856 new_ci
= xcalloc(1, sizeof(*new_ci
));
2857 /* We don't really want new_ci->merged.result copied, but it'll
2858 * be overwritten below so it doesn't matter. We also don't
2859 * want any directory mode/oid values copied, but we'll zero
2860 * those out immediately. We do want the rest of ci copied.
2862 memcpy(new_ci
, ci
, sizeof(*ci
));
2863 new_ci
->match_mask
= (new_ci
->match_mask
& ~new_ci
->dirmask
);
2864 new_ci
->dirmask
= 0;
2865 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++) {
2866 if (new_ci
->filemask
& (1 << i
))
2868 /* zero out any entries related to directories */
2869 new_ci
->stages
[i
].mode
= 0;
2870 oidcpy(&new_ci
->stages
[i
].oid
, &null_oid
);
2874 * Find out which side this file came from; note that we
2875 * cannot just use ci->filemask, because renames could cause
2876 * the filemask to go back to 7. So we use dirmask, then
2877 * pick the opposite side's index.
2879 df_file_index
= (ci
->dirmask
& (1 << 1)) ? 2 : 1;
2880 branch
= (df_file_index
== 1) ? opt
->branch1
: opt
->branch2
;
2881 path
= unique_path(&opt
->priv
->paths
, path
, branch
);
2882 strmap_put(&opt
->priv
->paths
, path
, new_ci
);
2884 path_msg(opt
, path
, 0,
2885 _("CONFLICT (file/directory): directory in the way "
2886 "of %s from %s; moving it to %s instead."),
2887 old_path
, branch
, path
);
2890 * Zero out the filemask for the old ci. At this point, ci
2891 * was just an entry for a directory, so we don't need to
2892 * do anything more with it.
2897 * Now note that we're working on the new entry (path was
2904 * NOTE: Below there is a long switch-like if-elseif-elseif... block
2905 * which the code goes through even for the df_conflict cases
2908 if (ci
->match_mask
) {
2909 ci
->merged
.clean
= 1;
2910 if (ci
->match_mask
== 6) {
2911 /* stages[1] == stages[2] */
2912 ci
->merged
.result
.mode
= ci
->stages
[1].mode
;
2913 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[1].oid
);
2915 /* determine the mask of the side that didn't match */
2916 unsigned int othermask
= 7 & ~ci
->match_mask
;
2917 int side
= (othermask
== 4) ? 2 : 1;
2919 ci
->merged
.result
.mode
= ci
->stages
[side
].mode
;
2920 ci
->merged
.is_null
= !ci
->merged
.result
.mode
;
2921 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[side
].oid
);
2923 assert(othermask
== 2 || othermask
== 4);
2924 assert(ci
->merged
.is_null
==
2925 (ci
->filemask
== ci
->match_mask
));
2927 } else if (ci
->filemask
>= 6 &&
2928 (S_IFMT
& ci
->stages
[1].mode
) !=
2929 (S_IFMT
& ci
->stages
[2].mode
)) {
2930 /* Two different items from (file/submodule/symlink) */
2931 if (opt
->priv
->call_depth
) {
2932 /* Just use the version from the merge base */
2933 ci
->merged
.clean
= 0;
2934 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[0].oid
);
2935 ci
->merged
.result
.mode
= ci
->stages
[0].mode
;
2936 ci
->merged
.is_null
= (ci
->merged
.result
.mode
== 0);
2938 /* Handle by renaming one or both to separate paths. */
2939 unsigned o_mode
= ci
->stages
[0].mode
;
2940 unsigned a_mode
= ci
->stages
[1].mode
;
2941 unsigned b_mode
= ci
->stages
[2].mode
;
2942 struct conflict_info
*new_ci
;
2943 const char *a_path
= NULL
, *b_path
= NULL
;
2944 int rename_a
= 0, rename_b
= 0;
2946 new_ci
= xmalloc(sizeof(*new_ci
));
2948 if (S_ISREG(a_mode
))
2950 else if (S_ISREG(b_mode
))
2957 path_msg(opt
, path
, 0,
2958 _("CONFLICT (distinct types): %s had different "
2959 "types on each side; renamed %s of them so "
2960 "each can be recorded somewhere."),
2962 (rename_a
&& rename_b
) ? _("both") : _("one"));
2964 ci
->merged
.clean
= 0;
2965 memcpy(new_ci
, ci
, sizeof(*new_ci
));
2967 /* Put b into new_ci, removing a from stages */
2968 new_ci
->merged
.result
.mode
= ci
->stages
[2].mode
;
2969 oidcpy(&new_ci
->merged
.result
.oid
, &ci
->stages
[2].oid
);
2970 new_ci
->stages
[1].mode
= 0;
2971 oidcpy(&new_ci
->stages
[1].oid
, &null_oid
);
2972 new_ci
->filemask
= 5;
2973 if ((S_IFMT
& b_mode
) != (S_IFMT
& o_mode
)) {
2974 new_ci
->stages
[0].mode
= 0;
2975 oidcpy(&new_ci
->stages
[0].oid
, &null_oid
);
2976 new_ci
->filemask
= 4;
2979 /* Leave only a in ci, fixing stages. */
2980 ci
->merged
.result
.mode
= ci
->stages
[1].mode
;
2981 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[1].oid
);
2982 ci
->stages
[2].mode
= 0;
2983 oidcpy(&ci
->stages
[2].oid
, &null_oid
);
2985 if ((S_IFMT
& a_mode
) != (S_IFMT
& o_mode
)) {
2986 ci
->stages
[0].mode
= 0;
2987 oidcpy(&ci
->stages
[0].oid
, &null_oid
);
2991 /* Insert entries into opt->priv_paths */
2992 assert(rename_a
|| rename_b
);
2994 a_path
= unique_path(&opt
->priv
->paths
,
2995 path
, opt
->branch1
);
2996 strmap_put(&opt
->priv
->paths
, a_path
, ci
);
3000 b_path
= unique_path(&opt
->priv
->paths
,
3001 path
, opt
->branch2
);
3004 strmap_put(&opt
->priv
->paths
, b_path
, new_ci
);
3006 if (rename_a
&& rename_b
) {
3007 strmap_remove(&opt
->priv
->paths
, path
, 0);
3009 * We removed path from opt->priv->paths. path
3010 * will also eventually need to be freed, but
3011 * it may still be used by e.g. ci->pathnames.
3012 * So, store it in another string-list for now.
3014 string_list_append(&opt
->priv
->paths_to_free
,
3019 * Do special handling for b_path since process_entry()
3020 * won't be called on it specially.
3022 strmap_put(&opt
->priv
->conflicted
, b_path
, new_ci
);
3023 record_entry_for_tree(dir_metadata
, b_path
,
3027 * Remaining code for processing this entry should
3028 * think in terms of processing a_path.
3033 } else if (ci
->filemask
>= 6) {
3034 /* Need a two-way or three-way content merge */
3035 struct version_info merged_file
;
3036 unsigned clean_merge
;
3037 struct version_info
*o
= &ci
->stages
[0];
3038 struct version_info
*a
= &ci
->stages
[1];
3039 struct version_info
*b
= &ci
->stages
[2];
3041 clean_merge
= handle_content_merge(opt
, path
, o
, a
, b
,
3043 opt
->priv
->call_depth
* 2,
3045 ci
->merged
.clean
= clean_merge
&&
3046 !ci
->df_conflict
&& !ci
->path_conflict
;
3047 ci
->merged
.result
.mode
= merged_file
.mode
;
3048 ci
->merged
.is_null
= (merged_file
.mode
== 0);
3049 oidcpy(&ci
->merged
.result
.oid
, &merged_file
.oid
);
3050 if (clean_merge
&& ci
->df_conflict
) {
3051 assert(df_file_index
== 1 || df_file_index
== 2);
3052 ci
->filemask
= 1 << df_file_index
;
3053 ci
->stages
[df_file_index
].mode
= merged_file
.mode
;
3054 oidcpy(&ci
->stages
[df_file_index
].oid
, &merged_file
.oid
);
3057 const char *reason
= _("content");
3058 if (ci
->filemask
== 6)
3059 reason
= _("add/add");
3060 if (S_ISGITLINK(merged_file
.mode
))
3061 reason
= _("submodule");
3062 path_msg(opt
, path
, 0,
3063 _("CONFLICT (%s): Merge conflict in %s"),
3066 } else if (ci
->filemask
== 3 || ci
->filemask
== 5) {
3068 const char *modify_branch
, *delete_branch
;
3069 int side
= (ci
->filemask
== 5) ? 2 : 1;
3070 int index
= opt
->priv
->call_depth
? 0 : side
;
3072 ci
->merged
.result
.mode
= ci
->stages
[index
].mode
;
3073 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[index
].oid
);
3074 ci
->merged
.clean
= 0;
3076 modify_branch
= (side
== 1) ? opt
->branch1
: opt
->branch2
;
3077 delete_branch
= (side
== 1) ? opt
->branch2
: opt
->branch1
;
3079 if (ci
->path_conflict
&&
3080 oideq(&ci
->stages
[0].oid
, &ci
->stages
[side
].oid
)) {
3082 * This came from a rename/delete; no action to take,
3083 * but avoid printing "modify/delete" conflict notice
3084 * since the contents were not modified.
3087 path_msg(opt
, path
, 0,
3088 _("CONFLICT (modify/delete): %s deleted in %s "
3089 "and modified in %s. Version %s of %s left "
3091 path
, delete_branch
, modify_branch
,
3092 modify_branch
, path
);
3094 } else if (ci
->filemask
== 2 || ci
->filemask
== 4) {
3095 /* Added on one side */
3096 int side
= (ci
->filemask
== 4) ? 2 : 1;
3097 ci
->merged
.result
.mode
= ci
->stages
[side
].mode
;
3098 oidcpy(&ci
->merged
.result
.oid
, &ci
->stages
[side
].oid
);
3099 ci
->merged
.clean
= !ci
->df_conflict
&& !ci
->path_conflict
;
3100 } else if (ci
->filemask
== 1) {
3101 /* Deleted on both sides */
3102 ci
->merged
.is_null
= 1;
3103 ci
->merged
.result
.mode
= 0;
3104 oidcpy(&ci
->merged
.result
.oid
, &null_oid
);
3105 ci
->merged
.clean
= !ci
->path_conflict
;
3109 * If still conflicted, record it separately. This allows us to later
3110 * iterate over just conflicted entries when updating the index instead
3111 * of iterating over all entries.
3113 if (!ci
->merged
.clean
)
3114 strmap_put(&opt
->priv
->conflicted
, path
, ci
);
3115 record_entry_for_tree(dir_metadata
, path
, &ci
->merged
);
3118 static void process_entries(struct merge_options
*opt
,
3119 struct object_id
*result_oid
)
3121 struct hashmap_iter iter
;
3122 struct strmap_entry
*e
;
3123 struct string_list plist
= STRING_LIST_INIT_NODUP
;
3124 struct string_list_item
*entry
;
3125 struct directory_versions dir_metadata
= { STRING_LIST_INIT_NODUP
,
3126 STRING_LIST_INIT_NODUP
,
3129 trace2_region_enter("merge", "process_entries setup", opt
->repo
);
3130 if (strmap_empty(&opt
->priv
->paths
)) {
3131 oidcpy(result_oid
, opt
->repo
->hash_algo
->empty_tree
);
3135 /* Hack to pre-allocate plist to the desired size */
3136 trace2_region_enter("merge", "plist grow", opt
->repo
);
3137 ALLOC_GROW(plist
.items
, strmap_get_size(&opt
->priv
->paths
), plist
.alloc
);
3138 trace2_region_leave("merge", "plist grow", opt
->repo
);
3140 /* Put every entry from paths into plist, then sort */
3141 trace2_region_enter("merge", "plist copy", opt
->repo
);
3142 strmap_for_each_entry(&opt
->priv
->paths
, &iter
, e
) {
3143 string_list_append(&plist
, e
->key
)->util
= e
->value
;
3145 trace2_region_leave("merge", "plist copy", opt
->repo
);
3147 trace2_region_enter("merge", "plist special sort", opt
->repo
);
3148 plist
.cmp
= string_list_df_name_compare
;
3149 string_list_sort(&plist
);
3150 trace2_region_leave("merge", "plist special sort", opt
->repo
);
3152 trace2_region_leave("merge", "process_entries setup", opt
->repo
);
3155 * Iterate over the items in reverse order, so we can handle paths
3156 * below a directory before needing to handle the directory itself.
3158 * This allows us to write subtrees before we need to write trees,
3159 * and it also enables sane handling of directory/file conflicts
3160 * (because it allows us to know whether the directory is still in
3161 * the way when it is time to process the file at the same path).
3163 trace2_region_enter("merge", "processing", opt
->repo
);
3164 for (entry
= &plist
.items
[plist
.nr
-1]; entry
>= plist
.items
; --entry
) {
3165 char *path
= entry
->string
;
3167 * NOTE: mi may actually be a pointer to a conflict_info, but
3168 * we have to check mi->clean first to see if it's safe to
3169 * reassign to such a pointer type.
3171 struct merged_info
*mi
= entry
->util
;
3173 write_completed_directory(opt
, mi
->directory_name
,
3176 record_entry_for_tree(&dir_metadata
, path
, mi
);
3178 struct conflict_info
*ci
= (struct conflict_info
*)mi
;
3179 process_entry(opt
, path
, ci
, &dir_metadata
);
3182 trace2_region_leave("merge", "processing", opt
->repo
);
3184 trace2_region_enter("merge", "process_entries cleanup", opt
->repo
);
3185 if (dir_metadata
.offsets
.nr
!= 1 ||
3186 (uintptr_t)dir_metadata
.offsets
.items
[0].util
!= 0) {
3187 printf("dir_metadata.offsets.nr = %d (should be 1)\n",
3188 dir_metadata
.offsets
.nr
);
3189 printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n",
3190 (unsigned)(uintptr_t)dir_metadata
.offsets
.items
[0].util
);
3192 BUG("dir_metadata accounting completely off; shouldn't happen");
3194 write_tree(result_oid
, &dir_metadata
.versions
, 0,
3195 opt
->repo
->hash_algo
->rawsz
);
3196 string_list_clear(&plist
, 0);
3197 string_list_clear(&dir_metadata
.versions
, 0);
3198 string_list_clear(&dir_metadata
.offsets
, 0);
3199 trace2_region_leave("merge", "process_entries cleanup", opt
->repo
);
3202 /*** Function Grouping: functions related to merge_switch_to_result() ***/
3204 static int checkout(struct merge_options
*opt
,
3208 /* Switch the index/working copy from old to new */
3210 struct tree_desc trees
[2];
3211 struct unpack_trees_options unpack_opts
;
3213 memset(&unpack_opts
, 0, sizeof(unpack_opts
));
3214 unpack_opts
.head_idx
= -1;
3215 unpack_opts
.src_index
= opt
->repo
->index
;
3216 unpack_opts
.dst_index
= opt
->repo
->index
;
3218 setup_unpack_trees_porcelain(&unpack_opts
, "merge");
3221 * NOTE: if this were just "git checkout" code, we would probably
3222 * read or refresh the cache and check for a conflicted index, but
3223 * builtin/merge.c or sequencer.c really needs to read the index
3224 * and check for conflicted entries before starting merging for a
3225 * good user experience (no sense waiting for merges/rebases before
3226 * erroring out), so there's no reason to duplicate that work here.
3229 /* 2-way merge to the new branch */
3230 unpack_opts
.update
= 1;
3231 unpack_opts
.merge
= 1;
3232 unpack_opts
.quiet
= 0; /* FIXME: sequencer might want quiet? */
3233 unpack_opts
.verbose_update
= (opt
->verbosity
> 2);
3234 unpack_opts
.fn
= twoway_merge
;
3235 if (1/* FIXME: opts->overwrite_ignore*/) {
3236 unpack_opts
.dir
= xcalloc(1, sizeof(*unpack_opts
.dir
));
3237 unpack_opts
.dir
->flags
|= DIR_SHOW_IGNORED
;
3238 setup_standard_excludes(unpack_opts
.dir
);
3241 init_tree_desc(&trees
[0], prev
->buffer
, prev
->size
);
3243 init_tree_desc(&trees
[1], next
->buffer
, next
->size
);
3245 ret
= unpack_trees(2, trees
, &unpack_opts
);
3246 clear_unpack_trees_porcelain(&unpack_opts
);
3247 dir_clear(unpack_opts
.dir
);
3248 FREE_AND_NULL(unpack_opts
.dir
);
3252 static int record_conflicted_index_entries(struct merge_options
*opt
,
3253 struct index_state
*index
,
3254 struct strmap
*paths
,
3255 struct strmap
*conflicted
)
3257 struct hashmap_iter iter
;
3258 struct strmap_entry
*e
;
3260 int original_cache_nr
;
3262 if (strmap_empty(conflicted
))
3265 original_cache_nr
= index
->cache_nr
;
3267 /* Put every entry from paths into plist, then sort */
3268 strmap_for_each_entry(conflicted
, &iter
, e
) {
3269 const char *path
= e
->key
;
3270 struct conflict_info
*ci
= e
->value
;
3272 struct cache_entry
*ce
;
3278 * The index will already have a stage=0 entry for this path,
3279 * because we created an as-merged-as-possible version of the
3280 * file and checkout() moved the working copy and index over
3283 * However, previous iterations through this loop will have
3284 * added unstaged entries to the end of the cache which
3285 * ignore the standard alphabetical ordering of cache
3286 * entries and break invariants needed for index_name_pos()
3287 * to work. However, we know the entry we want is before
3288 * those appended cache entries, so do a temporary swap on
3289 * cache_nr to only look through entries of interest.
3291 SWAP(index
->cache_nr
, original_cache_nr
);
3292 pos
= index_name_pos(index
, path
, strlen(path
));
3293 SWAP(index
->cache_nr
, original_cache_nr
);
3295 if (ci
->filemask
!= 1)
3296 BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path
);
3297 cache_tree_invalidate_path(index
, path
);
3299 ce
= index
->cache
[pos
];
3302 * Clean paths with CE_SKIP_WORKTREE set will not be
3303 * written to the working tree by the unpack_trees()
3304 * call in checkout(). Our conflicted entries would
3305 * have appeared clean to that code since we ignored
3306 * the higher order stages. Thus, we need override
3307 * the CE_SKIP_WORKTREE bit and manually write those
3308 * files to the working disk here.
3310 * TODO: Implement this CE_SKIP_WORKTREE fixup.
3314 * Mark this cache entry for removal and instead add
3315 * new stage>0 entries corresponding to the
3316 * conflicts. If there are many conflicted entries, we
3317 * want to avoid memmove'ing O(NM) entries by
3318 * inserting the new entries one at a time. So,
3319 * instead, we just add the new cache entries to the
3320 * end (ignoring normal index requirements on sort
3321 * order) and sort the index once we're all done.
3323 ce
->ce_flags
|= CE_REMOVE
;
3326 for (i
= MERGE_BASE
; i
<= MERGE_SIDE2
; i
++) {
3327 struct version_info
*vi
;
3328 if (!(ci
->filemask
& (1ul << i
)))
3330 vi
= &ci
->stages
[i
];
3331 ce
= make_cache_entry(index
, vi
->mode
, &vi
->oid
,
3333 add_index_entry(index
, ce
, ADD_CACHE_JUST_APPEND
);
3338 * Remove the unused cache entries (and invalidate the relevant
3339 * cache-trees), then sort the index entries to get the conflicted
3340 * entries we added to the end into their right locations.
3342 remove_marked_cache_entries(index
, 1);
3344 * No need for STABLE_QSORT -- cmp_cache_name_compare sorts primarily
3345 * on filename and secondarily on stage, and (name, stage #) are a
3348 QSORT(index
->cache
, index
->cache_nr
, cmp_cache_name_compare
);
3353 void merge_switch_to_result(struct merge_options
*opt
,
3355 struct merge_result
*result
,
3356 int update_worktree_and_index
,
3357 int display_update_msgs
)
3359 assert(opt
->priv
== NULL
);
3360 if (result
->clean
>= 0 && update_worktree_and_index
) {
3361 struct merge_options_internal
*opti
= result
->priv
;
3363 trace2_region_enter("merge", "checkout", opt
->repo
);
3364 if (checkout(opt
, head
, result
->tree
)) {
3365 /* failure to function */
3369 trace2_region_leave("merge", "checkout", opt
->repo
);
3371 trace2_region_enter("merge", "record_conflicted", opt
->repo
);
3372 if (record_conflicted_index_entries(opt
, opt
->repo
->index
,
3374 &opti
->conflicted
)) {
3375 /* failure to function */
3379 trace2_region_leave("merge", "record_conflicted", opt
->repo
);
3382 if (display_update_msgs
) {
3383 struct merge_options_internal
*opti
= result
->priv
;
3384 struct hashmap_iter iter
;
3385 struct strmap_entry
*e
;
3386 struct string_list olist
= STRING_LIST_INIT_NODUP
;
3389 trace2_region_enter("merge", "display messages", opt
->repo
);
3391 /* Hack to pre-allocate olist to the desired size */
3392 ALLOC_GROW(olist
.items
, strmap_get_size(&opti
->output
),
3395 /* Put every entry from output into olist, then sort */
3396 strmap_for_each_entry(&opti
->output
, &iter
, e
) {
3397 string_list_append(&olist
, e
->key
)->util
= e
->value
;
3399 string_list_sort(&olist
);
3401 /* Iterate over the items, printing them */
3402 for (i
= 0; i
< olist
.nr
; ++i
) {
3403 struct strbuf
*sb
= olist
.items
[i
].util
;
3405 printf("%s", sb
->buf
);
3407 string_list_clear(&olist
, 0);
3409 /* Also include needed rename limit adjustment now */
3410 diff_warn_rename_limit("merge.renamelimit",
3411 opti
->renames
.needed_limit
, 0);
3413 trace2_region_leave("merge", "display messages", opt
->repo
);
3416 merge_finalize(opt
, result
);
3419 void merge_finalize(struct merge_options
*opt
,
3420 struct merge_result
*result
)
3422 struct merge_options_internal
*opti
= result
->priv
;
3424 if (opt
->renormalize
)
3425 git_attr_set_direction(GIT_ATTR_CHECKIN
);
3426 assert(opt
->priv
== NULL
);
3428 clear_or_reinit_internal_opts(opti
, 0);
3429 FREE_AND_NULL(opti
);
3432 /*** Function Grouping: helper functions for merge_incore_*() ***/
3434 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
3439 static struct commit
*make_virtual_commit(struct repository
*repo
,
3441 const char *comment
)
3443 struct commit
*commit
= alloc_commit_node(repo
);
3445 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
3446 set_commit_tree(commit
, tree
);
3447 commit
->object
.parsed
= 1;
3451 static void merge_start(struct merge_options
*opt
, struct merge_result
*result
)
3453 struct rename_info
*renames
;
3456 /* Sanity checks on opt */
3457 trace2_region_enter("merge", "sanity checks", opt
->repo
);
3460 assert(opt
->branch1
&& opt
->branch2
);
3462 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3463 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3464 assert(opt
->rename_limit
>= -1);
3465 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3466 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3468 assert(opt
->xdl_opts
>= 0);
3469 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3470 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3473 * detect_renames, verbosity, buffer_output, and obuf are ignored
3474 * fields that were used by "recursive" rather than "ort" -- but
3475 * sanity check them anyway.
3477 assert(opt
->detect_renames
>= -1 &&
3478 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3479 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3480 assert(opt
->buffer_output
<= 2);
3481 assert(opt
->obuf
.len
== 0);
3483 assert(opt
->priv
== NULL
);
3485 opt
->priv
= result
->priv
;
3486 result
->priv
= NULL
;
3488 * opt->priv non-NULL means we had results from a previous
3489 * run; do a few sanity checks that user didn't mess with
3490 * it in an obvious fashion.
3492 assert(opt
->priv
->call_depth
== 0);
3493 assert(!opt
->priv
->toplevel_dir
||
3494 0 == strlen(opt
->priv
->toplevel_dir
));
3496 trace2_region_leave("merge", "sanity checks", opt
->repo
);
3498 /* Default to histogram diff. Actually, just hardcode it...for now. */
3499 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3501 /* Handle attr direction stuff for renormalization */
3502 if (opt
->renormalize
)
3503 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
3505 /* Initialization of opt->priv, our internal merge data */
3506 trace2_region_enter("merge", "allocate/init", opt
->repo
);
3508 clear_or_reinit_internal_opts(opt
->priv
, 1);
3509 trace2_region_leave("merge", "allocate/init", opt
->repo
);
3512 opt
->priv
= xcalloc(1, sizeof(*opt
->priv
));
3514 /* Initialization of various renames fields */
3515 renames
= &opt
->priv
->renames
;
3516 for (i
= MERGE_SIDE1
; i
<= MERGE_SIDE2
; i
++) {
3517 strintmap_init_with_options(&renames
->dirs_removed
[i
],
3518 NOT_RELEVANT
, NULL
, 0);
3519 strmap_init_with_options(&renames
->dir_rename_count
[i
],
3521 strmap_init_with_options(&renames
->dir_renames
[i
],
3523 strintmap_init_with_options(&renames
->relevant_sources
[i
],
3528 * Although we initialize opt->priv->paths with strdup_strings=0,
3529 * that's just to avoid making yet another copy of an allocated
3530 * string. Putting the entry into paths means we are taking
3531 * ownership, so we will later free it. paths_to_free is similar.
3533 * In contrast, conflicted just has a subset of keys from paths, so
3534 * we don't want to free those (it'd be a duplicate free).
3536 strmap_init_with_options(&opt
->priv
->paths
, NULL
, 0);
3537 strmap_init_with_options(&opt
->priv
->conflicted
, NULL
, 0);
3538 string_list_init(&opt
->priv
->paths_to_free
, 0);
3541 * keys & strbufs in output will sometimes need to outlive "paths",
3542 * so it will have a copy of relevant keys. It's probably a small
3543 * subset of the overall paths that have special output.
3545 strmap_init(&opt
->priv
->output
);
3547 trace2_region_leave("merge", "allocate/init", opt
->repo
);
3550 /*** Function Grouping: merge_incore_*() and their internal variants ***/
3553 * Originally from merge_trees_internal(); heavily adapted, though.
3555 static void merge_ort_nonrecursive_internal(struct merge_options
*opt
,
3556 struct tree
*merge_base
,
3559 struct merge_result
*result
)
3561 struct object_id working_tree_oid
;
3563 trace2_region_enter("merge", "collect_merge_info", opt
->repo
);
3564 if (collect_merge_info(opt
, merge_base
, side1
, side2
) != 0) {
3566 * TRANSLATORS: The %s arguments are: 1) tree hash of a merge
3567 * base, and 2-3) the trees for the two trees we're merging.
3569 err(opt
, _("collecting merge info failed for trees %s, %s, %s"),
3570 oid_to_hex(&merge_base
->object
.oid
),
3571 oid_to_hex(&side1
->object
.oid
),
3572 oid_to_hex(&side2
->object
.oid
));
3576 trace2_region_leave("merge", "collect_merge_info", opt
->repo
);
3578 trace2_region_enter("merge", "renames", opt
->repo
);
3579 result
->clean
= detect_and_process_renames(opt
, merge_base
,
3581 trace2_region_leave("merge", "renames", opt
->repo
);
3583 trace2_region_enter("merge", "process_entries", opt
->repo
);
3584 process_entries(opt
, &working_tree_oid
);
3585 trace2_region_leave("merge", "process_entries", opt
->repo
);
3587 /* Set return values */
3588 result
->tree
= parse_tree_indirect(&working_tree_oid
);
3589 /* existence of conflicted entries implies unclean */
3590 result
->clean
&= strmap_empty(&opt
->priv
->conflicted
);
3591 if (!opt
->priv
->call_depth
) {
3592 result
->priv
= opt
->priv
;
3598 * Originally from merge_recursive_internal(); somewhat adapted, though.
3600 static void merge_ort_internal(struct merge_options
*opt
,
3601 struct commit_list
*merge_bases
,
3604 struct merge_result
*result
)
3606 struct commit_list
*iter
;
3607 struct commit
*merged_merge_bases
;
3608 const char *ancestor_name
;
3609 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3612 merge_bases
= get_merge_bases(h1
, h2
);
3613 /* See merge-ort.h:merge_incore_recursive() declaration NOTE */
3614 merge_bases
= reverse_commit_list(merge_bases
);
3617 merged_merge_bases
= pop_commit(&merge_bases
);
3618 if (merged_merge_bases
== NULL
) {
3619 /* if there is no common ancestor, use an empty tree */
3622 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3623 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3625 ancestor_name
= "empty tree";
3626 } else if (merge_bases
) {
3627 ancestor_name
= "merged common ancestors";
3629 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3630 &merged_merge_bases
->object
.oid
,
3632 ancestor_name
= merge_base_abbrev
.buf
;
3635 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3636 const char *saved_b1
, *saved_b2
;
3637 struct commit
*prev
= merged_merge_bases
;
3639 opt
->priv
->call_depth
++;
3641 * When the merge fails, the result contains files
3642 * with conflict markers. The cleanness flag is
3643 * ignored (unless indicating an error), it was never
3644 * actually used, as result of merge_trees has always
3645 * overwritten it: the committed "conflicts" were
3648 saved_b1
= opt
->branch1
;
3649 saved_b2
= opt
->branch2
;
3650 opt
->branch1
= "Temporary merge branch 1";
3651 opt
->branch2
= "Temporary merge branch 2";
3652 merge_ort_internal(opt
, NULL
, prev
, iter
->item
, result
);
3653 if (result
->clean
< 0)
3655 opt
->branch1
= saved_b1
;
3656 opt
->branch2
= saved_b2
;
3657 opt
->priv
->call_depth
--;
3659 merged_merge_bases
= make_virtual_commit(opt
->repo
,
3662 commit_list_insert(prev
, &merged_merge_bases
->parents
);
3663 commit_list_insert(iter
->item
,
3664 &merged_merge_bases
->parents
->next
);
3666 clear_or_reinit_internal_opts(opt
->priv
, 1);
3669 opt
->ancestor
= ancestor_name
;
3670 merge_ort_nonrecursive_internal(opt
,
3671 repo_get_commit_tree(opt
->repo
,
3672 merged_merge_bases
),
3673 repo_get_commit_tree(opt
->repo
, h1
),
3674 repo_get_commit_tree(opt
->repo
, h2
),
3676 strbuf_release(&merge_base_abbrev
);
3677 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3680 void merge_incore_nonrecursive(struct merge_options
*opt
,
3681 struct tree
*merge_base
,
3684 struct merge_result
*result
)
3686 trace2_region_enter("merge", "incore_nonrecursive", opt
->repo
);
3688 trace2_region_enter("merge", "merge_start", opt
->repo
);
3689 assert(opt
->ancestor
!= NULL
);
3690 merge_start(opt
, result
);
3691 trace2_region_leave("merge", "merge_start", opt
->repo
);
3693 merge_ort_nonrecursive_internal(opt
, merge_base
, side1
, side2
, result
);
3694 trace2_region_leave("merge", "incore_nonrecursive", opt
->repo
);
3697 void merge_incore_recursive(struct merge_options
*opt
,
3698 struct commit_list
*merge_bases
,
3699 struct commit
*side1
,
3700 struct commit
*side2
,
3701 struct merge_result
*result
)
3703 trace2_region_enter("merge", "incore_recursive", opt
->repo
);
3705 /* We set the ancestor label based on the merge_bases */
3706 assert(opt
->ancestor
== NULL
);
3708 trace2_region_enter("merge", "merge_start", opt
->repo
);
3709 merge_start(opt
, result
);
3710 trace2_region_leave("merge", "merge_start", opt
->repo
);
3712 merge_ort_internal(opt
, merge_bases
, side1
, side2
, result
);
3713 trace2_region_leave("merge", "incore_recursive", opt
->repo
);