Coverage Report

Created: 2026-12-01 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libgit2/deps/xdiff/xmerge.c
Line
Count
Source
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, see
17
 *  <http://www.gnu.org/licenses/>.
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
   * 3 = no conflict, take both.
32
   */
33
  int mode;
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
   */
40
  long i1, i2;
41
  long chg1, chg2;
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;
48
} xdmerge_t;
49
50
static int xdl_append_merge(xdmerge_t **merge, int mode,
51
          long i0, long chg0,
52
          long i1, long chg1,
53
          long i2, long chg2)
54
0
{
55
0
  xdmerge_t *m = *merge;
56
0
  if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) {
57
0
    if (mode != m->mode)
58
0
      m->mode = 0;
59
0
    m->chg0 = i0 + chg0 - m->i0;
60
0
    m->chg1 = i1 + chg1 - m->i1;
61
0
    m->chg2 = i2 + chg2 - m->i2;
62
0
  } else {
63
0
    m = xdl_malloc(sizeof(xdmerge_t));
64
0
    if (!m)
65
0
      return -1;
66
0
    m->next = NULL;
67
0
    m->mode = mode;
68
0
    m->i0 = i0;
69
0
    m->chg0 = chg0;
70
0
    m->i1 = i1;
71
0
    m->chg1 = chg1;
72
0
    m->i2 = i2;
73
0
    m->chg2 = chg2;
74
0
    if (*merge)
75
0
      (*merge)->next = m;
76
0
    *merge = m;
77
0
  }
78
0
  return 0;
79
0
}
80
81
static int xdl_cleanup_merge(xdmerge_t *c)
82
0
{
83
0
  int count = 0;
84
0
  xdmerge_t *next_c;
85
86
  /* were there conflicts? */
87
0
  for (; c; c = next_c) {
88
0
    if (c->mode == 0)
89
0
      count++;
90
0
    next_c = c->next;
91
0
    xdl_free(c);
92
0
  }
93
0
  return count;
94
0
}
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
0
{
99
0
  int i;
100
0
  xrecord_t *rec1 = xe1->xdf2.recs + i1;
101
0
  xrecord_t *rec2 = xe2->xdf2.recs + i2;
102
103
0
  for (i = 0; i < line_count; i++) {
104
0
    int result = xdl_recmatch((const char *)rec1[i].ptr, (long)rec1[i].size,
105
0
      (const char *)rec2[i].ptr, (long)rec2[i].size, flags);
106
0
    if (!result)
107
0
      return -1;
108
0
  }
109
0
  return 0;
110
0
}
111
112
static size_t xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
113
0
{
114
0
  xrecord_t *recs;
115
0
  size_t size = 0;
116
117
0
  recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i;
118
119
0
  if (count < 1)
120
0
    return 0;
121
122
0
  for (i = 0; i < count; i++) {
123
0
    if (dest)
124
0
      memcpy(dest + size, recs[i].ptr, recs[i].size);
125
126
0
    if (xdl_st_add_overflow(size, recs[i].size, &size))
127
0
      return SIZE_MAX;
128
0
  }
129
130
0
  if (add_nl) {
131
0
    i = (int)recs[count - 1].size;
132
0
    if (i == 0 || recs[count - 1].ptr[i - 1] != '\n') {
133
0
      if (needs_cr) {
134
0
        if (dest)
135
0
          dest[size] = '\r';
136
137
0
        if (xdl_st_add_overflow(size, 1, &size))
138
0
          return SIZE_MAX;
139
0
      }
140
141
0
      if (dest)
142
0
        dest[size] = '\n';
143
144
0
      if (xdl_st_add_overflow(size, 1, &size))
145
0
        return SIZE_MAX;
146
0
    }
147
0
  }
148
0
  return size;
149
0
}
150
151
static size_t xdl_recs_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
152
0
{
153
0
  return xdl_recs_copy_0(0, xe, i, count, needs_cr, add_nl, dest);
154
0
}
155
156
static size_t xdl_orig_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
157
0
{
158
0
  return xdl_recs_copy_0(1, xe, i, count, needs_cr, add_nl, dest);
159
0
}
160
161
/*
162
 * Returns 1 if the i'th line ends in CR/LF (if it is the last line and
163
 * has no eol, the preceding line, if any), 0 if it ends in LF-only, and
164
 * -1 if the line ending cannot be determined.
165
 */
