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>
25 typedef struct s_xdmerge
{
26 struct s_xdmerge
*next
;
29 * 1 = no conflict, take first,
30 * 2 = no conflict, take second.
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
)) {
44 m
->chg1
= i1
+ chg1
- m
->i1
;
45 m
->chg2
= i2
+ chg2
- m
->i2
;
47 m
= xdl_malloc(sizeof(xdmerge_t
));
63 static int xdl_cleanup_merge(xdmerge_t
*c
)
68 /* were there conflicts? */
69 for (; c
; c
= next_c
) {
78 static int xdl_merge_cmp_lines(xdfenv_t
*xe1
, int i1
, xdfenv_t
*xe2
, int i2
,
79 int line_count
, long flags
)
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
);
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
;
102 for (i
= 0; i
< count
; size
+= recs
[i
++]->size
)
104 memcpy(dest
+ size
, recs
[i
]->ptr
, recs
[i
]->size
);
106 i
= recs
[count
- 1]->size
;
107 if (i
== 0 || recs
[count
- 1]->ptr
[i
- 1] != '\n') {
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
;
126 for (size
= i1
= 0; m
; m
= m
->next
) {
128 size
+= xdl_recs_copy(xe1
, i1
, m
->i1
- i1
, 0,
129 dest
? dest
+ size
: NULL
);
131 for (j
= 0; j
< marker_size
; j
++)
135 memcpy(dest
+ size
+ 1, name1
,
137 size
+= marker1_size
;
141 size
+= conflict_marker_size
;
142 size
+= xdl_recs_copy(xe1
, m
->i1
, m
->chg1
, 1,
143 dest
? dest
+ size
: NULL
);
145 for (j
= 0; j
< marker_size
; j
++)
149 size
+= xdl_recs_copy(xe2
, m
->i2
, m
->chg2
, 1,
150 dest
? dest
+ size
: NULL
);
152 for (j
= 0; j
< marker_size
; j
++)
156 memcpy(dest
+ size
+ 1, name2
,
158 size
+= marker2_size
;
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
);
171 i1
= m
->i1
+ m
->chg1
;
173 size
+= xdl_recs_copy(xe1
, i1
, xe1
->xdf2
.nrec
- i1
, 0,
174 dest
? dest
+ size
: NULL
);
179 * Sometimes, changes are not quite identical, but differ in only a few
180 * lines. Try hard to show only these few lines as conflicting.
182 static int xdl_refine_conflicts(xdfenv_t
*xe1
, xdfenv_t
*xe2
, xdmerge_t
*m
,
183 xpparam_t
const *xpp
)
185 for (; m
; m
= m
->next
) {
188 xdchange_t
*xscr
, *x
;
189 int i1
= m
->i1
, i2
= m
->i2
;
191 /* let's handle just the conflicts */
195 /* no sense refining a conflict when one side is empty */
196 if (m
->chg1
== 0 || m
->chg2
== 0)
200 * This probably does not work outside git, since
201 * we have a very simple mmfile structure.
203 t1
.ptr
= (char *)xe1
->xdf2
.recs
[m
->i1
]->ptr
;
204 t1
.size
= xe1
->xdf2
.recs
[m
->i1
+ m
->chg1
- 1]->ptr
205 + xe1
->xdf2
.recs
[m
->i1
+ m
->chg1
- 1]->size
- t1
.ptr
;
206 t2
.ptr
= (char *)xe2
->xdf2
.recs
[m
->i2
]->ptr
;
207 t2
.size
= xe2
->xdf2
.recs
[m
->i2
+ m
->chg2
- 1]->ptr
208 + xe2
->xdf2
.recs
[m
->i2
+ m
->chg2
- 1]->size
- t2
.ptr
;
209 if (xdl_do_diff(&t1
, &t2
, xpp
, &xe
) < 0)
211 if (xdl_change_compact(&xe
.xdf1
, &xe
.xdf2
, xpp
->flags
) < 0 ||
212 xdl_change_compact(&xe
.xdf2
, &xe
.xdf1
, xpp
->flags
) < 0 ||
213 xdl_build_script(&xe
, &xscr
) < 0) {
218 /* If this happens, the changes are identical. */
224 m
->i1
= xscr
->i1
+ i1
;
225 m
->chg1
= xscr
->chg1
;
226 m
->i2
= xscr
->i2
+ i2
;
227 m
->chg2
= xscr
->chg2
;
229 xdmerge_t
*m2
= xdl_malloc(sizeof(xdmerge_t
));
240 m
->i1
= xscr
->i1
+ i1
;
241 m
->chg1
= xscr
->chg1
;
242 m
->i2
= xscr
->i2
+ i2
;
243 m
->chg2
= xscr
->chg2
;
252 * This function merges m and m->next, marking everything between those hunks
253 * as conflicting, too.
255 static void xdl_merge_two_conflicts(xdmerge_t
*m
)
257 xdmerge_t
*next_m
= m
->next
;
258 m
->chg1
= next_m
->i1
+ next_m
->chg1
- m
->i1
;
259 m
->chg2
= next_m
->i2
+ next_m
->chg2
- m
->i2
;
260 m
->next
= next_m
->next
;
265 * If there are less than 3 non-conflicting lines between conflicts,
266 * it appears simpler -- because it takes up less (or as many) lines --
267 * if the lines are moved into the conflicts.
269 static int xdl_simplify_non_conflicts(xdfenv_t
*xe1
, xdmerge_t
*m
)
276 xdmerge_t
*next_m
= m
->next
;
282 begin
= m
->i1
+ m
->chg1
;
285 if (m
->mode
!= 0 || next_m
->mode
!= 0 || end
- begin
> 3)
289 xdl_merge_two_conflicts(m
);
295 * level == 0: mark all overlapping changes as conflict
296 * level == 1: mark overlapping changes as conflict only if not identical
297 * level == 2: analyze non-identical changes for minimal conflict set
299 * returns < 0 on error, == 0 for no conflicts, else number of conflicts
301 static int xdl_do_merge(xdfenv_t
*xe1
, xdchange_t
*xscr1
, const char *name1
,
302 xdfenv_t
*xe2
, xdchange_t
*xscr2
, const char *name2
,
303 int level
, xpparam_t
const *xpp
, mmbuffer_t
*result
) {
304 xdmerge_t
*changes
, *c
;
305 int i1
, i2
, chg1
, chg2
;
309 while (xscr1
&& xscr2
) {
312 if (xscr1
->i1
+ xscr1
->chg1
< xscr2
->i1
) {
314 i2
= xscr2
->i2
- xscr2
->i1
+ xscr1
->i1
;
317 if (xdl_append_merge(&c
, 1, i1
, chg1
, i2
, chg2
)) {
318 xdl_cleanup_merge(changes
);
324 if (xscr2
->i1
+ xscr2
->chg1
< xscr1
->i1
) {
325 i1
= xscr1
->i2
- xscr1
->i1
+ xscr2
->i1
;
329 if (xdl_append_merge(&c
, 2, i1
, chg1
, i2
, chg2
)) {
330 xdl_cleanup_merge(changes
);
336 if (level
< 1 || xscr1
->i1
!= xscr2
->i1
||
337 xscr1
->chg1
!= xscr2
->chg1
||
338 xscr1
->chg2
!= xscr2
->chg2
||
339 xdl_merge_cmp_lines(xe1
, xscr1
->i2
,
341 xscr1
->chg2
, xpp
->flags
)) {
343 int off
= xscr1
->i1
- xscr2
->i1
;
344 int ffo
= off
+ xscr1
->chg1
- xscr2
->chg1
;
352 chg1
= xscr1
->i2
+ xscr1
->chg2
- i1
;
353 chg2
= xscr2
->i2
+ xscr2
->chg2
- i2
;
358 if (xdl_append_merge(&c
, 0, i1
, chg1
, i2
, chg2
)) {
359 xdl_cleanup_merge(changes
);
364 i1
= xscr1
->i1
+ xscr1
->chg1
;
365 i2
= xscr2
->i1
+ xscr2
->chg1
;
376 i2
= xscr1
->i1
+ xe2
->xdf2
.nrec
- xe2
->xdf1
.nrec
;
379 if (xdl_append_merge(&c
, 1, i1
, chg1
, i2
, chg2
)) {
380 xdl_cleanup_merge(changes
);
388 i1
= xscr2
->i1
+ xe1
->xdf2
.nrec
- xe1
->xdf1
.nrec
;
392 if (xdl_append_merge(&c
, 2, i1
, chg1
, i2
, chg2
)) {
393 xdl_cleanup_merge(changes
);
400 /* refine conflicts */
402 (xdl_refine_conflicts(xe1
, xe2
, changes
, xpp
) < 0 ||
403 xdl_simplify_non_conflicts(xe1
, changes
) < 0)) {
404 xdl_cleanup_merge(changes
);
409 int size
= xdl_fill_merge_buffer(xe1
, name1
, xe2
, name2
,
411 result
->ptr
= xdl_malloc(size
);
413 xdl_cleanup_merge(changes
);
417 xdl_fill_merge_buffer(xe1
, name1
, xe2
, name2
, changes
,
420 return xdl_cleanup_merge(changes
);
423 int xdl_merge(mmfile_t
*orig
, mmfile_t
*mf1
, const char *name1
,
424 mmfile_t
*mf2
, const char *name2
,
425 xpparam_t
const *xpp
, int level
, mmbuffer_t
*result
) {
426 xdchange_t
*xscr1
, *xscr2
;
433 if (xdl_do_diff(orig
, mf1
, xpp
, &xe1
) < 0 ||
434 xdl_do_diff(orig
, mf2
, xpp
, &xe2
) < 0) {
437 if (xdl_change_compact(&xe1
.xdf1
, &xe1
.xdf2
, xpp
->flags
) < 0 ||
438 xdl_change_compact(&xe1
.xdf2
, &xe1
.xdf1
, xpp
->flags
) < 0 ||
439 xdl_build_script(&xe1
, &xscr1
) < 0) {
443 if (xdl_change_compact(&xe2
.xdf1
, &xe2
.xdf2
, xpp
->flags
) < 0 ||
444 xdl_change_compact(&xe2
.xdf2
, &xe2
.xdf1
, xpp
->flags
) < 0 ||
445 xdl_build_script(&xe2
, &xscr2
) < 0) {
450 if (xscr1
|| xscr2
) {
452 result
->ptr
= xdl_malloc(mf2
->size
);
453 memcpy(result
->ptr
, mf2
->ptr
, mf2
->size
);
454 result
->size
= mf2
->size
;
456 result
->ptr
= xdl_malloc(mf1
->size
);
457 memcpy(result
->ptr
, mf1
->ptr
, mf1
->size
);
458 result
->size
= mf1
->size
;
460 status
= xdl_do_merge(&xe1
, xscr1
, name1
,
464 xdl_free_script(xscr1
);
465 xdl_free_script(xscr2
);