read_index_from: remove bogus errno assignments
[gitweb.git] / xdiff / xmerge.c
index 82b3573e7ada8c6df13ac24a78650b80af91ea73..9e13b25abc90350de4488276074dc282edc216b1 100644 (file)
@@ -28,19 +28,35 @@ typedef struct s_xdmerge {
         * 0 = conflict,
         * 1 = no conflict, take first,
         * 2 = no conflict, take second.
+        * 3 = no conflict, take both.
         */
        int mode;
+       /*
+        * These point at the respective postimages.  E.g. <i1,chg1> is
+        * how side #1 wants to change the common ancestor; if there is no
+        * overlap, lines before i1 in the postimage of side #1 appear
+        * in the merge result as a region touched by neither side.
+        */
        long i1, i2;
        long chg1, chg2;
+       /*
+        * These point at the preimage; of course there is just one
+        * preimage, that is from the shared common ancestor.
+        */
+       long i0;
+       long chg0;
 } xdmerge_t;
 
 static int xdl_append_merge(xdmerge_t **merge, int mode,
-               long i1, long chg1, long i2, long chg2)
+                           long i0, long chg0,
+                           long i1, long chg1,
+                           long i2, long chg2)
 {
        xdmerge_t *m = *merge;
        if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) {
                if (mode != m->mode)
                        m->mode = 0;
+               m->chg0 = i0 + chg0 - m->i0;
                m->chg1 = i1 + chg1 - m->i1;
                m->chg2 = i2 + chg2 - m->i2;
        } else {
@@ -49,6 +65,8 @@ static int xdl_append_merge(xdmerge_t **merge, int mode,
                        return -1;
                m->next = NULL;
                m->mode = mode;
+               m->i0 = i0;
+               m->chg0 = chg0;
                m->i1 = i1;
                m->chg1 = chg1;
                m->i2 = i2;
@@ -91,11 +109,13 @@ static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
        return 0;
 }
 
-static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
+static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest)
 {
-       xrecord_t **recs = xe->xdf2.recs + i;
+       xrecord_t **recs;
        int size = 0;
 
+       recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i;
+
        if (count < 1)
                return 0;
 
@@ -113,65 +133,130 @@ static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
        return size;
 }
 