166
static int is_eol_crlf(xdfile_t *file, int i)
167
0
{
168
0
  size_t size;
169
170
0
  if (i < (long)file->nrec - 1)
171
    /* All lines before the last *must* end in LF */
172
0
    return (size = file->recs[i].size) > 1 &&
173
0
      file->recs[i].ptr[size - 2] == '\r';
174
0
  if (!file->nrec)
175
    /* Cannot determine eol style from empty file */
176
0
    return -1;
177
0
  if ((size = file->recs[i].size) &&
178
0
      file->recs[i].ptr[size - 1] == '\n')
179
    /* Last line; ends in LF; Is it CR/LF? */
180
0
    return size > 1 &&
181
0
      file->recs[i].ptr[size - 2] == '\r';
182
0
  if (!i)
183
    /* The only line has no eol */
184
0
    return -1;
185
  /* Determine eol from second-to-last line */
186
0
  return (size = file->recs[i - 1].size) > 1 &&
187
0
    file->recs[i - 1].ptr[size - 2] == '\r';
188
0
}
189
190
static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m)
191
0
{
192
0
  int needs_cr;
193
194
  /* Match post-images' preceding, or first, lines' end-of-line style */
195
0
  needs_cr = is_eol_crlf(&xe1->xdf2, m->i1 ? m->i1 - 1 : 0);
196
0
  if (needs_cr)
197
0
    needs_cr = is_eol_crlf(&xe2->xdf2, m->i2 ? m->i2 - 1 : 0);
198
  /* Look at pre-image's first line, unless we already settled on LF */
199
0
  if (needs_cr)
200
0
    needs_cr = is_eol_crlf(&xe1->xdf1, 0);
201
  /* If still undecided, use LF-only */
202
0
  return needs_cr < 0 ? 0 : needs_cr;
203
0
}
204
205
static size_t fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
206
         xdfenv_t *xe2, const char *name2,
207
         const char *name3,
208
         size_t size, int i, int style,
209
         xdmerge_t *m, char *dest, int marker_size)
210
0
{
211
0
  int marker1_size = (name1 && *name1 ? strlen(name1) + 1 : 0);
212
0
  int marker2_size = (name2 && *name2 ? strlen(name2) + 1 : 0);
213
0
  int marker3_size = (name3 && *name3 ? strlen(name3) + 1 : 0);
214
0
  int needs_cr = is_cr_needed(xe1, xe2, m);
215
216
0
  if (marker_size <= 0)
217
0
    marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
218
219
  /* Before conflicting part */
220
0
  if (xdl_st_add_overflow(size, xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
221
0
          dest ? dest + size : NULL), &size))
222
0
    return SIZE_MAX;
223
224
0
  if (!dest) {
225
0
    if (xdl_st_add5_overflow(size, marker_size, 1, needs_cr, marker1_size, &size))
226
0
      return SIZE_MAX;
227
0
  } else {
228
0
    memset(dest + size, '<', marker_size);
229
0
    size += marker_size;
230
0
    if (marker1_size) {
231
0
      dest[size] = ' ';
232
0
      memcpy(dest + size + 1, name1, marker1_size - 1);
233
0
      size += marker1_size;
234
0
    }
235
0
    if (needs_cr)
236
0
      dest[size++] = '\r';
237
0
    dest[size++] = '\n';
238
0
  }
239
240
  /* Postimage from side #1 */
241
0
  if (xdl_st_add_overflow(size, xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
242
0
          dest ? dest + size : NULL), &size))
243
0
    return SIZE_MAX;
244
245
0
  if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
246
    /* Shared preimage */
247
0
    if (!dest) {
248
0
      if (xdl_st_add5_overflow(size, marker_size, 1, needs_cr, marker3_size, &size))
249
0
        return SIZE_MAX;
250
0
    } else {
251
0
      memset(dest + size, '|', marker_size);
252
0
      size += marker_size;
253
0
      if (marker3_size) {
254
0
        dest[size] = ' ';
255
0
        memcpy(dest + size + 1, name3, marker3_size - 1);
256
0
        size += marker3_size;
257
0
      }
258
0
      if (needs_cr)
259
0
        dest[size++] = '\r';
260
0
      dest[size++] = '\n';
261
0
    }
