]>
Commit | Line | Data |
---|---|---|
17e5574b EN |
1 | /* |
2 | * "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant | |
3 | * as a drop-in replacement for the "recursive" merge strategy, allowing one | |
4 | * to replace | |
5 | * | |
6 | * git merge [-s recursive] | |
7 | * | |
8 | * with | |
9 | * | |
10 | * git merge -s ort | |
11 | * | |
12 | * Note: git's parser allows the space between '-s' and its argument to be | |
13 | * missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo", | |
14 | * "cale", "peedy", or "ins" instead of "ort"?) | |
15 | */ | |
16 | ||
17 | #include "cache.h" | |
18 | #include "merge-ort.h" | |
19 | ||
e4171b1b EN |
20 | #include "diff.h" |
21 | #include "diffcore.h" | |
5b59c3db | 22 | #include "strmap.h" |
231e2dd4 | 23 | #include "tree.h" |
c8017176 | 24 | #include "xdiff-interface.h" |
5b59c3db | 25 | |
d2bc1994 EN |
26 | /* |
27 | * We have many arrays of size 3. Whenever we have such an array, the | |
28 | * indices refer to one of the sides of the three-way merge. This is so | |
29 | * pervasive that the constants 0, 1, and 2 are used in many places in the | |
30 | * code (especially in arithmetic operations to find the other side's index | |
31 | * or to compute a relevant mask), but sometimes these enum names are used | |
32 | * to aid code clarity. | |
33 | * | |
34 | * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side" | |
35 | * referred to there is one of these three sides. | |
36 | */ | |
37 | enum merge_side { | |
38 | MERGE_BASE = 0, | |
39 | MERGE_SIDE1 = 1, | |
40 | MERGE_SIDE2 = 2 | |
41 | }; | |
42 | ||
5b59c3db EN |
43 | struct merge_options_internal { |
44 | /* | |
45 | * paths: primary data structure in all of merge ort. | |
46 | * | |
47 | * The keys of paths: | |
48 | * * are full relative paths from the toplevel of the repository | |
49 | * (e.g. "drivers/firmware/raspberrypi.c"). | |
50 | * * store all relevant paths in the repo, both directories and | |
51 | * files (e.g. drivers, drivers/firmware would also be included) | |
52 | * * these keys serve to intern all the path strings, which allows | |
53 | * us to do pointer comparison on directory names instead of | |
54 | * strcmp; we just have to be careful to use the interned strings. | |
55 | * | |
56 | * The values of paths: | |
57 | * * either a pointer to a merged_info, or a conflict_info struct | |
58 | * * merged_info contains all relevant information for a | |
59 | * non-conflicted entry. | |
60 | * * conflict_info contains a merged_info, plus any additional | |
61 | * information about a conflict such as the higher orders stages | |
62 | * involved and the names of the paths those came from (handy | |
63 | * once renames get involved). | |
64 | * * a path may start "conflicted" (i.e. point to a conflict_info) | |
65 | * and then a later step (e.g. three-way content merge) determines | |
66 | * it can be cleanly merged, at which point it'll be marked clean | |
67 | * and the algorithm will ignore any data outside the contained | |
68 | * merged_info for that entry | |
69 | * * If an entry remains conflicted, the merged_info portion of a | |
70 | * conflict_info will later be filled with whatever version of | |
71 | * the file should be placed in the working directory (e.g. an | |
72 | * as-merged-as-possible variation that contains conflict markers). | |
73 | */ | |
74 | struct strmap paths; | |
75 | ||
76 | /* | |
77 | * conflicted: a subset of keys->values from "paths" | |
78 | * | |
79 | * conflicted is basically an optimization between process_entries() | |
80 | * and record_conflicted_index_entries(); the latter could loop over | |
81 | * ALL the entries in paths AGAIN and look for the ones that are | |
82 | * still conflicted, but since process_entries() has to loop over | |
83 | * all of them, it saves the ones it couldn't resolve in this strmap | |
84 | * so that record_conflicted_index_entries() can iterate just the | |
85 | * relevant entries. | |
86 | */ | |
87 | struct strmap conflicted; | |
88 | ||
89 | /* | |
90 | * current_dir_name: temporary var used in collect_merge_info_callback() | |
91 | * | |
92 | * Used to set merged_info.directory_name; see documentation for that | |
93 | * variable and the requirements placed on that field. | |
94 | */ | |
95 | const char *current_dir_name; | |
96 | ||
97 | /* call_depth: recursion level counter for merging merge bases */ | |
98 | int call_depth; | |
99 | }; | |
100 | ||
101 | struct version_info { | |
102 | struct object_id oid; | |
103 | unsigned short mode; | |
104 | }; | |
105 | ||
106 | struct merged_info { | |
107 | /* if is_null, ignore result. otherwise result has oid & mode */ | |
108 | struct version_info result; | |
109 | unsigned is_null:1; | |
110 | ||
111 | /* | |
112 | * clean: whether the path in question is cleanly merged. | |
113 | * | |
114 | * see conflict_info.merged for more details. | |
115 | */ | |
116 | unsigned clean:1; | |
117 | ||
118 | /* | |
119 | * basename_offset: offset of basename of path. | |
120 | * | |
121 | * perf optimization to avoid recomputing offset of final '/' | |
122 | * character in pathname (0 if no '/' in pathname). | |
123 | */ | |
124 | size_t basename_offset; | |
125 | ||
126 | /* | |
127 | * directory_name: containing directory name. | |
128 | * | |
129 | * Note that we assume directory_name is constructed such that | |
130 | * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name, | |
131 | * i.e. string equality is equivalent to pointer equality. For this | |
132 | * to hold, we have to be careful setting directory_name. | |
133 | */ | |
134 | const char *directory_name; | |
135 | }; | |
136 | ||
137 | struct conflict_info { | |
138 | /* | |
139 | * merged: the version of the path that will be written to working tree | |
140 | * | |
141 | * WARNING: It is critical to check merged.clean and ensure it is 0 | |
142 | * before reading any conflict_info fields outside of merged. | |
143 | * Allocated merge_info structs will always have clean set to 1. | |
144 | * Allocated conflict_info structs will have merged.clean set to 0 | |
145 | * initially. The merged.clean field is how we know if it is safe | |
146 | * to access other parts of conflict_info besides merged; if a | |
147 | * conflict_info's merged.clean is changed to 1, the rest of the | |
148 | * algorithm is not allowed to look at anything outside of the | |
149 | * merged member anymore. | |
150 | */ | |
151 | struct merged_info merged; | |
152 | ||
153 | /* oids & modes from each of the three trees for this path */ | |
154 | struct version_info stages[3]; | |
155 | ||
156 | /* pathnames for each stage; may differ due to rename detection */ | |
157 | const char *pathnames[3]; | |
158 | ||
159 | /* Whether this path is/was involved in a directory/file conflict */ | |
160 | unsigned df_conflict:1; | |
161 | ||
162 | /* | |
163 | * For filemask and dirmask, the ith bit corresponds to whether the | |
164 | * ith entry is a file (filemask) or a directory (dirmask). Thus, | |
165 | * filemask & dirmask is always zero, and filemask | dirmask is at | |
166 | * most 7 but can be less when a path does not appear as either a | |
167 | * file or a directory on at least one side of history. | |
168 | * | |
169 | * Note that these masks are related to enum merge_side, as the ith | |
170 | * entry corresponds to side i. | |
171 | * | |
172 | * These values come from a traverse_trees() call; more info may be | |
173 | * found looking at tree-walk.h's struct traverse_info, | |
174 | * particularly the documentation above the "fn" member (note that | |
175 | * filemask = mask & ~dirmask from that documentation). | |
176 | */ | |
177 | unsigned filemask:3; | |
178 | unsigned dirmask:3; | |
179 | ||
180 | /* | |
181 | * Optimization to track which stages match, to avoid the need to | |
182 | * recompute it in multiple steps. Either 0 or at least 2 bits are | |
183 | * set; if at least 2 bits are set, their corresponding stages match. | |
184 | */ | |
185 | unsigned match_mask:3; | |
186 | }; | |
187 | ||
98bf9841 EN |
188 | /* |
189 | * For the next three macros, see warning for conflict_info.merged. | |
190 | * | |
191 | * In each of the below, mi is a struct merged_info*, and ci was defined | |
192 | * as a struct conflict_info* (but we need to verify ci isn't actually | |
193 | * pointed at a struct merged_info*). | |
194 | * | |
195 | * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise. | |
196 | * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one. | |
197 | * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first. | |
198 | */ | |
199 | #define INITIALIZE_CI(ci, mi) do { \ | |
200 | (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \ | |
201 | } while (0) | |
202 | #define VERIFY_CI(ci) assert(ci && !ci->merged.clean); | |
203 | #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \ | |
204 | (ci) = (struct conflict_info *)(mi); \ | |
205 | assert((ci) && !(mi)->clean); \ | |
206 | } while (0) | |
207 | ||
0c0d705b EN |
208 | static int err(struct merge_options *opt, const char *err, ...) |
209 | { | |
210 | va_list params; | |
211 | struct strbuf sb = STRBUF_INIT; | |
212 | ||
213 | strbuf_addstr(&sb, "error: "); | |
214 | va_start(params, err); | |
215 | strbuf_vaddf(&sb, err, params); | |
216 | va_end(params); | |
217 | ||
218 | error("%s", sb.buf); | |
219 | strbuf_release(&sb); | |
220 | ||
221 | return -1; | |
222 | } | |
223 | ||
98bf9841 EN |
224 | static void setup_path_info(struct merge_options *opt, |
225 | struct string_list_item *result, | |
226 | const char *current_dir_name, | |
227 | int current_dir_name_len, | |
228 | char *fullpath, /* we'll take over ownership */ | |
229 | struct name_entry *names, | |
230 | struct name_entry *merged_version, | |
231 | unsigned is_null, /* boolean */ | |
232 | unsigned df_conflict, /* boolean */ | |
233 | unsigned filemask, | |
234 | unsigned dirmask, | |
235 | int resolved /* boolean */) | |
236 | { | |
237 | /* result->util is void*, so mi is a convenience typed variable */ | |
238 | struct merged_info *mi; | |
239 | ||
240 | assert(!is_null || resolved); | |
241 | assert(!df_conflict || !resolved); /* df_conflict implies !resolved */ | |
242 | assert(resolved == (merged_version != NULL)); | |
243 | ||
244 | mi = xcalloc(1, resolved ? sizeof(struct merged_info) : | |
245 | sizeof(struct conflict_info)); | |
246 | mi->directory_name = current_dir_name; | |
247 | mi->basename_offset = current_dir_name_len; | |
248 | mi->clean = !!resolved; | |
249 | if (resolved) { | |
250 | mi->result.mode = merged_version->mode; | |
251 | oidcpy(&mi->result.oid, &merged_version->oid); | |
252 | mi->is_null = !!is_null; | |
253 | } else { | |
254 | int i; | |
255 | struct conflict_info *ci; | |
256 | ||
257 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
258 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
259 | ci->pathnames[i] = fullpath; | |
260 | ci->stages[i].mode = names[i].mode; | |
261 | oidcpy(&ci->stages[i].oid, &names[i].oid); | |
262 | } | |
263 | ci->filemask = filemask; | |
264 | ci->dirmask = dirmask; | |
265 | ci->df_conflict = !!df_conflict; | |
266 | if (dirmask) | |
267 | /* | |
268 | * Assume is_null for now, but if we have entries | |
269 | * under the directory then when it is complete in | |
270 | * write_completed_directory() it'll update this. | |
271 | * Also, for D/F conflicts, we have to handle the | |
272 | * directory first, then clear this bit and process | |
273 | * the file to see how it is handled -- that occurs | |
274 | * near the top of process_entry(). | |
275 | */ | |
276 | mi->is_null = 1; | |
277 | } | |
278 | strmap_put(&opt->priv->paths, fullpath, mi); | |
279 | result->string = fullpath; | |
280 | result->util = mi; | |
281 | } | |
282 | ||
d2bc1994 EN |
283 | static int collect_merge_info_callback(int n, |
284 | unsigned long mask, | |
285 | unsigned long dirmask, | |
286 | struct name_entry *names, | |
287 | struct traverse_info *info) | |
288 | { | |
289 | /* | |
290 | * n is 3. Always. | |
291 | * common ancestor (mbase) has mask 1, and stored in index 0 of names | |
292 | * head of side 1 (side1) has mask 2, and stored in index 1 of names | |
293 | * head of side 2 (side2) has mask 4, and stored in index 2 of names | |
294 | */ | |
295 | struct merge_options *opt = info->data; | |
296 | struct merge_options_internal *opti = opt->priv; | |
98bf9841 EN |
297 | struct string_list_item pi; /* Path Info */ |
298 | struct conflict_info *ci; /* typed alias to pi.util (which is void*) */ | |
d2bc1994 EN |
299 | struct name_entry *p; |
300 | size_t len; | |
301 | char *fullpath; | |
98bf9841 | 302 | const char *dirname = opti->current_dir_name; |
d2bc1994 | 303 | unsigned filemask = mask & ~dirmask; |
34e557af | 304 | unsigned match_mask = 0; /* will be updated below */ |
d2bc1994 EN |
305 | unsigned mbase_null = !(mask & 1); |
306 | unsigned side1_null = !(mask & 2); | |
307 | unsigned side2_null = !(mask & 4); | |
885f0063 EN |
308 | unsigned side1_matches_mbase = (!side1_null && !mbase_null && |
309 | names[0].mode == names[1].mode && | |
310 | oideq(&names[0].oid, &names[1].oid)); | |
311 | unsigned side2_matches_mbase = (!side2_null && !mbase_null && | |
312 | names[0].mode == names[2].mode && | |
313 | oideq(&names[0].oid, &names[2].oid)); | |
314 | unsigned sides_match = (!side1_null && !side2_null && | |
315 | names[1].mode == names[2].mode && | |
316 | oideq(&names[1].oid, &names[2].oid)); | |
d2bc1994 | 317 | |
34e557af EN |
318 | /* |
319 | * Note: When a path is a file on one side of history and a directory | |
320 | * in another, we have a directory/file conflict. In such cases, if | |
321 | * the conflict doesn't resolve from renames and deletions, then we | |
322 | * always leave directories where they are and move files out of the | |
323 | * way. Thus, while struct conflict_info has a df_conflict field to | |
324 | * track such conflicts, we ignore that field for any directories at | |
325 | * a path and only pay attention to it for files at the given path. | |
326 | * The fact that we leave directories were they are also means that | |
327 | * we do not need to worry about getting additional df_conflict | |
328 | * information propagated from parent directories down to children | |
329 | * (unlike, say traverse_trees_recursive() in unpack-trees.c, which | |
330 | * sets a newinfo.df_conflicts field specifically to propagate it). | |
331 | */ | |
332 | unsigned df_conflict = (filemask != 0) && (dirmask != 0); | |
333 | ||
d2bc1994 EN |
334 | /* n = 3 is a fundamental assumption. */ |
335 | if (n != 3) | |
336 | BUG("Called collect_merge_info_callback wrong"); | |
337 | ||
338 | /* | |
339 | * A bunch of sanity checks verifying that traverse_trees() calls | |
340 | * us the way I expect. Could just remove these at some point, | |
341 | * though maybe they are helpful to future code readers. | |
342 | */ | |
343 | assert(mbase_null == is_null_oid(&names[0].oid)); | |
344 | assert(side1_null == is_null_oid(&names[1].oid)); | |
345 | assert(side2_null == is_null_oid(&names[2].oid)); | |
346 | assert(!mbase_null || !side1_null || !side2_null); | |
347 | assert(mask > 0 && mask < 8); | |
348 | ||
34e557af EN |
349 | /* Determine match_mask */ |
350 | if (side1_matches_mbase) | |
351 | match_mask = (side2_matches_mbase ? 7 : 3); | |
352 | else if (side2_matches_mbase) | |
353 | match_mask = 5; | |
354 | else if (sides_match) | |
355 | match_mask = 6; | |
356 | ||
d2bc1994 EN |
357 | /* |
358 | * Get the name of the relevant filepath, which we'll pass to | |
359 | * setup_path_info() for tracking. | |
360 | */ | |
361 | p = names; | |
362 | while (!p->mode) | |
363 | p++; | |
364 | len = traverse_path_len(info, p->pathlen); | |
365 | ||
366 | /* +1 in both of the following lines to include the NUL byte */ | |
367 | fullpath = xmalloc(len + 1); | |
368 | make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen); | |
369 | ||
291f29ca EN |
370 | /* |
371 | * If mbase, side1, and side2 all match, we can resolve early. Even | |
372 | * if these are trees, there will be no renames or anything | |
373 | * underneath. | |
374 | */ | |
375 | if (side1_matches_mbase && side2_matches_mbase) { | |
376 | /* mbase, side1, & side2 all match; use mbase as resolution */ | |
377 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
378 | names, names+0, mbase_null, 0, | |
379 | filemask, dirmask, 1); | |
380 | return mask; | |
381 | } | |
382 | ||
d2bc1994 | 383 | /* |
98bf9841 EN |
384 | * Record information about the path so we can resolve later in |
385 | * process_entries. | |
d2bc1994 | 386 | */ |
98bf9841 EN |
387 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, |
388 | names, NULL, 0, df_conflict, filemask, dirmask, 0); | |
389 | ||
390 | ci = pi.util; | |
391 | VERIFY_CI(ci); | |
34e557af | 392 | ci->match_mask = match_mask; |
d2bc1994 EN |
393 | |
394 | /* If dirmask, recurse into subdirectories */ | |
395 | if (dirmask) { | |
396 | struct traverse_info newinfo; | |
397 | struct tree_desc t[3]; | |
398 | void *buf[3] = {NULL, NULL, NULL}; | |
399 | const char *original_dir_name; | |
400 | int i, ret; | |
401 | ||
402 | ci->match_mask &= filemask; | |
403 | newinfo = *info; | |
404 | newinfo.prev = info; | |
405 | newinfo.name = p->path; | |
406 | newinfo.namelen = p->pathlen; | |
407 | newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1); | |
34e557af EN |
408 | /* |
409 | * If this directory we are about to recurse into cared about | |
410 | * its parent directory (the current directory) having a D/F | |
411 | * conflict, then we'd propagate the masks in this way: | |
412 | * newinfo.df_conflicts |= (mask & ~dirmask); | |
413 | * But we don't worry about propagating D/F conflicts. (See | |
414 | * comment near setting of local df_conflict variable near | |
415 | * the beginning of this function). | |
416 | */ | |
d2bc1994 EN |
417 | |
418 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
885f0063 EN |
419 | if (i == 1 && side1_matches_mbase) |
420 | t[1] = t[0]; | |
421 | else if (i == 2 && side2_matches_mbase) | |
422 | t[2] = t[0]; | |
423 | else if (i == 2 && sides_match) | |
424 | t[2] = t[1]; | |
425 | else { | |
426 | const struct object_id *oid = NULL; | |
427 | if (dirmask & 1) | |
428 | oid = &names[i].oid; | |
429 | buf[i] = fill_tree_descriptor(opt->repo, | |
430 | t + i, oid); | |
431 | } | |
d2bc1994 EN |
432 | dirmask >>= 1; |
433 | } | |
434 | ||
435 | original_dir_name = opti->current_dir_name; | |
98bf9841 | 436 | opti->current_dir_name = pi.string; |
d2bc1994 EN |
437 | ret = traverse_trees(NULL, 3, t, &newinfo); |
438 | opti->current_dir_name = original_dir_name; | |
439 | ||
440 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) | |
441 | free(buf[i]); | |
442 | ||
443 | if (ret < 0) | |
444 | return -1; | |
445 | } | |
446 | ||
447 | return mask; | |
448 | } | |
449 | ||
231e2dd4 EN |
450 | static int collect_merge_info(struct merge_options *opt, |
451 | struct tree *merge_base, | |
452 | struct tree *side1, | |
453 | struct tree *side2) | |
454 | { | |
d2bc1994 EN |
455 | int ret; |
456 | struct tree_desc t[3]; | |
457 | struct traverse_info info; | |
458 | const char *toplevel_dir_placeholder = ""; | |
459 | ||
460 | opt->priv->current_dir_name = toplevel_dir_placeholder; | |
461 | setup_traverse_info(&info, toplevel_dir_placeholder); | |
462 | info.fn = collect_merge_info_callback; | |
463 | info.data = opt; | |
464 | info.show_all_errors = 1; | |
465 | ||
466 | parse_tree(merge_base); | |
467 | parse_tree(side1); | |
468 | parse_tree(side2); | |
469 | init_tree_desc(t + 0, merge_base->buffer, merge_base->size); | |
470 | init_tree_desc(t + 1, side1->buffer, side1->size); | |
471 | init_tree_desc(t + 2, side2->buffer, side2->size); | |
472 | ||
473 | ret = traverse_trees(NULL, 3, t, &info); | |
474 | ||
475 | return ret; | |
231e2dd4 EN |
476 | } |
477 | ||
478 | static int detect_and_process_renames(struct merge_options *opt, | |
479 | struct tree *merge_base, | |
480 | struct tree *side1, | |
481 | struct tree *side2) | |
482 | { | |
483 | int clean = 1; | |
484 | ||
485 | /* | |
486 | * Rename detection works by detecting file similarity. Here we use | |
487 | * a really easy-to-implement scheme: files are similar IFF they have | |
488 | * the same filename. Therefore, by this scheme, there are no renames. | |
489 | * | |
490 | * TODO: Actually implement a real rename detection scheme. | |
491 | */ | |
492 | return clean; | |
493 | } | |
494 | ||
8adffaa8 EN |
495 | static int string_list_df_name_compare(const char *one, const char *two) |
496 | { | |
497 | int onelen = strlen(one); | |
498 | int twolen = strlen(two); | |
499 | /* | |
500 | * Here we only care that entries for D/F conflicts are | |
501 | * adjacent, in particular with the file of the D/F conflict | |
502 | * appearing before files below the corresponding directory. | |
503 | * The order of the rest of the list is irrelevant for us. | |
504 | * | |
505 | * To achieve this, we sort with df_name_compare and provide | |
506 | * the mode S_IFDIR so that D/F conflicts will sort correctly. | |
507 | * We use the mode S_IFDIR for everything else for simplicity, | |
508 | * since in other cases any changes in their order due to | |
509 | * sorting cause no problems for us. | |
510 | */ | |
511 | int cmp = df_name_compare(one, onelen, S_IFDIR, | |
512 | two, twolen, S_IFDIR); | |
513 | /* | |
514 | * Now that 'foo' and 'foo/bar' compare equal, we have to make sure | |
515 | * that 'foo' comes before 'foo/bar'. | |
516 | */ | |
517 | if (cmp) | |
518 | return cmp; | |
519 | return onelen - twolen; | |
520 | } | |
521 | ||
6a02dd90 EN |
522 | /* Per entry merge function */ |
523 | static void process_entry(struct merge_options *opt, | |
524 | const char *path, | |
525 | struct conflict_info *ci) | |
526 | { | |
527 | VERIFY_CI(ci); | |
528 | assert(ci->filemask >= 0 && ci->filemask <= 7); | |
529 | /* ci->match_mask == 7 was handled in collect_merge_info_callback() */ | |
530 | assert(ci->match_mask == 0 || ci->match_mask == 3 || | |
531 | ci->match_mask == 5 || ci->match_mask == 6); | |
532 | ||
533 | if (ci->df_conflict) { | |
534 | die("Not yet implemented."); | |
535 | } | |
536 | ||
537 | /* | |
538 | * NOTE: Below there is a long switch-like if-elseif-elseif... block | |
539 | * which the code goes through even for the df_conflict cases | |
540 | * above. Well, it will once we don't die-not-implemented above. | |
541 | */ | |
542 | if (ci->match_mask) { | |
543 | ci->merged.clean = 1; | |
544 | if (ci->match_mask == 6) { | |
545 | /* stages[1] == stages[2] */ | |
546 | ci->merged.result.mode = ci->stages[1].mode; | |
547 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
548 | } else { | |
549 | /* determine the mask of the side that didn't match */ | |
550 | unsigned int othermask = 7 & ~ci->match_mask; | |
551 | int side = (othermask == 4) ? 2 : 1; | |
552 | ||
553 | ci->merged.result.mode = ci->stages[side].mode; | |
554 | ci->merged.is_null = !ci->merged.result.mode; | |
555 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
556 | ||
557 | assert(othermask == 2 || othermask == 4); | |
558 | assert(ci->merged.is_null == | |
559 | (ci->filemask == ci->match_mask)); | |
560 | } | |
561 | } else if (ci->filemask >= 6 && | |
562 | (S_IFMT & ci->stages[1].mode) != | |
563 | (S_IFMT & ci->stages[2].mode)) { | |
564 | /* | |
565 | * Two different items from (file/submodule/symlink) | |
566 | */ | |
567 | die("Not yet implemented."); | |
568 | } else if (ci->filemask >= 6) { | |
569 | /* | |
570 | * TODO: Needs a two-way or three-way content merge, but we're | |
571 | * just being lazy and copying the version from HEAD and | |
572 | * leaving it as conflicted. | |
573 | */ | |
574 | ci->merged.clean = 0; | |
575 | ci->merged.result.mode = ci->stages[1].mode; | |
576 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
577 | } else if (ci->filemask == 3 || ci->filemask == 5) { | |
578 | /* Modify/delete */ | |
579 | die("Not yet implemented."); | |
580 | } else if (ci->filemask == 2 || ci->filemask == 4) { | |
581 | /* Added on one side */ | |
582 | int side = (ci->filemask == 4) ? 2 : 1; | |
583 | ci->merged.result.mode = ci->stages[side].mode; | |
584 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
585 | ci->merged.clean = !ci->df_conflict; | |
586 | } else if (ci->filemask == 1) { | |
587 | /* Deleted on both sides */ | |
588 | ci->merged.is_null = 1; | |
589 | ci->merged.result.mode = 0; | |
590 | oidcpy(&ci->merged.result.oid, &null_oid); | |
591 | ci->merged.clean = 1; | |
592 | } | |
593 | ||
594 | /* | |
595 | * If still conflicted, record it separately. This allows us to later | |
596 | * iterate over just conflicted entries when updating the index instead | |
597 | * of iterating over all entries. | |
598 | */ | |
599 | if (!ci->merged.clean) | |
600 | strmap_put(&opt->priv->conflicted, path, ci); | |
601 | } | |
602 | ||
231e2dd4 EN |
603 | static void process_entries(struct merge_options *opt, |
604 | struct object_id *result_oid) | |
605 | { | |
6a02dd90 EN |
606 | struct hashmap_iter iter; |
607 | struct strmap_entry *e; | |
8adffaa8 EN |
608 | struct string_list plist = STRING_LIST_INIT_NODUP; |
609 | struct string_list_item *entry; | |
6a02dd90 EN |
610 | |
611 | if (strmap_empty(&opt->priv->paths)) { | |
612 | oidcpy(result_oid, opt->repo->hash_algo->empty_tree); | |
613 | return; | |
614 | } | |
615 | ||
8adffaa8 EN |
616 | /* Hack to pre-allocate plist to the desired size */ |
617 | ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc); | |
618 | ||
619 | /* Put every entry from paths into plist, then sort */ | |
6a02dd90 | 620 | strmap_for_each_entry(&opt->priv->paths, &iter, e) { |
8adffaa8 EN |
621 | string_list_append(&plist, e->key)->util = e->value; |
622 | } | |
623 | plist.cmp = string_list_df_name_compare; | |
624 | string_list_sort(&plist); | |
625 | ||
626 | /* | |
627 | * Iterate over the items in reverse order, so we can handle paths | |
628 | * below a directory before needing to handle the directory itself. | |
629 | */ | |
630 | for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) { | |
631 | char *path = entry->string; | |
6a02dd90 EN |
632 | /* |
633 | * NOTE: mi may actually be a pointer to a conflict_info, but | |
634 | * we have to check mi->clean first to see if it's safe to | |
635 | * reassign to such a pointer type. | |
636 | */ | |
8adffaa8 | 637 | struct merged_info *mi = entry->util; |
6a02dd90 | 638 | |
8adffaa8 EN |
639 | if (!mi->clean) { |
640 | struct conflict_info *ci = (struct conflict_info *)mi; | |
641 | process_entry(opt, path, ci); | |
642 | } | |
6a02dd90 EN |
643 | } |
644 | ||
8adffaa8 | 645 | string_list_clear(&plist, 0); |
6a02dd90 | 646 | die("Tree creation not yet implemented"); |
231e2dd4 EN |
647 | } |
648 | ||
17e5574b EN |
649 | void merge_switch_to_result(struct merge_options *opt, |
650 | struct tree *head, | |
651 | struct merge_result *result, | |
652 | int update_worktree_and_index, | |
653 | int display_update_msgs) | |
654 | { | |
655 | die("Not yet implemented"); | |
656 | merge_finalize(opt, result); | |
657 | } | |
658 | ||
659 | void merge_finalize(struct merge_options *opt, | |
660 | struct merge_result *result) | |
661 | { | |
662 | die("Not yet implemented"); | |
663 | } | |
664 | ||
231e2dd4 EN |
665 | static void merge_start(struct merge_options *opt, struct merge_result *result) |
666 | { | |
e4171b1b EN |
667 | /* Sanity checks on opt */ |
668 | assert(opt->repo); | |
669 | ||
670 | assert(opt->branch1 && opt->branch2); | |
671 | ||
672 | assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE && | |
673 | opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE); | |
674 | assert(opt->rename_limit >= -1); | |
675 | assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE); | |
676 | assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1); | |
677 | ||
678 | assert(opt->xdl_opts >= 0); | |
679 | assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL && | |
680 | opt->recursive_variant <= MERGE_VARIANT_THEIRS); | |
681 | ||
682 | /* | |
683 | * detect_renames, verbosity, buffer_output, and obuf are ignored | |
684 | * fields that were used by "recursive" rather than "ort" -- but | |
685 | * sanity check them anyway. | |
686 | */ | |
687 | assert(opt->detect_renames >= -1 && | |
688 | opt->detect_renames <= DIFF_DETECT_COPY); | |
689 | assert(opt->verbosity >= 0 && opt->verbosity <= 5); | |
690 | assert(opt->buffer_output <= 2); | |
691 | assert(opt->obuf.len == 0); | |
692 | ||
693 | assert(opt->priv == NULL); | |
694 | ||
c8017176 EN |
695 | /* Default to histogram diff. Actually, just hardcode it...for now. */ |
696 | opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF); | |
697 | ||
e4171b1b EN |
698 | /* Initialization of opt->priv, our internal merge data */ |
699 | opt->priv = xcalloc(1, sizeof(*opt->priv)); | |
700 | ||
701 | /* | |
702 | * Although we initialize opt->priv->paths with strdup_strings=0, | |
703 | * that's just to avoid making yet another copy of an allocated | |
704 | * string. Putting the entry into paths means we are taking | |
705 | * ownership, so we will later free it. | |
706 | * | |
707 | * In contrast, conflicted just has a subset of keys from paths, so | |
708 | * we don't want to free those (it'd be a duplicate free). | |
709 | */ | |
710 | strmap_init_with_options(&opt->priv->paths, NULL, 0); | |
711 | strmap_init_with_options(&opt->priv->conflicted, NULL, 0); | |
231e2dd4 EN |
712 | } |
713 | ||
714 | /* | |
715 | * Originally from merge_trees_internal(); heavily adapted, though. | |
716 | */ | |
717 | static void merge_ort_nonrecursive_internal(struct merge_options *opt, | |
718 | struct tree *merge_base, | |
719 | struct tree *side1, | |
720 | struct tree *side2, | |
721 | struct merge_result *result) | |
722 | { | |
723 | struct object_id working_tree_oid; | |
724 | ||
0c0d705b EN |
725 | if (collect_merge_info(opt, merge_base, side1, side2) != 0) { |
726 | /* | |
727 | * TRANSLATORS: The %s arguments are: 1) tree hash of a merge | |
728 | * base, and 2-3) the trees for the two trees we're merging. | |
729 | */ | |
730 | err(opt, _("collecting merge info failed for trees %s, %s, %s"), | |
731 | oid_to_hex(&merge_base->object.oid), | |
732 | oid_to_hex(&side1->object.oid), | |
733 | oid_to_hex(&side2->object.oid)); | |
734 | result->clean = -1; | |
735 | return; | |
736 | } | |
737 | ||
231e2dd4 EN |
738 | result->clean = detect_and_process_renames(opt, merge_base, |
739 | side1, side2); | |
740 | process_entries(opt, &working_tree_oid); | |
741 | ||
742 | /* Set return values */ | |
743 | result->tree = parse_tree_indirect(&working_tree_oid); | |
744 | /* existence of conflicted entries implies unclean */ | |
745 | result->clean &= strmap_empty(&opt->priv->conflicted); | |
746 | if (!opt->priv->call_depth) { | |
747 | result->priv = opt->priv; | |
748 | opt->priv = NULL; | |
749 | } | |
750 | } | |
751 | ||
17e5574b EN |
752 | void merge_incore_nonrecursive(struct merge_options *opt, |
753 | struct tree *merge_base, | |
754 | struct tree *side1, | |
755 | struct tree *side2, | |
756 | struct merge_result *result) | |
757 | { | |
231e2dd4 EN |
758 | assert(opt->ancestor != NULL); |
759 | merge_start(opt, result); | |
760 | merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result); | |
17e5574b EN |
761 | } |
762 | ||
763 | void merge_incore_recursive(struct merge_options *opt, | |
764 | struct commit_list *merge_bases, | |
765 | struct commit *side1, | |
766 | struct commit *side2, | |
767 | struct merge_result *result) | |
768 | { | |
769 | die("Not yet implemented"); | |
770 | } |