]> git.ipfire.org Git - thirdparty/git.git/blob - merge-ort.c
Merge branch 'fc/pull-merge-rebase'
[thirdparty/git.git] / merge-ort.c
1 /*
2 * "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant
3 * as a drop-in replacement for the "recursive" merge strategy, allowing one
4 * to replace
5 *
6 * git merge [-s recursive]
7 *
8 * with
9 *
10 * git merge -s ort
11 *
12 * Note: git's parser allows the space between '-s' and its argument to be
13 * missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo",
14 * "cale", "peedy", or "ins" instead of "ort"?)
15 */
16
17 #include "cache.h"
18 #include "merge-ort.h"
19
20 #include "blob.h"
21 #include "cache-tree.h"
22 #include "commit-reach.h"
23 #include "diff.h"
24 #include "diffcore.h"
25 #include "dir.h"
26 #include "object-store.h"
27 #include "strmap.h"
28 #include "tree.h"
29 #include "unpack-trees.h"
30 #include "xdiff-interface.h"
31
32 /*
33 * We have many arrays of size 3. Whenever we have such an array, the
34 * indices refer to one of the sides of the three-way merge. This is so
35 * pervasive that the constants 0, 1, and 2 are used in many places in the
36 * code (especially in arithmetic operations to find the other side's index
37 * or to compute a relevant mask), but sometimes these enum names are used
38 * to aid code clarity.
39 *
40 * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
41 * referred to there is one of these three sides.
42 */
43 enum merge_side {
44 MERGE_BASE = 0,
45 MERGE_SIDE1 = 1,
46 MERGE_SIDE2 = 2
47 };
48
49 struct merge_options_internal {
50 /*
51 * paths: primary data structure in all of merge ort.
52 *
53 * The keys of paths:
54 * * are full relative paths from the toplevel of the repository
55 * (e.g. "drivers/firmware/raspberrypi.c").
56 * * store all relevant paths in the repo, both directories and
57 * files (e.g. drivers, drivers/firmware would also be included)
58 * * these keys serve to intern all the path strings, which allows
59 * us to do pointer comparison on directory names instead of
60 * strcmp; we just have to be careful to use the interned strings.
61 * (Technically paths_to_free may track some strings that were
62 * removed from froms paths.)
63 *
64 * The values of paths:
65 * * either a pointer to a merged_info, or a conflict_info struct
66 * * merged_info contains all relevant information for a
67 * non-conflicted entry.
68 * * conflict_info contains a merged_info, plus any additional
69 * information about a conflict such as the higher orders stages
70 * involved and the names of the paths those came from (handy
71 * once renames get involved).
72 * * a path may start "conflicted" (i.e. point to a conflict_info)
73 * and then a later step (e.g. three-way content merge) determines
74 * it can be cleanly merged, at which point it'll be marked clean
75 * and the algorithm will ignore any data outside the contained
76 * merged_info for that entry
77 * * If an entry remains conflicted, the merged_info portion of a
78 * conflict_info will later be filled with whatever version of
79 * the file should be placed in the working directory (e.g. an
80 * as-merged-as-possible variation that contains conflict markers).
81 */
82 struct strmap paths;
83
84 /*
85 * conflicted: a subset of keys->values from "paths"
86 *
87 * conflicted is basically an optimization between process_entries()
88 * and record_conflicted_index_entries(); the latter could loop over
89 * ALL the entries in paths AGAIN and look for the ones that are
90 * still conflicted, but since process_entries() has to loop over
91 * all of them, it saves the ones it couldn't resolve in this strmap
92 * so that record_conflicted_index_entries() can iterate just the
93 * relevant entries.
94 */
95 struct strmap conflicted;
96
97 /*
98 * paths_to_free: additional list of strings to free
99 *
100 * If keys are removed from "paths", they are added to paths_to_free
101 * to ensure they are later freed. We avoid free'ing immediately since
102 * other places (e.g. conflict_info.pathnames[]) may still be
103 * referencing these paths.
104 */
105 struct string_list paths_to_free;
106
107 /*
108 * output: special messages and conflict notices for various paths
109 *
110 * This is a map of pathnames (a subset of the keys in "paths" above)
111 * to strbufs. It gathers various warning/conflict/notice messages
112 * for later processing.
113 */
114 struct strmap output;
115
116 /*
117 * current_dir_name: temporary var used in collect_merge_info_callback()
118 *
119 * Used to set merged_info.directory_name; see documentation for that
120 * variable and the requirements placed on that field.
121 */
122 const char *current_dir_name;
123
124 /* call_depth: recursion level counter for merging merge bases */
125 int call_depth;
126 };
127
128 struct version_info {
129 struct object_id oid;
130 unsigned short mode;
131 };
132
133 struct merged_info {
134 /* if is_null, ignore result. otherwise result has oid & mode */
135 struct version_info result;
136 unsigned is_null:1;
137
138 /*
139 * clean: whether the path in question is cleanly merged.
140 *
141 * see conflict_info.merged for more details.
142 */
143 unsigned clean:1;
144
145 /*
146 * basename_offset: offset of basename of path.
147 *
148 * perf optimization to avoid recomputing offset of final '/'
149 * character in pathname (0 if no '/' in pathname).
150 */
151 size_t basename_offset;
152
153 /*
154 * directory_name: containing directory name.
155 *
156 * Note that we assume directory_name is constructed such that
157 * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
158 * i.e. string equality is equivalent to pointer equality. For this
159 * to hold, we have to be careful setting directory_name.
160 */
161 const char *directory_name;
162 };
163
164 struct conflict_info {
165 /*
166 * merged: the version of the path that will be written to working tree
167 *
168 * WARNING: It is critical to check merged.clean and ensure it is 0
169 * before reading any conflict_info fields outside of merged.
170 * Allocated merge_info structs will always have clean set to 1.
171 * Allocated conflict_info structs will have merged.clean set to 0
172 * initially. The merged.clean field is how we know if it is safe
173 * to access other parts of conflict_info besides merged; if a
174 * conflict_info's merged.clean is changed to 1, the rest of the
175 * algorithm is not allowed to look at anything outside of the
176 * merged member anymore.
177 */
178 struct merged_info merged;
179
180 /* oids & modes from each of the three trees for this path */
181 struct version_info stages[3];
182
183 /* pathnames for each stage; may differ due to rename detection */
184 const char *pathnames[3];
185
186 /* Whether this path is/was involved in a directory/file conflict */
187 unsigned df_conflict:1;
188
189 /*
190 * Whether this path is/was involved in a non-content conflict other
191 * than a directory/file conflict (e.g. rename/rename, rename/delete,
192 * file location based on possible directory rename).
193 */
194 unsigned path_conflict:1;
195
196 /*
197 * For filemask and dirmask, the ith bit corresponds to whether the
198 * ith entry is a file (filemask) or a directory (dirmask). Thus,
199 * filemask & dirmask is always zero, and filemask | dirmask is at
200 * most 7 but can be less when a path does not appear as either a
201 * file or a directory on at least one side of history.
202 *
203 * Note that these masks are related to enum merge_side, as the ith
204 * entry corresponds to side i.
205 *
206 * These values come from a traverse_trees() call; more info may be
207 * found looking at tree-walk.h's struct traverse_info,
208 * particularly the documentation above the "fn" member (note that
209 * filemask = mask & ~dirmask from that documentation).
210 */
211 unsigned filemask:3;
212 unsigned dirmask:3;
213
214 /*
215 * Optimization to track which stages match, to avoid the need to
216 * recompute it in multiple steps. Either 0 or at least 2 bits are
217 * set; if at least 2 bits are set, their corresponding stages match.
218 */
219 unsigned match_mask:3;
220 };
221
222 /*** Function Grouping: various utility functions ***/
223
224 /*
225 * For the next three macros, see warning for conflict_info.merged.
226 *
227 * In each of the below, mi is a struct merged_info*, and ci was defined
228 * as a struct conflict_info* (but we need to verify ci isn't actually
229 * pointed at a struct merged_info*).
230 *
231 * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
232 * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
233 * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
234 */
235 #define INITIALIZE_CI(ci, mi) do { \
236 (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
237 } while (0)
238 #define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
239 #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \
240 (ci) = (struct conflict_info *)(mi); \
241 assert((ci) && !(mi)->clean); \
242 } while (0)
243
244 static void free_strmap_strings(struct strmap *map)
245 {
246 struct hashmap_iter iter;
247 struct strmap_entry *entry;
248
249 strmap_for_each_entry(map, &iter, entry) {
250 free((char*)entry->key);
251 }
252 }
253
254 static void clear_internal_opts(struct merge_options_internal *opti,
255 int reinitialize)
256 {
257 assert(!reinitialize);
258
259 /*
260 * We marked opti->paths with strdup_strings = 0, so that we
261 * wouldn't have to make another copy of the fullpath created by
262 * make_traverse_path from setup_path_info(). But, now that we've
263 * used it and have no other references to these strings, it is time
264 * to deallocate them.
265 */
266 free_strmap_strings(&opti->paths);
267 strmap_clear(&opti->paths, 1);
268
269 /*
270 * All keys and values in opti->conflicted are a subset of those in
271 * opti->paths. We don't want to deallocate anything twice, so we
272 * don't free the keys and we pass 0 for free_values.
273 */
274 strmap_clear(&opti->conflicted, 0);
275
276 /*
277 * opti->paths_to_free is similar to opti->paths; we created it with
278 * strdup_strings = 0 to avoid making _another_ copy of the fullpath
279 * but now that we've used it and have no other references to these
280 * strings, it is time to deallocate them. We do so by temporarily
281 * setting strdup_strings to 1.
282 */
283 opti->paths_to_free.strdup_strings = 1;
284 string_list_clear(&opti->paths_to_free, 0);
285 opti->paths_to_free.strdup_strings = 0;
286
287 if (!reinitialize) {
288 struct hashmap_iter iter;
289 struct strmap_entry *e;
290
291 /* Release and free each strbuf found in output */
292 strmap_for_each_entry(&opti->output, &iter, e) {
293 struct strbuf *sb = e->value;
294 strbuf_release(sb);
295 /*
296 * While strictly speaking we don't need to free(sb)
297 * here because we could pass free_values=1 when
298 * calling strmap_clear() on opti->output, that would
299 * require strmap_clear to do another
300 * strmap_for_each_entry() loop, so we just free it
301 * while we're iterating anyway.
302 */
303 free(sb);
304 }
305 strmap_clear(&opti->output, 0);
306 }
307 }
308
309 static int err(struct merge_options *opt, const char *err, ...)
310 {
311 va_list params;
312 struct strbuf sb = STRBUF_INIT;
313
314 strbuf_addstr(&sb, "error: ");
315 va_start(params, err);
316 strbuf_vaddf(&sb, err, params);
317 va_end(params);
318
319 error("%s", sb.buf);
320 strbuf_release(&sb);
321
322 return -1;
323 }
324
325 __attribute__((format (printf, 4, 5)))
326 static void path_msg(struct merge_options *opt,
327 const char *path,
328 int omittable_hint, /* skippable under --remerge-diff */
329 const char *fmt, ...)
330 {
331 va_list ap;
332 struct strbuf *sb = strmap_get(&opt->priv->output, path);
333 if (!sb) {
334 sb = xmalloc(sizeof(*sb));
335 strbuf_init(sb, 0);
336 strmap_put(&opt->priv->output, path, sb);
337 }
338
339 va_start(ap, fmt);
340 strbuf_vaddf(sb, fmt, ap);
341 va_end(ap);
342
343 strbuf_addch(sb, '\n');
344 }
345
346 /*** Function Grouping: functions related to collect_merge_info() ***/
347
348 static void setup_path_info(struct merge_options *opt,
349 struct string_list_item *result,
350 const char *current_dir_name,
351 int current_dir_name_len,
352 char *fullpath, /* we'll take over ownership */
353 struct name_entry *names,
354 struct name_entry *merged_version,
355 unsigned is_null, /* boolean */
356 unsigned df_conflict, /* boolean */
357 unsigned filemask,
358 unsigned dirmask,
359 int resolved /* boolean */)
360 {
361 /* result->util is void*, so mi is a convenience typed variable */
362 struct merged_info *mi;
363
364 assert(!is_null || resolved);
365 assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
366 assert(resolved == (merged_version != NULL));
367
368 mi = xcalloc(1, resolved ? sizeof(struct merged_info) :
369 sizeof(struct conflict_info));
370 mi->directory_name = current_dir_name;
371 mi->basename_offset = current_dir_name_len;
372 mi->clean = !!resolved;
373 if (resolved) {
374 mi->result.mode = merged_version->mode;
375 oidcpy(&mi->result.oid, &merged_version->oid);
376 mi->is_null = !!is_null;
377 } else {
378 int i;
379 struct conflict_info *ci;
380
381 ASSIGN_AND_VERIFY_CI(ci, mi);
382 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
383 ci->pathnames[i] = fullpath;
384 ci->stages[i].mode = names[i].mode;
385 oidcpy(&ci->stages[i].oid, &names[i].oid);
386 }
387 ci->filemask = filemask;
388 ci->dirmask = dirmask;
389 ci->df_conflict = !!df_conflict;
390 if (dirmask)
391 /*
392 * Assume is_null for now, but if we have entries
393 * under the directory then when it is complete in
394 * write_completed_directory() it'll update this.
395 * Also, for D/F conflicts, we have to handle the
396 * directory first, then clear this bit and process
397 * the file to see how it is handled -- that occurs
398 * near the top of process_entry().
399 */
400 mi->is_null = 1;
401 }
402 strmap_put(&opt->priv->paths, fullpath, mi);
403 result->string = fullpath;
404 result->util = mi;
405 }
406
407 static int collect_merge_info_callback(int n,
408 unsigned long mask,
409 unsigned long dirmask,
410 struct name_entry *names,
411 struct traverse_info *info)
412 {
413 /*
414 * n is 3. Always.
415 * common ancestor (mbase) has mask 1, and stored in index 0 of names
416 * head of side 1 (side1) has mask 2, and stored in index 1 of names
417 * head of side 2 (side2) has mask 4, and stored in index 2 of names
418 */
419 struct merge_options *opt = info->data;
420 struct merge_options_internal *opti = opt->priv;
421 struct string_list_item pi; /* Path Info */
422 struct conflict_info *ci; /* typed alias to pi.util (which is void*) */
423 struct name_entry *p;
424 size_t len;
425 char *fullpath;
426 const char *dirname = opti->current_dir_name;
427 unsigned filemask = mask & ~dirmask;
428 unsigned match_mask = 0; /* will be updated below */
429 unsigned mbase_null = !(mask & 1);
430 unsigned side1_null = !(mask & 2);
431 unsigned side2_null = !(mask & 4);
432 unsigned side1_matches_mbase = (!side1_null && !mbase_null &&
433 names[0].mode == names[1].mode &&
434 oideq(&names[0].oid, &names[1].oid));
435 unsigned side2_matches_mbase = (!side2_null && !mbase_null &&
436 names[0].mode == names[2].mode &&
437 oideq(&names[0].oid, &names[2].oid));
438 unsigned sides_match = (!side1_null && !side2_null &&
439 names[1].mode == names[2].mode &&
440 oideq(&names[1].oid, &names[2].oid));
441
442 /*
443 * Note: When a path is a file on one side of history and a directory
444 * in another, we have a directory/file conflict. In such cases, if
445 * the conflict doesn't resolve from renames and deletions, then we
446 * always leave directories where they are and move files out of the
447 * way. Thus, while struct conflict_info has a df_conflict field to
448 * track such conflicts, we ignore that field for any directories at
449 * a path and only pay attention to it for files at the given path.
450 * The fact that we leave directories were they are also means that
451 * we do not need to worry about getting additional df_conflict
452 * information propagated from parent directories down to children
453 * (unlike, say traverse_trees_recursive() in unpack-trees.c, which
454 * sets a newinfo.df_conflicts field specifically to propagate it).
455 */
456 unsigned df_conflict = (filemask != 0) && (dirmask != 0);
457
458 /* n = 3 is a fundamental assumption. */
459 if (n != 3)
460 BUG("Called collect_merge_info_callback wrong");
461
462 /*
463 * A bunch of sanity checks verifying that traverse_trees() calls
464 * us the way I expect. Could just remove these at some point,
465 * though maybe they are helpful to future code readers.
466 */
467 assert(mbase_null == is_null_oid(&names[0].oid));
468 assert(side1_null == is_null_oid(&names[1].oid));
469 assert(side2_null == is_null_oid(&names[2].oid));
470 assert(!mbase_null || !side1_null || !side2_null);
471 assert(mask > 0 && mask < 8);
472
473 /* Determine match_mask */
474 if (side1_matches_mbase)
475 match_mask = (side2_matches_mbase ? 7 : 3);
476 else if (side2_matches_mbase)
477 match_mask = 5;
478 else if (sides_match)
479 match_mask = 6;
480
481 /*
482 * Get the name of the relevant filepath, which we'll pass to
483 * setup_path_info() for tracking.
484 */
485 p = names;
486 while (!p->mode)
487 p++;
488 len = traverse_path_len(info, p->pathlen);
489
490 /* +1 in both of the following lines to include the NUL byte */
491 fullpath = xmalloc(len + 1);
492 make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen);
493
494 /*
495 * If mbase, side1, and side2 all match, we can resolve early. Even
496 * if these are trees, there will be no renames or anything
497 * underneath.
498 */
499 if (side1_matches_mbase && side2_matches_mbase) {
500 /* mbase, side1, & side2 all match; use mbase as resolution */
501 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
502 names, names+0, mbase_null, 0,
503 filemask, dirmask, 1);
504 return mask;
505 }
506
507 /*
508 * Record information about the path so we can resolve later in
509 * process_entries.
510 */
511 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
512 names, NULL, 0, df_conflict, filemask, dirmask, 0);
513
514 ci = pi.util;
515 VERIFY_CI(ci);
516 ci->match_mask = match_mask;
517
518 /* If dirmask, recurse into subdirectories */
519 if (dirmask) {
520 struct traverse_info newinfo;
521 struct tree_desc t[3];
522 void *buf[3] = {NULL, NULL, NULL};
523 const char *original_dir_name;
524 int i, ret;
525
526 ci->match_mask &= filemask;
527 newinfo = *info;
528 newinfo.prev = info;
529 newinfo.name = p->path;
530 newinfo.namelen = p->pathlen;
531 newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1);
532 /*
533 * If this directory we are about to recurse into cared about
534 * its parent directory (the current directory) having a D/F
535 * conflict, then we'd propagate the masks in this way:
536 * newinfo.df_conflicts |= (mask & ~dirmask);
537 * But we don't worry about propagating D/F conflicts. (See
538 * comment near setting of local df_conflict variable near
539 * the beginning of this function).
540 */
541
542 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
543 if (i == 1 && side1_matches_mbase)
544 t[1] = t[0];
545 else if (i == 2 && side2_matches_mbase)
546 t[2] = t[0];
547 else if (i == 2 && sides_match)
548 t[2] = t[1];
549 else {
550 const struct object_id *oid = NULL;
551 if (dirmask & 1)
552 oid = &names[i].oid;
553 buf[i] = fill_tree_descriptor(opt->repo,
554 t + i, oid);
555 }
556 dirmask >>= 1;
557 }
558
559 original_dir_name = opti->current_dir_name;
560 opti->current_dir_name = pi.string;
561 ret = traverse_trees(NULL, 3, t, &newinfo);
562 opti->current_dir_name = original_dir_name;
563
564 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++)
565 free(buf[i]);
566
567 if (ret < 0)
568 return -1;
569 }
570
571 return mask;
572 }
573
574 static int collect_merge_info(struct merge_options *opt,
575 struct tree *merge_base,
576 struct tree *side1,
577 struct tree *side2)
578 {
579 int ret;
580 struct tree_desc t[3];
581 struct traverse_info info;
582 const char *toplevel_dir_placeholder = "";
583
584 opt->priv->current_dir_name = toplevel_dir_placeholder;
585 setup_traverse_info(&info, toplevel_dir_placeholder);
586 info.fn = collect_merge_info_callback;
587 info.data = opt;
588 info.show_all_errors = 1;
589
590 parse_tree(merge_base);
591 parse_tree(side1);
592 parse_tree(side2);
593 init_tree_desc(t + 0, merge_base->buffer, merge_base->size);
594 init_tree_desc(t + 1, side1->buffer, side1->size);
595 init_tree_desc(t + 2, side2->buffer, side2->size);
596
597 ret = traverse_trees(NULL, 3, t, &info);
598
599 return ret;
600 }
601
602 /*** Function Grouping: functions related to threeway content merges ***/
603
604 static int handle_content_merge(struct merge_options *opt,
605 const char *path,
606 const struct version_info *o,
607 const struct version_info *a,
608 const struct version_info *b,
609 const char *pathnames[3],
610 const int extra_marker_size,
611 struct version_info *result)
612 {
613 die("Not yet implemented");
614 }
615
616 /*** Function Grouping: functions related to detect_and_process_renames(), ***
617 *** which are split into directory and regular rename detection sections. ***/
618
619 /*** Function Grouping: functions related to directory rename detection ***/
620
621 /*** Function Grouping: functions related to regular rename detection ***/
622
623 static int detect_and_process_renames(struct merge_options *opt,
624 struct tree *merge_base,
625 struct tree *side1,
626 struct tree *side2)
627 {
628 int clean = 1;
629
630 /*
631 * Rename detection works by detecting file similarity. Here we use
632 * a really easy-to-implement scheme: files are similar IFF they have
633 * the same filename. Therefore, by this scheme, there are no renames.
634 *
635 * TODO: Actually implement a real rename detection scheme.
636 */
637 return clean;
638 }
639
640 /*** Function Grouping: functions related to process_entries() ***/
641
642 static int string_list_df_name_compare(const char *one, const char *two)
643 {
644 int onelen = strlen(one);
645 int twolen = strlen(two);
646 /*
647 * Here we only care that entries for D/F conflicts are
648 * adjacent, in particular with the file of the D/F conflict
649 * appearing before files below the corresponding directory.
650 * The order of the rest of the list is irrelevant for us.
651 *
652 * To achieve this, we sort with df_name_compare and provide
653 * the mode S_IFDIR so that D/F conflicts will sort correctly.
654 * We use the mode S_IFDIR for everything else for simplicity,
655 * since in other cases any changes in their order due to
656 * sorting cause no problems for us.
657 */
658 int cmp = df_name_compare(one, onelen, S_IFDIR,
659 two, twolen, S_IFDIR);
660 /*
661 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
662 * that 'foo' comes before 'foo/bar'.
663 */
664 if (cmp)
665 return cmp;
666 return onelen - twolen;
667 }
668
669 struct directory_versions {
670 /*
671 * versions: list of (basename -> version_info)
672 *
673 * The basenames are in reverse lexicographic order of full pathnames,
674 * as processed in process_entries(). This puts all entries within
675 * a directory together, and covers the directory itself after
676 * everything within it, allowing us to write subtrees before needing
677 * to record information for the tree itself.
678 */
679 struct string_list versions;
680
681 /*
682 * offsets: list of (full relative path directories -> integer offsets)
683 *
684 * Since versions contains basenames from files in multiple different
685 * directories, we need to know which entries in versions correspond
686 * to which directories. Values of e.g.
687 * "" 0
688 * src 2
689 * src/moduleA 5
690 * Would mean that entries 0-1 of versions are files in the toplevel
691 * directory, entries 2-4 are files under src/, and the remaining
692 * entries starting at index 5 are files under src/moduleA/.
693 */
694 struct string_list offsets;
695
696 /*
697 * last_directory: directory that previously processed file found in
698 *
699 * last_directory starts NULL, but records the directory in which the
700 * previous file was found within. As soon as
701 * directory(current_file) != last_directory
702 * then we need to start updating accounting in versions & offsets.
703 * Note that last_directory is always the last path in "offsets" (or
704 * NULL if "offsets" is empty) so this exists just for quick access.
705 */
706 const char *last_directory;
707
708 /* last_directory_len: cached computation of strlen(last_directory) */
709 unsigned last_directory_len;
710 };
711
712 static int tree_entry_order(const void *a_, const void *b_)
713 {
714 const struct string_list_item *a = a_;
715 const struct string_list_item *b = b_;
716
717 const struct merged_info *ami = a->util;
718 const struct merged_info *bmi = b->util;
719 return base_name_compare(a->string, strlen(a->string), ami->result.mode,
720 b->string, strlen(b->string), bmi->result.mode);
721 }
722
723 static void write_tree(struct object_id *result_oid,
724 struct string_list *versions,
725 unsigned int offset,
726 size_t hash_size)
727 {
728 size_t maxlen = 0, extra;
729 unsigned int nr = versions->nr - offset;
730 struct strbuf buf = STRBUF_INIT;
731 struct string_list relevant_entries = STRING_LIST_INIT_NODUP;
732 int i;
733
734 /*
735 * We want to sort the last (versions->nr-offset) entries in versions.
736 * Do so by abusing the string_list API a bit: make another string_list
737 * that contains just those entries and then sort them.
738 *
739 * We won't use relevant_entries again and will let it just pop off the
740 * stack, so there won't be allocation worries or anything.
741 */
742 relevant_entries.items = versions->items + offset;
743 relevant_entries.nr = versions->nr - offset;
744 QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order);
745
746 /* Pre-allocate some space in buf */
747 extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */
748 for (i = 0; i < nr; i++) {
749 maxlen += strlen(versions->items[offset+i].string) + extra;
750 }
751 strbuf_grow(&buf, maxlen);
752
753 /* Write each entry out to buf */
754 for (i = 0; i < nr; i++) {
755 struct merged_info *mi = versions->items[offset+i].util;
756 struct version_info *ri = &mi->result;
757 strbuf_addf(&buf, "%o %s%c",
758 ri->mode,
759 versions->items[offset+i].string, '\0');
760 strbuf_add(&buf, ri->oid.hash, hash_size);
761 }
762
763 /* Write this object file out, and record in result_oid */
764 write_object_file(buf.buf, buf.len, tree_type, result_oid);
765 strbuf_release(&buf);
766 }
767
768 static void record_entry_for_tree(struct directory_versions *dir_metadata,
769 const char *path,
770 struct merged_info *mi)
771 {
772 const char *basename;
773
774 if (mi->is_null)
775 /* nothing to record */
776 return;
777
778 basename = path + mi->basename_offset;
779 assert(strchr(basename, '/') == NULL);
780 string_list_append(&dir_metadata->versions,
781 basename)->util = &mi->result;
782 }
783
784 static void write_completed_directory(struct merge_options *opt,
785 const char *new_directory_name,
786 struct directory_versions *info)
787 {
788 const char *prev_dir;
789 struct merged_info *dir_info = NULL;
790 unsigned int offset;
791
792 /*
793 * Some explanation of info->versions and info->offsets...
794 *
795 * process_entries() iterates over all relevant files AND
796 * directories in reverse lexicographic order, and calls this
797 * function. Thus, an example of the paths that process_entries()
798 * could operate on (along with the directories for those paths
799 * being shown) is:
800 *
801 * xtract.c ""
802 * tokens.txt ""
803 * src/moduleB/umm.c src/moduleB
804 * src/moduleB/stuff.h src/moduleB
805 * src/moduleB/baz.c src/moduleB
806 * src/moduleB src
807 * src/moduleA/foo.c src/moduleA
808 * src/moduleA/bar.c src/moduleA
809 * src/moduleA src
810 * src ""
811 * Makefile ""
812 *
813 * info->versions:
814 *
815 * always contains the unprocessed entries and their
816 * version_info information. For example, after the first five
817 * entries above, info->versions would be:
818 *
819 * xtract.c <xtract.c's version_info>
820 * token.txt <token.txt's version_info>
821 * umm.c <src/moduleB/umm.c's version_info>
822 * stuff.h <src/moduleB/stuff.h's version_info>
823 * baz.c <src/moduleB/baz.c's version_info>
824 *
825 * Once a subdirectory is completed we remove the entries in
826 * that subdirectory from info->versions, writing it as a tree
827 * (write_tree()). Thus, as soon as we get to src/moduleB,
828 * info->versions would be updated to
829 *
830 * xtract.c <xtract.c's version_info>
831 * token.txt <token.txt's version_info>
832 * moduleB <src/moduleB's version_info>
833 *
834 * info->offsets:
835 *
836 * helps us track which entries in info->versions correspond to
837 * which directories. When we are N directories deep (e.g. 4
838 * for src/modA/submod/subdir/), we have up to N+1 unprocessed
839 * directories (+1 because of toplevel dir). Corresponding to
840 * the info->versions example above, after processing five entries
841 * info->offsets will be:
842 *
843 * "" 0
844 * src/moduleB 2
845 *
846 * which is used to know that xtract.c & token.txt are from the
847 * toplevel dirctory, while umm.c & stuff.h & baz.c are from the
848 * src/moduleB directory. Again, following the example above,
849 * once we need to process src/moduleB, then info->offsets is
850 * updated to
851 *
852 * "" 0
853 * src 2
854 *
855 * which says that moduleB (and only moduleB so far) is in the
856 * src directory.
857 *
858 * One unique thing to note about info->offsets here is that
859 * "src" was not added to info->offsets until there was a path
860 * (a file OR directory) immediately below src/ that got
861 * processed.
862 *
863 * Since process_entry() just appends new entries to info->versions,
864 * write_completed_directory() only needs to do work if the next path
865 * is in a directory that is different than the last directory found
866 * in info->offsets.
867 */
868
869 /*
870 * If we are working with the same directory as the last entry, there
871 * is no work to do. (See comments above the directory_name member of
872 * struct merged_info for why we can use pointer comparison instead of
873 * strcmp here.)
874 */
875 if (new_directory_name == info->last_directory)
876 return;
877
878 /*
879 * If we are just starting (last_directory is NULL), or last_directory
880 * is a prefix of the current directory, then we can just update
881 * info->offsets to record the offset where we started this directory
882 * and update last_directory to have quick access to it.
883 */
884 if (info->last_directory == NULL ||
885 !strncmp(new_directory_name, info->last_directory,
886 info->last_directory_len)) {
887 uintptr_t offset = info->versions.nr;
888
889 info->last_directory = new_directory_name;
890 info->last_directory_len = strlen(info->last_directory);
891 /*
892 * Record the offset into info->versions where we will
893 * start recording basenames of paths found within
894 * new_directory_name.
895 */
896 string_list_append(&info->offsets,
897 info->last_directory)->util = (void*)offset;
898 return;
899 }
900
901 /*
902 * The next entry that will be processed will be within
903 * new_directory_name. Since at this point we know that
904 * new_directory_name is within a different directory than
905 * info->last_directory, we have all entries for info->last_directory
906 * in info->versions and we need to create a tree object for them.
907 */
908 dir_info = strmap_get(&opt->priv->paths, info->last_directory);
909 assert(dir_info);
910 offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util;
911 if (offset == info->versions.nr) {
912 /*
913 * Actually, we don't need to create a tree object in this
914 * case. Whenever all files within a directory disappear
915 * during the merge (e.g. unmodified on one side and
916 * deleted on the other, or files were renamed elsewhere),
917 * then we get here and the directory itself needs to be
918 * omitted from its parent tree as well.
919 */
920 dir_info->is_null = 1;
921 } else {
922 /*
923 * Write out the tree to the git object directory, and also
924 * record the mode and oid in dir_info->result.
925 */
926 dir_info->is_null = 0;
927 dir_info->result.mode = S_IFDIR;
928 write_tree(&dir_info->result.oid, &info->versions, offset,
929 opt->repo->hash_algo->rawsz);
930 }
931
932 /*
933 * We've now used several entries from info->versions and one entry
934 * from info->offsets, so we get rid of those values.
935 */
936 info->offsets.nr--;
937 info->versions.nr = offset;
938
939 /*
940 * Now we've taken care of the completed directory, but we need to
941 * prepare things since future entries will be in
942 * new_directory_name. (In particular, process_entry() will be
943 * appending new entries to info->versions.) So, we need to make
944 * sure new_directory_name is the last entry in info->offsets.
945 */
946 prev_dir = info->offsets.nr == 0 ? NULL :
947 info->offsets.items[info->offsets.nr-1].string;
948 if (new_directory_name != prev_dir) {
949 uintptr_t c = info->versions.nr;
950 string_list_append(&info->offsets,
951 new_directory_name)->util = (void*)c;
952 }
953
954 /* And, of course, we need to update last_directory to match. */
955 info->last_directory = new_directory_name;
956 info->last_directory_len = strlen(info->last_directory);
957 }
958
959 /* Per entry merge function */
960 static void process_entry(struct merge_options *opt,
961 const char *path,
962 struct conflict_info *ci,
963 struct directory_versions *dir_metadata)
964 {
965 VERIFY_CI(ci);
966 assert(ci->filemask >= 0 && ci->filemask <= 7);
967 /* ci->match_mask == 7 was handled in collect_merge_info_callback() */
968 assert(ci->match_mask == 0 || ci->match_mask == 3 ||
969 ci->match_mask == 5 || ci->match_mask == 6);
970
971 if (ci->dirmask) {
972 record_entry_for_tree(dir_metadata, path, &ci->merged);
973 if (ci->filemask == 0)
974 /* nothing else to handle */
975 return;
976 assert(ci->df_conflict);
977 }
978
979 if (ci->df_conflict) {
980 die("Not yet implemented.");
981 }
982
983 /*
984 * NOTE: Below there is a long switch-like if-elseif-elseif... block
985 * which the code goes through even for the df_conflict cases
986 * above. Well, it will once we don't die-not-implemented above.
987 */
988 if (ci->match_mask) {
989 ci->merged.clean = 1;
990 if (ci->match_mask == 6) {
991 /* stages[1] == stages[2] */
992 ci->merged.result.mode = ci->stages[1].mode;
993 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
994 } else {
995 /* determine the mask of the side that didn't match */
996 unsigned int othermask = 7 & ~ci->match_mask;
997 int side = (othermask == 4) ? 2 : 1;
998
999 ci->merged.result.mode = ci->stages[side].mode;
1000 ci->merged.is_null = !ci->merged.result.mode;
1001 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1002
1003 assert(othermask == 2 || othermask == 4);
1004 assert(ci->merged.is_null ==
1005 (ci->filemask == ci->match_mask));
1006 }
1007 } else if (ci->filemask >= 6 &&
1008 (S_IFMT & ci->stages[1].mode) !=
1009 (S_IFMT & ci->stages[2].mode)) {
1010 /*
1011 * Two different items from (file/submodule/symlink)
1012 */
1013 die("Not yet implemented.");
1014 } else if (ci->filemask >= 6) {
1015 /*
1016 * TODO: Needs a two-way or three-way content merge, but we're
1017 * just being lazy and copying the version from HEAD and
1018 * leaving it as conflicted.
1019 */
1020 ci->merged.clean = 0;
1021 ci->merged.result.mode = ci->stages[1].mode;
1022 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
1023 /* When we fix above, we'll call handle_content_merge() */
1024 (void)handle_content_merge;
1025 } else if (ci->filemask == 3 || ci->filemask == 5) {
1026 /* Modify/delete */
1027 const char *modify_branch, *delete_branch;
1028 int side = (ci->filemask == 5) ? 2 : 1;
1029 int index = opt->priv->call_depth ? 0 : side;
1030
1031 ci->merged.result.mode = ci->stages[index].mode;
1032 oidcpy(&ci->merged.result.oid, &ci->stages[index].oid);
1033 ci->merged.clean = 0;
1034
1035 modify_branch = (side == 1) ? opt->branch1 : opt->branch2;
1036 delete_branch = (side == 1) ? opt->branch2 : opt->branch1;
1037
1038 path_msg(opt, path, 0,
1039 _("CONFLICT (modify/delete): %s deleted in %s "
1040 "and modified in %s. Version %s of %s left "
1041 "in tree."),
1042 path, delete_branch, modify_branch,
1043 modify_branch, path);
1044 } else if (ci->filemask == 2 || ci->filemask == 4) {
1045 /* Added on one side */
1046 int side = (ci->filemask == 4) ? 2 : 1;
1047 ci->merged.result.mode = ci->stages[side].mode;
1048 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1049 ci->merged.clean = !ci->df_conflict;
1050 } else if (ci->filemask == 1) {
1051 /* Deleted on both sides */
1052 ci->merged.is_null = 1;
1053 ci->merged.result.mode = 0;
1054 oidcpy(&ci->merged.result.oid, &null_oid);
1055 ci->merged.clean = 1;
1056 }
1057
1058 /*
1059 * If still conflicted, record it separately. This allows us to later
1060 * iterate over just conflicted entries when updating the index instead
1061 * of iterating over all entries.
1062 */
1063 if (!ci->merged.clean)
1064 strmap_put(&opt->priv->conflicted, path, ci);
1065 record_entry_for_tree(dir_metadata, path, &ci->merged);
1066 }
1067
1068 static void process_entries(struct merge_options *opt,
1069 struct object_id *result_oid)
1070 {
1071 struct hashmap_iter iter;
1072 struct strmap_entry *e;
1073 struct string_list plist = STRING_LIST_INIT_NODUP;
1074 struct string_list_item *entry;
1075 struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP,
1076 STRING_LIST_INIT_NODUP,
1077 NULL, 0 };
1078
1079 if (strmap_empty(&opt->priv->paths)) {
1080 oidcpy(result_oid, opt->repo->hash_algo->empty_tree);
1081 return;
1082 }
1083
1084 /* Hack to pre-allocate plist to the desired size */
1085 ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc);
1086
1087 /* Put every entry from paths into plist, then sort */
1088 strmap_for_each_entry(&opt->priv->paths, &iter, e) {
1089 string_list_append(&plist, e->key)->util = e->value;
1090 }
1091 plist.cmp = string_list_df_name_compare;
1092 string_list_sort(&plist);
1093
1094 /*
1095 * Iterate over the items in reverse order, so we can handle paths
1096 * below a directory before needing to handle the directory itself.
1097 *
1098 * This allows us to write subtrees before we need to write trees,
1099 * and it also enables sane handling of directory/file conflicts
1100 * (because it allows us to know whether the directory is still in
1101 * the way when it is time to process the file at the same path).
1102 */
1103 for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) {
1104 char *path = entry->string;
1105 /*
1106 * NOTE: mi may actually be a pointer to a conflict_info, but
1107 * we have to check mi->clean first to see if it's safe to
1108 * reassign to such a pointer type.
1109 */
1110 struct merged_info *mi = entry->util;
1111
1112 write_completed_directory(opt, mi->directory_name,
1113 &dir_metadata);
1114 if (mi->clean)
1115 record_entry_for_tree(&dir_metadata, path, mi);
1116 else {
1117 struct conflict_info *ci = (struct conflict_info *)mi;
1118 process_entry(opt, path, ci, &dir_metadata);
1119 }
1120 }
1121
1122 if (dir_metadata.offsets.nr != 1 ||
1123 (uintptr_t)dir_metadata.offsets.items[0].util != 0) {
1124 printf("dir_metadata.offsets.nr = %d (should be 1)\n",
1125 dir_metadata.offsets.nr);
1126 printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n",
1127 (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util);
1128 fflush(stdout);
1129 BUG("dir_metadata accounting completely off; shouldn't happen");
1130 }
1131 write_tree(result_oid, &dir_metadata.versions, 0,
1132 opt->repo->hash_algo->rawsz);
1133 string_list_clear(&plist, 0);
1134 string_list_clear(&dir_metadata.versions, 0);
1135 string_list_clear(&dir_metadata.offsets, 0);
1136 }
1137
1138 /*** Function Grouping: functions related to merge_switch_to_result() ***/
1139
1140 static int checkout(struct merge_options *opt,
1141 struct tree *prev,
1142 struct tree *next)
1143 {
1144 /* Switch the index/working copy from old to new */
1145 int ret;
1146 struct tree_desc trees[2];
1147 struct unpack_trees_options unpack_opts;
1148
1149 memset(&unpack_opts, 0, sizeof(unpack_opts));
1150 unpack_opts.head_idx = -1;
1151 unpack_opts.src_index = opt->repo->index;
1152 unpack_opts.dst_index = opt->repo->index;
1153
1154 setup_unpack_trees_porcelain(&unpack_opts, "merge");
1155
1156 /*
1157 * NOTE: if this were just "git checkout" code, we would probably
1158 * read or refresh the cache and check for a conflicted index, but
1159 * builtin/merge.c or sequencer.c really needs to read the index
1160 * and check for conflicted entries before starting merging for a
1161 * good user experience (no sense waiting for merges/rebases before
1162 * erroring out), so there's no reason to duplicate that work here.
1163 */
1164
1165 /* 2-way merge to the new branch */
1166 unpack_opts.update = 1;
1167 unpack_opts.merge = 1;
1168 unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
1169 unpack_opts.verbose_update = (opt->verbosity > 2);
1170 unpack_opts.fn = twoway_merge;
1171 if (1/* FIXME: opts->overwrite_ignore*/) {
1172 unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
1173 unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
1174 setup_standard_excludes(unpack_opts.dir);
1175 }
1176 parse_tree(prev);
1177 init_tree_desc(&trees[0], prev->buffer, prev->size);
1178 parse_tree(next);
1179 init_tree_desc(&trees[1], next->buffer, next->size);
1180
1181 ret = unpack_trees(2, trees, &unpack_opts);
1182 clear_unpack_trees_porcelain(&unpack_opts);
1183 dir_clear(unpack_opts.dir);
1184 FREE_AND_NULL(unpack_opts.dir);
1185 return ret;
1186 }
1187
1188 static int record_conflicted_index_entries(struct merge_options *opt,
1189 struct index_state *index,
1190 struct strmap *paths,
1191 struct strmap *conflicted)
1192 {
1193 struct hashmap_iter iter;
1194 struct strmap_entry *e;
1195 int errs = 0;
1196 int original_cache_nr;
1197
1198 if (strmap_empty(conflicted))
1199 return 0;
1200
1201 original_cache_nr = index->cache_nr;
1202
1203 /* Put every entry from paths into plist, then sort */
1204 strmap_for_each_entry(conflicted, &iter, e) {
1205 const char *path = e->key;
1206 struct conflict_info *ci = e->value;
1207 int pos;
1208 struct cache_entry *ce;
1209 int i;
1210
1211 VERIFY_CI(ci);
1212
1213 /*
1214 * The index will already have a stage=0 entry for this path,
1215 * because we created an as-merged-as-possible version of the
1216 * file and checkout() moved the working copy and index over
1217 * to that version.
1218 *
1219 * However, previous iterations through this loop will have
1220 * added unstaged entries to the end of the cache which
1221 * ignore the standard alphabetical ordering of cache
1222 * entries and break invariants needed for index_name_pos()
1223 * to work. However, we know the entry we want is before
1224 * those appended cache entries, so do a temporary swap on
1225 * cache_nr to only look through entries of interest.
1226 */
1227 SWAP(index->cache_nr, original_cache_nr);
1228 pos = index_name_pos(index, path, strlen(path));
1229 SWAP(index->cache_nr, original_cache_nr);
1230 if (pos < 0) {
1231 if (ci->filemask != 1)
1232 BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path);
1233 cache_tree_invalidate_path(index, path);
1234 } else {
1235 ce = index->cache[pos];
1236
1237 /*
1238 * Clean paths with CE_SKIP_WORKTREE set will not be
1239 * written to the working tree by the unpack_trees()
1240 * call in checkout(). Our conflicted entries would
1241 * have appeared clean to that code since we ignored
1242 * the higher order stages. Thus, we need override
1243 * the CE_SKIP_WORKTREE bit and manually write those
1244 * files to the working disk here.
1245 *
1246 * TODO: Implement this CE_SKIP_WORKTREE fixup.
1247 */
1248
1249 /*
1250 * Mark this cache entry for removal and instead add
1251 * new stage>0 entries corresponding to the
1252 * conflicts. If there are many conflicted entries, we
1253 * want to avoid memmove'ing O(NM) entries by
1254 * inserting the new entries one at a time. So,
1255 * instead, we just add the new cache entries to the
1256 * end (ignoring normal index requirements on sort
1257 * order) and sort the index once we're all done.
1258 */
1259 ce->ce_flags |= CE_REMOVE;
1260 }
1261
1262 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
1263 struct version_info *vi;
1264 if (!(ci->filemask & (1ul << i)))
1265 continue;
1266 vi = &ci->stages[i];
1267 ce = make_cache_entry(index, vi->mode, &vi->oid,
1268 path, i+1, 0);
1269 add_index_entry(index, ce, ADD_CACHE_JUST_APPEND);
1270 }
1271 }
1272
1273 /*
1274 * Remove the unused cache entries (and invalidate the relevant
1275 * cache-trees), then sort the index entries to get the conflicted
1276 * entries we added to the end into their right locations.
1277 */
1278 remove_marked_cache_entries(index, 1);
1279 QSORT(index->cache, index->cache_nr, cmp_cache_name_compare);
1280
1281 return errs;
1282 }
1283
1284 void merge_switch_to_result(struct merge_options *opt,
1285 struct tree *head,
1286 struct merge_result *result,
1287 int update_worktree_and_index,
1288 int display_update_msgs)
1289 {
1290 assert(opt->priv == NULL);
1291 if (result->clean >= 0 && update_worktree_and_index) {
1292 struct merge_options_internal *opti = result->priv;
1293
1294 if (checkout(opt, head, result->tree)) {
1295 /* failure to function */
1296 result->clean = -1;
1297 return;
1298 }
1299
1300 if (record_conflicted_index_entries(opt, opt->repo->index,
1301 &opti->paths,
1302 &opti->conflicted)) {
1303 /* failure to function */
1304 result->clean = -1;
1305 return;
1306 }
1307 }
1308
1309 if (display_update_msgs) {
1310 struct merge_options_internal *opti = result->priv;
1311 struct hashmap_iter iter;
1312 struct strmap_entry *e;
1313 struct string_list olist = STRING_LIST_INIT_NODUP;
1314 int i;
1315
1316 /* Hack to pre-allocate olist to the desired size */
1317 ALLOC_GROW(olist.items, strmap_get_size(&opti->output),
1318 olist.alloc);
1319
1320 /* Put every entry from output into olist, then sort */
1321 strmap_for_each_entry(&opti->output, &iter, e) {
1322 string_list_append(&olist, e->key)->util = e->value;
1323 }
1324 string_list_sort(&olist);
1325
1326 /* Iterate over the items, printing them */
1327 for (i = 0; i < olist.nr; ++i) {
1328 struct strbuf *sb = olist.items[i].util;
1329
1330 printf("%s", sb->buf);
1331 }
1332 string_list_clear(&olist, 0);
1333 }
1334
1335 merge_finalize(opt, result);
1336 }
1337
1338 void merge_finalize(struct merge_options *opt,
1339 struct merge_result *result)
1340 {
1341 struct merge_options_internal *opti = result->priv;
1342
1343 assert(opt->priv == NULL);
1344
1345 clear_internal_opts(opti, 0);
1346 FREE_AND_NULL(opti);
1347 }
1348
1349 /*** Function Grouping: helper functions for merge_incore_*() ***/
1350
1351 static void merge_start(struct merge_options *opt, struct merge_result *result)
1352 {
1353 /* Sanity checks on opt */
1354 assert(opt->repo);
1355
1356 assert(opt->branch1 && opt->branch2);
1357
1358 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
1359 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
1360 assert(opt->rename_limit >= -1);
1361 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
1362 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
1363
1364 assert(opt->xdl_opts >= 0);
1365 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
1366 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
1367
1368 /*
1369 * detect_renames, verbosity, buffer_output, and obuf are ignored
1370 * fields that were used by "recursive" rather than "ort" -- but
1371 * sanity check them anyway.
1372 */
1373 assert(opt->detect_renames >= -1 &&
1374 opt->detect_renames <= DIFF_DETECT_COPY);
1375 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
1376 assert(opt->buffer_output <= 2);
1377 assert(opt->obuf.len == 0);
1378
1379 assert(opt->priv == NULL);
1380
1381 /* Default to histogram diff. Actually, just hardcode it...for now. */
1382 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
1383
1384 /* Initialization of opt->priv, our internal merge data */
1385 opt->priv = xcalloc(1, sizeof(*opt->priv));
1386
1387 /*
1388 * Although we initialize opt->priv->paths with strdup_strings=0,
1389 * that's just to avoid making yet another copy of an allocated
1390 * string. Putting the entry into paths means we are taking
1391 * ownership, so we will later free it. paths_to_free is similar.
1392 *
1393 * In contrast, conflicted just has a subset of keys from paths, so
1394 * we don't want to free those (it'd be a duplicate free).
1395 */
1396 strmap_init_with_options(&opt->priv->paths, NULL, 0);
1397 strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
1398 string_list_init(&opt->priv->paths_to_free, 0);
1399
1400 /*
1401 * keys & strbufs in output will sometimes need to outlive "paths",
1402 * so it will have a copy of relevant keys. It's probably a small
1403 * subset of the overall paths that have special output.
1404 */
1405 strmap_init(&opt->priv->output);
1406 }
1407
1408 /*** Function Grouping: merge_incore_*() and their internal variants ***/
1409
1410 /*
1411 * Originally from merge_trees_internal(); heavily adapted, though.
1412 */
1413 static void merge_ort_nonrecursive_internal(struct merge_options *opt,
1414 struct tree *merge_base,
1415 struct tree *side1,
1416 struct tree *side2,
1417 struct merge_result *result)
1418 {
1419 struct object_id working_tree_oid;
1420
1421 if (collect_merge_info(opt, merge_base, side1, side2) != 0) {
1422 /*
1423 * TRANSLATORS: The %s arguments are: 1) tree hash of a merge
1424 * base, and 2-3) the trees for the two trees we're merging.
1425 */
1426 err(opt, _("collecting merge info failed for trees %s, %s, %s"),
1427 oid_to_hex(&merge_base->object.oid),
1428 oid_to_hex(&side1->object.oid),
1429 oid_to_hex(&side2->object.oid));
1430 result->clean = -1;
1431 return;
1432 }
1433
1434 result->clean = detect_and_process_renames(opt, merge_base,
1435 side1, side2);
1436 process_entries(opt, &working_tree_oid);
1437
1438 /* Set return values */
1439 result->tree = parse_tree_indirect(&working_tree_oid);
1440 /* existence of conflicted entries implies unclean */
1441 result->clean &= strmap_empty(&opt->priv->conflicted);
1442 if (!opt->priv->call_depth) {
1443 result->priv = opt->priv;
1444 opt->priv = NULL;
1445 }
1446 }
1447
1448 void merge_incore_nonrecursive(struct merge_options *opt,
1449 struct tree *merge_base,
1450 struct tree *side1,
1451 struct tree *side2,
1452 struct merge_result *result)
1453 {
1454 assert(opt->ancestor != NULL);
1455 merge_start(opt, result);
1456 merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
1457 }
1458
1459 void merge_incore_recursive(struct merge_options *opt,
1460 struct commit_list *merge_bases,
1461 struct commit *side1,
1462 struct commit *side2,
1463 struct merge_result *result)
1464 {
1465 die("Not yet implemented");
1466 }