262
263
0
    if (xdl_st_add_overflow(size, xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1,
264
0
            dest ? dest + size : NULL), &size))
265
0
      return SIZE_MAX;
266
0
  }
267
268
0
  if (!dest) {
269
0
    if (xdl_st_add4_overflow(size, marker_size, 1, needs_cr, &size))
270
0
      return SIZE_MAX;
271
0
  } else {
272
0
    memset(dest + size, '=', marker_size);
273
0
    size += marker_size;
274
0
    if (needs_cr)
275
0
      dest[size++] = '\r';
276
0
    dest[size++] = '\n';
277
0
  }
278
279
  /* Postimage from side #2 */
280
0
  if (xdl_st_add_overflow(size, xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1,
281
0
        dest ? dest + size : NULL), &size))
282
0
    return SIZE_MAX;
283
284
0
  if (!dest) {
285
0
    if (xdl_st_add5_overflow(size, marker_size, 1, needs_cr, marker2_size, &size))
286
0
      return SIZE_MAX;
287
0
  } else {
288
0
    memset(dest + size, '>', marker_size);
289
0
    size += marker_size;
290
0
    if (marker2_size) {
291
0
      dest[size] = ' ';
292
0
      memcpy(dest + size + 1, name2, marker2_size - 1);
293
0
      size += marker2_size;
294
0
    }
295
0
    if (needs_cr)
296
0
      dest[size++] = '\r';
297
0
    dest[size++] = '\n';
298
0
  }
299
0
  return size;
300
0
}
301
302
static size_t xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
303
            xdfenv_t *xe2, const char *name2,
304
            const char *ancestor_name,
305
            int favor,
306
            xdmerge_t *m, char *dest, int style,
307
            int marker_size)
308
0
{
309
0
  size_t size;
310
0
  int i;
311
312
0
  for (size = i = 0; m; m = m->next) {
313
0
    if (favor && !m->mode)
314
0
      m->mode = favor;
315
316
0
    if (m->mode == 0) {
317
0
      size = fill_conflict_hunk(xe1, name1, xe2, name2,
318
0
              ancestor_name,
319
0
              size, i, style, m, dest,
320
0
              marker_size);
321
322
0
      if (size > LONG_MAX)
323
0
        return SIZE_MAX;
324
0
    } else if (m->mode & 3) {
325
      /* Before conflicting part */
326
0
      if (xdl_st_add_overflow(size,
327
0
          xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
328
0
          dest ? dest + size : NULL), &size))
329
0
        return SIZE_MAX;
330
331
      /* Postimage from side #1 */
332
0
      if (m->mode & 1) {
333
0
        int needs_cr = is_cr_needed(xe1, xe2, m);
334
335
0
        if (xdl_st_add_overflow(size,
336
0
            xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, (m->mode & 2),
337
0
            dest ? dest + size : NULL), &size))
338
0
          return SIZE_MAX;
339
0
      }
340
      /* Postimage from side #2 */
341
0
      if (m->mode & 2) {
342
0
        if (xdl_st_add_overflow(size,
343
0
            xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0,
344
0
            dest ? dest + size : NULL), &size))
345
0
          return SIZE_MAX;
346
0
      }
347
0
    } else
348
0
      continue;
349
0
    i = m->i1 + m->chg1;
350
0
  }
351
352
0
  if (xdl_st_add_overflow(size, xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0,
353
0
      dest ? dest + size : NULL), &size))
354
0
    return SIZE_MAX;
355
356
0
  return size;
357
0
}
358
359
static int recmatch(xrecord_t *rec1, xrecord_t *rec2, unsigned long flags)
360
0
{
361
0
  return xdl_recmatch((const char *)rec1->ptr, (long)rec1->size,
362
0
          (const char *)rec2->ptr, (long)rec2->size, flags);
363
0
}
364
365
/*
366
 * Remove any common lines from the beginning and end of the conflicted region.
367
 */
368
static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
369
    xpparam_t const *xpp)
