merge-ort: add some high-level algorithm structure

merge_ort_nonrecursive_internal() will be used by both merge_inmemory_nonrecursive() and merge_inmemory_recursive(); let's focus on it for now. It involves some setup -- merge_start() -- followed by the following chain of functions: collect_merge_info() This function will populate merge_options_internal's paths field, via a call to traverse_trees() and a new callback that will be added later. detect_and_process_renames() This function will detect renames, and then adjust entries in paths to move conflict stages from old pathnames into those for new pathnames, so that the next step doesn't have to think about renames and just can do three-way content merging and such. process_entries() This function determines how to take the various stages (versions of a file from the three different sides) and merge them, and whether to mark the result as conflicted or cleanly merged. It also writes out these merged file versions as it goes to create a tree. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 13, 2020 at 08:04 UTC 231e2dd49d11c1ea5d19956920651bd3a90ccfce
1 file changed +67 -1
merge-ort.c
+67 -1
@@ -18,6 +18,7 @@
18 #include "merge-ort.h"
19
20 #include "strmap.h"
21 +#include "tree.h"
22
23 struct merge_options_internal {
24 /*
@@ -164,6 +165,38 @@ struct conflict_info {
165 unsigned match_mask:3;
166 };
167
168 +static int collect_merge_info(struct merge_options *opt,
169 + struct tree *merge_base,
170 + struct tree *side1,
171 + struct tree *side2)
172 +{
173 + /* TODO: Implement this using traverse_trees() */
174 + die("Not yet implemented.");
175 +}
176 +
177 +static int detect_and_process_renames(struct merge_options *opt,
178 + struct tree *merge_base,
179 + struct tree *side1,
180 + struct tree *side2)
181 +{
182 + int clean = 1;
183 +
184 + /*
185 + * Rename detection works by detecting file similarity. Here we use
186 + * a really easy-to-implement scheme: files are similar IFF they have
187 + * the same filename. Therefore, by this scheme, there are no renames.
188 + *
189 + * TODO: Actually implement a real rename detection scheme.
190 + */
191 + return clean;
192 +}
193 +
194 +static void process_entries(struct merge_options *opt,
195 + struct object_id *result_oid)
196 +{
197 + die("Not yet implemented.");
198 +}
199 +
200 void merge_switch_to_result(struct merge_options *opt,
201 struct tree *head,
202 struct merge_result *result,
@@ -180,13 +213,46 @@ void merge_finalize(struct merge_options *opt,
213 die("Not yet implemented");
214 }
215
216 +static void merge_start(struct merge_options *opt, struct merge_result *result)
217 +{
218 + die("Not yet implemented.");
219 +}
220 +
221 +/*
222 + * Originally from merge_trees_internal(); heavily adapted, though.
223 + */
224 +static void merge_ort_nonrecursive_internal(struct merge_options *opt,
225 + struct tree *merge_base,
226 + struct tree *side1,
227 + struct tree *side2,
228 + struct merge_result *result)
229 +{
230 + struct object_id working_tree_oid;
231 +
232 + collect_merge_info(opt, merge_base, side1, side2);
233 + result->clean = detect_and_process_renames(opt, merge_base,
234 + side1, side2);
235 + process_entries(opt, &working_tree_oid);
236 +
237 + /* Set return values */
238 + result->tree = parse_tree_indirect(&working_tree_oid);
239 + /* existence of conflicted entries implies unclean */
240 + result->clean &= strmap_empty(&opt->priv->conflicted);
241 + if (!opt->priv->call_depth) {
242 + result->priv = opt->priv;
243 + opt->priv = NULL;
244 + }
245 +}
246 +
247 void merge_incore_nonrecursive(struct merge_options *opt,
248 struct tree *merge_base,
249 struct tree *side1,
250 struct tree *side2,
251 struct merge_result *result)
252 {
189 - die("Not yet implemented");
253 + assert(opt->ancestor != NULL);
254 + merge_start(opt, result);
255 + merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
256 }
257
258 void merge_incore_recursive(struct merge_options *opt,