Patchwork D2575: xdiff: add a bdiff hunk mode

login
register
mail settings
Submitter phabricator
Date March 3, 2018, 12:25 a.m.
Message ID <differential-rev-PHID-DREV-esj4ci27xfslcdx4xu6c-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/28757/
State Superseded
Headers show

Comments

phabricator - March 3, 2018, 12:25 a.m.
ryanmce created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  xdiff generated hunks for the differences (ex. questionmarks in the `@@ -?,?
  +?,? @@` part from `diff --git` output). However, bdiff generates matched
  hunks instead.
  
  This patch adds a `XDL_EMIT_BDIFFHUNK` flag used by the output function
  `xdl_call_hunk_func`.  Once set, xdiff will generate bdiff-like hunks
  instead.
  
  Note that since `bdiff('', '')` returns `[(0, 0, 0, 0)]`, the shortcut path
  `if (xscr)` is removed. I have checked functions called with `xscr` argument
  (`xdl_mark_ignorable`, `xdl_call_hunk_func`, `xdl_emit_diff`,
  `xdl_free_script`) work just fine with `xscr = NULL`.

TEST PLAN
  `cd contrib/xdiff; make` - still builds. Actual logic tested in the next
  patch.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2575

AFFECTED FILES
  mercurial/thirdparty/xdiff/xdiff.h
  mercurial/thirdparty/xdiff/xdiffi.c

CHANGE DETAILS




To: ryanmce, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/thirdparty/xdiff/xdiffi.c b/mercurial/thirdparty/xdiff/xdiffi.c
--- a/mercurial/thirdparty/xdiff/xdiffi.c
+++ b/mercurial/thirdparty/xdiff/xdiffi.c
@@ -975,15 +975,32 @@ 
 			      xdemitconf_t const *xecfg)
 {
 	xdchange_t *xch, *xche;
-
-	for (xch = xscr; xch; xch = xche->next) {
-		xche = xdl_get_hunk(&xch, xecfg);
-		if (!xch)
-			break;
-		if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
-				     xch->i2, xche->i2 + xche->chg2 - xch->i2,
-				     ecb->priv) < 0)
+	if ((xecfg->flags & XDL_EMIT_BDIFFHUNK) != 0) {
+		long i1 = 0, i2 = 0, n1 = xe->xdf1.nrec, n2 = xe->xdf2.nrec;
+		for (xch = xscr; xch; xch = xche->next) {
+			xche = xdl_get_hunk(&xch, xecfg);
+			if (!xch)
+				break;
+			if (xch->i1 > i1 || xch->i2 > i2) {
+				if (xecfg->hunk_func(i1, xch->i1, i2, xch->i2, ecb->priv) < 0)
+					return -1;
+			}
+			i1 = xche->i1 + xche->chg1;
+			i2 = xche->i2 + xche->chg2;
+		}
+		if (xecfg->hunk_func(i1, n1, i2, n2, ecb->priv) < 0)
 			return -1;
+	} else {
+		for (xch = xscr; xch; xch = xche->next) {
+			xche = xdl_get_hunk(&xch, xecfg);
+			if (!xch)
+				break;
+			if (xecfg->hunk_func(
+					xch->i1, xche->i1 + xche->chg1 - xch->i1,
+					xch->i2, xche->i2 + xche->chg2 - xch->i2,
+					ecb->priv) < 0)
+				return -1;
+		}
 	}
 	return 0;
 }
@@ -1026,18 +1043,15 @@ 
 		xdl_free_env(&xe);
 		return -1;
 	}
-	if (xscr) {
-		if (xpp->flags & XDF_IGNORE_BLANK_LINES)
-			xdl_mark_ignorable(xscr, &xe, xpp->flags);
 
-		if (ef(&xe, xscr, ecb, xecfg) < 0) {
-
-			xdl_free_script(xscr);
-			xdl_free_env(&xe);
-			return -1;
-		}
+	if (xpp->flags & XDF_IGNORE_BLANK_LINES)
+		xdl_mark_ignorable(xscr, &xe, xpp->flags);
+	if (ef(&xe, xscr, ecb, xecfg) < 0) {
 		xdl_free_script(xscr);
+		xdl_free_env(&xe);
+		return -1;
 	}
+	xdl_free_script(xscr);
 	xdl_free_env(&xe);
 
 	return 0;
diff --git a/mercurial/thirdparty/xdiff/xdiff.h b/mercurial/thirdparty/xdiff/xdiff.h
--- a/mercurial/thirdparty/xdiff/xdiff.h
+++ b/mercurial/thirdparty/xdiff/xdiff.h
@@ -48,6 +48,9 @@ 
 /* xdemitconf_t.flags */
 #define XDL_EMIT_FUNCNAMES (1 << 0)
 #define XDL_EMIT_FUNCCONTEXT (1 << 2)
+/* emit bdiff-style "matched" (a1, a2, b1, b2) hunks instead of "different"
+ * (a1, a2 - a1, b1, b2 - b1) hunks */
+#define XDL_EMIT_BDIFFHUNK (1 << 4)
 
 #define XDL_MMB_READONLY (1 << 0)