370
0
{
371
0
  xrecord_t *rec1 = xe1->xdf2.recs, *rec2 = xe2->xdf2.recs;
372
0
  for (; m; m = m->next) {
373
    /* let's handle just the conflicts */
374
0
    if (m->mode)
375
0
      continue;
376
377
0
    while(m->chg1 && m->chg2 &&
378
0
          recmatch(&rec1[m->i1], &rec2[m->i2], xpp->flags)) {
379
0
      m->chg1--;
380
0
      m->chg2--;
381
0
      m->i1++;
382
0
      m->i2++;
383
0
    }
384
0
    while (m->chg1 && m->chg2 &&
385
0
           recmatch(&rec1[m->i1 + m->chg1 - 1],
386
0
        &rec2[m->i2 + m->chg2 - 1], xpp->flags)) {
387
0
      m->chg1--;
388
0
      m->chg2--;
389
0
    }
390
0
  }
391
0
}
392
393
/*
394
 * Sometimes, changes are not quite identical, but differ in only a few
395
 * lines. Try hard to show only these few lines as conflicting.
396
 */
397
static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
398
    xpparam_t const *xpp)
399
0
{
400
0
  for (; m; m = m->next) {
401
0
    mmfile_t t1, t2;
402
0
    xdfenv_t xe;
403
0
    xdchange_t *xscr, *x;
404
0
    int i1 = m->i1, i2 = m->i2;
405
406
    /* let's handle just the conflicts */
407
0
    if (m->mode)
408
0
      continue;
409
410
    /* no sense refining a conflict when one side is empty */
411
0
    if (m->chg1 == 0 || m->chg2 == 0)
412
0
      continue;
413
414
    /*
415
     * This probably does not work outside git, since
416
     * we have a very simple mmfile structure.
417
     */
418
0
    t1.ptr = (char *)xe1->xdf2.recs[m->i1].ptr;
419
0
    t1.size = (char *)xe1->xdf2.recs[m->i1 + m->chg1 - 1].ptr
420
0
      + xe1->xdf2.recs[m->i1 + m->chg1 - 1].size - t1.ptr;
421
0
    t2.ptr = (char *)xe2->xdf2.recs[m->i2].ptr;
422
0
    t2.size = (char *)xe2->xdf2.recs[m->i2 + m->chg2 - 1].ptr
423
0
      + xe2->xdf2.recs[m->i2 + m->chg2 - 1].size - t2.ptr;
424
0
    if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0)
425
0
      return -1;
426
0
    if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
427
0
        xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
428
0
        xdl_build_script(&xe, &xscr) < 0) {
429
0
      xdl_free_env(&xe);
430
0
      return -1;
431
0
    }
432
0
    if (!xscr) {
433
      /* If this happens, the changes are identical. */
434
0
      xdl_free_env(&xe);
435
0
      m->mode = 4;
436
0
      continue;
437
0
    }
438
0
    x = xscr;
439
0
    m->i1 = xscr->i1 + i1;
440
0
    m->chg1 = xscr->chg1;
441
0
    m->i2 = xscr->i2 + i2;
442
0
    m->chg2 = xscr->chg2;
443
0
    while (xscr->next) {
444
0
      xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t));
445
0
      if (!m2) {
446
0
        xdl_free_env(&xe);
447
0
        xdl_free_script(x);
448
0
        return -1;
449
0
      }
450
0
      xscr = xscr->next;
451
0
      m2->next = m->next;
452
0
      m->next = m2;
453
0
      m = m2;
454
0
      m->mode = 0;
455
0
      m->i1 = xscr->i1 + i1;
456
0
      m->chg1 = xscr->chg1;
457
0
      m->i2 = xscr->i2 + i2;
458
0
      m->chg2 = xscr->chg2;
459
0
    }
460
0
    xdl_free_env(&xe);
461
0
    xdl_free_script(x);
462
0
  }
463
0
  return 0;
464
0
}
465
466
static int line_contains_alnum(const char *ptr, long size)
467
0
{
468
0
  while (size--)
469
0
    if (isalnum((unsigned char)*(ptr++)))
470
0
      return 1;
471
0
  return 0;
472
0
}
473
474
static int lines_contain_alnum(xdfenv_t *xe, int i, int chg)
475
0
{
476
0
  for (; chg; chg--, i++)
477
0
    if (line_contains_alnum((const char *)xe->xdf2.recs[i].ptr,
478
0
        (long)xe->xdf2.recs[i].size))
479
0
      return 1;
480
0
  return 0;
481
0
}
482
483
/*
484
 * This function merges m and m->next, marking everything between those hunks
485
 * as conflicting, too.
486
 */
