Merge branch 'jc/read-tree-df' (early part)
[gitweb.git] / xdiff / xmerge.c
index 7b85aa59062e5f6e617f62ef1003c4f9dd13de18..b83b3348cc3aab66b13cb565a0a0fabaef4b689b 100644 (file)
@@ -38,8 +38,9 @@ static int xdl_append_merge(xdmerge_t **merge, int mode,
                long i1, long chg1, long i2, long chg2)
 {
        xdmerge_t *m = *merge;
-       if (m && mode == m->mode &&
-                       (i1 == m->i1 + m->chg1 || i2 == m->i2 + m->chg2)) {
+       if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) {
+               if (mode != m->mode)
+                       m->mode = 0;
                m->chg1 = i1 + chg1 - m->i1;
                m->chg2 = i2 + chg2 - m->i2;
        } else {
@@ -165,6 +166,8 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
                        size += xdl_recs_copy(xe2, m->i2 - m->i1 + i1,
                                        m->i1 + m->chg2 - i1, 0,
                                        dest ? dest + size : NULL);
+               else
+                       continue;
                i1 = m->i1 + m->chg1;
        }
        size += xdl_recs_copy(xe1, i1, xe1->xdf2.nrec - i1, 0,
@@ -189,16 +192,20 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
                if (m->mode)
                        continue;
 
+               /* no sense refining a conflict when one side is empty */
+               if (m->chg1 == 0 || m->chg2 == 0)
+                       continue;
+
                /*
                 * This probably does not work outside git, since
                 * we have a very simple mmfile structure.
                 */
                t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr;
-               t1.size = xe1->xdf2.recs[m->i1 + m->chg1]->ptr
-                       + xe1->xdf2.recs[m->i1 + m->chg1]->size - t1.ptr;
-               t2.ptr = (char *)xe2->xdf2.recs[m->i1]->ptr;
-               t2.size = xe2->xdf2.recs[m->i1 + m->chg1]->ptr
-                       + xe2->xdf2.recs[m->i1 + m->chg1]->size - t2.ptr;
+               t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr
+                       + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr;
+               t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr;
+               t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr
+                       + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr;
                if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0)
                        return -1;
                if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
@@ -208,9 +215,10 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
                        return -1;
                }
                if (!xscr) {
-                       /* If this happens, it's a bug. */
+                       /* If this happens, the changes are identical. */
                        xdl_free_env(&xe);
-                       return -2;
+                       m->mode = 4;
+                       continue;
                }
                x = xscr;
                m->i1 = xscr->i1 + i1;
@@ -313,22 +321,10 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
                i1 = xscr1->i1 + xscr1->chg1;
                i2 = xscr2->i1 + xscr2->chg1;
 
-               if (i1 > i2) {
-                       xscr1->chg1 -= i1 - i2;
-                       xscr1->i1 = i2;
-                       xscr1->i2 += xscr1->chg2;
-                       xscr1->chg2 = 0;
-                       xscr1 = xscr1->next;
-               } else if (i2 > i1) {
-                       xscr2->chg1 -= i2 - i1;
-                       xscr2->i1 = i1;
-                       xscr2->i2 += xscr2->chg2;
-                       xscr2->chg2 = 0;
+               if (i1 >= i2)
                        xscr2 = xscr2->next;
-               } else {
+               if (i2 >= i1)
                        xscr1 = xscr1->next;
-                       xscr2 = xscr2->next;
-               }
        }
        while (xscr1) {
                if (!changes)
@@ -384,6 +380,7 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
                xpparam_t const *xpp, int level, mmbuffer_t *result) {
        xdchange_t *xscr1, *xscr2;
        xdfenv_t xe1, xe2;
+       int status;
 
        result->ptr = NULL;
        result->size = 0;
@@ -404,6 +401,7 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
                xdl_free_env(&xe2);
                return -1;
        }
+       status = 0;
        if (xscr1 || xscr2) {
                if (!xscr1) {
                        result->ptr = xdl_malloc(mf2->size);
@@ -413,14 +411,10 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
                        result->ptr = xdl_malloc(mf1->size);
                        memcpy(result->ptr, mf1->ptr, mf1->size);
                        result->size = mf1->size;
-               } else if (xdl_do_merge(&xe1, xscr1, name1,
-                                       &xe2, xscr2, name2,
-                                       level, xpp, result) < 0) {
-                       xdl_free_script(xscr1);
-                       xdl_free_script(xscr2);
-                       xdl_free_env(&xe1);
-                       xdl_free_env(&xe2);
-                       return -1;
+               } else {
+                       status = xdl_do_merge(&xe1, xscr1, name1,
+                                             &xe2, xscr2, name2,
+                                             level, xpp, result);
                }
                xdl_free_script(xscr1);
                xdl_free_script(xscr2);
@@ -428,6 +422,5 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
        xdl_free_env(&xe1);
        xdl_free_env(&xe2);
 
-       return 0;
+       return status;
 }
-