]>
Commit | Line | Data |
---|---|---|
857b933e JS |
1 | /* |
2 | * LibXDiff by Davide Libenzi ( File Differential Library ) | |
3 | * Copyright (C) 2003-2006 Davide Libenzi, Johannes E. Schindelin | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | * Davide Libenzi <davidel@xmailserver.org> | |
20 | * | |
21 | */ | |
22 | ||
23 | #include "xinclude.h" | |
24 | ||
25 | typedef struct s_xdmerge { | |
26 | struct s_xdmerge *next; | |
27 | /* | |
28 | * 0 = conflict, | |
29 | * 1 = no conflict, take first, | |
30 | * 2 = no conflict, take second. | |
31 | */ | |
32 | int mode; | |
e0af48e4 JH |
33 | /* |
34 | * These point at the respective postimages. E.g. <i1,chg1> is | |
35 | * how side #1 wants to change the common ancestor; if there is no | |
36 | * overlap, lines before i1 in the postimage of side #1 appear | |
37 | * in the merge result as a region touched by neither side. | |
38 | */ | |
857b933e JS |
39 | long i1, i2; |
40 | long chg1, chg2; | |
e0af48e4 JH |
41 | /* |
42 | * These point at the preimage; of course there is just one | |
43 | * preimage, that is from the shared common ancestor. | |
44 | */ | |
45 | long i0; | |
46 | long chg0; | |
857b933e JS |
47 | } xdmerge_t; |
48 | ||
49 | static int xdl_append_merge(xdmerge_t **merge, int mode, | |
e0af48e4 JH |
50 | long i0, long chg0, |
51 | long i1, long chg1, | |
52 | long i2, long chg2) | |
857b933e JS |
53 | { |
54 | xdmerge_t *m = *merge; | |
98e6da8a JS |
55 | if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) { |
56 | if (mode != m->mode) | |
57 | m->mode = 0; | |
e0af48e4 | 58 | m->chg0 = i0 + chg0 - m->i0; |
857b933e JS |
59 | m->chg1 = i1 + chg1 - m->i1; |
60 | m->chg2 = i2 + chg2 - m->i2; | |
61 | } else { | |
62 | m = xdl_malloc(sizeof(xdmerge_t)); | |
63 | if (!m) | |
64 | return -1; | |
65 | m->next = NULL; | |
66 | m->mode = mode; | |
e0af48e4 JH |
67 | m->i0 = i0; |
68 | m->chg0 = chg0; | |
857b933e JS |
69 | m->i1 = i1; |
70 | m->chg1 = chg1; | |
71 | m->i2 = i2; | |
72 | m->chg2 = chg2; | |
73 | if (*merge) | |
74 | (*merge)->next = m; | |
75 | *merge = m; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
80 | static int xdl_cleanup_merge(xdmerge_t *c) | |
81 | { | |
82 | int count = 0; | |
83 | xdmerge_t *next_c; | |
84 | ||
85 | /* were there conflicts? */ | |
86 | for (; c; c = next_c) { | |
87 | if (c->mode == 0) | |
88 | count++; | |
89 | next_c = c->next; | |
90 | free(c); | |
91 | } | |
92 | return count; | |
93 | } | |
94 | ||
95 | static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2, | |
96 | int line_count, long flags) | |
97 | { | |
98 | int i; | |
99 | xrecord_t **rec1 = xe1->xdf2.recs + i1; | |
100 | xrecord_t **rec2 = xe2->xdf2.recs + i2; | |
101 | ||
102 | for (i = 0; i < line_count; i++) { | |
103 | int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, | |
104 | rec2[i]->ptr, rec2[i]->size, flags); | |
105 | if (!result) | |
106 | return -1; | |
107 | } | |
108 | return 0; | |
109 | } | |
110 | ||
e0af48e4 | 111 | static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest) |
857b933e | 112 | { |
e0af48e4 | 113 | xrecord_t **recs; |
857b933e JS |
114 | int size = 0; |
115 | ||
e0af48e4 JH |
116 | recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; |
117 | ||
857b933e JS |
118 | if (count < 1) |
119 | return 0; | |
120 | ||
121 | for (i = 0; i < count; size += recs[i++]->size) | |
122 | if (dest) | |
123 | memcpy(dest + size, recs[i]->ptr, recs[i]->size); | |
124 | if (add_nl) { | |
125 | i = recs[count - 1]->size; | |
126 | if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { | |
127 | if (dest) | |
128 | dest[size] = '\n'; | |
129 | size++; | |
130 | } | |
131 | } | |
132 | return size; | |
133 | } | |
134 | ||
e0af48e4 JH |
135 | static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) |
136 | { | |
137 | return xdl_recs_copy_0(0, xe, i, count, add_nl, dest); | |
138 | } | |
139 | ||
140 | static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) | |
141 | { | |
142 | return xdl_recs_copy_0(1, xe, i, count, add_nl, dest); | |
143 | } | |
144 | ||
f2b25dd8 JH |
145 | static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, |
146 | xdfenv_t *xe2, const char *name2, | |
e0af48e4 | 147 | int size, int i, int style, |
9914cf46 | 148 | xdmerge_t *m, char *dest, int marker_size) |
857b933e | 149 | { |
857b933e JS |
150 | int marker1_size = (name1 ? strlen(name1) + 1 : 0); |
151 | int marker2_size = (name2 ? strlen(name2) + 1 : 0); | |
f2b25dd8 JH |
152 | int j; |
153 | ||
9914cf46 JH |
154 | if (marker_size <= 0) |
155 | marker_size = DEFAULT_CONFLICT_MARKER_SIZE; | |
156 | ||
f2b25dd8 JH |
157 | /* Before conflicting part */ |
158 | size += xdl_recs_copy(xe1, i, m->i1 - i, 0, | |
159 | dest ? dest + size : NULL); | |
160 | ||
161 | if (!dest) { | |
162 | size += marker_size + 1 + marker1_size; | |
163 | } else { | |
164 | for (j = 0; j < marker_size; j++) | |
165 | dest[size++] = '<'; | |
166 | if (marker1_size) { | |
167 | dest[size] = ' '; | |
168 | memcpy(dest + size + 1, name1, marker1_size - 1); | |
169 | size += marker1_size; | |
170 | } | |
171 | dest[size++] = '\n'; | |
172 | } | |
173 | ||
174 | /* Postimage from side #1 */ | |
175 | size += xdl_recs_copy(xe1, m->i1, m->chg1, 1, | |
176 | dest ? dest + size : NULL); | |
e0af48e4 JH |
177 | |
178 | if (style == XDL_MERGE_DIFF3) { | |
179 | /* Shared preimage */ | |
180 | if (!dest) { | |
181 | size += marker_size + 1; | |
182 | } else { | |
183 | for (j = 0; j < marker_size; j++) | |
184 | dest[size++] = '|'; | |
185 | dest[size++] = '\n'; | |
186 | } | |
187 | size += xdl_orig_copy(xe1, m->i0, m->chg0, 1, | |
188 | dest ? dest + size : NULL); | |
189 | } | |
190 | ||
f2b25dd8 JH |
191 | if (!dest) { |
192 | size += marker_size + 1; | |
193 | } else { | |
194 | for (j = 0; j < marker_size; j++) | |
195 | dest[size++] = '='; | |
196 | dest[size++] = '\n'; | |
197 | } | |
198 | ||
199 | /* Postimage from side #2 */ | |
200 | size += xdl_recs_copy(xe2, m->i2, m->chg2, 1, | |
201 | dest ? dest + size : NULL); | |
202 | if (!dest) { | |
203 | size += marker_size + 1 + marker2_size; | |
204 | } else { | |
205 | for (j = 0; j < marker_size; j++) | |
206 | dest[size++] = '>'; | |
207 | if (marker2_size) { | |
208 | dest[size] = ' '; | |
209 | memcpy(dest + size + 1, name2, marker2_size - 1); | |
210 | size += marker2_size; | |
211 | } | |
212 | dest[size++] = '\n'; | |
213 | } | |
214 | return size; | |
215 | } | |
216 | ||
217 | static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, | |
e0af48e4 | 218 | xdfenv_t *xe2, const char *name2, |
73eb40ee | 219 | int favor, |
9914cf46 JH |
220 | xdmerge_t *m, char *dest, int style, |
221 | int marker_size) | |
f2b25dd8 JH |
222 | { |
223 | int size, i; | |
224 | ||
225 | for (size = i = 0; m; m = m->next) { | |
73eb40ee JH |
226 | if (favor && !m->mode) |
227 | m->mode = favor; | |
228 | ||
f2b25dd8 JH |
229 | if (m->mode == 0) |
230 | size = fill_conflict_hunk(xe1, name1, xe2, name2, | |
9914cf46 JH |
231 | size, i, style, m, dest, |
232 | marker_size); | |
f2b25dd8 JH |
233 | else if (m->mode == 1) |
234 | size += xdl_recs_copy(xe1, i, m->i1 + m->chg1 - i, 0, | |
235 | dest ? dest + size : NULL); | |
857b933e | 236 | else if (m->mode == 2) |
f2b25dd8 JH |
237 | size += xdl_recs_copy(xe2, m->i2 - m->i1 + i, |
238 | m->i1 + m->chg2 - i, 0, | |
239 | dest ? dest + size : NULL); | |
22b6abcd JS |
240 | else |
241 | continue; | |
f2b25dd8 | 242 | i = m->i1 + m->chg1; |
857b933e | 243 | } |
f2b25dd8 JH |
244 | size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, |
245 | dest ? dest + size : NULL); | |
857b933e JS |
246 | return size; |
247 | } | |
248 | ||
249 | /* | |
250 | * Sometimes, changes are not quite identical, but differ in only a few | |
251 | * lines. Try hard to show only these few lines as conflicting. | |
252 | */ | |
253 | static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, | |
254 | xpparam_t const *xpp) | |
255 | { | |
256 | for (; m; m = m->next) { | |
257 | mmfile_t t1, t2; | |
258 | xdfenv_t xe; | |
259 | xdchange_t *xscr, *x; | |
260 | int i1 = m->i1, i2 = m->i2; | |
261 | ||
262 | /* let's handle just the conflicts */ | |
263 | if (m->mode) | |
264 | continue; | |
265 | ||
5d6b151f JS |
266 | /* no sense refining a conflict when one side is empty */ |
267 | if (m->chg1 == 0 || m->chg2 == 0) | |
268 | continue; | |
269 | ||
857b933e JS |
270 | /* |
271 | * This probably does not work outside git, since | |
272 | * we have a very simple mmfile structure. | |
273 | */ | |
274 | t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; | |
875b8ce4 JS |
275 | t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr |
276 | + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; | |
277 | t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; | |
278 | t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr | |
279 | + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; | |
857b933e JS |
280 | if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) |
281 | return -1; | |
282 | if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || | |
283 | xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || | |
284 | xdl_build_script(&xe, &xscr) < 0) { | |
285 | xdl_free_env(&xe); | |
286 | return -1; | |
287 | } | |
288 | if (!xscr) { | |
22b6abcd | 289 | /* If this happens, the changes are identical. */ |
857b933e | 290 | xdl_free_env(&xe); |
22b6abcd JS |
291 | m->mode = 4; |
292 | continue; | |
857b933e JS |
293 | } |
294 | x = xscr; | |
295 | m->i1 = xscr->i1 + i1; | |
296 | m->chg1 = xscr->chg1; | |
297 | m->i2 = xscr->i2 + i2; | |
298 | m->chg2 = xscr->chg2; | |
299 | while (xscr->next) { | |
300 | xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); | |
301 | if (!m2) { | |
302 | xdl_free_env(&xe); | |
303 | xdl_free_script(x); | |
304 | return -1; | |
305 | } | |
306 | xscr = xscr->next; | |
307 | m2->next = m->next; | |
308 | m->next = m2; | |
309 | m = m2; | |
310 | m->mode = 0; | |
311 | m->i1 = xscr->i1 + i1; | |
312 | m->chg1 = xscr->chg1; | |
313 | m->i2 = xscr->i2 + i2; | |
314 | m->chg2 = xscr->chg2; | |
315 | } | |
316 | xdl_free_env(&xe); | |
317 | xdl_free_script(x); | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
ee95ec5d JS |
322 | static int line_contains_alnum(const char *ptr, long size) |
323 | { | |
324 | while (size--) | |
325 | if (isalnum(*(ptr++))) | |
326 | return 1; | |
327 | return 0; | |
328 | } | |
329 | ||
330 | static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) | |
331 | { | |
332 | for (; chg; chg--, i++) | |
333 | if (line_contains_alnum(xe->xdf2.recs[i]->ptr, | |
334 | xe->xdf2.recs[i]->size)) | |
335 | return 1; | |
336 | return 0; | |
337 | } | |
338 | ||
f407f14d JS |
339 | /* |
340 | * This function merges m and m->next, marking everything between those hunks | |
341 | * as conflicting, too. | |
342 | */ | |
343 | static void xdl_merge_two_conflicts(xdmerge_t *m) | |
344 | { | |
345 | xdmerge_t *next_m = m->next; | |
346 | m->chg1 = next_m->i1 + next_m->chg1 - m->i1; | |
347 | m->chg2 = next_m->i2 + next_m->chg2 - m->i2; | |
348 | m->next = next_m->next; | |
349 | free(next_m); | |
350 | } | |
351 | ||
352 | /* | |
353 | * If there are less than 3 non-conflicting lines between conflicts, | |
354 | * it appears simpler -- because it takes up less (or as many) lines -- | |
355 | * if the lines are moved into the conflicts. | |
356 | */ | |
ee95ec5d JS |
357 | static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, |
358 | int simplify_if_no_alnum) | |
f407f14d JS |
359 | { |
360 | int result = 0; | |
361 | ||
362 | if (!m) | |
363 | return result; | |
364 | for (;;) { | |
365 | xdmerge_t *next_m = m->next; | |
366 | int begin, end; | |
367 | ||
368 | if (!next_m) | |
369 | return result; | |
370 | ||
371 | begin = m->i1 + m->chg1; | |
372 | end = next_m->i1; | |
373 | ||
ee95ec5d JS |
374 | if (m->mode != 0 || next_m->mode != 0 || |
375 | (end - begin > 3 && | |
376 | (!simplify_if_no_alnum || | |
377 | lines_contain_alnum(xe1, begin, end - begin)))) { | |
f407f14d | 378 | m = next_m; |
ee95ec5d | 379 | } else { |
f407f14d JS |
380 | result++; |
381 | xdl_merge_two_conflicts(m); | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
857b933e JS |
386 | /* |
387 | * level == 0: mark all overlapping changes as conflict | |
388 | * level == 1: mark overlapping changes as conflict only if not identical | |
389 | * level == 2: analyze non-identical changes for minimal conflict set | |
ee95ec5d JS |
390 | * level == 3: analyze non-identical changes for minimal conflict set, but |
391 | * treat hunks not containing any letter or number as conflicting | |
857b933e JS |
392 | * |
393 | * returns < 0 on error, == 0 for no conflicts, else number of conflicts | |
394 | */ | |
395 | static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1, | |
396 | xdfenv_t *xe2, xdchange_t *xscr2, const char *name2, | |
9914cf46 | 397 | int flags, xmparam_t const *xmp, mmbuffer_t *result) { |
857b933e | 398 | xdmerge_t *changes, *c; |
9914cf46 | 399 | xpparam_t const *xpp = &xmp->xpp; |
e0af48e4 JH |
400 | int i0, i1, i2, chg0, chg1, chg2; |
401 | int level = flags & XDL_MERGE_LEVEL_MASK; | |
402 | int style = flags & XDL_MERGE_STYLE_MASK; | |
73eb40ee | 403 | int favor = XDL_MERGE_FAVOR(flags); |
857b933e | 404 | |
83133740 JH |
405 | if (style == XDL_MERGE_DIFF3) { |
406 | /* | |
407 | * "diff3 -m" output does not make sense for anything | |
408 | * more aggressive than XDL_MERGE_EAGER. | |
409 | */ | |
410 | if (XDL_MERGE_EAGER < level) | |
411 | level = XDL_MERGE_EAGER; | |
412 | } | |
413 | ||
857b933e JS |
414 | c = changes = NULL; |
415 | ||
416 | while (xscr1 && xscr2) { | |
417 | if (!changes) | |
418 | changes = c; | |
419 | if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { | |
e0af48e4 | 420 | i0 = xscr1->i1; |
857b933e JS |
421 | i1 = xscr1->i2; |
422 | i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; | |
e0af48e4 | 423 | chg0 = xscr1->chg1; |
857b933e JS |
424 | chg1 = xscr1->chg2; |
425 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
426 | if (xdl_append_merge(&c, 1, |
427 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
428 | xdl_cleanup_merge(changes); |
429 | return -1; | |
430 | } | |
431 | xscr1 = xscr1->next; | |
432 | continue; | |
433 | } | |
434 | if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { | |
e0af48e4 | 435 | i0 = xscr2->i1; |
857b933e JS |
436 | i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; |
437 | i2 = xscr2->i2; | |
e0af48e4 | 438 | chg0 = xscr2->chg1; |
857b933e JS |
439 | chg1 = xscr2->chg1; |
440 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
441 | if (xdl_append_merge(&c, 2, |
442 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
443 | xdl_cleanup_merge(changes); |
444 | return -1; | |
445 | } | |
446 | xscr2 = xscr2->next; | |
447 | continue; | |
448 | } | |
838338cd | 449 | if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || |
857b933e JS |
450 | xscr1->chg1 != xscr2->chg1 || |
451 | xscr1->chg2 != xscr2->chg2 || | |
452 | xdl_merge_cmp_lines(xe1, xscr1->i2, | |
453 | xe2, xscr2->i2, | |
454 | xscr1->chg2, xpp->flags)) { | |
455 | /* conflict */ | |
456 | int off = xscr1->i1 - xscr2->i1; | |
457 | int ffo = off + xscr1->chg1 - xscr2->chg1; | |
458 | ||
e0af48e4 | 459 | i0 = xscr1->i1; |
857b933e JS |
460 | i1 = xscr1->i2; |
461 | i2 = xscr2->i2; | |
e0af48e4 JH |
462 | if (off > 0) { |
463 | i0 -= off; | |
857b933e | 464 | i1 -= off; |
e0af48e4 | 465 | } |
857b933e JS |
466 | else |
467 | i2 += off; | |
e0af48e4 | 468 | chg0 = xscr1->i1 + xscr1->chg1 - i0; |
857b933e JS |
469 | chg1 = xscr1->i2 + xscr1->chg2 - i1; |
470 | chg2 = xscr2->i2 + xscr2->chg2 - i2; | |
838338cd | 471 | if (ffo < 0) { |
e0af48e4 | 472 | chg0 -= ffo; |
857b933e | 473 | chg1 -= ffo; |
838338cd JH |
474 | } else |
475 | chg2 += ffo; | |
e0af48e4 JH |
476 | if (xdl_append_merge(&c, 0, |
477 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
478 | xdl_cleanup_merge(changes); |
479 | return -1; | |
480 | } | |
481 | } | |
482 | ||
483 | i1 = xscr1->i1 + xscr1->chg1; | |
484 | i2 = xscr2->i1 + xscr2->chg1; | |
485 | ||
98e6da8a | 486 | if (i1 >= i2) |
710daa83 | 487 | xscr2 = xscr2->next; |
98e6da8a | 488 | if (i2 >= i1) |
857b933e | 489 | xscr1 = xscr1->next; |
857b933e JS |
490 | } |
491 | while (xscr1) { | |
492 | if (!changes) | |
493 | changes = c; | |
e0af48e4 | 494 | i0 = xscr1->i1; |
857b933e JS |
495 | i1 = xscr1->i2; |
496 | i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; | |
e0af48e4 | 497 | chg0 = xscr1->chg1; |
857b933e JS |
498 | chg1 = xscr1->chg2; |
499 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
500 | if (xdl_append_merge(&c, 1, |
501 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
502 | xdl_cleanup_merge(changes); |
503 | return -1; | |
504 | } | |
505 | xscr1 = xscr1->next; | |
506 | } | |
507 | while (xscr2) { | |
508 | if (!changes) | |
509 | changes = c; | |
e0af48e4 | 510 | i0 = xscr2->i1; |
857b933e JS |
511 | i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; |
512 | i2 = xscr2->i2; | |
e0af48e4 | 513 | chg0 = xscr2->chg1; |
857b933e JS |
514 | chg1 = xscr2->chg1; |
515 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
516 | if (xdl_append_merge(&c, 2, |
517 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
518 | xdl_cleanup_merge(changes); |
519 | return -1; | |
520 | } | |
521 | xscr2 = xscr2->next; | |
522 | } | |
523 | if (!changes) | |
524 | changes = c; | |
525 | /* refine conflicts */ | |
838338cd | 526 | if (XDL_MERGE_ZEALOUS <= level && |
f407f14d | 527 | (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || |
838338cd JH |
528 | xdl_simplify_non_conflicts(xe1, changes, |
529 | XDL_MERGE_ZEALOUS < level) < 0)) { | |
857b933e JS |
530 | xdl_cleanup_merge(changes); |
531 | return -1; | |
532 | } | |
533 | /* output */ | |
534 | if (result) { | |
9914cf46 | 535 | int marker_size = xmp->marker_size; |
857b933e | 536 | int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, |
06dbc1ea | 537 | favor, changes, NULL, style, |
9914cf46 | 538 | marker_size); |
857b933e JS |
539 | result->ptr = xdl_malloc(size); |
540 | if (!result->ptr) { | |
541 | xdl_cleanup_merge(changes); | |
542 | return -1; | |
543 | } | |
544 | result->size = size; | |
73eb40ee | 545 | xdl_fill_merge_buffer(xe1, name1, xe2, name2, favor, changes, |
9914cf46 | 546 | result->ptr, style, marker_size); |
857b933e JS |
547 | } |
548 | return xdl_cleanup_merge(changes); | |
549 | } | |
550 | ||
551 | int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1, | |
552 | mmfile_t *mf2, const char *name2, | |
00f8f97d | 553 | xmparam_t const *xmp, int flags, mmbuffer_t *result) { |
857b933e JS |
554 | xdchange_t *xscr1, *xscr2; |
555 | xdfenv_t xe1, xe2; | |
53a7085a | 556 | int status; |
00f8f97d | 557 | xpparam_t const *xpp = &xmp->xpp; |
857b933e JS |
558 | |
559 | result->ptr = NULL; | |
560 | result->size = 0; | |
561 | ||
562 | if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 || | |
563 | xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { | |
564 | return -1; | |
565 | } | |
566 | if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || | |
567 | xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || | |
568 | xdl_build_script(&xe1, &xscr1) < 0) { | |
569 | xdl_free_env(&xe1); | |
570 | return -1; | |
571 | } | |
572 | if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || | |
573 | xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || | |
574 | xdl_build_script(&xe2, &xscr2) < 0) { | |
575 | xdl_free_env(&xe2); | |
576 | return -1; | |
577 | } | |
53a7085a | 578 | status = 0; |
5719db91 CB |
579 | if (!xscr1) { |
580 | result->ptr = xdl_malloc(mf2->size); | |
581 | memcpy(result->ptr, mf2->ptr, mf2->size); | |
582 | result->size = mf2->size; | |
583 | } else if (!xscr2) { | |
584 | result->ptr = xdl_malloc(mf1->size); | |
585 | memcpy(result->ptr, mf1->ptr, mf1->size); | |
586 | result->size = mf1->size; | |
587 | } else { | |
588 | status = xdl_do_merge(&xe1, xscr1, name1, | |
589 | &xe2, xscr2, name2, | |
9914cf46 | 590 | flags, xmp, result); |
857b933e | 591 | } |
5719db91 CB |
592 | xdl_free_script(xscr1); |
593 | xdl_free_script(xscr2); | |
594 | ||
857b933e JS |
595 | xdl_free_env(&xe1); |
596 | xdl_free_env(&xe2); | |
597 | ||
53a7085a | 598 | return status; |
857b933e | 599 | } |