Merge branch 'maint'
[git] / xdiff / xmerge.c
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;
33         long i1, i2;
34         long chg1, chg2;
35 } xdmerge_t;
36
37 static int xdl_append_merge(xdmerge_t **merge, int mode,
38                 long i1, long chg1, long i2, long chg2)
39 {
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;
59         }
60         return 0;
61 }
62
63 static int xdl_cleanup_merge(xdmerge_t *c)
64 {
65         int count = 0;
66         xdmerge_t *next_c;
67
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);
74         }
75         return count;
76 }
77
78 static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
79                 int line_count, long flags)
80 {
81         int i;
82         xrecord_t **rec1 = xe1->xdf2.recs + i1;
83         xrecord_t **rec2 = xe2->xdf2.recs + i2;
84
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;
90         }
91         return 0;
92 }
93
94 static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
95 {
96         xrecord_t **recs = xe->xdf2.recs + i;
97         int size = 0;
98
99         if (count < 1)
100                 return 0;
101
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++;
111                 }
112         }
113         return size;
114 }
115
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)
118 {
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;
125
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;
138                                 }
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';
148                         }
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;
159                                 }
160                                 dest[size++] = '\n';
161                         }
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;
170         }
171         size += xdl_recs_copy(xe1, i1, xe1->xdf2.nrec - i1, 0,
172                         dest ? dest + size : NULL);
173         return size;
174 }
175
176 /*
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.
179  */
180 static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
181                 xpparam_t const *xpp)
182 {
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;
188
189                 /* let's handle just the conflicts */
190                 if (m->mode)
191                         continue;
192
193                 /*
194                  * This probably does not work outside git, since
195                  * we have a very simple mmfile structure.
196                  */
197                 t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr;
198                 t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr
199                         + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr;
200                 t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr;
201                 t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr
202                         + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr;
203                 if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0)
204                         return -1;
205                 if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
206                     xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
207                     xdl_build_script(&xe, &xscr) < 0) {
208                         xdl_free_env(&xe);
209                         return -1;
210                 }
211                 if (!xscr) {
212                         /* If this happens, it's a bug. */
213                         xdl_free_env(&xe);
214                         return -2;
215                 }
216                 x = xscr;
217                 m->i1 = xscr->i1 + i1;
218                 m->chg1 = xscr->chg1;
219                 m->i2 = xscr->i2 + i2;
220                 m->chg2 = xscr->chg2;
221                 while (xscr->next) {
222                         xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t));
223                         if (!m2) {
224                                 xdl_free_env(&xe);
225                                 xdl_free_script(x);
226                                 return -1;
227                         }
228                         xscr = xscr->next;
229                         m2->next = m->next;
230                         m->next = m2;
231                         m = m2;
232                         m->mode = 0;
233                         m->i1 = xscr->i1 + i1;
234                         m->chg1 = xscr->chg1;
235                         m->i2 = xscr->i2 + i2;
236                         m->chg2 = xscr->chg2;
237                 }
238                 xdl_free_env(&xe);
239                 xdl_free_script(x);
240         }
241         return 0;
242 }
243
244 /*
245  * level == 0: mark all overlapping changes as conflict
246  * level == 1: mark overlapping changes as conflict only if not identical
247  * level == 2: analyze non-identical changes for minimal conflict set
248  *
249  * returns < 0 on error, == 0 for no conflicts, else number of conflicts
250  */
251 static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
252                 xdfenv_t *xe2, xdchange_t *xscr2, const char *name2,
253                 int level, xpparam_t const *xpp, mmbuffer_t *result) {
254         xdmerge_t *changes, *c;
255         int i1, i2, chg1, chg2;
256
257         c = changes = NULL;
258
259         while (xscr1 && xscr2) {
260                 if (!changes)
261                         changes = c;
262                 if (xscr1->i1 + xscr1->chg1 < xscr2->i1) {
263                         i1 = xscr1->i2;
264                         i2 = xscr2->i2 - xscr2->i1 + xscr1->i1;
265                         chg1 = xscr1->chg2;
266                         chg2 = xscr1->chg1;
267                         if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
268                                 xdl_cleanup_merge(changes);
269                                 return -1;
270                         }
271                         xscr1 = xscr1->next;
272                         continue;
273                 }
274                 if (xscr2->i1 + xscr2->chg1 < xscr1->i1) {
275                         i1 = xscr1->i2 - xscr1->i1 + xscr2->i1;
276                         i2 = xscr2->i2;
277                         chg1 = xscr2->chg1;
278                         chg2 = xscr2->chg2;
279                         if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
280                                 xdl_cleanup_merge(changes);
281                                 return -1;
282                         }
283                         xscr2 = xscr2->next;
284                         continue;
285                 }
286                 if (level < 1 || xscr1->i1 != xscr2->i1 ||
287                                 xscr1->chg1 != xscr2->chg1 ||
288                                 xscr1->chg2 != xscr2->chg2 ||
289                                 xdl_merge_cmp_lines(xe1, xscr1->i2,
290                                         xe2, xscr2->i2,
291                                         xscr1->chg2, xpp->flags)) {
292                         /* conflict */
293                         int off = xscr1->i1 - xscr2->i1;
294                         int ffo = off + xscr1->chg1 - xscr2->chg1;
295
296                         i1 = xscr1->i2;
297                         i2 = xscr2->i2;
298                         if (off > 0)
299                                 i1 -= off;
300                         else
301                                 i2 += off;
302                         chg1 = xscr1->i2 + xscr1->chg2 - i1;
303                         chg2 = xscr2->i2 + xscr2->chg2 - i2;
304                         if (ffo > 0)
305                                 chg2 += ffo;
306                         else
307                                 chg1 -= ffo;
308                         if (xdl_append_merge(&c, 0, i1, chg1, i2, chg2)) {
309                                 xdl_cleanup_merge(changes);
310                                 return -1;
311                         }
312                 }
313
314                 i1 = xscr1->i1 + xscr1->chg1;
315                 i2 = xscr2->i1 + xscr2->chg1;
316
317                 if (i1 >= i2)
318                         xscr2 = xscr2->next;
319                 if (i2 >= i1)
320                         xscr1 = xscr1->next;
321         }
322         while (xscr1) {
323                 if (!changes)
324                         changes = c;
325                 i1 = xscr1->i2;
326                 i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec;
327                 chg1 = xscr1->chg2;
328                 chg2 = xscr1->chg1;
329                 if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
330                         xdl_cleanup_merge(changes);
331                         return -1;
332                 }
333                 xscr1 = xscr1->next;
334         }
335         while (xscr2) {
336                 if (!changes)
337                         changes = c;
338                 i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec;
339                 i2 = xscr2->i2;
340                 chg1 = xscr2->chg1;
341                 chg2 = xscr2->chg2;
342                 if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
343                         xdl_cleanup_merge(changes);
344                         return -1;
345                 }
346                 xscr2 = xscr2->next;
347         }
348         if (!changes)
349                 changes = c;
350         /* refine conflicts */
351         if (level > 1 && xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0) {
352                 xdl_cleanup_merge(changes);
353                 return -1;
354         }
355         /* output */
356         if (result) {
357                 int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
358                         changes, NULL);
359                 result->ptr = xdl_malloc(size);
360                 if (!result->ptr) {
361                         xdl_cleanup_merge(changes);
362                         return -1;
363                 }
364                 result->size = size;
365                 xdl_fill_merge_buffer(xe1, name1, xe2, name2, changes,
366                                 result->ptr);
367         }
368         return xdl_cleanup_merge(changes);
369 }
370
371 int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
372                 mmfile_t *mf2, const char *name2,
373                 xpparam_t const *xpp, int level, mmbuffer_t *result) {
374         xdchange_t *xscr1, *xscr2;
375         xdfenv_t xe1, xe2;
376         int status;
377
378         result->ptr = NULL;
379         result->size = 0;
380
381         if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 ||
382                         xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
383                 return -1;
384         }
385         if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 ||
386             xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 ||
387             xdl_build_script(&xe1, &xscr1) < 0) {
388                 xdl_free_env(&xe1);
389                 return -1;
390         }
391         if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
392             xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
393             xdl_build_script(&xe2, &xscr2) < 0) {
394                 xdl_free_env(&xe2);
395                 return -1;
396         }
397         status = 0;
398         if (xscr1 || xscr2) {
399                 if (!xscr1) {
400                         result->ptr = xdl_malloc(mf2->size);
401                         memcpy(result->ptr, mf2->ptr, mf2->size);
402                         result->size = mf2->size;
403                 } else if (!xscr2) {
404                         result->ptr = xdl_malloc(mf1->size);
405                         memcpy(result->ptr, mf1->ptr, mf1->size);
406                         result->size = mf1->size;
407                 } else {
408                         status = xdl_do_merge(&xe1, xscr1, name1,
409                                               &xe2, xscr2, name2,
410                                               level, xpp, result);
411                 }
412                 xdl_free_script(xscr1);
413                 xdl_free_script(xscr2);
414         }
415         xdl_free_env(&xe1);
416         xdl_free_env(&xe2);
417
418         return status;
419 }