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"
22 #include "cache-tree.h"
24 #include "commit-reach.h"
28 #include "object-store.h"
31 #include "unpack-trees.h"
32 #include "xdiff-interface.h"
35 * We have many arrays of size 3. Whenever we have such an array, the
36 * indices refer to one of the sides of the three-way merge. This is so
37 * pervasive that the constants 0, 1, and 2 are used in many places in the
38 * code (especially in arithmetic operations to find the other side's index
39 * or to compute a relevant mask), but sometimes these enum names are used
40 * to aid code clarity.
42 * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
43 * referred to there is one of these three sides.
53 * All variables that are arrays of size 3 correspond to data tracked
54 * for the sides in enum merge_side. Index 0 is almost always unused
55 * because we often only need to track information for MERGE_SIDE1 and
56 * MERGE_SIDE2 (MERGE_BASE can't have rename information since renames
57 * are determined relative to what changed since the MERGE_BASE).
61 * pairs: pairing of filenames from diffcore_rename()
63 struct diff_queue_struct pairs[3];
66 * dirs_removed: directories removed on a given side of history.
68 struct strset dirs_removed[3];
71 * dir_rename_count: tracking where parts of a directory were renamed to
73 * When files in a directory are renamed, they may not all go to the
74 * same location. Each strmap here tracks:
75 * old_dir => {new_dir => int}
76 * That is, dir_rename_count[side] is a strmap to a strintmap.
78 struct strmap dir_rename_count[3];
81 * dir_renames: computed directory renames
83 * This is a map of old_dir => new_dir and is derived in part from
86 struct strmap dir_renames[3];
89 * needed_limit: value needed for inexact rename detection to run
91 * If the current rename limit wasn't high enough for inexact
92 * rename detection to run, this records the limit needed. Otherwise,
93 * this value remains 0.
98 struct merge_options_internal {
100 * paths: primary data structure in all of merge ort.
103 * * are full relative paths from the toplevel of the repository
104 * (e.g. "drivers/firmware/raspberrypi.c").
105 * * store all relevant paths in the repo, both directories and
106 * files (e.g. drivers, drivers/firmware would also be included)
107 * * these keys serve to intern all the path strings, which allows
108 * us to do pointer comparison on directory names instead of
109 * strcmp; we just have to be careful to use the interned strings.
110 * (Technically paths_to_free may track some strings that were
111 * removed from froms paths.)
113 * The values of paths:
114 * * either a pointer to a merged_info, or a conflict_info struct
115 * * merged_info contains all relevant information for a
116 * non-conflicted entry.
117 * * conflict_info contains a merged_info, plus any additional
118 * information about a conflict such as the higher orders stages
119 * involved and the names of the paths those came from (handy
120 * once renames get involved).
121 * * a path may start "conflicted" (i.e. point to a conflict_info)
122 * and then a later step (e.g. three-way content merge) determines
123 * it can be cleanly merged, at which point it'll be marked clean
124 * and the algorithm will ignore any data outside the contained
125 * merged_info for that entry
126 * * If an entry remains conflicted, the merged_info portion of a
127 * conflict_info will later be filled with whatever version of
128 * the file should be placed in the working directory (e.g. an
129 * as-merged-as-possible variation that contains conflict markers).
134 * conflicted: a subset of keys->values from "paths"
136 * conflicted is basically an optimization between process_entries()
137 * and record_conflicted_index_entries(); the latter could loop over
138 * ALL the entries in paths AGAIN and look for the ones that are
139 * still conflicted, but since process_entries() has to loop over
140 * all of them, it saves the ones it couldn't resolve in this strmap
141 * so that record_conflicted_index_entries() can iterate just the
144 struct strmap conflicted;
147 * paths_to_free: additional list of strings to free
149 * If keys are removed from "paths", they are added to paths_to_free
150 * to ensure they are later freed. We avoid free'ing immediately since
151 * other places (e.g. conflict_info.pathnames[]) may still be
152 * referencing these paths.
154 struct string_list paths_to_free;
157 * output: special messages and conflict notices for various paths
159 * This is a map of pathnames (a subset of the keys in "paths" above)
160 * to strbufs. It gathers various warning/conflict/notice messages
161 * for later processing.
163 struct strmap output;
166 * renames: various data relating to rename detection
168 struct rename_info renames;
171 * current_dir_name: temporary var used in collect_merge_info_callback()
173 * Used to set merged_info.directory_name; see documentation for that
174 * variable and the requirements placed on that field.
176 const char *current_dir_name;
178 /* call_depth: recursion level counter for merging merge bases */
182 struct version_info {
183 struct object_id oid;
188 /* if is_null, ignore result. otherwise result has oid & mode */
189 struct version_info result;
193 * clean: whether the path in question is cleanly merged.
195 * see conflict_info.merged for more details.
200 * basename_offset: offset of basename of path.
202 * perf optimization to avoid recomputing offset of final '/'
203 * character in pathname (0 if no '/' in pathname).
205 size_t basename_offset;
208 * directory_name: containing directory name.
210 * Note that we assume directory_name is constructed such that
211 * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
212 * i.e. string equality is equivalent to pointer equality. For this
213 * to hold, we have to be careful setting directory_name.
215 const char *directory_name;
218 struct conflict_info {
220 * merged: the version of the path that will be written to working tree
222 * WARNING: It is critical to check merged.clean and ensure it is 0
223 * before reading any conflict_info fields outside of merged.
224 * Allocated merge_info structs will always have clean set to 1.
225 * Allocated conflict_info structs will have merged.clean set to 0
226 * initially. The merged.clean field is how we know if it is safe
227 * to access other parts of conflict_info besides merged; if a
228 * conflict_info's merged.clean is changed to 1, the rest of the
229 * algorithm is not allowed to look at anything outside of the
230 * merged member anymore.
232 struct merged_info merged;
234 /* oids & modes from each of the three trees for this path */
235 struct version_info stages[3];
237 /* pathnames for each stage; may differ due to rename detection */
238 const char *pathnames[3];
240 /* Whether this path is/was involved in a directory/file conflict */
241 unsigned df_conflict:1;
244 * Whether this path is/was involved in a non-content conflict other
245 * than a directory/file conflict (e.g. rename/rename, rename/delete,
246 * file location based on possible directory rename).
248 unsigned path_conflict:1;
251 * For filemask and dirmask, the ith bit corresponds to whether the
252 * ith entry is a file (filemask) or a directory (dirmask). Thus,
253 * filemask & dirmask is always zero, and filemask | dirmask is at
254 * most 7 but can be less when a path does not appear as either a
255 * file or a directory on at least one side of history.
257 * Note that these masks are related to enum merge_side, as the ith
258 * entry corresponds to side i.
260 * These values come from a traverse_trees() call; more info may be
261 * found looking at tree-walk.h's struct traverse_info,
262 * particularly the documentation above the "fn" member (note that
263 * filemask = mask & ~dirmask from that documentation).
269 * Optimization to track which stages match, to avoid the need to
270 * recompute it in multiple steps. Either 0 or at least 2 bits are
271 * set; if at least 2 bits are set, their corresponding stages match.
273 unsigned match_mask:3;
276 /*** Function Grouping: various utility functions ***/
279 * For the next three macros, see warning for conflict_info.merged.
281 * In each of the below, mi is a struct merged_info*, and ci was defined
282 * as a struct conflict_info* (but we need to verify ci isn't actually
283 * pointed at a struct merged_info*).
285 * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
286 * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
287 * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
289 #define INITIALIZE_CI(ci, mi) do { \
290 (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
292 #define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
293 #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \
294 (ci) = (struct conflict_info *)(mi); \
295 assert((ci) && !(mi)->clean); \
298 static void free_strmap_strings(struct strmap *map)
300 struct hashmap_iter iter;
301 struct strmap_entry *entry;
303 strmap_for_each_entry(map, &iter, entry) {
304 free((char*)entry->key);
308 static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
311 struct rename_info *renames = &opti->renames;
313 void (*strmap_func)(struct strmap *, int) =
314 reinitialize ? strmap_partial_clear : strmap_clear;
315 void (*strset_func)(struct strset *) =
316 reinitialize ? strset_partial_clear : strset_clear;
319 * We marked opti->paths with strdup_strings = 0, so that we
320 * wouldn't have to make another copy of the fullpath created by
321 * make_traverse_path from setup_path_info(). But, now that we've
322 * used it and have no other references to these strings, it is time
323 * to deallocate them.
325 free_strmap_strings(&opti->paths);
326 strmap_func(&opti->paths, 1);
329 * All keys and values in opti->conflicted are a subset of those in
330 * opti->paths. We don't want to deallocate anything twice, so we
331 * don't free the keys and we pass 0 for free_values.
333 strmap_func(&opti->conflicted, 0);
336 * opti->paths_to_free is similar to opti->paths; we created it with
337 * strdup_strings = 0 to avoid making _another_ copy of the fullpath
338 * but now that we've used it and have no other references to these
339 * strings, it is time to deallocate them. We do so by temporarily
340 * setting strdup_strings to 1.
342 opti->paths_to_free.strdup_strings = 1;
343 string_list_clear(&opti->paths_to_free, 0);
344 opti->paths_to_free.strdup_strings = 0;
346 /* Free memory used by various renames maps */
347 for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) {
348 struct hashmap_iter iter;
349 struct strmap_entry *entry;
351 strset_func(&renames->dirs_removed[i]);
353 strmap_for_each_entry(&renames->dir_rename_count[i],
355 struct strintmap *counts = entry->value;
356 strintmap_clear(counts);
358 strmap_func(&renames->dir_rename_count[i], 1);
360 strmap_func(&renames->dir_renames[i], 0);
364 struct hashmap_iter iter;
365 struct strmap_entry *e;
367 /* Release and free each strbuf found in output */
368 strmap_for_each_entry(&opti->output, &iter, e) {
369 struct strbuf *sb = e->value;
372 * While strictly speaking we don't need to free(sb)
373 * here because we could pass free_values=1 when
374 * calling strmap_clear() on opti->output, that would
375 * require strmap_clear to do another
376 * strmap_for_each_entry() loop, so we just free it
377 * while we're iterating anyway.
381 strmap_clear(&opti->output, 0);
385 static int err(struct merge_options *opt, const char *err, ...)
388 struct strbuf sb = STRBUF_INIT;
390 strbuf_addstr(&sb, "error: ");
391 va_start(params, err);
392 strbuf_vaddf(&sb, err, params);
401 __attribute__((format (printf, 4, 5)))
402 static void path_msg(struct merge_options *opt,
404 int omittable_hint, /* skippable under --remerge-diff */
405 const char *fmt, ...)
408 struct strbuf *sb = strmap_get(&opt->priv->output, path);
410 sb = xmalloc(sizeof(*sb));
412 strmap_put(&opt->priv->output, path, sb);
416 strbuf_vaddf(sb, fmt, ap);
419 strbuf_addch(sb, '\n');
422 /*** Function Grouping: functions related to collect_merge_info() ***/
424 static void setup_path_info(struct merge_options *opt,
425 struct string_list_item *result,
426 const char *current_dir_name,
427 int current_dir_name_len,
428 char *fullpath, /* we'll take over ownership */
429 struct name_entry *names,
430 struct name_entry *merged_version,
431 unsigned is_null, /* boolean */
432 unsigned df_conflict, /* boolean */
435 int resolved /* boolean */)
437 /* result->util is void*, so mi is a convenience typed variable */
438 struct merged_info *mi;
440 assert(!is_null || resolved);
441 assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
442 assert(resolved == (merged_version != NULL));
444 mi = xcalloc(1, resolved ? sizeof(struct merged_info) :
445 sizeof(struct conflict_info));
446 mi->directory_name = current_dir_name;
447 mi->basename_offset = current_dir_name_len;
448 mi->clean = !!resolved;
450 mi->result.mode = merged_version->mode;
451 oidcpy(&mi->result.oid, &merged_version->oid);
452 mi->is_null = !!is_null;
455 struct conflict_info *ci;
457 ASSIGN_AND_VERIFY_CI(ci, mi);
458 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
459 ci->pathnames[i] = fullpath;
460 ci->stages[i].mode = names[i].mode;
461 oidcpy(&ci->stages[i].oid, &names[i].oid);
463 ci->filemask = filemask;
464 ci->dirmask = dirmask;
465 ci->df_conflict = !!df_conflict;
468 * Assume is_null for now, but if we have entries
469 * under the directory then when it is complete in
470 * write_completed_directory() it'll update this.
471 * Also, for D/F conflicts, we have to handle the
472 * directory first, then clear this bit and process
473 * the file to see how it is handled -- that occurs
474 * near the top of process_entry().
478 strmap_put(&opt->priv->paths, fullpath, mi);
479 result->string = fullpath;
483 static void collect_rename_info(struct merge_options *opt,
484 struct name_entry *names,
486 const char *fullname,
491 struct rename_info *renames = &opt->priv->renames;
493 /* Update dirs_removed, as needed */
494 if (dirmask == 1 || dirmask == 3 || dirmask == 5) {
495 /* absent_mask = 0x07 - dirmask; sides = absent_mask/2 */
496 unsigned sides = (0x07 - dirmask)/2;
498 strset_add(&renames->dirs_removed[1], fullname);
500 strset_add(&renames->dirs_removed[2], fullname);
504 static int collect_merge_info_callback(int n,
506 unsigned long dirmask,
507 struct name_entry *names,
508 struct traverse_info *info)
512 * common ancestor (mbase) has mask 1, and stored in index 0 of names
513 * head of side 1 (side1) has mask 2, and stored in index 1 of names
514 * head of side 2 (side2) has mask 4, and stored in index 2 of names
516 struct merge_options *opt = info->data;
517 struct merge_options_internal *opti = opt->priv;
518 struct string_list_item pi; /* Path Info */
519 struct conflict_info *ci; /* typed alias to pi.util (which is void*) */
520 struct name_entry *p;
523 const char *dirname = opti->current_dir_name;
524 unsigned filemask = mask & ~dirmask;
525 unsigned match_mask = 0; /* will be updated below */
526 unsigned mbase_null = !(mask & 1);
527 unsigned side1_null = !(mask & 2);
528 unsigned side2_null = !(mask & 4);
529 unsigned side1_matches_mbase = (!side1_null && !mbase_null &&
530 names[0].mode == names[1].mode &&
531 oideq(&names[0].oid, &names[1].oid));
532 unsigned side2_matches_mbase = (!side2_null && !mbase_null &&
533 names[0].mode == names[2].mode &&
534 oideq(&names[0].oid, &names[2].oid));
535 unsigned sides_match = (!side1_null && !side2_null &&
536 names[1].mode == names[2].mode &&
537 oideq(&names[1].oid, &names[2].oid));
540 * Note: When a path is a file on one side of history and a directory
541 * in another, we have a directory/file conflict. In such cases, if
542 * the conflict doesn't resolve from renames and deletions, then we
543 * always leave directories where they are and move files out of the
544 * way. Thus, while struct conflict_info has a df_conflict field to
545 * track such conflicts, we ignore that field for any directories at
546 * a path and only pay attention to it for files at the given path.
547 * The fact that we leave directories were they are also means that
548 * we do not need to worry about getting additional df_conflict
549 * information propagated from parent directories down to children
550 * (unlike, say traverse_trees_recursive() in unpack-trees.c, which
551 * sets a newinfo.df_conflicts field specifically to propagate it).
553 unsigned df_conflict = (filemask != 0) && (dirmask != 0);
555 /* n = 3 is a fundamental assumption. */
557 BUG("Called collect_merge_info_callback wrong");
560 * A bunch of sanity checks verifying that traverse_trees() calls
561 * us the way I expect. Could just remove these at some point,
562 * though maybe they are helpful to future code readers.
564 assert(mbase_null == is_null_oid(&names[0].oid));
565 assert(side1_null == is_null_oid(&names[1].oid));
566 assert(side2_null == is_null_oid(&names[2].oid));
567 assert(!mbase_null || !side1_null || !side2_null);
568 assert(mask > 0 && mask < 8);
570 /* Determine match_mask */
571 if (side1_matches_mbase)
572 match_mask = (side2_matches_mbase ? 7 : 3);
573 else if (side2_matches_mbase)
575 else if (sides_match)
579 * Get the name of the relevant filepath, which we'll pass to
580 * setup_path_info() for tracking.
585 len = traverse_path_len(info, p->pathlen);
587 /* +1 in both of the following lines to include the NUL byte */
588 fullpath = xmalloc(len + 1);
589 make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen);
592 * If mbase, side1, and side2 all match, we can resolve early. Even
593 * if these are trees, there will be no renames or anything
596 if (side1_matches_mbase && side2_matches_mbase) {
597 /* mbase, side1, & side2 all match; use mbase as resolution */
598 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
599 names, names+0, mbase_null, 0,
600 filemask, dirmask, 1);
605 * Gather additional information used in rename detection.
607 collect_rename_info(opt, names, dirname, fullpath,
608 filemask, dirmask, match_mask);
611 * Record information about the path so we can resolve later in
614 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
615 names, NULL, 0, df_conflict, filemask, dirmask, 0);
619 ci->match_mask = match_mask;
621 /* If dirmask, recurse into subdirectories */
623 struct traverse_info newinfo;
624 struct tree_desc t[3];
625 void *buf[3] = {NULL, NULL, NULL};
626 const char *original_dir_name;
629 ci->match_mask &= filemask;
632 newinfo.name = p->path;
633 newinfo.namelen = p->pathlen;
634 newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1);
636 * If this directory we are about to recurse into cared about
637 * its parent directory (the current directory) having a D/F
638 * conflict, then we'd propagate the masks in this way:
639 * newinfo.df_conflicts |= (mask & ~dirmask);
640 * But we don't worry about propagating D/F conflicts. (See
641 * comment near setting of local df_conflict variable near
642 * the beginning of this function).
645 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
646 if (i == 1 && side1_matches_mbase)
648 else if (i == 2 && side2_matches_mbase)
650 else if (i == 2 && sides_match)
653 const struct object_id *oid = NULL;
656 buf[i] = fill_tree_descriptor(opt->repo,
662 original_dir_name = opti->current_dir_name;
663 opti->current_dir_name = pi.string;
664 ret = traverse_trees(NULL, 3, t, &newinfo);
665 opti->current_dir_name = original_dir_name;
667 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++)
677 static int collect_merge_info(struct merge_options *opt,
678 struct tree *merge_base,
683 struct tree_desc t[3];
684 struct traverse_info info;
685 const char *toplevel_dir_placeholder = "";
687 opt->priv->current_dir_name = toplevel_dir_placeholder;
688 setup_traverse_info(&info, toplevel_dir_placeholder);
689 info.fn = collect_merge_info_callback;
691 info.show_all_errors = 1;
693 parse_tree(merge_base);
696 init_tree_desc(t + 0, merge_base->buffer, merge_base->size);
697 init_tree_desc(t + 1, side1->buffer, side1->size);
698 init_tree_desc(t + 2, side2->buffer, side2->size);
700 ret = traverse_trees(NULL, 3, t, &info);
705 /*** Function Grouping: functions related to threeway content merges ***/
707 static int handle_content_merge(struct merge_options *opt,
709 const struct version_info *o,
710 const struct version_info *a,
711 const struct version_info *b,
712 const char *pathnames[3],
713 const int extra_marker_size,
714 struct version_info *result)
716 die("Not yet implemented");
719 /*** Function Grouping: functions related to detect_and_process_renames(), ***
720 *** which are split into directory and regular rename detection sections. ***/
722 /*** Function Grouping: functions related to directory rename detection ***/
724 struct collision_info {
725 struct string_list source_files;
726 unsigned reported_already:1;
729 static void get_renamed_dir_portion(const char *old_path, const char *new_path,
730 char **old_dir, char **new_dir)
732 char *end_of_old, *end_of_new;
734 /* Default return values: NULL, meaning no rename */
740 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
741 * the "e/foo.c" part is the same, we just want to know that
742 * "a/b/c/d" was renamed to "a/b/some/thing/else"
743 * so, for this example, this function returns "a/b/c/d" in
744 * *old_dir and "a/b/some/thing/else" in *new_dir.
748 * If the basename of the file changed, we don't care. We want
749 * to know which portion of the directory, if any, changed.
751 end_of_old = strrchr(old_path, '/');
752 end_of_new = strrchr(new_path, '/');
755 * If end_of_old is NULL, old_path wasn't in a directory, so there
756 * could not be a directory rename (our rule elsewhere that a
757 * directory which still exists is not considered to have been
758 * renamed means the root directory can never be renamed -- because
759 * the root directory always exists).
761 if (end_of_old == NULL)
762 return; /* Note: *old_dir and *new_dir are still NULL */
765 * If new_path contains no directory (end_of_new is NULL), then we
766 * have a rename of old_path's directory to the root directory.
768 if (end_of_new == NULL) {
769 *old_dir = xstrndup(old_path, end_of_old - old_path);
770 *new_dir = xstrdup("");
774 /* Find the first non-matching character traversing backwards */
775 while (*--end_of_new == *--end_of_old &&
776 end_of_old != old_path &&
777 end_of_new != new_path)
778 ; /* Do nothing; all in the while loop */
781 * If both got back to the beginning of their strings, then the
782 * directory didn't change at all, only the basename did.
784 if (end_of_old == old_path && end_of_new == new_path &&
785 *end_of_old == *end_of_new)
786 return; /* Note: *old_dir and *new_dir are still NULL */
789 * If end_of_new got back to the beginning of its string, and
790 * end_of_old got back to the beginning of some subdirectory, then
791 * we have a rename/merge of a subdirectory into the root, which
792 * needs slightly special handling.
794 * Note: There is no need to consider the opposite case, with a
795 * rename/merge of the root directory into some subdirectory
796 * because as noted above the root directory always exists so it
797 * cannot be considered to be renamed.
799 if (end_of_new == new_path &&
800 end_of_old != old_path && end_of_old[-1] == '/') {
801 *old_dir = xstrndup(old_path, --end_of_old - old_path);
802 *new_dir = xstrdup("");
807 * We've found the first non-matching character in the directory
808 * paths. That means the current characters we were looking at
809 * were part of the first non-matching subdir name going back from
810 * the end of the strings. Get the whole name by advancing both
811 * end_of_old and end_of_new to the NEXT '/' character. That will
812 * represent the entire directory rename.
814 * The reason for the increment is cases like
815 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
816 * After dropping the basename and going back to the first
817 * non-matching character, we're now comparing:
819 * and we want to be comparing:
820 * a/b/star/ and a/b/tar/
821 * but without the pre-increment, the one on the right would stay
824 end_of_old = strchr(++end_of_old, '/');
825 end_of_new = strchr(++end_of_new, '/');
827 /* Copy the old and new directories into *old_dir and *new_dir. */
828 *old_dir = xstrndup(old_path, end_of_old - old_path);
829 *new_dir = xstrndup(new_path, end_of_new - new_path);
832 static void increment_count(struct strmap *dir_rename_count,
836 struct strintmap *counts;
837 struct strmap_entry *e;
839 /* Get the {new_dirs -> counts} mapping using old_dir */
840 e = strmap_get_entry(dir_rename_count, old_dir);
844 counts = xmalloc(sizeof(*counts));
845 strintmap_init_with_options(counts, 0, NULL, 1);
846 strmap_put(dir_rename_count, old_dir, counts);
849 /* Increment the count for new_dir */
850 strintmap_incr(counts, new_dir, 1);
853 static void compute_rename_counts(struct diff_queue_struct *pairs,
854 struct strmap *dir_rename_count,
855 struct strset *dirs_removed)
859 for (i = 0; i < pairs->nr; ++i) {
860 char *old_dir, *new_dir;
861 struct diff_filepair *pair = pairs->queue[i];
863 /* File not part of directory rename if it wasn't renamed */
864 if (pair->status != 'R')
867 /* Get the old and new directory names */
868 get_renamed_dir_portion(pair->one->path, pair->two->path,
871 /* Directory didn't change at all; ignore this one. */
875 * Make dir_rename_count contain a map of a map:
876 * old_directory -> {new_directory -> count}
877 * In other words, for every pair look at the directories for
878 * the old filename and the new filename and count how many
879 * times that pairing occurs.
881 if (strset_contains(dirs_removed, old_dir))
882 increment_count(dir_rename_count, old_dir, new_dir);
884 /* Free resources we don't need anymore */
890 static void get_provisional_directory_renames(struct merge_options *opt,
894 struct hashmap_iter iter;
895 struct strmap_entry *entry;
896 struct rename_info *renames = &opt->priv->renames;
898 compute_rename_counts(&renames->pairs[side],
899 &renames->dir_rename_count[side],
900 &renames->dirs_removed[side]);
903 * dir_rename_count: old_directory -> {new_directory -> count}
905 * dir_renames: old_directory -> best_new_directory
906 * where best_new_directory is the one with the unique highest count.
908 strmap_for_each_entry(&renames->dir_rename_count[side], &iter, entry) {
909 const char *source_dir = entry->key;
910 struct strintmap *counts = entry->value;
911 struct hashmap_iter count_iter;
912 struct strmap_entry *count_entry;
915 const char *best = NULL;
917 strintmap_for_each_entry(counts, &count_iter, count_entry) {
918 const char *target_dir = count_entry->key;
919 intptr_t count = (intptr_t)count_entry->value;
923 else if (count > max) {
929 if (bad_max == max) {
930 path_msg(opt, source_dir, 0,
931 _("CONFLICT (directory rename split): "
932 "Unclear where to rename %s to; it was "
933 "renamed to multiple other directories, with "
934 "no destination getting a majority of the "
939 strmap_put(&renames->dir_renames[side],
940 source_dir, (void*)best);
945 static void handle_directory_level_conflicts(struct merge_options *opt)
947 struct hashmap_iter iter;
948 struct strmap_entry *entry;
949 struct string_list duplicated = STRING_LIST_INIT_NODUP;
950 struct rename_info *renames = &opt->priv->renames;
951 struct strmap *side1_dir_renames = &renames->dir_renames[MERGE_SIDE1];
952 struct strmap *side2_dir_renames = &renames->dir_renames[MERGE_SIDE2];
955 strmap_for_each_entry(side1_dir_renames, &iter, entry) {
956 if (strmap_contains(side2_dir_renames, entry->key))
957 string_list_append(&duplicated, entry->key);
960 for (i = 0; i < duplicated.nr; i++) {
961 strmap_remove(side1_dir_renames, duplicated.items[i].string, 0);
962 strmap_remove(side2_dir_renames, duplicated.items[i].string, 0);
964 string_list_clear(&duplicated, 0);
967 static void compute_collisions(struct strmap *collisions,
968 struct strmap *dir_renames,
969 struct diff_queue_struct *pairs)
971 die("Not yet implemented.");
974 static char *check_for_directory_rename(struct merge_options *opt,
977 struct strmap *dir_renames,
978 struct strmap *dir_rename_exclusions,
979 struct strmap *collisions,
982 die("Not yet implemented.");
985 static void apply_directory_rename_modifications(struct merge_options *opt,
986 struct diff_filepair *pair,
989 die("Not yet implemented.");
992 /*** Function Grouping: functions related to regular rename detection ***/
994 static int process_renames(struct merge_options *opt,
995 struct diff_queue_struct *renames)
997 int clean_merge = 1, i;
999 for (i = 0; i < renames->nr; ++i) {
1000 const char *oldpath = NULL, *newpath;
1001 struct diff_filepair *pair = renames->queue[i];
1002 struct conflict_info *oldinfo = NULL, *newinfo = NULL;
1003 struct strmap_entry *old_ent, *new_ent;
1004 unsigned int old_sidemask;
1005 int target_index, other_source_index;
1006 int source_deleted, collision, type_changed;
1007 const char *rename_branch = NULL, *delete_branch = NULL;
1009 old_ent = strmap_get_entry(&opt->priv->paths, pair->one->path);
1010 oldpath = old_ent->key;
1011 oldinfo = old_ent->value;
1013 new_ent = strmap_get_entry(&opt->priv->paths, pair->two->path);
1014 newpath = new_ent->key;
1015 newinfo = new_ent->value;
1018 * diff_filepairs have copies of pathnames, thus we have to
1019 * use standard 'strcmp()' (negated) instead of '=='.
1021 if (i + 1 < renames->nr &&
1022 !strcmp(oldpath, renames->queue[i+1]->one->path)) {
1023 /* Handle rename/rename(1to2) or rename/rename(1to1) */
1024 const char *pathnames[3];
1025 struct version_info merged;
1026 struct conflict_info *base, *side1, *side2;
1027 unsigned was_binary_blob = 0;
1029 pathnames[0] = oldpath;
1030 pathnames[1] = newpath;
1031 pathnames[2] = renames->queue[i+1]->two->path;
1033 base = strmap_get(&opt->priv->paths, pathnames[0]);
1034 side1 = strmap_get(&opt->priv->paths, pathnames[1]);
1035 side2 = strmap_get(&opt->priv->paths, pathnames[2]);
1041 if (!strcmp(pathnames[1], pathnames[2])) {
1042 /* Both sides renamed the same way */
1043 assert(side1 == side2);
1044 memcpy(&side1->stages[0], &base->stages[0],
1046 side1->filemask |= (1 << MERGE_BASE);
1047 /* Mark base as resolved by removal */
1048 base->merged.is_null = 1;
1049 base->merged.clean = 1;
1051 /* We handled both renames, i.e. i+1 handled */
1053 /* Move to next rename */
1057 /* This is a rename/rename(1to2) */
1058 clean_merge = handle_content_merge(opt,
1064 1 + 2 * opt->priv->call_depth,
1067 merged.mode == side1->stages[1].mode &&
1068 oideq(&merged.oid, &side1->stages[1].oid))
1069 was_binary_blob = 1;
1070 memcpy(&side1->stages[1], &merged, sizeof(merged));
1071 if (was_binary_blob) {
1073 * Getting here means we were attempting to
1074 * merge a binary blob.
1076 * Since we can't merge binaries,
1077 * handle_content_merge() just takes one
1078 * side. But we don't want to copy the
1079 * contents of one side to both paths. We
1080 * used the contents of side1 above for
1081 * side1->stages, let's use the contents of
1082 * side2 for side2->stages below.
1084 oidcpy(&merged.oid, &side2->stages[2].oid);
1085 merged.mode = side2->stages[2].mode;
1087 memcpy(&side2->stages[2], &merged, sizeof(merged));
1089 side1->path_conflict = 1;
1090 side2->path_conflict = 1;
1092 * TODO: For renames we normally remove the path at the
1093 * old name. It would thus seem consistent to do the
1094 * same for rename/rename(1to2) cases, but we haven't
1095 * done so traditionally and a number of the regression
1096 * tests now encode an expectation that the file is
1097 * left there at stage 1. If we ever decide to change
1098 * this, add the following two lines here:
1099 * base->merged.is_null = 1;
1100 * base->merged.clean = 1;
1101 * and remove the setting of base->path_conflict to 1.
1103 base->path_conflict = 1;
1104 path_msg(opt, oldpath, 0,
1105 _("CONFLICT (rename/rename): %s renamed to "
1106 "%s in %s and to %s in %s."),
1108 pathnames[1], opt->branch1,
1109 pathnames[2], opt->branch2);
1111 i++; /* We handled both renames, i.e. i+1 handled */
1117 target_index = pair->score; /* from collect_renames() */
1118 assert(target_index == 1 || target_index == 2);
1119 other_source_index = 3 - target_index;
1120 old_sidemask = (1 << other_source_index); /* 2 or 4 */
1121 source_deleted = (oldinfo->filemask == 1);
1122 collision = ((newinfo->filemask & old_sidemask) != 0);
1123 type_changed = !source_deleted &&
1124 (S_ISREG(oldinfo->stages[other_source_index].mode) !=
1125 S_ISREG(newinfo->stages[target_index].mode));
1126 if (type_changed && collision) {
1128 * special handling so later blocks can handle this...
1130 * if type_changed && collision are both true, then this
1131 * was really a double rename, but one side wasn't
1132 * detected due to lack of break detection. I.e.
1134 * orig: has normal file 'foo'
1135 * side1: renames 'foo' to 'bar', adds 'foo' symlink
1136 * side2: renames 'foo' to 'bar'
1137 * In this case, the foo->bar rename on side1 won't be
1138 * detected because the new symlink named 'foo' is
1139 * there and we don't do break detection. But we detect
1140 * this here because we don't want to merge the content
1141 * of the foo symlink with the foo->bar file, so we
1142 * have some logic to handle this special case. The
1143 * easiest way to do that is make 'bar' on side1 not
1144 * be considered a colliding file but the other part
1145 * of a normal rename. If the file is very different,
1146 * well we're going to get content merge conflicts
1147 * anyway so it doesn't hurt. And if the colliding
1148 * file also has a different type, that'll be handled
1149 * by the content merge logic in process_entry() too.
1151 * See also t6430, 'rename vs. rename/symlink'
1155 if (source_deleted) {
1156 if (target_index == 1) {
1157 rename_branch = opt->branch1;
1158 delete_branch = opt->branch2;
1160 rename_branch = opt->branch2;
1161 delete_branch = opt->branch1;
1165 assert(source_deleted || oldinfo->filemask & old_sidemask);
1167 /* Need to check for special types of rename conflicts... */
1168 if (collision && !source_deleted) {
1169 /* collision: rename/add or rename/rename(2to1) */
1170 const char *pathnames[3];
1171 struct version_info merged;
1173 struct conflict_info *base, *side1, *side2;
1176 pathnames[0] = oldpath;
1177 pathnames[other_source_index] = oldpath;
1178 pathnames[target_index] = newpath;
1180 base = strmap_get(&opt->priv->paths, pathnames[0]);
1181 side1 = strmap_get(&opt->priv->paths, pathnames[1]);
1182 side2 = strmap_get(&opt->priv->paths, pathnames[2]);
1188 clean = handle_content_merge(opt, pair->one->path,
1193 1 + 2 * opt->priv->call_depth,
1196 memcpy(&newinfo->stages[target_index], &merged,
1199 path_msg(opt, newpath, 0,
1200 _("CONFLICT (rename involved in "
1201 "collision): rename of %s -> %s has "
1202 "content conflicts AND collides "
1203 "with another path; this may result "
1204 "in nested conflict markers."),
1207 } else if (collision && source_deleted) {
1209 * rename/add/delete or rename/rename(2to1)/delete:
1210 * since oldpath was deleted on the side that didn't
1211 * do the rename, there's not much of a content merge
1212 * we can do for the rename. oldinfo->merged.is_null
1213 * was already set, so we just leave things as-is so
1214 * they look like an add/add conflict.
1217 newinfo->path_conflict = 1;
1218 path_msg(opt, newpath, 0,
1219 _("CONFLICT (rename/delete): %s renamed "
1220 "to %s in %s, but deleted in %s."),
1221 oldpath, newpath, rename_branch, delete_branch);
1224 * a few different cases...start by copying the
1225 * existing stage(s) from oldinfo over the newinfo
1226 * and update the pathname(s).
1228 memcpy(&newinfo->stages[0], &oldinfo->stages[0],
1229 sizeof(newinfo->stages[0]));
1230 newinfo->filemask |= (1 << MERGE_BASE);
1231 newinfo->pathnames[0] = oldpath;
1233 /* rename vs. typechange */
1234 /* Mark the original as resolved by removal */
1235 memcpy(&oldinfo->stages[0].oid, &null_oid,
1236 sizeof(oldinfo->stages[0].oid));
1237 oldinfo->stages[0].mode = 0;
1238 oldinfo->filemask &= 0x06;
1239 } else if (source_deleted) {
1241 newinfo->path_conflict = 1;
1242 path_msg(opt, newpath, 0,
1243 _("CONFLICT (rename/delete): %s renamed"
1244 " to %s in %s, but deleted in %s."),
1246 rename_branch, delete_branch);
1249 memcpy(&newinfo->stages[other_source_index],
1250 &oldinfo->stages[other_source_index],
1251 sizeof(newinfo->stages[0]));
1252 newinfo->filemask |= (1 << other_source_index);
1253 newinfo->pathnames[other_source_index] = oldpath;
1257 if (!type_changed) {
1258 /* Mark the original as resolved by removal */
1259 oldinfo->merged.is_null = 1;
1260 oldinfo->merged.clean = 1;
1268 static int compare_pairs(const void *a_, const void *b_)
1270 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
1271 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
1273 return strcmp(a->one->path, b->one->path);
1276 /* Call diffcore_rename() to compute which files have changed on given side */
1277 static void detect_regular_renames(struct merge_options *opt,
1278 struct tree *merge_base,
1280 unsigned side_index)
1282 struct diff_options diff_opts;
1283 struct rename_info *renames = &opt->priv->renames;
1285 repo_diff_setup(opt->repo, &diff_opts);
1286 diff_opts.flags.recursive = 1;
1287 diff_opts.flags.rename_empty = 0;
1288 diff_opts.detect_rename = DIFF_DETECT_RENAME;
1289 diff_opts.rename_limit = opt->rename_limit;
1290 if (opt->rename_limit <= 0)
1291 diff_opts.rename_limit = 1000;
1292 diff_opts.rename_score = opt->rename_score;
1293 diff_opts.show_rename_progress = opt->show_rename_progress;
1294 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1295 diff_setup_done(&diff_opts);
1296 diff_tree_oid(&merge_base->object.oid, &side->object.oid, "",
1298 diffcore_std(&diff_opts);
1300 if (diff_opts.needed_rename_limit > renames->needed_limit)
1301 renames->needed_limit = diff_opts.needed_rename_limit;
1303 renames->pairs[side_index] = diff_queued_diff;
1305 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1306 diff_queued_diff.nr = 0;
1307 diff_queued_diff.queue = NULL;
1308 diff_flush(&diff_opts);
1312 * Get information of all renames which occurred in 'side_pairs', discarding
1315 static int collect_renames(struct merge_options *opt,
1316 struct diff_queue_struct *result,
1317 unsigned side_index,
1318 struct strmap *dir_renames_for_side,
1319 struct strmap *rename_exclusions)
1322 struct strmap collisions;
1323 struct diff_queue_struct *side_pairs;
1324 struct hashmap_iter iter;
1325 struct strmap_entry *entry;
1326 struct rename_info *renames = &opt->priv->renames;
1328 side_pairs = &renames->pairs[side_index];
1329 compute_collisions(&collisions, dir_renames_for_side, side_pairs);
1331 for (i = 0; i < side_pairs->nr; ++i) {
1332 struct diff_filepair *p = side_pairs->queue[i];
1333 char *new_path; /* non-NULL only with directory renames */
1335 if (p->status != 'A' && p->status != 'R') {
1336 diff_free_filepair(p);
1340 new_path = check_for_directory_rename(opt, p->two->path,
1342 dir_renames_for_side,
1347 if (p->status != 'R' && !new_path) {
1348 diff_free_filepair(p);
1353 apply_directory_rename_modifications(opt, p, new_path);
1356 * p->score comes back from diffcore_rename_extended() with
1357 * the similarity of the renamed file. The similarity is
1358 * was used to determine that the two files were related
1359 * and are a rename, which we have already used, but beyond
1360 * that we have no use for the similarity. So p->score is
1361 * now irrelevant. However, process_renames() will need to
1362 * know which side of the merge this rename was associated
1363 * with, so overwrite p->score with that value.
1365 p->score = side_index;
1366 result->queue[result->nr++] = p;
1369 /* Free each value in the collisions map */
1370 strmap_for_each_entry(&collisions, &iter, entry) {
1371 struct collision_info *info = entry->value;
1372 string_list_clear(&info->source_files, 0);
1375 * In compute_collisions(), we set collisions.strdup_strings to 0
1376 * so that we wouldn't have to make another copy of the new_path
1377 * allocated by apply_dir_rename(). But now that we've used them
1378 * and have no other references to these strings, it is time to
1381 free_strmap_strings(&collisions);
1382 strmap_clear(&collisions, 1);
1386 static int detect_and_process_renames(struct merge_options *opt,
1387 struct tree *merge_base,
1391 struct diff_queue_struct combined;
1392 struct rename_info *renames = &opt->priv->renames;
1393 int need_dir_renames, s, clean = 1;
1395 memset(&combined, 0, sizeof(combined));
1397 detect_regular_renames(opt, merge_base, side1, MERGE_SIDE1);
1398 detect_regular_renames(opt, merge_base, side2, MERGE_SIDE2);
1401 !opt->priv->call_depth &&
1402 (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE ||
1403 opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT);
1405 if (need_dir_renames) {
1406 get_provisional_directory_renames(opt, MERGE_SIDE1, &clean);
1407 get_provisional_directory_renames(opt, MERGE_SIDE2, &clean);
1408 handle_directory_level_conflicts(opt);
1411 ALLOC_GROW(combined.queue,
1412 renames->pairs[1].nr + renames->pairs[2].nr,
1414 clean &= collect_renames(opt, &combined, MERGE_SIDE1,
1415 &renames->dir_renames[2],
1416 &renames->dir_renames[1]);
1417 clean &= collect_renames(opt, &combined, MERGE_SIDE2,
1418 &renames->dir_renames[1],
1419 &renames->dir_renames[2]);
1420 QSORT(combined.queue, combined.nr, compare_pairs);
1422 clean &= process_renames(opt, &combined);
1424 /* Free memory for renames->pairs[] and combined */
1425 for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) {
1426 free(renames->pairs[s].queue);
1427 DIFF_QUEUE_CLEAR(&renames->pairs[s]);
1431 for (i = 0; i < combined.nr; i++)
1432 diff_free_filepair(combined.queue[i]);
1433 free(combined.queue);
1439 /*** Function Grouping: functions related to process_entries() ***/
1441 static int string_list_df_name_compare(const char *one, const char *two)
1443 int onelen = strlen(one);
1444 int twolen = strlen(two);
1446 * Here we only care that entries for D/F conflicts are
1447 * adjacent, in particular with the file of the D/F conflict
1448 * appearing before files below the corresponding directory.
1449 * The order of the rest of the list is irrelevant for us.
1451 * To achieve this, we sort with df_name_compare and provide
1452 * the mode S_IFDIR so that D/F conflicts will sort correctly.
1453 * We use the mode S_IFDIR for everything else for simplicity,
1454 * since in other cases any changes in their order due to
1455 * sorting cause no problems for us.
1457 int cmp = df_name_compare(one, onelen, S_IFDIR,
1458 two, twolen, S_IFDIR);
1460 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
1461 * that 'foo' comes before 'foo/bar'.
1465 return onelen - twolen;
1468 struct directory_versions {
1470 * versions: list of (basename -> version_info)
1472 * The basenames are in reverse lexicographic order of full pathnames,
1473 * as processed in process_entries(). This puts all entries within
1474 * a directory together, and covers the directory itself after
1475 * everything within it, allowing us to write subtrees before needing
1476 * to record information for the tree itself.
1478 struct string_list versions;
1481 * offsets: list of (full relative path directories -> integer offsets)
1483 * Since versions contains basenames from files in multiple different
1484 * directories, we need to know which entries in versions correspond
1485 * to which directories. Values of e.g.
1489 * Would mean that entries 0-1 of versions are files in the toplevel
1490 * directory, entries 2-4 are files under src/, and the remaining
1491 * entries starting at index 5 are files under src/moduleA/.
1493 struct string_list offsets;
1496 * last_directory: directory that previously processed file found in
1498 * last_directory starts NULL, but records the directory in which the
1499 * previous file was found within. As soon as
1500 * directory(current_file) != last_directory
1501 * then we need to start updating accounting in versions & offsets.
1502 * Note that last_directory is always the last path in "offsets" (or
1503 * NULL if "offsets" is empty) so this exists just for quick access.
1505 const char *last_directory;
1507 /* last_directory_len: cached computation of strlen(last_directory) */
1508 unsigned last_directory_len;
1511 static int tree_entry_order(const void *a_, const void *b_)
1513 const struct string_list_item *a = a_;
1514 const struct string_list_item *b = b_;
1516 const struct merged_info *ami = a->util;
1517 const struct merged_info *bmi = b->util;
1518 return base_name_compare(a->string, strlen(a->string), ami->result.mode,
1519 b->string, strlen(b->string), bmi->result.mode);
1522 static void write_tree(struct object_id *result_oid,
1523 struct string_list *versions,
1524 unsigned int offset,
1527 size_t maxlen = 0, extra;
1528 unsigned int nr = versions->nr - offset;
1529 struct strbuf buf = STRBUF_INIT;
1530 struct string_list relevant_entries = STRING_LIST_INIT_NODUP;
1534 * We want to sort the last (versions->nr-offset) entries in versions.
1535 * Do so by abusing the string_list API a bit: make another string_list
1536 * that contains just those entries and then sort them.
1538 * We won't use relevant_entries again and will let it just pop off the
1539 * stack, so there won't be allocation worries or anything.
1541 relevant_entries.items = versions->items + offset;
1542 relevant_entries.nr = versions->nr - offset;
1543 QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order);
1545 /* Pre-allocate some space in buf */
1546 extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */
1547 for (i = 0; i < nr; i++) {
1548 maxlen += strlen(versions->items[offset+i].string) + extra;
1550 strbuf_grow(&buf, maxlen);
1552 /* Write each entry out to buf */
1553 for (i = 0; i < nr; i++) {
1554 struct merged_info *mi = versions->items[offset+i].util;
1555 struct version_info *ri = &mi->result;
1556 strbuf_addf(&buf, "%o %s%c",
1558 versions->items[offset+i].string, '\0');
1559 strbuf_add(&buf, ri->oid.hash, hash_size);
1562 /* Write this object file out, and record in result_oid */
1563 write_object_file(buf.buf, buf.len, tree_type, result_oid);
1564 strbuf_release(&buf);
1567 static void record_entry_for_tree(struct directory_versions *dir_metadata,
1569 struct merged_info *mi)
1571 const char *basename;
1574 /* nothing to record */
1577 basename = path + mi->basename_offset;
1578 assert(strchr(basename, '/') == NULL);
1579 string_list_append(&dir_metadata->versions,
1580 basename)->util = &mi->result;
1583 static void write_completed_directory(struct merge_options *opt,
1584 const char *new_directory_name,
1585 struct directory_versions *info)
1587 const char *prev_dir;
1588 struct merged_info *dir_info = NULL;
1589 unsigned int offset;
1592 * Some explanation of info->versions and info->offsets...
1594 * process_entries() iterates over all relevant files AND
1595 * directories in reverse lexicographic order, and calls this
1596 * function. Thus, an example of the paths that process_entries()
1597 * could operate on (along with the directories for those paths
1602 * src/moduleB/umm.c src/moduleB
1603 * src/moduleB/stuff.h src/moduleB
1604 * src/moduleB/baz.c src/moduleB
1606 * src/moduleA/foo.c src/moduleA
1607 * src/moduleA/bar.c src/moduleA
1614 * always contains the unprocessed entries and their
1615 * version_info information. For example, after the first five
1616 * entries above, info->versions would be:
1618 * xtract.c <xtract.c's version_info>
1619 * token.txt <token.txt's version_info>
1620 * umm.c <src/moduleB/umm.c's version_info>
1621 * stuff.h <src/moduleB/stuff.h's version_info>
1622 * baz.c <src/moduleB/baz.c's version_info>
1624 * Once a subdirectory is completed we remove the entries in
1625 * that subdirectory from info->versions, writing it as a tree
1626 * (write_tree()). Thus, as soon as we get to src/moduleB,
1627 * info->versions would be updated to
1629 * xtract.c <xtract.c's version_info>
1630 * token.txt <token.txt's version_info>
1631 * moduleB <src/moduleB's version_info>
1635 * helps us track which entries in info->versions correspond to
1636 * which directories. When we are N directories deep (e.g. 4
1637 * for src/modA/submod/subdir/), we have up to N+1 unprocessed
1638 * directories (+1 because of toplevel dir). Corresponding to
1639 * the info->versions example above, after processing five entries
1640 * info->offsets will be:
1645 * which is used to know that xtract.c & token.txt are from the
1646 * toplevel dirctory, while umm.c & stuff.h & baz.c are from the
1647 * src/moduleB directory. Again, following the example above,
1648 * once we need to process src/moduleB, then info->offsets is
1654 * which says that moduleB (and only moduleB so far) is in the
1657 * One unique thing to note about info->offsets here is that
1658 * "src" was not added to info->offsets until there was a path
1659 * (a file OR directory) immediately below src/ that got
1662 * Since process_entry() just appends new entries to info->versions,
1663 * write_completed_directory() only needs to do work if the next path
1664 * is in a directory that is different than the last directory found
1669 * If we are working with the same directory as the last entry, there
1670 * is no work to do. (See comments above the directory_name member of
1671 * struct merged_info for why we can use pointer comparison instead of
1674 if (new_directory_name == info->last_directory)
1678 * If we are just starting (last_directory is NULL), or last_directory
1679 * is a prefix of the current directory, then we can just update
1680 * info->offsets to record the offset where we started this directory
1681 * and update last_directory to have quick access to it.
1683 if (info->last_directory == NULL ||
1684 !strncmp(new_directory_name, info->last_directory,
1685 info->last_directory_len)) {
1686 uintptr_t offset = info->versions.nr;
1688 info->last_directory = new_directory_name;
1689 info->last_directory_len = strlen(info->last_directory);
1691 * Record the offset into info->versions where we will
1692 * start recording basenames of paths found within
1693 * new_directory_name.
1695 string_list_append(&info->offsets,
1696 info->last_directory)->util = (void*)offset;
1701 * The next entry that will be processed will be within
1702 * new_directory_name. Since at this point we know that
1703 * new_directory_name is within a different directory than
1704 * info->last_directory, we have all entries for info->last_directory
1705 * in info->versions and we need to create a tree object for them.
1707 dir_info = strmap_get(&opt->priv->paths, info->last_directory);
1709 offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util;
1710 if (offset == info->versions.nr) {
1712 * Actually, we don't need to create a tree object in this
1713 * case. Whenever all files within a directory disappear
1714 * during the merge (e.g. unmodified on one side and
1715 * deleted on the other, or files were renamed elsewhere),
1716 * then we get here and the directory itself needs to be
1717 * omitted from its parent tree as well.
1719 dir_info->is_null = 1;
1722 * Write out the tree to the git object directory, and also
1723 * record the mode and oid in dir_info->result.
1725 dir_info->is_null = 0;
1726 dir_info->result.mode = S_IFDIR;
1727 write_tree(&dir_info->result.oid, &info->versions, offset,
1728 opt->repo->hash_algo->rawsz);
1732 * We've now used several entries from info->versions and one entry
1733 * from info->offsets, so we get rid of those values.
1736 info->versions.nr = offset;
1739 * Now we've taken care of the completed directory, but we need to
1740 * prepare things since future entries will be in
1741 * new_directory_name. (In particular, process_entry() will be
1742 * appending new entries to info->versions.) So, we need to make
1743 * sure new_directory_name is the last entry in info->offsets.
1745 prev_dir = info->offsets.nr == 0 ? NULL :
1746 info->offsets.items[info->offsets.nr-1].string;
1747 if (new_directory_name != prev_dir) {
1748 uintptr_t c = info->versions.nr;
1749 string_list_append(&info->offsets,
1750 new_directory_name)->util = (void*)c;
1753 /* And, of course, we need to update last_directory to match. */
1754 info->last_directory = new_directory_name;
1755 info->last_directory_len = strlen(info->last_directory);
1758 /* Per entry merge function */
1759 static void process_entry(struct merge_options *opt,
1761 struct conflict_info *ci,
1762 struct directory_versions *dir_metadata)
1765 assert(ci->filemask >= 0 && ci->filemask <= 7);
1766 /* ci->match_mask == 7 was handled in collect_merge_info_callback() */
1767 assert(ci->match_mask == 0 || ci->match_mask == 3 ||
1768 ci->match_mask == 5 || ci->match_mask == 6);
1771 record_entry_for_tree(dir_metadata, path, &ci->merged);
1772 if (ci->filemask == 0)
1773 /* nothing else to handle */
1775 assert(ci->df_conflict);
1778 if (ci->df_conflict) {
1779 die("Not yet implemented.");
1783 * NOTE: Below there is a long switch-like if-elseif-elseif... block
1784 * which the code goes through even for the df_conflict cases
1785 * above. Well, it will once we don't die-not-implemented above.
1787 if (ci->match_mask) {
1788 ci->merged.clean = 1;
1789 if (ci->match_mask == 6) {
1790 /* stages[1] == stages[2] */
1791 ci->merged.result.mode = ci->stages[1].mode;
1792 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
1794 /* determine the mask of the side that didn't match */
1795 unsigned int othermask = 7 & ~ci->match_mask;
1796 int side = (othermask == 4) ? 2 : 1;
1798 ci->merged.result.mode = ci->stages[side].mode;
1799 ci->merged.is_null = !ci->merged.result.mode;
1800 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1802 assert(othermask == 2 || othermask == 4);
1803 assert(ci->merged.is_null ==
1804 (ci->filemask == ci->match_mask));
1806 } else if (ci->filemask >= 6 &&
1807 (S_IFMT & ci->stages[1].mode) !=
1808 (S_IFMT & ci->stages[2].mode)) {
1810 * Two different items from (file/submodule/symlink)
1812 die("Not yet implemented.");
1813 } else if (ci->filemask >= 6) {
1815 * TODO: Needs a two-way or three-way content merge, but we're
1816 * just being lazy and copying the version from HEAD and
1817 * leaving it as conflicted.
1819 ci->merged.clean = 0;
1820 ci->merged.result.mode = ci->stages[1].mode;
1821 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
1822 /* When we fix above, we'll call handle_content_merge() */
1823 (void)handle_content_merge;
1824 } else if (ci->filemask == 3 || ci->filemask == 5) {
1826 const char *modify_branch, *delete_branch;
1827 int side = (ci->filemask == 5) ? 2 : 1;
1828 int index = opt->priv->call_depth ? 0 : side;
1830 ci->merged.result.mode = ci->stages[index].mode;
1831 oidcpy(&ci->merged.result.oid, &ci->stages[index].oid);
1832 ci->merged.clean = 0;
1834 modify_branch = (side == 1) ? opt->branch1 : opt->branch2;
1835 delete_branch = (side == 1) ? opt->branch2 : opt->branch1;
1837 if (ci->path_conflict &&
1838 oideq(&ci->stages[0].oid, &ci->stages[side].oid)) {
1840 * This came from a rename/delete; no action to take,
1841 * but avoid printing "modify/delete" conflict notice
1842 * since the contents were not modified.
1845 path_msg(opt, path, 0,
1846 _("CONFLICT (modify/delete): %s deleted in %s "
1847 "and modified in %s. Version %s of %s left "
1849 path, delete_branch, modify_branch,
1850 modify_branch, path);
1852 } else if (ci->filemask == 2 || ci->filemask == 4) {
1853 /* Added on one side */
1854 int side = (ci->filemask == 4) ? 2 : 1;
1855 ci->merged.result.mode = ci->stages[side].mode;
1856 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1857 ci->merged.clean = !ci->df_conflict && !ci->path_conflict;
1858 } else if (ci->filemask == 1) {
1859 /* Deleted on both sides */
1860 ci->merged.is_null = 1;
1861 ci->merged.result.mode = 0;
1862 oidcpy(&ci->merged.result.oid, &null_oid);
1863 ci->merged.clean = !ci->path_conflict;
1867 * If still conflicted, record it separately. This allows us to later
1868 * iterate over just conflicted entries when updating the index instead
1869 * of iterating over all entries.
1871 if (!ci->merged.clean)
1872 strmap_put(&opt->priv->conflicted, path, ci);
1873 record_entry_for_tree(dir_metadata, path, &ci->merged);
1876 static void process_entries(struct merge_options *opt,
1877 struct object_id *result_oid)
1879 struct hashmap_iter iter;
1880 struct strmap_entry *e;
1881 struct string_list plist = STRING_LIST_INIT_NODUP;
1882 struct string_list_item *entry;
1883 struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP,
1884 STRING_LIST_INIT_NODUP,
1887 if (strmap_empty(&opt->priv->paths)) {
1888 oidcpy(result_oid, opt->repo->hash_algo->empty_tree);
1892 /* Hack to pre-allocate plist to the desired size */
1893 ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc);
1895 /* Put every entry from paths into plist, then sort */
1896 strmap_for_each_entry(&opt->priv->paths, &iter, e) {
1897 string_list_append(&plist, e->key)->util = e->value;
1899 plist.cmp = string_list_df_name_compare;
1900 string_list_sort(&plist);
1903 * Iterate over the items in reverse order, so we can handle paths
1904 * below a directory before needing to handle the directory itself.
1906 * This allows us to write subtrees before we need to write trees,
1907 * and it also enables sane handling of directory/file conflicts
1908 * (because it allows us to know whether the directory is still in
1909 * the way when it is time to process the file at the same path).
1911 for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) {
1912 char *path = entry->string;
1914 * NOTE: mi may actually be a pointer to a conflict_info, but
1915 * we have to check mi->clean first to see if it's safe to
1916 * reassign to such a pointer type.
1918 struct merged_info *mi = entry->util;
1920 write_completed_directory(opt, mi->directory_name,
1923 record_entry_for_tree(&dir_metadata, path, mi);
1925 struct conflict_info *ci = (struct conflict_info *)mi;
1926 process_entry(opt, path, ci, &dir_metadata);
1930 if (dir_metadata.offsets.nr != 1 ||
1931 (uintptr_t)dir_metadata.offsets.items[0].util != 0) {
1932 printf("dir_metadata.offsets.nr = %d (should be 1)\n",
1933 dir_metadata.offsets.nr);
1934 printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n",
1935 (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util);
1937 BUG("dir_metadata accounting completely off; shouldn't happen");
1939 write_tree(result_oid, &dir_metadata.versions, 0,
1940 opt->repo->hash_algo->rawsz);
1941 string_list_clear(&plist, 0);
1942 string_list_clear(&dir_metadata.versions, 0);
1943 string_list_clear(&dir_metadata.offsets, 0);
1946 /*** Function Grouping: functions related to merge_switch_to_result() ***/
1948 static int checkout(struct merge_options *opt,
1952 /* Switch the index/working copy from old to new */
1954 struct tree_desc trees[2];
1955 struct unpack_trees_options unpack_opts;
1957 memset(&unpack_opts, 0, sizeof(unpack_opts));
1958 unpack_opts.head_idx = -1;
1959 unpack_opts.src_index = opt->repo->index;
1960 unpack_opts.dst_index = opt->repo->index;
1962 setup_unpack_trees_porcelain(&unpack_opts, "merge");
1965 * NOTE: if this were just "git checkout" code, we would probably
1966 * read or refresh the cache and check for a conflicted index, but
1967 * builtin/merge.c or sequencer.c really needs to read the index
1968 * and check for conflicted entries before starting merging for a
1969 * good user experience (no sense waiting for merges/rebases before
1970 * erroring out), so there's no reason to duplicate that work here.
1973 /* 2-way merge to the new branch */
1974 unpack_opts.update = 1;
1975 unpack_opts.merge = 1;
1976 unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
1977 unpack_opts.verbose_update = (opt->verbosity > 2);
1978 unpack_opts.fn = twoway_merge;
1979 if (1/* FIXME: opts->overwrite_ignore*/) {
1980 unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
1981 unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
1982 setup_standard_excludes(unpack_opts.dir);
1985 init_tree_desc(&trees[0], prev->buffer, prev->size);
1987 init_tree_desc(&trees[1], next->buffer, next->size);
1989 ret = unpack_trees(2, trees, &unpack_opts);
1990 clear_unpack_trees_porcelain(&unpack_opts);
1991 dir_clear(unpack_opts.dir);
1992 FREE_AND_NULL(unpack_opts.dir);
1996 static int record_conflicted_index_entries(struct merge_options *opt,
1997 struct index_state *index,
1998 struct strmap *paths,
1999 struct strmap *conflicted)
2001 struct hashmap_iter iter;
2002 struct strmap_entry *e;
2004 int original_cache_nr;
2006 if (strmap_empty(conflicted))
2009 original_cache_nr = index->cache_nr;
2011 /* Put every entry from paths into plist, then sort */
2012 strmap_for_each_entry(conflicted, &iter, e) {
2013 const char *path = e->key;
2014 struct conflict_info *ci = e->value;
2016 struct cache_entry *ce;
2022 * The index will already have a stage=0 entry for this path,
2023 * because we created an as-merged-as-possible version of the
2024 * file and checkout() moved the working copy and index over
2027 * However, previous iterations through this loop will have
2028 * added unstaged entries to the end of the cache which
2029 * ignore the standard alphabetical ordering of cache
2030 * entries and break invariants needed for index_name_pos()
2031 * to work. However, we know the entry we want is before
2032 * those appended cache entries, so do a temporary swap on
2033 * cache_nr to only look through entries of interest.
2035 SWAP(index->cache_nr, original_cache_nr);
2036 pos = index_name_pos(index, path, strlen(path));
2037 SWAP(index->cache_nr, original_cache_nr);
2039 if (ci->filemask != 1)
2040 BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path);
2041 cache_tree_invalidate_path(index, path);
2043 ce = index->cache[pos];
2046 * Clean paths with CE_SKIP_WORKTREE set will not be
2047 * written to the working tree by the unpack_trees()
2048 * call in checkout(). Our conflicted entries would
2049 * have appeared clean to that code since we ignored
2050 * the higher order stages. Thus, we need override
2051 * the CE_SKIP_WORKTREE bit and manually write those
2052 * files to the working disk here.
2054 * TODO: Implement this CE_SKIP_WORKTREE fixup.
2058 * Mark this cache entry for removal and instead add
2059 * new stage>0 entries corresponding to the
2060 * conflicts. If there are many conflicted entries, we
2061 * want to avoid memmove'ing O(NM) entries by
2062 * inserting the new entries one at a time. So,
2063 * instead, we just add the new cache entries to the
2064 * end (ignoring normal index requirements on sort
2065 * order) and sort the index once we're all done.
2067 ce->ce_flags |= CE_REMOVE;
2070 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
2071 struct version_info *vi;
2072 if (!(ci->filemask & (1ul << i)))
2074 vi = &ci->stages[i];
2075 ce = make_cache_entry(index, vi->mode, &vi->oid,
2077 add_index_entry(index, ce, ADD_CACHE_JUST_APPEND);
2082 * Remove the unused cache entries (and invalidate the relevant
2083 * cache-trees), then sort the index entries to get the conflicted
2084 * entries we added to the end into their right locations.
2086 remove_marked_cache_entries(index, 1);
2087 QSORT(index->cache, index->cache_nr, cmp_cache_name_compare);
2092 void merge_switch_to_result(struct merge_options *opt,
2094 struct merge_result *result,
2095 int update_worktree_and_index,
2096 int display_update_msgs)
2098 assert(opt->priv == NULL);
2099 if (result->clean >= 0 && update_worktree_and_index) {
2100 struct merge_options_internal *opti = result->priv;
2102 if (checkout(opt, head, result->tree)) {
2103 /* failure to function */
2108 if (record_conflicted_index_entries(opt, opt->repo->index,
2110 &opti->conflicted)) {
2111 /* failure to function */
2117 if (display_update_msgs) {
2118 struct merge_options_internal *opti = result->priv;
2119 struct hashmap_iter iter;
2120 struct strmap_entry *e;
2121 struct string_list olist = STRING_LIST_INIT_NODUP;
2124 /* Hack to pre-allocate olist to the desired size */
2125 ALLOC_GROW(olist.items, strmap_get_size(&opti->output),
2128 /* Put every entry from output into olist, then sort */
2129 strmap_for_each_entry(&opti->output, &iter, e) {
2130 string_list_append(&olist, e->key)->util = e->value;
2132 string_list_sort(&olist);
2134 /* Iterate over the items, printing them */
2135 for (i = 0; i < olist.nr; ++i) {
2136 struct strbuf *sb = olist.items[i].util;
2138 printf("%s", sb->buf);
2140 string_list_clear(&olist, 0);
2142 /* Also include needed rename limit adjustment now */
2143 diff_warn_rename_limit("merge.renamelimit",
2144 opti->renames.needed_limit, 0);
2147 merge_finalize(opt, result);
2150 void merge_finalize(struct merge_options *opt,
2151 struct merge_result *result)
2153 struct merge_options_internal *opti = result->priv;
2155 assert(opt->priv == NULL);
2157 clear_or_reinit_internal_opts(opti, 0);
2158 FREE_AND_NULL(opti);
2161 /*** Function Grouping: helper functions for merge_incore_*() ***/
2163 static inline void set_commit_tree(struct commit *c, struct tree *t)
2168 static struct commit *make_virtual_commit(struct repository *repo,
2170 const char *comment)
2172 struct commit *commit = alloc_commit_node(repo);
2174 set_merge_remote_desc(commit, comment, (struct object *)commit);
2175 set_commit_tree(commit, tree);
2176 commit->object.parsed = 1;
2180 static void merge_start(struct merge_options *opt, struct merge_result *result)
2182 struct rename_info *renames;
2185 /* Sanity checks on opt */
2188 assert(opt->branch1 && opt->branch2);
2190 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
2191 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
2192 assert(opt->rename_limit >= -1);
2193 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
2194 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
2196 assert(opt->xdl_opts >= 0);
2197 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
2198 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
2201 * detect_renames, verbosity, buffer_output, and obuf are ignored
2202 * fields that were used by "recursive" rather than "ort" -- but
2203 * sanity check them anyway.
2205 assert(opt->detect_renames >= -1 &&
2206 opt->detect_renames <= DIFF_DETECT_COPY);
2207 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
2208 assert(opt->buffer_output <= 2);
2209 assert(opt->obuf.len == 0);
2211 assert(opt->priv == NULL);
2213 /* Default to histogram diff. Actually, just hardcode it...for now. */
2214 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
2216 /* Initialization of opt->priv, our internal merge data */
2217 opt->priv = xcalloc(1, sizeof(*opt->priv));
2219 /* Initialization of various renames fields */
2220 renames = &opt->priv->renames;
2221 for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) {
2222 strset_init_with_options(&renames->dirs_removed[i],
2224 strmap_init_with_options(&renames->dir_rename_count[i],
2226 strmap_init_with_options(&renames->dir_renames[i],
2231 * Although we initialize opt->priv->paths with strdup_strings=0,
2232 * that's just to avoid making yet another copy of an allocated
2233 * string. Putting the entry into paths means we are taking
2234 * ownership, so we will later free it. paths_to_free is similar.
2236 * In contrast, conflicted just has a subset of keys from paths, so
2237 * we don't want to free those (it'd be a duplicate free).
2239 strmap_init_with_options(&opt->priv->paths, NULL, 0);
2240 strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
2241 string_list_init(&opt->priv->paths_to_free, 0);
2244 * keys & strbufs in output will sometimes need to outlive "paths",
2245 * so it will have a copy of relevant keys. It's probably a small
2246 * subset of the overall paths that have special output.
2248 strmap_init(&opt->priv->output);
2251 /*** Function Grouping: merge_incore_*() and their internal variants ***/
2254 * Originally from merge_trees_internal(); heavily adapted, though.
2256 static void merge_ort_nonrecursive_internal(struct merge_options *opt,
2257 struct tree *merge_base,
2260 struct merge_result *result)
2262 struct object_id working_tree_oid;
2264 if (collect_merge_info(opt, merge_base, side1, side2) != 0) {
2266 * TRANSLATORS: The %s arguments are: 1) tree hash of a merge
2267 * base, and 2-3) the trees for the two trees we're merging.
2269 err(opt, _("collecting merge info failed for trees %s, %s, %s"),
2270 oid_to_hex(&merge_base->object.oid),
2271 oid_to_hex(&side1->object.oid),
2272 oid_to_hex(&side2->object.oid));
2277 result->clean = detect_and_process_renames(opt, merge_base,
2279 process_entries(opt, &working_tree_oid);
2281 /* Set return values */
2282 result->tree = parse_tree_indirect(&working_tree_oid);
2283 /* existence of conflicted entries implies unclean */
2284 result->clean &= strmap_empty(&opt->priv->conflicted);
2285 if (!opt->priv->call_depth) {
2286 result->priv = opt->priv;
2292 * Originally from merge_recursive_internal(); somewhat adapted, though.
2294 static void merge_ort_internal(struct merge_options *opt,
2295 struct commit_list *merge_bases,
2298 struct merge_result *result)
2300 struct commit_list *iter;
2301 struct commit *merged_merge_bases;
2302 const char *ancestor_name;
2303 struct strbuf merge_base_abbrev = STRBUF_INIT;
2306 merge_bases = get_merge_bases(h1, h2);
2307 /* See merge-ort.h:merge_incore_recursive() declaration NOTE */
2308 merge_bases = reverse_commit_list(merge_bases);
2311 merged_merge_bases = pop_commit(&merge_bases);
2312 if (merged_merge_bases == NULL) {
2313 /* if there is no common ancestor, use an empty tree */
2316 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
2317 merged_merge_bases = make_virtual_commit(opt->repo, tree,
2319 ancestor_name = "empty tree";
2320 } else if (merge_bases) {
2321 ancestor_name = "merged common ancestors";
2323 strbuf_add_unique_abbrev(&merge_base_abbrev,
2324 &merged_merge_bases->object.oid,
2326 ancestor_name = merge_base_abbrev.buf;
2329 for (iter = merge_bases; iter; iter = iter->next) {
2330 const char *saved_b1, *saved_b2;
2331 struct commit *prev = merged_merge_bases;
2333 opt->priv->call_depth++;
2335 * When the merge fails, the result contains files
2336 * with conflict markers. The cleanness flag is
2337 * ignored (unless indicating an error), it was never
2338 * actually used, as result of merge_trees has always
2339 * overwritten it: the committed "conflicts" were
2342 saved_b1 = opt->branch1;
2343 saved_b2 = opt->branch2;
2344 opt->branch1 = "Temporary merge branch 1";
2345 opt->branch2 = "Temporary merge branch 2";
2346 merge_ort_internal(opt, NULL, prev, iter->item, result);
2347 if (result->clean < 0)
2349 opt->branch1 = saved_b1;
2350 opt->branch2 = saved_b2;
2351 opt->priv->call_depth--;
2353 merged_merge_bases = make_virtual_commit(opt->repo,
2356 commit_list_insert(prev, &merged_merge_bases->parents);
2357 commit_list_insert(iter->item,
2358 &merged_merge_bases->parents->next);
2360 clear_or_reinit_internal_opts(opt->priv, 1);
2363 opt->ancestor = ancestor_name;
2364 merge_ort_nonrecursive_internal(opt,
2365 repo_get_commit_tree(opt->repo,
2366 merged_merge_bases),
2367 repo_get_commit_tree(opt->repo, h1),
2368 repo_get_commit_tree(opt->repo, h2),
2370 strbuf_release(&merge_base_abbrev);
2371 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
2374 void merge_incore_nonrecursive(struct merge_options *opt,
2375 struct tree *merge_base,
2378 struct merge_result *result)
2380 assert(opt->ancestor != NULL);
2381 merge_start(opt, result);
2382 merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
2385 void merge_incore_recursive(struct merge_options *opt,
2386 struct commit_list *merge_bases,
2387 struct commit *side1,
2388 struct commit *side2,
2389 struct merge_result *result)
2391 /* We set the ancestor label based on the merge_bases */
2392 assert(opt->ancestor == NULL);
2394 merge_start(opt, result);
2395 merge_ort_internal(opt, merge_bases, side1, side2, result);