]>
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. | |
cd1d61c4 | 31 | * 3 = no conflict, take both. |
857b933e JS |
32 | */ |
33 | int mode; | |
e0af48e4 JH |
34 | /* |
35 | * These point at the respective postimages. E.g. <i1,chg1> is | |
36 | * how side #1 wants to change the common ancestor; if there is no | |
37 | * overlap, lines before i1 in the postimage of side #1 appear | |
38 | * in the merge result as a region touched by neither side. | |
39 | */ | |
857b933e JS |
40 | long i1, i2; |
41 | long chg1, chg2; | |
e0af48e4 JH |
42 | /* |
43 | * These point at the preimage; of course there is just one | |
44 | * preimage, that is from the shared common ancestor. | |
45 | */ | |
46 | long i0; | |
47 | long chg0; | |
857b933e JS |
48 | } xdmerge_t; |
49 | ||
50 | static int xdl_append_merge(xdmerge_t **merge, int mode, | |
e0af48e4 JH |
51 | long i0, long chg0, |
52 | long i1, long chg1, | |
53 | long i2, long chg2) | |
857b933e JS |
54 | { |
55 | xdmerge_t *m = *merge; | |
98e6da8a JS |
56 | if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) { |
57 | if (mode != m->mode) | |
58 | m->mode = 0; | |
e0af48e4 | 59 | m->chg0 = i0 + chg0 - m->i0; |
857b933e JS |
60 | m->chg1 = i1 + chg1 - m->i1; |
61 | m->chg2 = i2 + chg2 - m->i2; | |
62 | } else { | |
63 | m = xdl_malloc(sizeof(xdmerge_t)); | |
64 | if (!m) | |
65 | return -1; | |
66 | m->next = NULL; | |
67 | m->mode = mode; | |
e0af48e4 JH |
68 | m->i0 = i0; |
69 | m->chg0 = chg0; | |
857b933e JS |
70 | m->i1 = i1; |
71 | m->chg1 = chg1; | |
72 | m->i2 = i2; | |
73 | m->chg2 = chg2; | |
74 | if (*merge) | |
75 | (*merge)->next = m; | |
76 | *merge = m; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static int xdl_cleanup_merge(xdmerge_t *c) | |
82 | { | |
83 | int count = 0; | |
84 | xdmerge_t *next_c; | |
85 | ||
86 | /* were there conflicts? */ | |
87 | for (; c; c = next_c) { | |
88 | if (c->mode == 0) | |
89 | count++; | |
90 | next_c = c->next; | |
91 | free(c); | |
92 | } | |
93 | return count; | |
94 | } | |
95 | ||
96 | static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2, | |
97 | int line_count, long flags) | |
98 | { | |
99 | int i; | |
100 | xrecord_t **rec1 = xe1->xdf2.recs + i1; | |
101 | xrecord_t **rec2 = xe2->xdf2.recs + i2; | |
102 | ||
103 | for (i = 0; i < line_count; i++) { | |
104 | int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, | |
105 | rec2[i]->ptr, rec2[i]->size, flags); | |
106 | if (!result) | |
107 | return -1; | |
108 | } | |
109 | return 0; | |
110 | } | |
111 | ||
e0af48e4 | 112 | static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest) |
857b933e | 113 | { |
e0af48e4 | 114 | xrecord_t **recs; |
857b933e JS |
115 | int size = 0; |
116 | ||
e0af48e4 JH |
117 | recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; |
118 | ||
857b933e JS |
119 | if (count < 1) |
120 | return 0; | |
121 | ||
122 | for (i = 0; i < count; size += recs[i++]->size) | |
123 | if (dest) | |
124 | memcpy(dest + size, recs[i]->ptr, recs[i]->size); | |
125 | if (add_nl) { | |
126 | i = recs[count - 1]->size; | |
127 | if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { | |
128 | if (dest) | |
129 | dest[size] = '\n'; | |
130 | size++; | |
131 | } | |
132 | } | |
133 | return size; | |
134 | } | |
135 | ||
e0af48e4 JH |
136 | static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) |
137 | { | |
138 | return xdl_recs_copy_0(0, xe, i, count, add_nl, dest); | |
139 | } | |
140 | ||
141 | static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) | |
142 | { | |
143 | return xdl_recs_copy_0(1, xe, i, count, add_nl, dest); | |
144 | } | |
145 | ||
86efa215 JS |
146 | /* |
147 | * Returns 1 if the i'th line ends in CR/LF (if it is the last line and | |
148 | * has no eol, the preceding line, if any), 0 if it ends in LF-only, and | |
149 | * -1 if the line ending cannot be determined. | |
150 | */ | |
151 | static int is_eol_crlf(xdfile_t *file, int i) | |
152 | { | |
153 | long size; | |
154 | ||
155 | if (i < file->nrec - 1) | |
156 | /* All lines before the last *must* end in LF */ | |
157 | return (size = file->recs[i]->size) > 1 && | |
158 | file->recs[i]->ptr[size - 2] == '\r'; | |
159 | if (!file->nrec) | |
160 | /* Cannot determine eol style from empty file */ | |
161 | return -1; | |
162 | if ((size = file->recs[i]->size) && | |
163 | file->recs[i]->ptr[size - 1] == '\n') | |
164 | /* Last line; ends in LF; Is it CR/LF? */ | |
165 | return size > 1 && | |
166 | file->recs[i]->ptr[size - 2] == '\r'; | |
167 | if (!i) | |
168 | /* The only line has no eol */ | |
169 | return -1; | |
170 | /* Determine eol from second-to-last line */ | |
171 | return (size = file->recs[i - 1]->size) > 1 && | |
172 | file->recs[i - 1]->ptr[size - 2] == '\r'; | |
173 | } | |
174 | ||
175 | static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m) | |
176 | { | |
177 | int needs_cr; | |
178 | ||
179 | /* Match post-images' preceding, or first, lines' end-of-line style */ | |
180 | needs_cr = is_eol_crlf(&xe1->xdf2, m->i1 ? m->i1 - 1 : 0); | |
181 | if (needs_cr) | |
182 | needs_cr = is_eol_crlf(&xe2->xdf2, m->i2 ? m->i2 - 1 : 0); | |
183 | /* Look at pre-image's first line, unless we already settled on LF */ | |
184 | if (needs_cr) | |
185 | needs_cr = is_eol_crlf(&xe1->xdf1, 0); | |
186 | /* If still undecided, use LF-only */ | |
187 | return needs_cr < 0 ? 0 : needs_cr; | |
188 | } | |
189 | ||
f2b25dd8 JH |
190 | static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, |
191 | xdfenv_t *xe2, const char *name2, | |
8a161433 | 192 | const char *name3, |
e0af48e4 | 193 | int size, int i, int style, |
9914cf46 | 194 | xdmerge_t *m, char *dest, int marker_size) |
857b933e | 195 | { |
857b933e JS |
196 | int marker1_size = (name1 ? strlen(name1) + 1 : 0); |
197 | int marker2_size = (name2 ? strlen(name2) + 1 : 0); | |
8a161433 | 198 | int marker3_size = (name3 ? strlen(name3) + 1 : 0); |
86efa215 | 199 | int needs_cr = is_cr_needed(xe1, xe2, m); |
f2b25dd8 | 200 | |
9914cf46 JH |
201 | if (marker_size <= 0) |
202 | marker_size = DEFAULT_CONFLICT_MARKER_SIZE; | |
203 | ||
f2b25dd8 JH |
204 | /* Before conflicting part */ |
205 | size += xdl_recs_copy(xe1, i, m->i1 - i, 0, | |
206 | dest ? dest + size : NULL); | |
207 | ||
208 | if (!dest) { | |
86efa215 | 209 | size += marker_size + 1 + needs_cr + marker1_size; |
f2b25dd8 | 210 | } else { |
c8c073c4 AM |
211 | memset(dest + size, '<', marker_size); |
212 | size += marker_size; | |
f2b25dd8 JH |
213 | if (marker1_size) { |
214 | dest[size] = ' '; | |
215 | memcpy(dest + size + 1, name1, marker1_size - 1); | |
216 | size += marker1_size; | |
217 | } | |
86efa215 JS |
218 | if (needs_cr) |
219 | dest[size++] = '\r'; | |
f2b25dd8 JH |
220 | dest[size++] = '\n'; |
221 | } | |
222 | ||
223 | /* Postimage from side #1 */ | |
224 | size += xdl_recs_copy(xe1, m->i1, m->chg1, 1, | |
225 | dest ? dest + size : NULL); | |
e0af48e4 JH |
226 | |
227 | if (style == XDL_MERGE_DIFF3) { | |
228 | /* Shared preimage */ | |
229 | if (!dest) { | |
86efa215 | 230 | size += marker_size + 1 + needs_cr + marker3_size; |
e0af48e4 | 231 | } else { |
c8c073c4 AM |
232 | memset(dest + size, '|', marker_size); |
233 | size += marker_size; | |
8a161433 JN |
234 | if (marker3_size) { |
235 | dest[size] = ' '; | |
236 | memcpy(dest + size + 1, name3, marker3_size - 1); | |
237 | size += marker3_size; | |
238 | } | |
86efa215 JS |
239 | if (needs_cr) |
240 | dest[size++] = '\r'; | |
e0af48e4 JH |
241 | dest[size++] = '\n'; |
242 | } | |
243 | size += xdl_orig_copy(xe1, m->i0, m->chg0, 1, | |
244 | dest ? dest + size : NULL); | |
245 | } | |
246 | ||
f2b25dd8 | 247 | if (!dest) { |
86efa215 | 248 | size += marker_size + 1 + needs_cr; |
f2b25dd8 | 249 | } else { |
c8c073c4 AM |
250 | memset(dest + size, '=', marker_size); |
251 | size += marker_size; | |
86efa215 JS |
252 | if (needs_cr) |
253 | dest[size++] = '\r'; | |
f2b25dd8 JH |
254 | dest[size++] = '\n'; |
255 | } | |
256 | ||
257 | /* Postimage from side #2 */ | |
258 | size += xdl_recs_copy(xe2, m->i2, m->chg2, 1, | |
259 | dest ? dest + size : NULL); | |
260 | if (!dest) { | |
86efa215 | 261 | size += marker_size + 1 + needs_cr + marker2_size; |
f2b25dd8 | 262 | } else { |
c8c073c4 AM |
263 | memset(dest + size, '>', marker_size); |
264 | size += marker_size; | |
f2b25dd8 JH |
265 | if (marker2_size) { |
266 | dest[size] = ' '; | |
267 | memcpy(dest + size + 1, name2, marker2_size - 1); | |
268 | size += marker2_size; | |
269 | } | |
86efa215 JS |
270 | if (needs_cr) |
271 | dest[size++] = '\r'; | |
f2b25dd8 JH |
272 | dest[size++] = '\n'; |
273 | } | |
274 | return size; | |
275 | } | |
276 | ||
277 | static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, | |
e0af48e4 | 278 | xdfenv_t *xe2, const char *name2, |
8a161433 | 279 | const char *ancestor_name, |
73eb40ee | 280 | int favor, |
9914cf46 JH |
281 | xdmerge_t *m, char *dest, int style, |
282 | int marker_size) | |
f2b25dd8 JH |
283 | { |
284 | int size, i; | |
285 | ||
286 | for (size = i = 0; m; m = m->next) { | |
73eb40ee JH |
287 | if (favor && !m->mode) |
288 | m->mode = favor; | |
289 | ||
f2b25dd8 JH |
290 | if (m->mode == 0) |
291 | size = fill_conflict_hunk(xe1, name1, xe2, name2, | |
8a161433 | 292 | ancestor_name, |
9914cf46 JH |
293 | size, i, style, m, dest, |
294 | marker_size); | |
cd1d61c4 BW |
295 | else if (m->mode & 3) { |
296 | /* Before conflicting part */ | |
297 | size += xdl_recs_copy(xe1, i, m->i1 - i, 0, | |
f2b25dd8 | 298 | dest ? dest + size : NULL); |
cd1d61c4 BW |
299 | /* Postimage from side #1 */ |
300 | if (m->mode & 1) | |
ba311807 | 301 | size += xdl_recs_copy(xe1, m->i1, m->chg1, (m->mode & 2), |
cd1d61c4 BW |
302 | dest ? dest + size : NULL); |
303 | /* Postimage from side #2 */ | |
304 | if (m->mode & 2) | |
ba311807 | 305 | size += xdl_recs_copy(xe2, m->i2, m->chg2, 0, |
cd1d61c4 BW |
306 | dest ? dest + size : NULL); |
307 | } else | |
22b6abcd | 308 | continue; |
f2b25dd8 | 309 | i = m->i1 + m->chg1; |
857b933e | 310 | } |
f2b25dd8 JH |
311 | size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, |
312 | dest ? dest + size : NULL); | |
857b933e JS |
313 | return size; |
314 | } | |
315 | ||
316 | /* | |
317 | * Sometimes, changes are not quite identical, but differ in only a few | |
318 | * lines. Try hard to show only these few lines as conflicting. | |
319 | */ | |
320 | static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, | |
321 | xpparam_t const *xpp) | |
322 | { | |
323 | for (; m; m = m->next) { | |
324 | mmfile_t t1, t2; | |
325 | xdfenv_t xe; | |
326 | xdchange_t *xscr, *x; | |
327 | int i1 = m->i1, i2 = m->i2; | |
328 | ||
329 | /* let's handle just the conflicts */ | |
330 | if (m->mode) | |
331 | continue; | |
332 | ||
5d6b151f JS |
333 | /* no sense refining a conflict when one side is empty */ |
334 | if (m->chg1 == 0 || m->chg2 == 0) | |
335 | continue; | |
336 | ||
857b933e JS |
337 | /* |
338 | * This probably does not work outside git, since | |
339 | * we have a very simple mmfile structure. | |
340 | */ | |
341 | t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; | |
875b8ce4 JS |
342 | t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr |
343 | + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; | |
344 | t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; | |
345 | t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr | |
346 | + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; | |
857b933e JS |
347 | if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) |
348 | return -1; | |
349 | if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || | |
350 | xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || | |
351 | xdl_build_script(&xe, &xscr) < 0) { | |
352 | xdl_free_env(&xe); | |
353 | return -1; | |
354 | } | |
355 | if (!xscr) { | |
22b6abcd | 356 | /* If this happens, the changes are identical. */ |
857b933e | 357 | xdl_free_env(&xe); |
22b6abcd JS |
358 | m->mode = 4; |
359 | continue; | |
857b933e JS |
360 | } |
361 | x = xscr; | |
362 | m->i1 = xscr->i1 + i1; | |
363 | m->chg1 = xscr->chg1; | |
364 | m->i2 = xscr->i2 + i2; | |
365 | m->chg2 = xscr->chg2; | |
366 | while (xscr->next) { | |
367 | xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); | |
368 | if (!m2) { | |
369 | xdl_free_env(&xe); | |
370 | xdl_free_script(x); | |
371 | return -1; | |
372 | } | |
373 | xscr = xscr->next; | |
374 | m2->next = m->next; | |
375 | m->next = m2; | |
376 | m = m2; | |
377 | m->mode = 0; | |
378 | m->i1 = xscr->i1 + i1; | |
379 | m->chg1 = xscr->chg1; | |
380 | m->i2 = xscr->i2 + i2; | |
381 | m->chg2 = xscr->chg2; | |
382 | } | |
383 | xdl_free_env(&xe); | |
384 | xdl_free_script(x); | |
385 | } | |
386 | return 0; | |
387 | } | |
388 | ||
ee95ec5d JS |
389 | static int line_contains_alnum(const char *ptr, long size) |
390 | { | |
391 | while (size--) | |
349362cc | 392 | if (isalnum((unsigned char)*(ptr++))) |
ee95ec5d JS |
393 | return 1; |
394 | return 0; | |
395 | } | |
396 | ||
397 | static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) | |
398 | { | |
399 | for (; chg; chg--, i++) | |
400 | if (line_contains_alnum(xe->xdf2.recs[i]->ptr, | |
401 | xe->xdf2.recs[i]->size)) | |
402 | return 1; | |
403 | return 0; | |
404 | } | |
405 | ||
f407f14d JS |
406 | /* |
407 | * This function merges m and m->next, marking everything between those hunks | |
408 | * as conflicting, too. | |
409 | */ | |
410 | static void xdl_merge_two_conflicts(xdmerge_t *m) | |
411 | { | |
412 | xdmerge_t *next_m = m->next; | |
413 | m->chg1 = next_m->i1 + next_m->chg1 - m->i1; | |
414 | m->chg2 = next_m->i2 + next_m->chg2 - m->i2; | |
415 | m->next = next_m->next; | |
416 | free(next_m); | |
417 | } | |
418 | ||
419 | /* | |
420 | * If there are less than 3 non-conflicting lines between conflicts, | |
421 | * it appears simpler -- because it takes up less (or as many) lines -- | |
422 | * if the lines are moved into the conflicts. | |
423 | */ | |
ee95ec5d JS |
424 | static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, |
425 | int simplify_if_no_alnum) | |
f407f14d JS |
426 | { |
427 | int result = 0; | |
428 | ||
429 | if (!m) | |
430 | return result; | |
431 | for (;;) { | |
432 | xdmerge_t *next_m = m->next; | |
433 | int begin, end; | |
434 | ||
435 | if (!next_m) | |
436 | return result; | |
437 | ||
438 | begin = m->i1 + m->chg1; | |
439 | end = next_m->i1; | |
440 | ||
ee95ec5d JS |
441 | if (m->mode != 0 || next_m->mode != 0 || |
442 | (end - begin > 3 && | |
443 | (!simplify_if_no_alnum || | |
444 | lines_contain_alnum(xe1, begin, end - begin)))) { | |
f407f14d | 445 | m = next_m; |
ee95ec5d | 446 | } else { |
f407f14d JS |
447 | result++; |
448 | xdl_merge_two_conflicts(m); | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
857b933e JS |
453 | /* |
454 | * level == 0: mark all overlapping changes as conflict | |
455 | * level == 1: mark overlapping changes as conflict only if not identical | |
456 | * level == 2: analyze non-identical changes for minimal conflict set | |
ee95ec5d JS |
457 | * level == 3: analyze non-identical changes for minimal conflict set, but |
458 | * treat hunks not containing any letter or number as conflicting | |
857b933e JS |
459 | * |
460 | * returns < 0 on error, == 0 for no conflicts, else number of conflicts | |
461 | */ | |
a4b5e91c JN |
462 | static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, |
463 | xdfenv_t *xe2, xdchange_t *xscr2, | |
464 | xmparam_t const *xmp, mmbuffer_t *result) | |
465 | { | |
857b933e | 466 | xdmerge_t *changes, *c; |
9914cf46 | 467 | xpparam_t const *xpp = &xmp->xpp; |
8a161433 | 468 | const char *const ancestor_name = xmp->ancestor; |
a4b5e91c JN |
469 | const char *const name1 = xmp->file1; |
470 | const char *const name2 = xmp->file2; | |
e0af48e4 | 471 | int i0, i1, i2, chg0, chg1, chg2; |
560119b9 BW |
472 | int level = xmp->level; |
473 | int style = xmp->style; | |
474 | int favor = xmp->favor; | |
857b933e | 475 | |
83133740 JH |
476 | if (style == XDL_MERGE_DIFF3) { |
477 | /* | |
478 | * "diff3 -m" output does not make sense for anything | |
479 | * more aggressive than XDL_MERGE_EAGER. | |
480 | */ | |
481 | if (XDL_MERGE_EAGER < level) | |
482 | level = XDL_MERGE_EAGER; | |
483 | } | |
484 | ||
857b933e JS |
485 | c = changes = NULL; |
486 | ||
487 | while (xscr1 && xscr2) { | |
488 | if (!changes) | |
489 | changes = c; | |
490 | if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { | |
e0af48e4 | 491 | i0 = xscr1->i1; |
857b933e JS |
492 | i1 = xscr1->i2; |
493 | i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; | |
e0af48e4 | 494 | chg0 = xscr1->chg1; |
857b933e JS |
495 | chg1 = xscr1->chg2; |
496 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
497 | if (xdl_append_merge(&c, 1, |
498 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
499 | xdl_cleanup_merge(changes); |
500 | return -1; | |
501 | } | |
502 | xscr1 = xscr1->next; | |
503 | continue; | |
504 | } | |
505 | if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { | |
e0af48e4 | 506 | i0 = xscr2->i1; |
857b933e JS |
507 | i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; |
508 | i2 = xscr2->i2; | |
e0af48e4 | 509 | chg0 = xscr2->chg1; |
857b933e JS |
510 | chg1 = xscr2->chg1; |
511 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
512 | if (xdl_append_merge(&c, 2, |
513 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
514 | xdl_cleanup_merge(changes); |
515 | return -1; | |
516 | } | |
517 | xscr2 = xscr2->next; | |
518 | continue; | |
519 | } | |
838338cd | 520 | if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || |
857b933e JS |
521 | xscr1->chg1 != xscr2->chg1 || |
522 | xscr1->chg2 != xscr2->chg2 || | |
523 | xdl_merge_cmp_lines(xe1, xscr1->i2, | |
524 | xe2, xscr2->i2, | |
525 | xscr1->chg2, xpp->flags)) { | |
526 | /* conflict */ | |
527 | int off = xscr1->i1 - xscr2->i1; | |
528 | int ffo = off + xscr1->chg1 - xscr2->chg1; | |
529 | ||
e0af48e4 | 530 | i0 = xscr1->i1; |
857b933e JS |
531 | i1 = xscr1->i2; |
532 | i2 = xscr2->i2; | |
e0af48e4 JH |
533 | if (off > 0) { |
534 | i0 -= off; | |
857b933e | 535 | i1 -= off; |
e0af48e4 | 536 | } |
857b933e JS |
537 | else |
538 | i2 += off; | |
e0af48e4 | 539 | chg0 = xscr1->i1 + xscr1->chg1 - i0; |
857b933e JS |
540 | chg1 = xscr1->i2 + xscr1->chg2 - i1; |
541 | chg2 = xscr2->i2 + xscr2->chg2 - i2; | |
838338cd | 542 | if (ffo < 0) { |
e0af48e4 | 543 | chg0 -= ffo; |
857b933e | 544 | chg1 -= ffo; |
838338cd JH |
545 | } else |
546 | chg2 += ffo; | |
e0af48e4 JH |
547 | if (xdl_append_merge(&c, 0, |
548 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
549 | xdl_cleanup_merge(changes); |
550 | return -1; | |
551 | } | |
552 | } | |
553 | ||
554 | i1 = xscr1->i1 + xscr1->chg1; | |
555 | i2 = xscr2->i1 + xscr2->chg1; | |
556 | ||
98e6da8a | 557 | if (i1 >= i2) |
710daa83 | 558 | xscr2 = xscr2->next; |
98e6da8a | 559 | if (i2 >= i1) |
857b933e | 560 | xscr1 = xscr1->next; |
857b933e JS |
561 | } |
562 | while (xscr1) { | |
563 | if (!changes) | |
564 | changes = c; | |
e0af48e4 | 565 | i0 = xscr1->i1; |
857b933e JS |
566 | i1 = xscr1->i2; |
567 | i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; | |
e0af48e4 | 568 | chg0 = xscr1->chg1; |
857b933e JS |
569 | chg1 = xscr1->chg2; |
570 | chg2 = xscr1->chg1; | |
e0af48e4 JH |
571 | if (xdl_append_merge(&c, 1, |
572 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
573 | xdl_cleanup_merge(changes); |
574 | return -1; | |
575 | } | |
576 | xscr1 = xscr1->next; | |
577 | } | |
578 | while (xscr2) { | |
579 | if (!changes) | |
580 | changes = c; | |
e0af48e4 | 581 | i0 = xscr2->i1; |
857b933e JS |
582 | i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; |
583 | i2 = xscr2->i2; | |
e0af48e4 | 584 | chg0 = xscr2->chg1; |
857b933e JS |
585 | chg1 = xscr2->chg1; |
586 | chg2 = xscr2->chg2; | |
e0af48e4 JH |
587 | if (xdl_append_merge(&c, 2, |
588 | i0, chg0, i1, chg1, i2, chg2)) { | |
857b933e JS |
589 | xdl_cleanup_merge(changes); |
590 | return -1; | |
591 | } | |
592 | xscr2 = xscr2->next; | |
593 | } | |
594 | if (!changes) | |
595 | changes = c; | |
596 | /* refine conflicts */ | |
838338cd | 597 | if (XDL_MERGE_ZEALOUS <= level && |
f407f14d | 598 | (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || |
838338cd JH |
599 | xdl_simplify_non_conflicts(xe1, changes, |
600 | XDL_MERGE_ZEALOUS < level) < 0)) { | |
857b933e JS |
601 | xdl_cleanup_merge(changes); |
602 | return -1; | |
603 | } | |
604 | /* output */ | |
605 | if (result) { | |
9914cf46 | 606 | int marker_size = xmp->marker_size; |
857b933e | 607 | int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, |
8a161433 | 608 | ancestor_name, |
06dbc1ea | 609 | favor, changes, NULL, style, |
9914cf46 | 610 | marker_size); |
857b933e JS |
611 | result->ptr = xdl_malloc(size); |
612 | if (!result->ptr) { | |
613 | xdl_cleanup_merge(changes); | |
614 | return -1; | |
615 | } | |
616 | result->size = size; | |
8a161433 JN |
617 | xdl_fill_merge_buffer(xe1, name1, xe2, name2, |
618 | ancestor_name, favor, changes, | |
9914cf46 | 619 | result->ptr, style, marker_size); |
857b933e JS |
620 | } |
621 | return xdl_cleanup_merge(changes); | |
622 | } | |
623 | ||
a4b5e91c JN |
624 | int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, |
625 | xmparam_t const *xmp, mmbuffer_t *result) | |
626 | { | |
857b933e JS |
627 | xdchange_t *xscr1, *xscr2; |
628 | xdfenv_t xe1, xe2; | |
53a7085a | 629 | int status; |
00f8f97d | 630 | xpparam_t const *xpp = &xmp->xpp; |
857b933e JS |
631 | |
632 | result->ptr = NULL; | |
633 | result->size = 0; | |
634 | ||
635 | if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 || | |
636 | xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { | |
637 | return -1; | |
638 | } | |
639 | if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || | |
640 | xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || | |
641 | xdl_build_script(&xe1, &xscr1) < 0) { | |
642 | xdl_free_env(&xe1); | |
643 | return -1; | |
644 | } | |
645 | if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || | |
646 | xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || | |
647 | xdl_build_script(&xe2, &xscr2) < 0) { | |
648 | xdl_free_env(&xe2); | |
649 | return -1; | |
650 | } | |
53a7085a | 651 | status = 0; |
5719db91 CB |
652 | if (!xscr1) { |
653 | result->ptr = xdl_malloc(mf2->size); | |
654 | memcpy(result->ptr, mf2->ptr, mf2->size); | |
655 | result->size = mf2->size; | |
656 | } else if (!xscr2) { | |
657 | result->ptr = xdl_malloc(mf1->size); | |
658 | memcpy(result->ptr, mf1->ptr, mf1->size); | |
659 | result->size = mf1->size; | |
660 | } else { | |
a4b5e91c JN |
661 | status = xdl_do_merge(&xe1, xscr1, |
662 | &xe2, xscr2, | |
560119b9 | 663 | xmp, result); |
857b933e | 664 | } |
5719db91 CB |
665 | xdl_free_script(xscr1); |
666 | xdl_free_script(xscr2); | |
667 | ||
857b933e JS |
668 | xdl_free_env(&xe1); |
669 | xdl_free_env(&xe2); | |
670 | ||
53a7085a | 671 | return status; |
857b933e | 672 | } |