-static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
-               xdfenv_t *xe2, const char *name2, xdmerge_t *m, char *dest)
+static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
+{
+       return xdl_recs_copy_0(0, xe, i, count, add_nl, dest);
+}
+
+static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
+{
+       return xdl_recs_copy_0(1, xe, i, count, add_nl, dest);
+}
+
+static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
+                             xdfenv_t *xe2, const char *name2,
+                             const char *name3,
+                             int size, int i, int style,
+                             xdmerge_t *m, char *dest, int marker_size)
 {
-       const int marker_size = 7;
        int marker1_size = (name1 ? strlen(name1) + 1 : 0);
        int marker2_size = (name2 ? strlen(name2) + 1 : 0);
-       int conflict_marker_size = 3 * (marker_size + 1)
-               + marker1_size + marker2_size;
-       int size, i1, j;
-
-       for (size = i1 = 0; m; m = m->next) {
-               if (m->mode == 0) {
-                       size += xdl_recs_copy(xe1, i1, m->i1 - i1, 0,
-                                       dest ? dest + size : NULL);
-                       if (dest) {
-                               for (j = 0; j < marker_size; j++)
-                                       dest[size++] = '<';
-                               if (marker1_size) {
-                                       dest[size] = ' ';
-                                       memcpy(dest + size + 1, name1,
-                                                       marker1_size - 1);
-                                       size += marker1_size;
-                               }
-                               dest[size++] = '\n';
-                       } else
-                               size += conflict_marker_size;
-                       size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
-                                       dest ? dest + size : NULL);
-                       if (dest) {
-                               for (j = 0; j < marker_size; j++)
-                                       dest[size++] = '=';
-                               dest[size++] = '\n';
-                       }
-                       size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
-                                       dest ? dest + size : NULL);
-                       if (dest) {
-                               for (j = 0; j < marker_size; j++)
-                                       dest[size++] = '>';
-                               if (marker2_size) {
-                                       dest[size] = ' ';
-                                       memcpy(dest + size + 1, name2,
-                                                       marker2_size - 1);
-                                       size += marker2_size;
-                               }
-                               dest[size++] = '\n';
+       int marker3_size = (name3 ? strlen(name3) + 1 : 0);
+
+       if (marker_size <= 0)
+               marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+
+       /* Before conflicting part */
+       size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
+                             dest ? dest + size : NULL);
+
+       if (!dest) {
+               size += marker_size + 1 + marker1_size;
+       } else {
+               memset(dest + size, '<', marker_size);
+               size += marker_size;
+               if (marker1_size) {
+                       dest[size] = ' ';
+                       memcpy(dest + size + 1, name1, marker1_size - 1);
+                       size += marker1_size;
+               }
+               dest[size++] = '\n';
+       }
+
+       /* Postimage from side #1 */
+       size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
+                             dest ? dest + size : NULL);
+
+       if (style == XDL_MERGE_DIFF3) {
+               /* Shared preimage */
+               if (!dest) {
+                       size += marker_size + 1 + marker3_size;
+               } else {
+                       memset(dest + size, '|', marker_size);
+                       size += marker_size;
+                       if (marker3_size) {
+                               dest[size] = ' ';
+                               memcpy(dest + size + 1, name3, marker3_size - 1);
+                               size += marker3_size;
                        }
-               } else if (m->mode == 1)
-                       size += xdl_recs_copy(xe1, i1, m->i1 + m->chg1 - i1, 0,
-                                       dest ? dest + size : NULL);
-               else if (m->mode == 2)
-                       size += xdl_recs_copy(xe2, m->i2 - m->i1 + i1,
-                                       m->i1 + m->chg2 - i1, 0,
-                                       dest ? dest + size : NULL);
-               else
+                       dest[size++] = '\n';
+               }
+               size += xdl_orig_copy(xe1, m->i0, m->chg0, 1,
+                                     dest ? dest + size : NULL);
+       }
+
+       if (!dest) {
+               size += marker_size + 1;
+       } else {
+               memset(dest + size, '=', marker_size);
+               size += marker_size;
+               dest[size++] = '\n';
+       }
+
+       /* Postimage from side #2 */
+       size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
+                             dest ? dest + size : NULL);
+       if (!dest) {
+               size += marker_size + 1 + marker2_size;
+       } else {
+               memset(dest + size, '>', marker_size);
+               size += marker_size;
+               if (marker2_size) {
+                       dest[size] = ' ';
+                       memcpy(dest + size + 1, name2, marker2_size - 1);
+                       size += marker2_size;
+               }
+               dest[size++] = '\n';
+       }
+       return size;
+}
+
+static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
+                                xdfenv_t *xe2, const char *name2,
+                                const char *ancestor_name,
+                                int favor,
+                                xdmerge_t *m, char *dest, int style,
+                                int marker_size)
+{
+       int size, i;
+
+       for (size = i = 0; m; m = m->next) {
+               if (favor && !m->mode)
+                       m->mode = favor;
+
+               if (m->mode == 0)
+                       size = fill_conflict_hunk(xe1, name1, xe2, name2,
+                                                 ancestor_name,
+                                                 size, i, style, m, dest,
+                                                 marker_size);
+               else if (m->mode & 3) {
+                       /* Before conflicting part */
+                       size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
+                                             dest ? dest + size : NULL);
+                       /* Postimage from side #1 */
+                       if (m->mode & 1)
+                               size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
+                                                     dest ? dest + size : NULL);
+                       /* Postimage from side #2 */
+                       if (m->mode & 2)
+                               size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
+                                                     dest ? dest + size : NULL);
+               } else
                        continue;
-               i1 = m->i1 + m->chg1;
+               i = m->i1 + m->chg1;
        }
-       size += xdl_recs_copy(xe1, i1, xe1->xdf2.nrec - i1, 0,
-                       dest ? dest + size : NULL);
+       size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0,
+                             dest ? dest + size : NULL);
        return size;
 }
 
@@ -251,7 +336,7 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
 static int line_contains_alnum(const char *ptr, long size)
 {
        while (size--)
-               if (isalnum(*(ptr++)))
+               if (isalnum((unsigned char)*(ptr++)))
                        return 1;
        return 0;
 }
