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