t5400 send-pack test: try a bit more nontrivial transfer.
[git/dscho.git] / xdiff / xmerge.c
blob294450b89916180ab603d566e7d3f281773a3b92
1 /*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003-2006 Davide Libenzi, Johannes E. Schindelin
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.
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.
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
19 * Davide Libenzi <davidel@xmailserver.org>
23 #include "xinclude.h"
25 typedef struct s_xdmerge {
26 struct s_xdmerge *next;
28 * 0 = conflict,
29 * 1 = no conflict, take first,
30 * 2 = no conflict, take second.
32 int mode;
33 long i1, i2;
34 long chg1, chg2;
35 } xdmerge_t;
37 static int xdl_append_merge(xdmerge_t **merge, int mode,
38 long i1, long chg1, long i2, long chg2)
40 xdmerge_t *m = *merge;
41 if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) {
42 if (mode != m->mode)
43 m->mode = 0;
44 m->chg1 = i1 + chg1 - m->i1;
45 m->chg2 = i2 + chg2 - m->i2;
46 } else {
47 m = xdl_malloc(sizeof(xdmerge_t));
48 if (!m)
49 return -1;
50 m->next = NULL;
51 m->mode = mode;
52 m->i1 = i1;
53 m->chg1 = chg1;
54 m->i2 = i2;
55 m->chg2 = chg2;
56 if (*merge)
57 (*merge)->next = m;
58 *merge = m;
60 return 0;
63 static int xdl_cleanup_merge(xdmerge_t *c)
65 int count = 0;
66 xdmerge_t *next_c;
68 /* were there conflicts? */
69 for (; c; c = next_c) {
70 if (c->mode == 0)
71 count++;
72 next_c = c->next;
73 free(c);
75 return count;
78 static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
79 int line_count, long flags)
81 int i;
82 xrecord_t **rec1 = xe1->xdf2.recs + i1;
83 xrecord_t **rec2 = xe2->xdf2.recs + i2;
85 for (i = 0; i < line_count; i++) {
86 int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size,
87 rec2[i]->ptr, rec2[i]->size, flags);
88 if (!result)
89 return -1;
91 return 0;
94 static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
96 xrecord_t **recs = xe->xdf2.recs + i;
97 int size = 0;
99 if (count < 1)
100 return 0;
102 for (i = 0; i < count; size += recs[i++]->size)
103 if (dest)
104 memcpy(dest + size, recs[i]->ptr, recs[i]->size);
105 if (add_nl) {
106 i = recs[count - 1]->size;
107 if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') {
108 if (dest)
109 dest[size] = '\n';
110 size++;
113 return size;
116 static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
117 xdfenv_t *xe2, const char *name2, xdmerge_t *m, char *dest)
119 const int marker_size = 7;
120 int marker1_size = (name1 ? strlen(name1) + 1 : 0);
121 int marker2_size = (name2 ? strlen(name2) + 1 : 0);
122 int conflict_marker_size = 3 * (marker_size + 1)
123 + marker1_size + marker2_size;
124 int size, i1, j;
126 for (size = i1 = 0; m; m = m->next) {
127 if (m->mode == 0) {
128 size += xdl_recs_copy(xe1, i1, m->i1 - i1, 0,
129 dest ? dest + size : NULL);
130 if (dest) {
131 for (j = 0; j < marker_size; j++)
132 dest[size++] = '<';
133 if (marker1_size) {
134 dest[size] = ' ';
135 memcpy(dest + size + 1, name1,
136 marker1_size - 1);
137 size += marker1_size;
139 dest[size++] = '\n';
140 } else
141 size += conflict_marker_size;
142 size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
143 dest ? dest + size : NULL);
144 if (dest) {
145 for (j = 0; j < marker_size; j++)
146 dest[size++] = '=';
147 dest[size++] = '\n';
149 size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
150 dest ? dest + size : NULL);
151 if (dest) {
152 for (j = 0; j < marker_size; j++)
153 dest[size++] = '>';
154 if (marker2_size) {
155 dest[size] = ' ';
156 memcpy(dest + size + 1, name2,
157 marker2_size - 1);
158 size += marker2_size;
160 dest[size++] = '\n';
162 } else if (m->mode == 1)
163 size += xdl_recs_copy(xe1, i1, m->i1 + m->chg1 - i1, 0,
164 dest ? dest + size : NULL);
165 else if (m->mode == 2)
166 size += xdl_recs_copy(xe2, m->i2 - m->i1 + i1,
167 m->i1 + m->chg2 - i1, 0,
168 dest ? dest + size : NULL);
169 i1 = m->i1 + m->chg1;
171 size += xdl_recs_copy(xe1, i1, xe1->xdf2.nrec - i1, 0,
172 dest ? dest + size : NULL);
173 return size;
177 * Sometimes, changes are not quite identical, but differ in only a few
178 * lines. Try hard to show only these few lines as conflicting.
180 static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
181 xpparam_t const *xpp)
183 for (; m; m = m->next) {
184 mmfile_t t1, t2;
185 xdfenv_t xe;
186 xdchange_t *xscr, *x;
187 int i1 = m->i1, i2 = m->i2;
189 /* let's handle just the conflicts */
190 if (m->mode)
191 continue;
193 /* no sense refining a conflict when one side is empty */
194 if (m->chg1 == 0 || m->chg2 == 0)
195 continue;
198 * This probably does not work outside git, since
199 * we have a very simple mmfile structure.
201 t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr;
202 t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr
203 + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr;
204 t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr;
205 t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr
206 + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr;
207 if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0)
208 return -1;
209 if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
210 xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
211 xdl_build_script(&xe, &xscr) < 0) {
212 xdl_free_env(&xe);
213 return -1;
215 if (!xscr) {
216 /* If this happens, it's a bug. */
217 xdl_free_env(&xe);
218 return -2;
220 x = xscr;
221 m->i1 = xscr->i1 + i1;
222 m->chg1 = xscr->chg1;
223 m->i2 = xscr->i2 + i2;
224 m->chg2 = xscr->chg2;
225 while (xscr->next) {
226 xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t));
227 if (!m2) {
228 xdl_free_env(&xe);
229 xdl_free_script(x);
230 return -1;
232 xscr = xscr->next;
233 m2->next = m->next;
234 m->next = m2;
235 m = m2;
236 m->mode = 0;
237 m->i1 = xscr->i1 + i1;
238 m->chg1 = xscr->chg1;
239 m->i2 = xscr->i2 + i2;
240 m->chg2 = xscr->chg2;
242 xdl_free_env(&xe);
243 xdl_free_script(x);
245 return 0;
249 * level == 0: mark all overlapping changes as conflict
250 * level == 1: mark overlapping changes as conflict only if not identical
251 * level == 2: analyze non-identical changes for minimal conflict set
253 * returns < 0 on error, == 0 for no conflicts, else number of conflicts
255 static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
256 xdfenv_t *xe2, xdchange_t *xscr2, const char *name2,
257 int level, xpparam_t const *xpp, mmbuffer_t *result) {
258 xdmerge_t *changes, *c;
259 int i1, i2, chg1, chg2;
261 c = changes = NULL;
263 while (xscr1 && xscr2) {
264 if (!changes)
265 changes = c;
266 if (xscr1->i1 + xscr1->chg1 < xscr2->i1) {
267 i1 = xscr1->i2;
268 i2 = xscr2->i2 - xscr2->i1 + xscr1->i1;
269 chg1 = xscr1->chg2;
270 chg2 = xscr1->chg1;
271 if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
272 xdl_cleanup_merge(changes);
273 return -1;
275 xscr1 = xscr1->next;
276 continue;
278 if (xscr2->i1 + xscr2->chg1 < xscr1->i1) {
279 i1 = xscr1->i2 - xscr1->i1 + xscr2->i1;
280 i2 = xscr2->i2;
281 chg1 = xscr2->chg1;
282 chg2 = xscr2->chg2;
283 if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
284 xdl_cleanup_merge(changes);
285 return -1;
287 xscr2 = xscr2->next;
288 continue;
290 if (level < 1 || xscr1->i1 != xscr2->i1 ||
291 xscr1->chg1 != xscr2->chg1 ||
292 xscr1->chg2 != xscr2->chg2 ||
293 xdl_merge_cmp_lines(xe1, xscr1->i2,
294 xe2, xscr2->i2,
295 xscr1->chg2, xpp->flags)) {
296 /* conflict */
297 int off = xscr1->i1 - xscr2->i1;
298 int ffo = off + xscr1->chg1 - xscr2->chg1;
300 i1 = xscr1->i2;
301 i2 = xscr2->i2;
302 if (off > 0)
303 i1 -= off;
304 else
305 i2 += off;
306 chg1 = xscr1->i2 + xscr1->chg2 - i1;
307 chg2 = xscr2->i2 + xscr2->chg2 - i2;
308 if (ffo > 0)
309 chg2 += ffo;
310 else
311 chg1 -= ffo;
312 if (xdl_append_merge(&c, 0, i1, chg1, i2, chg2)) {
313 xdl_cleanup_merge(changes);
314 return -1;
318 i1 = xscr1->i1 + xscr1->chg1;
319 i2 = xscr2->i1 + xscr2->chg1;
321 if (i1 >= i2)
322 xscr2 = xscr2->next;
323 if (i2 >= i1)
324 xscr1 = xscr1->next;
326 while (xscr1) {
327 if (!changes)
328 changes = c;
329 i1 = xscr1->i2;
330 i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec;
331 chg1 = xscr1->chg2;
332 chg2 = xscr1->chg1;
333 if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
334 xdl_cleanup_merge(changes);
335 return -1;
337 xscr1 = xscr1->next;
339 while (xscr2) {
340 if (!changes)
341 changes = c;
342 i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec;
343 i2 = xscr2->i2;
344 chg1 = xscr2->chg1;
345 chg2 = xscr2->chg2;
346 if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
347 xdl_cleanup_merge(changes);
348 return -1;
350 xscr2 = xscr2->next;
352 if (!changes)
353 changes = c;
354 /* refine conflicts */
355 if (level > 1 && xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0) {
356 xdl_cleanup_merge(changes);
357 return -1;
359 /* output */
360 if (result) {
361 int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
362 changes, NULL);
363 result->ptr = xdl_malloc(size);
364 if (!result->ptr) {
365 xdl_cleanup_merge(changes);
366 return -1;
368 result->size = size;
369 xdl_fill_merge_buffer(xe1, name1, xe2, name2, changes,
370 result->ptr);
372 return xdl_cleanup_merge(changes);
375 int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
376 mmfile_t *mf2, const char *name2,
377 xpparam_t const *xpp, int level, mmbuffer_t *result) {
378 xdchange_t *xscr1, *xscr2;
379 xdfenv_t xe1, xe2;
380 int status;
382 result->ptr = NULL;
383 result->size = 0;
385 if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 ||
386 xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
387 return -1;
389 if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 ||
390 xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 ||
391 xdl_build_script(&xe1, &xscr1) < 0) {
392 xdl_free_env(&xe1);
393 return -1;
395 if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
396 xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
397 xdl_build_script(&xe2, &xscr2) < 0) {
398 xdl_free_env(&xe2);
399 return -1;
401 status = 0;
402 if (xscr1 || xscr2) {
403 if (!xscr1) {
404 result->ptr = xdl_malloc(mf2->size);
405 memcpy(result->ptr, mf2->ptr, mf2->size);
406 result->size = mf2->size;
407 } else if (!xscr2) {
408 result->ptr = xdl_malloc(mf1->size);
409 memcpy(result->ptr, mf1->ptr, mf1->size);
410 result->size = mf1->size;
411 } else {
412 status = xdl_do_merge(&xe1, xscr1, name1,
413 &xe2, xscr2, name2,
414 level, xpp, result);
416 xdl_free_script(xscr1);
417 xdl_free_script(xscr2);
419 xdl_free_env(&xe1);
420 xdl_free_env(&xe2);
422 return status;