487
static void xdl_merge_two_conflicts(xdmerge_t *m)
488
0
{
489
0
  xdmerge_t *next_m = m->next;
490
0
  m->chg1 = next_m->i1 + next_m->chg1 - m->i1;
491
0
  m->chg2 = next_m->i2 + next_m->chg2 - m->i2;
492
0
  m->next = next_m->next;
493
0
  xdl_free(next_m);
494
0
}
495
496
/*
497
 * If there are less than 3 non-conflicting lines between conflicts,
498
 * it appears simpler -- because it takes up less (or as many) lines --
499
 * if the lines are moved into the conflicts.
500
 */
501
static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m,
502
              int simplify_if_no_alnum)
503
0
{
504
0
  int result = 0;
505
506
0
  if (!m)
507
0
    return result;
508
0
  for (;;) {
509
0
    xdmerge_t *next_m = m->next;
510
0
    int begin, end;
511
512
0
    if (!next_m)
513
0
      return result;
514
515
0
    begin = m->i1 + m->chg1;
516
0
    end = next_m->i1;
517
518
0
    if (m->mode != 0 || next_m->mode != 0 ||
519
0
        (end - begin > 3 &&
520
0
         (!simplify_if_no_alnum ||
521
0
          lines_contain_alnum(xe1, begin, end - begin)))) {
522
0
      m = next_m;
523
0
    } else {
524
0
      result++;
525
0
      xdl_merge_two_conflicts(m);
526
0
    }
527
0
  }
528
0
}
529
530
/*
531
 * level == 0: mark all overlapping changes as conflict
532
 * level == 1: mark overlapping changes as conflict only if not identical
533
 * level == 2: analyze non-identical changes for minimal conflict set
534
 * level == 3: analyze non-identical changes for minimal conflict set, but
535
 *             treat hunks not containing any letter or number as conflicting
536
 *
537
 * returns < 0 on error, == 0 for no conflicts, else number of conflicts
538
 */
539
static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
540
    xdfenv_t *xe2, xdchange_t *xscr2,
541
    xmparam_t const *xmp, mmbuffer_t *result)
542
0
{
543
0
  xdmerge_t *changes, *c;
544
0
  xpparam_t const *xpp = &xmp->xpp;
545
0
  const char *const ancestor_name = xmp->ancestor;
546
0
  const char *const name1 = xmp->file1;
547
0
  const char *const name2 = xmp->file2;
548
0
  int i0, i1, i2, chg0, chg1, chg2;
549
0
  int level = xmp->level;
550
0
  int style = xmp->style;
551
0
  int favor = xmp->favor;
552
553
  /*
554
   * XDL_MERGE_DIFF3 does not attempt to refine conflicts by looking
555
   * at common areas of sides 1 & 2, because the base (side 0) does
556
   * not match and is being shown.  Similarly, simplification of
557
   * non-conflicts is also skipped due to the skipping of conflict
558
   * refinement.
559
   *
560
   * XDL_MERGE_ZEALOUS_DIFF3, on the other hand, will attempt to
561
   * refine conflicts looking for common areas of sides 1 & 2.
562
   * However, since the base is being shown and does not match,
563
   * it will only look for common areas at the beginning or end
564
   * of the conflict block.  Since XDL_MERGE_ZEALOUS_DIFF3's
565
   * conflict refinement is much more limited in this fashion, the
566
   * conflict simplification will be skipped.
567
   */
568
0
  if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
569
    /*
570
     * "diff3 -m" output does not make sense for anything
571
     * more aggressive than XDL_MERGE_EAGER.
572
     */
573
0
    if (XDL_MERGE_EAGER < level)
574
0
      level = XDL_MERGE_EAGER;
575
0
  }
576
577
0
  c = changes = NULL;
