Merge branch 'pb/cvsimport'
[gitweb.git] / xdiff / xmerge.c
index 294450b89916180ab603d566e7d3f281773a3b92..82b3573e7ada8c6df13ac24a78650b80af91ea73 100644 (file)
@@ -166,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,
@@ -213,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;
@@ -245,10 +248,76 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
        return 0;
 }
 
+static int line_contains_alnum(const char *ptr, long size)
+{
+       while (size--)
+               if (isalnum(*(ptr++)))
+                       return 1;
+       return 0;
+}
+
+static int lines_contain_alnum(xdfenv_t *xe, int i, int chg)
+{
+       for (; chg; chg--, i++)
+               if (line_contains_alnum(xe->xdf2.recs[i]->ptr,
+                               xe->xdf2.recs[i]->size))
+                       return 1;
+       return 0;
+}
+
+/*
+ * This function merges m and m->next, marking everything between those hunks
+ * as conflicting, too.
+ */
+static void xdl_merge_two_conflicts(xdmerge_t *m)
+{
+       xdmerge_t *next_m = m->next;
+       m->chg1 = next_m->i1 + next_m->chg1 - m->i1;
+       m->chg2 = next_m->i2 + next_m->chg2 - m->i2;
+       m->next = next_m->next;
+       free(next_m);
+}
+
+/*
+ * If there are less than 3 non-conflicting lines between conflicts,
+ * it appears simpler -- because it takes up less (or as many) lines --
+ * if the lines are moved into the conflicts.
+ */
+static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m,
+                                     int simplify_if_no_alnum)
+{
+       int result = 0;
+
+       if (!m)
+               return result;
+       for (;;) {
+               xdmerge_t *next_m = m->next;
+               int begin, end;
+
+               if (!next_m)
+                       return result;
+
+               begin = m->i1 + m->chg1;
+               end = next_m->i1;
+
+               if (m->mode != 0 || next_m->mode != 0 ||
+                   (end - begin > 3 &&
+                    (!simplify_if_no_alnum ||
+                     lines_contain_alnum(xe1, begin, end - begin)))) {
+                       m = next_m;
+               } else {
+                       result++;
+                       xdl_merge_two_conflicts(m);
+               }
+       }
+}
+
 /*
  * level == 0: mark all overlapping changes as conflict
  * level == 1: mark overlapping changes as conflict only if not identical
  * level == 2: analyze non-identical changes for minimal conflict set
+ * level == 3: analyze non-identical changes for minimal conflict set, but
+ *             treat hunks not containing any letter or number as conflicting
  *
  * returns < 0 on error, == 0 for no conflicts, else number of conflicts
  */
@@ -352,7 +421,9 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        if (!changes)
                changes = c;
        /* refine conflicts */
-       if (level > 1 && xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0) {
+       if (level > 1 &&
+           (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 ||
+            xdl_simplify_non_conflicts(xe1, changes, level > 2) < 0)) {
                xdl_cleanup_merge(changes);
                return -1;
        }