]>
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, |
f2b25dd8 | 148 | xdmerge_t *m, char *dest) |
857b933e JS |
149 | { |
150 | const int marker_size = 7; | |
151 | int marker1_size = (name1 ? strlen(name1) + 1 : 0); | |
152 | int marker2_size = (name2 ? strlen(name2) + 1 : 0); | |
f2b25dd8 JH |
153 | int j; |
154 | ||
155 | /* Before conflicting part */ | |
156 | size += xdl_recs_copy(xe1, i, m->i1 - i, 0, | |
157 | dest ? dest + size : NULL); | |
158 | ||
159 | if (!dest) { | |
160 | size += marker_size + 1 + marker1_size; | |
161 | } else { | |
162 | for (j = 0; j < marker_size; j++) | |
163 | dest[size++] = '<'; | |
164 | if (marker1_size) { | |
165 | dest[size] = ' '; | |
166 | memcpy(dest + size + 1, name1, marker1_size - 1); | |
167 | size += marker1_size; | |
168 | } | |
169 | dest[size++] = '\n'; | |
170 | } | |
171 | ||
172 | /* Postimage from side #1 */ | |
173 | size += xdl_recs_copy(xe1, m->i1, m->chg1, 1, | |
174 | dest ? dest + size : NULL); | |
e0af48e4 JH |
175 | |
176 | if (style == XDL_MERGE_DIFF3) { | |
177 | /* Shared preimage */ | |
178 | if (!dest) { | |
179 | size += marker_size + 1; | |
180 | } else { | |
181 | for (j = 0; j < marker_size; j++) | |
182 | dest[size++] = '|'; | |
183 | dest[size++] = '\n'; | |
184 | } | |
185 | size += xdl_orig_copy(xe1, m->i0, m->chg0, 1, | |
186 | dest ? dest + size : NULL); | |
187 | } | |
188 | ||
f2b25dd8 JH |
189 | if (!dest) { |
190 | size += marker_size + 1; | |
191 | } else { | |
192 | for (j = 0; j < marker_size; j++) | |
193 | dest[size++] = '='; | |
194 | dest[size++] = '\n'; | |
195 | } | |
196 | ||
197 | /* Postimage from side #2 */ | |
198 | size += xdl_recs_copy(xe2, m->i2, m->chg2, 1, | |
199 | dest ? dest + size : NULL); | |
200 | if (!dest) { | |
201 | size += marker_size + 1 + marker2_size; | |
202 | } else { | |
203 | for (j = 0; j < marker_size; j++) | |
204 | dest[size++] = '>'; | |
205 | if (marker2_size) { | |
206 | dest[size] = ' '; | |
207 | memcpy(dest + size + 1, name2, marker2_size - 1); | |
208 | size += marker2_size; | |
209 | } | |
210 | dest[size++] = '\n'; | |
211 | } | |
212 | return size; | |
213 | } | |
214 | ||
215 | static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, | |
e0af48e4 | 216 | xdfenv_t *xe2, const char *name2, |
73eb40ee | 217 | int favor, |
e0af48e4 | 218 | xdmerge_t *m, char *dest, int style) |
f2b25dd8 JH |
219 | { |
220 | int size, i; | |
221 | ||
222 | for (size = i = 0; m; m = m->next) { | |
73eb40ee JH |
223 | if (favor && !m->mode) |
224 | m->mode = favor; | |
225 | ||
f2b25dd8 JH |
226 | if (m->mode == 0) |
227 | size = fill_conflict_hunk(xe1, name1, xe2, name2, | |
e0af48e4 | 228 | size, i, style, m, dest); |
f2b25dd8 JH |
229 | else if (m->mode == 1) |
230 | size += xdl_recs_copy(xe1, i, m->i1 + m->chg1 - i, 0, | |
231 | dest ? dest + size : NULL); | |
857b933e | 232 | else if (m->mode == 2) |
f2b25dd8 JH |
233 | size += xdl_recs_copy(xe2, m->i2 - m->i1 + i, |
234 | m->i1 + m->chg2 - i, 0, | |
235 | dest ? dest + size : NULL); | |
22b6abcd JS |
236 | else |
237 | continue; | |
f2b25dd8 | 238 | i = m->i1 + m->chg1; |
857b933e | 239 | } |
f2b25dd8 JH |
240 | size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, |
241 | dest ? dest + size : NULL); | |
857b933e JS |
242 | return size; |
243 | } | |
244 | ||
245 | /* | |
246 | * Sometimes, changes are not quite identical, but differ in only a few | |
247 | * lines. Try hard to show only these few lines as conflicting. | |
248 | */ | |
249 | static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, | |
250 | xpparam_t const *xpp) | |
251 | { | |
252 | for (; m; m = m->next) { | |
253 | mmfile_t t1, t2; | |
254 | xdfenv_t xe; | |
255 | xdchange_t *xscr, *x; | |
256 | int i1 = m->i1, i2 = m->i2; | |
257 | ||
258 | /* let's handle just the conflicts */ | |
259 | if (m->mode) | |
260 | continue; | |
261 | ||
5d6b151f JS |
262 | /* no sense refining a conflict when one side is empty */ |
263 | if (m->chg1 == 0 || m->chg2 == 0) | |
264 | continue; | |
265 | ||
857b933e JS |
266 | /* |
267 | * This probably does not work outside git, since | |
268 | * we have a very simple mmfile structure. | |
269 | */ | |
270 | t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; | |
875b8ce4 JS |
271 | t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr |
272 | + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; | |
273 | t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; | |
274 | t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr | |
275 | + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; | |
857b933e JS |
276 | if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) |
277 | return -1; | |
278 | if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || | |
279 | xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || | |
280 | xdl_build_script(&xe, &xscr) < 0) { | |
281 | xdl_free_env(&xe); | |
282 | return -1; | |
283 | } | |
284 | if (!xscr) { | |
22b6abcd | 285 | /* If this happens, the changes are identical. */ |
857b933e | 286 | xdl_free_env(&xe); |
22b6abcd JS |
287 | m->mode = 4; |
288 | continue; | |
857b933e JS |
289 | } |
290 | x = xscr; | |
291 | m->i1 = xscr->i1 + i1; | |
292 | m->chg1 = xscr->chg1; | |
293 | m->i2 = xscr->i2 + i2; | |
294 | m->chg2 = xscr->chg2; | |
295 | while (xscr->next) { | |
296 | xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); | |
297 | if (!m2) { | |
298 | xdl_free_env(&xe); | |
299 | xdl_free_script(x); | |
300 | return -1; | |
301 | } | |
302 | xscr = xscr->next; | |
303 | m2->next = m->next; | |
304 | m->next = m2; | |
305 | m = m2; | |
306 | m->mode = 0; | |
307 | m->i1 = xscr->i1 + i1; | |
308 | m->chg1 = xscr->chg1; | |
309 | m->i2 = xscr->i2 + i2; | |
310 | m->chg2 = xscr->chg2; | |
311 | } | |
312 | xdl_free_env(&xe); | |
313 | xdl_free_script(x); | |
314 | } | |
315 | return 0; | |
316 | } | |
317 | ||
ee95ec5d JS |
318 | static int line_contains_alnum(const char *ptr, long size) |
319 | { | |
320 | while (size--) | |
321 | if (isalnum(*(ptr++))) | |
322 | return 1; | |
323 | return 0; | |
324 | } | |
325 | ||
326 | static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) | |
327 | { | |
328 | for (; chg; chg--, i++) | |
329 | if (line_contains_alnum(xe->xdf2.recs[i]->ptr, | |
330 | xe->xdf2.recs[i]->size)) | |
331 | return 1; | |
332 | return 0; | |
333 | } | |
334 | ||
f407f14d JS |
335 | /* |
336 | * This function merges m and m->next, marking everything between those hunks | |
337 | * as conflicting, too. | |
338 | */ | |
339 | static void xdl_merge_two_conflicts(xdmerge_t *m) | |
340 | { | |
341 | xdmerge_t *next_m = m->next; | |
342 | m->chg1 = next_m->i1 + next_m->chg1 - m->i1; | |
343 | m->chg2 = next_m->i2 + next_m->chg2 - m->i2; | |
344 | m->next = next_m->next; | |
345 | free(next_m); | |
346 | } | |
347 | ||
348 | /* | |
349 | * If there are less than 3 non-conflicting lines between conflicts, | |
350 | * it appears simpler -- because it takes up less (or as many) lines -- | |
351 | * if the lines are moved into the conflicts. | |
352 | */ | |
ee95ec5d JS |
353 | static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, |
354 | int simplify_if_no_alnum) | |
f407f14d JS |
355 | { |
356 | int result = 0; | |
357 | ||
358 | if (!m) | |
359 | return result; | |
360 | for (;;) { | |
361 | xdmerge_t *next_m = m->next; | |
362 | int begin, end; | |
363 | ||
364 | if (!next_m) | |
365 | return result; | |
366 | ||
367 | begin = m->i1 + m->chg1; | |
368 | end = next_m->i1; | |
369 | ||
ee95ec5d JS |
370 | if (m->mode != 0 || next_m->mode != 0 || |
371 | (end - begin > 3 && | |
372 | (!simplify_if_no_alnum || | |
373 | lines_contain_alnum(xe1, begin, end - begin)))) { | |
f407f14d | 374 | m = next_m; |
ee95ec5d | 375 | } else { |
f407f14d JS |
376 | result++; |
377 | xdl_merge_two_conflicts(m); | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
857b933e JS |
382 | /* |
383 | * level == 0: mark all overlapping changes as conflict | |
384 | * level == 1: mark overlapping changes as conflict only if not identical | |
385 | * level == 2: analyze non-identical changes for minimal conflict set | |
ee95ec5d JS |
386 | * level == 3: analyze non-identical changes for minimal conflict set, but |
387 | * treat hunks not containing any letter or number as conflicting | |
857b933e JS |
388 | * |
389 | * returns < 0 on error, == 0 for no conflicts, else number of conflicts | |
390 | */ | |
391 | static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1, | |
392 | xdfenv_t *xe2, xdchange_t *xscr2, const char *name2, | |
e0af48e4 | 393 | int flags, xpparam_t const *xpp, mmbuffer_t *result) { |
857b933e | 394 | xdmerge_t *changes, *c; |
e0af48e4 JH |
395 | int i0, i1, i2, chg0, chg1, chg2; |
396 | int level = flags & XDL_MERGE_LEVEL_MASK; | |
397 | int style = flags & XDL_MERGE_STYLE_MASK; | |
73eb40ee | 398 | int favor = XDL_MERGE_FAVOR(flags); |
857b933e | 399 | |
83133740 JH |
400 | if (style == XDL_MERGE_DIFF3) { |
401 | /* | |
402 | * "diff3 -m" output does not make sense for anything | |
403 | * more aggressive than XDL_MERGE_EAGER. | |
404 | */ | |
405 | if (XDL_MERGE_EAGER < level) | |
406 | level = XDL_MERGE_EAGER; | |
407 | } | |
408 | ||
857b933e JS |
409 | c = changes = NULL; |
410 | ||
411 | while (xscr1 && xscr2) { | |
412 | if (!changes) | |
413 | changes = c; | |
414 | if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { | |
e0af48e4 | 415 | i0 = xscr1->i1; |
857b933e JS |
416 | i1 = xscr1->i2; |
417 | i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; | |
e0af48e4 | 418 | chg0 = xscr1->chg1; |
857b933e JS |
419 | chg1 = xscr1->chg2; |
420 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
421 | if (xdl_append_merge(&c, 1, |
422 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
423 | xdl_cleanup_merge(changes); |
424 | return -1; | |
425 | } | |
426 | xscr1 = xscr1->next; | |
427 | continue; | |
428 | } | |
429 | if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { | |
e0af48e4 | 430 | i0 = xscr2->i1; |
857b933e JS |
431 | i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; |
432 | i2 = xscr2->i2; | |
e0af48e4 | 433 | chg0 = xscr2->chg1; |
857b933e JS |
434 | chg1 = xscr2->chg1; |
435 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
436 | if (xdl_append_merge(&c, 2, |
437 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
438 | xdl_cleanup_merge(changes); |
439 | return -1; | |
440 | } | |
441 | xscr2 = xscr2->next; | |
442 | continue; | |
443 | } | |
838338cd | 444 | if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || |
857b933e JS |
445 | xscr1->chg1 != xscr2->chg1 || |
446 | xscr1->chg2 != xscr2->chg2 || | |
447 | xdl_merge_cmp_lines(xe1, xscr1->i2, | |
448 | xe2, xscr2->i2, | |
449 | xscr1->chg2, xpp->flags)) { | |
450 | /* conflict */ | |
451 | int off = xscr1->i1 - xscr2->i1; | |
452 | int ffo = off + xscr1->chg1 - xscr2->chg1; | |
453 | ||
e0af48e4 | 454 | i0 = xscr1->i1; |
857b933e JS |
455 | i1 = xscr1->i2; |
456 | i2 = xscr2->i2; | |
e0af48e4 JH |
457 | if (off > 0) { |
458 | i0 -= off; | |
857b933e | 459 | i1 -= off; |
e0af48e4 | 460 | } |
857b933e JS |
461 | else |
462 | i2 += off; | |
e0af48e4 | 463 | chg0 = xscr1->i1 + xscr1->chg1 - i0; |
857b933e JS |
464 | chg1 = xscr1->i2 + xscr1->chg2 - i1; |
465 | chg2 = xscr2->i2 + xscr2->chg2 - i2; | |
838338cd | 466 | if (ffo < 0) { |
e0af48e4 | 467 | chg0 -= ffo; |
857b933e | 468 | chg1 -= ffo; |
838338cd JH |
469 | } else |
470 | chg2 += ffo; | |
e0af48e4 JH |
471 | if (xdl_append_merge(&c, 0, |
472 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
473 | xdl_cleanup_merge(changes); |
474 | return -1; | |
475 | } | |
476 | } | |
477 | ||
478 | i1 = xscr1->i1 + xscr1->chg1; | |
479 | i2 = xscr2->i1 + xscr2->chg1; | |
480 | ||
98e6da8a | 481 | if (i1 >= i2) |
710daa83 | 482 | xscr2 = xscr2->next; |
98e6da8a | 483 | if (i2 >= i1) |
857b933e | 484 | xscr1 = xscr1->next; |
857b933e JS |
485 | } |
486 | while (xscr1) { | |
487 | if (!changes) | |
488 | changes = c; | |
e0af48e4 | 489 | i0 = xscr1->i1; |
857b933e JS |
490 | i1 = xscr1->i2; |
491 | i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; | |
e0af48e4 | 492 | chg0 = xscr1->chg1; |
857b933e JS |
493 | chg1 = xscr1->chg2; |
494 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
495 | if (xdl_append_merge(&c, 1, |
496 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
497 | xdl_cleanup_merge(changes); |
498 | return -1; | |
499 | } | |
500 | xscr1 = xscr1->next; | |
501 | } | |
502 | while (xscr2) { | |
503 | if (!changes) | |
504 | changes = c; | |
e0af48e4 | 505 | i0 = xscr2->i1; |
857b933e JS |
506 | i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; |
507 | i2 = xscr2->i2; | |
e0af48e4 | 508 | chg0 = xscr2->chg1; |
857b933e JS |
509 | chg1 = xscr2->chg1; |
510 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
511 | if (xdl_append_merge(&c, 2, |
512 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
513 | xdl_cleanup_merge(changes); |
514 | return -1; | |
515 | } | |
516 | xscr2 = xscr2->next; | |
517 | } | |
518 | if (!changes) | |
519 | changes = c; | |
520 | /* refine conflicts */ | |
838338cd | 521 | if (XDL_MERGE_ZEALOUS <= level && |
f407f14d | 522 | (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || |
838338cd JH |
523 | xdl_simplify_non_conflicts(xe1, changes, |
524 | XDL_MERGE_ZEALOUS < level) < 0)) { | |
857b933e JS |
525 | xdl_cleanup_merge(changes); |
526 | return -1; | |
527 | } | |
528 | /* output */ | |
529 | if (result) { | |
530 | int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, | |
73eb40ee | 531 | favor, changes, NULL, style); |
857b933e JS |
532 | result->ptr = xdl_malloc(size); |
533 | if (!result->ptr) { | |
534 | xdl_cleanup_merge(changes); | |
535 | return -1; | |
536 | } | |
537 | result->size = size; | |
73eb40ee | 538 | xdl_fill_merge_buffer(xe1, name1, xe2, name2, favor, changes, |
e0af48e4 | 539 | result->ptr, style); |
857b933e JS |
540 | } |
541 | return xdl_cleanup_merge(changes); | |
542 | } | |
543 | ||
544 | int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1, | |
545 | mmfile_t *mf2, const char *name2, | |
e0af48e4 | 546 | xpparam_t const *xpp, int flags, mmbuffer_t *result) { |
857b933e JS |
547 | xdchange_t *xscr1, *xscr2; |
548 | xdfenv_t xe1, xe2; | |
53a7085a | 549 | int status; |
857b933e JS |
550 | |
551 | result->ptr = NULL; | |
552 | result->size = 0; | |
553 | ||
554 | if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 || | |
555 | xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { | |
556 | return -1; | |
557 | } | |
558 | if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || | |
559 | xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || | |
560 | xdl_build_script(&xe1, &xscr1) < 0) { | |
561 | xdl_free_env(&xe1); | |
562 | return -1; | |
563 | } | |
564 | if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || | |
565 | xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || | |
566 | xdl_build_script(&xe2, &xscr2) < 0) { | |
567 | xdl_free_env(&xe2); | |
568 | return -1; | |
569 | } | |
53a7085a | 570 | status = 0; |
5719db91 CB |
571 | if (!xscr1) { |
572 | result->ptr = xdl_malloc(mf2->size); | |
573 | memcpy(result->ptr, mf2->ptr, mf2->size); | |
574 | result->size = mf2->size; | |
575 | } else if (!xscr2) { | |
576 | result->ptr = xdl_malloc(mf1->size); | |
577 | memcpy(result->ptr, mf1->ptr, mf1->size); | |
578 | result->size = mf1->size; | |
579 | } else { | |
580 | status = xdl_do_merge(&xe1, xscr1, name1, | |
581 | &xe2, xscr2, name2, | |
456cb4cf | 582 | flags, xpp, result); |
857b933e | 583 | } |
5719db91 CB |
584 | xdl_free_script(xscr1); |
585 | xdl_free_script(xscr2); | |
586 | ||
857b933e JS |
587 | xdl_free_env(&xe1); |
588 | xdl_free_env(&xe2); | |
589 | ||
53a7085a | 590 | return status; |
857b933e | 591 | } |