578
579
0
  while (xscr1 && xscr2) {
580
0
    if (!changes)
581
0
      changes = c;
582
0
    if (xscr1->i1 + xscr1->chg1 < xscr2->i1) {
583
0
      i0 = xscr1->i1;
584
0
      i1 = xscr1->i2;
585
0
      i2 = xscr2->i2 - xscr2->i1 + xscr1->i1;
586
0
      chg0 = xscr1->chg1;
587
0
      chg1 = xscr1->chg2;
588
0
      chg2 = xscr1->chg1;
589
0
      if (xdl_append_merge(&c, 1,
590
0
               i0, chg0, i1, chg1, i2, chg2)) {
591
0
        xdl_cleanup_merge(changes);
592
0
        return -1;
593
0
      }
594
0
      xscr1 = xscr1->next;
595
0
      continue;
596
0
    }
597
0
    if (xscr2->i1 + xscr2->chg1 < xscr1->i1) {
598
0
      i0 = xscr2->i1;
599
0
      i1 = xscr1->i2 - xscr1->i1 + xscr2->i1;
600
0
      i2 = xscr2->i2;
601
0
      chg0 = xscr2->chg1;
602
0
      chg1 = xscr2->chg1;
603
0
      chg2 = xscr2->chg2;
604
0
      if (xdl_append_merge(&c, 2,
605
0
               i0, chg0, i1, chg1, i2, chg2)) {
606
0
        xdl_cleanup_merge(changes);
607
0
        return -1;
608
0
      }
609
0
      xscr2 = xscr2->next;
610
0
      continue;
611
0
    }
612
0
    if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 ||
613
0
        xscr1->chg1 != xscr2->chg1 ||
614
0
        xscr1->chg2 != xscr2->chg2 ||
615
0
        xdl_merge_cmp_lines(xe1, xscr1->i2,
616
0
          xe2, xscr2->i2,
617
0
          xscr1->chg2, xpp->flags)) {
618
      /* conflict */
619
0
      int off = xscr1->i1 - xscr2->i1;
620
0
      int ffo = off + xscr1->chg1 - xscr2->chg1;
621
622
0
      i0 = xscr1->i1;
623
0
      i1 = xscr1->i2;
624
0
      i2 = xscr2->i2;
625
0
      if (off > 0) {
626
0
        i0 -= off;
627
0
        i1 -= off;
628
0
      }
629
0
      else
630
0
        i2 += off;
631
0
      chg0 = xscr1->i1 + xscr1->chg1 - i0;
632
0
      chg1 = xscr1->i2 + xscr1->chg2 - i1;
633
0
      chg2 = xscr2->i2 + xscr2->chg2 - i2;
634
0
      if (ffo < 0) {
635
0
        chg0 -= ffo;
636
0
        chg1 -= ffo;
637
0
      } else
638
0
        chg2 += ffo;
639
0
      if (xdl_append_merge(&c, 0,
640
0
               i0, chg0, i1, chg1, i2, chg2)) {
641
0
        xdl_cleanup_merge(changes);
642
0
        return -1;
643
0
      }
644
0
    }
645
646
0
    i1 = xscr1->i1 + xscr1->chg1;
647
0
    i2 = xscr2->i1 + xscr2->chg1;
648
649
0
    if (i1 >= i2)
650
0
      xscr2 = xscr2->next;
651
0
    if (i2 >= i1)
652
0
      xscr1 = xscr1->next;
653
0
  }
654
0
  while (xscr1) {
655
0
    if (!changes)
656
0
      changes = c;
657
0
    i0 = xscr1->i1;
658
0
    i1 = xscr1->i2;
659
0
    i2 = xscr1->i1 + (long)xe2->xdf2.nrec - (long)xe2->xdf1.nrec;
660
0
    chg0 = xscr1->chg1;
661
0
    chg1 = xscr1->chg2;
662
0
    chg2 = xscr1->chg1;
663
0
    if (xdl_append_merge(&c, 1,
664
0
             i0, chg0, i1, chg1, i2, chg2)) {
665
0
      xdl_cleanup_merge(changes);
666
0
      return -1;
667
0
    }
668
0
    xscr1 = xscr1->next;
669
0
  }