@@ -321,11 +406,28 @@ static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m,
  *
  * returns < 0 on error, == 0 for no conflicts, else number of conflicts
  */
-static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
-               xdfenv_t *xe2, xdchange_t *xscr2, const char *name2,
-               int level, xpparam_t const *xpp, mmbuffer_t *result) {
+static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
+               xdfenv_t *xe2, xdchange_t *xscr2,
+               xmparam_t const *xmp, mmbuffer_t *result)
+{
        xdmerge_t *changes, *c;
-       int i1, i2, chg1, chg2;
+       xpparam_t const *xpp = &xmp->xpp;
+       const char *const ancestor_name = xmp->ancestor;
+       const char *const name1 = xmp->file1;
+       const char *const name2 = xmp->file2;
+       int i0, i1, i2, chg0, chg1, chg2;
+       int level = xmp->level;
+       int style = xmp->style;
+       int favor = xmp->favor;
+
+       if (style == XDL_MERGE_DIFF3) {
+               /*
+                * "diff3 -m" output does not make sense for anything
+                * more aggressive than XDL_MERGE_EAGER.
+                */
+               if (XDL_MERGE_EAGER < level)
+                       level = XDL_MERGE_EAGER;
+       }
 
        c = changes = NULL;
 
@@ -333,11 +435,14 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
                if (!changes)
                        changes = c;
                if (xscr1->i1 + xscr1->chg1 < xscr2->i1) {
+                       i0 = xscr1->i1;
                        i1 = xscr1->i2;
                        i2 = xscr2->i2 - xscr2->i1 + xscr1->i1;
+                       chg0 = xscr1->chg1;
                        chg1 = xscr1->chg2;
                        chg2 = xscr1->chg1;
-                       if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
+                       if (xdl_append_merge(&c, 1,
+                                            i0, chg0, i1, chg1, i2, chg2)) {
                                xdl_cleanup_merge(changes);
                                return -1;
                        }
@@ -345,18 +450,21 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
                        continue;
                }
                if (xscr2->i1 + xscr2->chg1 < xscr1->i1) {
+                       i0 = xscr2->i1;
                        i1 = xscr1->i2 - xscr1->i1 + xscr2->i1;
                        i2 = xscr2->i2;
+                       chg0 = xscr2->chg1;
                        chg1 = xscr2->chg1;
                        chg2 = xscr2->chg2;
-                       if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
+                       if (xdl_append_merge(&c, 2,
+                                            i0, chg0, i1, chg1, i2, chg2)) {
                                xdl_cleanup_merge(changes);
                                return -1;
                        }
                        xscr2 = xscr2->next;
                        continue;
                }
-               if (level < 1 || xscr1->i1 != xscr2->i1 ||
+               if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 ||
                                xscr1->chg1 != xscr2->chg1 ||
                                xscr1->chg2 != xscr2->chg2 ||
                                xdl_merge_cmp_lines(xe1, xscr1->i2,
@@ -366,19 +474,25 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
                        int off = xscr1->i1 - xscr2->i1;
                        int ffo = off + xscr1->chg1 - xscr2->chg1;
 
+                       i0 = xscr1->i1;
                        i1 = xscr1->i2;
                        i2 = xscr2->i2;
-                       if (off > 0)
+                       if (off > 0) {
+                               i0 -= off;
                                i1 -= off;
+                       }
                        else
                                i2 += off;
+                       chg0 = xscr1->i1 + xscr1->chg1 - i0;
                        chg1 = xscr1->i2 + xscr1->chg2 - i1;
                        chg2 = xscr2->i2 + xscr2->chg2 - i2;
-                       if (ffo > 0)
-                               chg2 += ffo;
-                       else
+                       if (ffo < 0) {
+                               chg0 -= ffo;
                                chg1 -= ffo;
-                       if (xdl_append_merge(&c, 0, i1, chg1, i2, chg2)) {
+                       } else
+                               chg2 += ffo;
+                       if (xdl_append_merge(&c, 0,
+                                            i0, chg0, i1, chg1, i2, chg2)) {
                                xdl_cleanup_merge(changes);
                                return -1;
                        }
@@ -395,11 +509,14 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        while (xscr1) {
                if (!changes)
                        changes = c;
+               i0 = xscr1->i1;
                i1 = xscr1->i2;
                i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec;
+               chg0 = xscr1->chg1;
                chg1 = xscr1->chg2;
                chg2 = xscr1->chg1;
-               if (xdl_append_merge(&c, 1, i1, chg1, i2, chg2)) {
+               if (xdl_append_merge(&c, 1,
+                                    i0, chg0, i1, chg1, i2, chg2)) {
                        xdl_cleanup_merge(changes);
                        return -1;
                }
@@ -408,11 +525,14 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        while (xscr2) {
                if (!changes)
                        changes = c;
+               i0 = xscr2->i1;
                i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec;
                i2 = xscr2->i2;
+               chg0 = xscr2->chg1;
                chg1 = xscr2->chg1;
                chg2 = xscr2->chg2;
-               if (xdl_append_merge(&c, 2, i1, chg1, i2, chg2)) {
+               if (xdl_append_merge(&c, 2,
+                                    i0, chg0, i1, chg1, i2, chg2)) {
                        xdl_cleanup_merge(changes);
                        return -1;
                }
@@ -421,34 +541,40 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        if (!changes)
                changes = c;
        /* refine conflicts */
-       if (level > 1 &&
+       if (XDL_MERGE_ZEALOUS <= level &&
            (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 ||
-            xdl_simplify_non_conflicts(xe1, changes, level > 2) < 0)) {
+            xdl_simplify_non_conflicts(xe1, changes,
+                                       XDL_MERGE_ZEALOUS < level) < 0)) {
                xdl_cleanup_merge(changes);
                return -1;
        }
        /* output */
        if (result) {
+               int marker_size = xmp->marker_size;
                int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
-                       changes, NULL);
+                                                ancestor_name,
+                                                favor, changes, NULL, style,
+                                                marker_size);
                result->ptr = xdl_malloc(size);
                if (!result->ptr) {
                        xdl_cleanup_merge(changes);
                        return -1;
                }
                result->size = size;
-               xdl_fill_merge_buffer(xe1, name1, xe2, name2, changes,
-                               result->ptr);
+               xdl_fill_merge_buffer(xe1, name1, xe2, name2,
+                                     ancestor_name, favor, changes,
+                                     result->ptr, style, marker_size);
        }
        return xdl_cleanup_merge(changes);
 }
 
-int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
-               mmfile_t *mf2, const char *name2,
-               xpparam_t const *xpp, int level, mmbuffer_t *result) {
+int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
+               xmparam_t const *xmp, mmbuffer_t *result)
+{
        xdchange_t *xscr1, *xscr2;
        xdfenv_t xe1, xe2;
        int status;
+       xpparam_t const *xpp = &xmp->xpp;
 
        result->ptr = NULL;
        result->size = 0;
@@ -470,23 +596,22 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
                return -1;
        }
        status = 0;
-       if (xscr1 || xscr2) {
-               if (!xscr1) {
-                       result->ptr = xdl_malloc(mf2->size);
-                       memcpy(result->ptr, mf2->ptr, mf2->size);
-                       result->size = mf2->size;
-               } else if (!xscr2) {
-                       result->ptr = xdl_malloc(mf1->size);
-                       memcpy(result->ptr, mf1->ptr, mf1->size);
-                       result->size = mf1->size;
-               } else {
-                       status = xdl_do_merge(&xe1, xscr1, name1,
-                                             &xe2, xscr2, name2,
-                                             level, xpp, result);
-               }
-               xdl_free_script(xscr1);
-               xdl_free_script(xscr2);
+       if (!xscr1) {
+               result->ptr = xdl_malloc(mf2->size);
+               memcpy(result->ptr, mf2->ptr, mf2->size);
+               result->size = mf2->size;
+       } else if (!xscr2) {
+               result->ptr = xdl_malloc(mf1->size);
+               memcpy(result->ptr, mf1->ptr, mf1->size);
+               result->size = mf1->size;
+       } else {
+               status = xdl_do_merge(&xe1, xscr1,
+                                     &xe2, xscr2,
+                                     xmp, result);
        }
+       xdl_free_script(xscr1);
+       xdl_free_script(xscr2);
+
        xdl_free_env(&xe1);
        xdl_free_env(&xe2);