670
0
  while (xscr2) {
671
0
    if (!changes)
672
0
      changes = c;
673
0
    i0 = xscr2->i1;
674
0
    i1 = xscr2->i1 + (long)xe1->xdf2.nrec - (long)xe1->xdf1.nrec;
675
0
    i2 = xscr2->i2;
676
0
    chg0 = xscr2->chg1;
677
0
    chg1 = xscr2->chg1;
678
0
    chg2 = xscr2->chg2;
679
0
    if (xdl_append_merge(&c, 2,
680
0
             i0, chg0, i1, chg1, i2, chg2)) {
681
0
      xdl_cleanup_merge(changes);
682
0
      return -1;
683
0
    }
684
0
    xscr2 = xscr2->next;
685
0
  }
686
0
  if (!changes)
687
0
    changes = c;
688
  /* refine conflicts */
689
0
  if (style == XDL_MERGE_ZEALOUS_DIFF3) {
690
0
    xdl_refine_zdiff3_conflicts(xe1, xe2, changes, xpp);
691
0
  } else if (XDL_MERGE_ZEALOUS <= level &&
692
0
       (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 ||
693
0
        xdl_simplify_non_conflicts(xe1, changes,
694
0
                 XDL_MERGE_ZEALOUS < level) < 0)) {
695
0
    xdl_cleanup_merge(changes);
696
0
    return -1;
697
0
  }
698
  /* output */
699
0
  if (result) {
700
0
    int marker_size = xmp->marker_size;
701
0
    size_t size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
702
0
                ancestor_name,
703
0
                favor, changes, NULL, style,
704
0
                marker_size);
705
0
    if (size > LONG_MAX) {
706
0
      xdl_cleanup_merge(changes);
707
0
      return -1;
708
0
    }
709
0
    result->ptr = xdl_malloc(size);
710
0
    if (!result->ptr) {
711
0
      xdl_cleanup_merge(changes);
712
0
      return -1;
713
0
    }
714
0
    result->size = size;
715
0
    xdl_fill_merge_buffer(xe1, name1, xe2, name2,
716
0
              ancestor_name, favor, changes,
717
0
              result->ptr, style, marker_size);
718
0
  }
719
0
  return xdl_cleanup_merge(changes);
720
0
}
721
722
int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
723
    xmparam_t const *xmp, mmbuffer_t *result)
724
0
{
725
0
  xdchange_t *xscr1 = NULL, *xscr2 = NULL;
726
0
  xdfenv_t xe1, xe2;
727
0
  int status = -1;
728
0
  xpparam_t const *xpp = &xmp->xpp;
729
730
0
  result->ptr = NULL;
731
0
  result->size = 0;
732
733
0
  if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0)
734
0
    return -1;
735
736
0
  if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0)
737
0
    goto free_xe1; /* avoid double free of xe2 */
738
739
0
  if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 ||
740
0
      xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 ||
741
0
      xdl_build_script(&xe1, &xscr1) < 0)
742
0
    goto out;
743
744
0
  if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
745
0
      xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
746
0
      xdl_build_script(&xe2, &xscr2) < 0)
747
0
    goto out;
748
749
0
  if (!xscr1) {
750
0
    if (mf2->size) {
751
0
      result->ptr = xdl_malloc(mf2->size);
752
0
      if (!result->ptr)
753
0
        goto out;
754
0
      memcpy(result->ptr, mf2->ptr, mf2->size);
755
0
      result->size = mf2->size;
756
0
    }
757
0
    status = 0;
758
0
  } else if (!xscr2) {
759
0
    if (mf1->size) {
760
0
      result->ptr = xdl_malloc(mf1->size);
761
0
      if (!result->ptr)
762
0
        goto out;
763
0
      memcpy(result->ptr, mf1->ptr, mf1->size);
764
0
      result->size = mf1->size;
765
0
    }
766
0
    status = 0;
767
0
  } else {
768
0
    status = xdl_do_merge(&xe1, xscr1,
769
0
              &xe2, xscr2,
770
0
              xmp, result);
771
0
  }
772
0
 out:
773
0
  xdl_free_script(xscr1);
774
0
  xdl_free_script(xscr2);
775
776
0
  xdl_free_env(&xe2);
777
0
 free_xe1:
778
0
  xdl_free_env(&xe1);
779
780
0
  return status;
781
0
}