summaryrefslogtreecommitdiffstats
path: root/xdiff
diff options
context:
space:
mode:
authorStefan Beller <sbeller@google.com>2016-04-19 08:21:30 -0700
committerJunio C Hamano <gitster@pobox.com>2016-04-19 10:53:34 -0700
commitd634d61ed6c5f19948937012b5e3a0ed2d631d3f (patch)
treef5c443c8ae8a4def49ffd7f147ccc82ab1420805 /xdiff
parent92e5b62fec0e9b647429e8d3736c571c434dd375 (diff)
downloadgit-d634d61ed6c5f19948937012b5e3a0ed2d631d3f.tar.gz
git-d634d61ed6c5f19948937012b5e3a0ed2d631d3f.tar.xz
xdiff: implement empty line chunk heuristic
In order to produce the smallest possible diff and combine several diff hunks together, we implement a heuristic from GNU Diff which moves diff hunks forward as far as possible when we find common context above and below a diff hunk. This sometimes produces less readable diffs when writing C, Shell, or other programming languages, ie: ... /* + * + * + */ + +/* ... instead of the more readable equivalent of ... +/* + * + * + */ + /* ... Implement the following heuristic to (optionally) produce the desired output. If there are diff chunks which can be shifted around, shift each hunk such that the last common empty line is below the chunk with the rest of the context above. This heuristic appears to resolve the above example and several other common issues without producing significantly weird results. However, as with any heuristic it is not really known whether this will always be more optimal. Thus, it can be disabled via diff.compactionHeuristic. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Jacob Keller <jacob.e.keller@intel.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'xdiff')
-rw-r--r--xdiff/xdiff.h2
-rw-r--r--xdiff/xdiffi.c26
2 files changed, 28 insertions, 0 deletions
diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h
index c0339919c..d1dbb2750 100644
--- a/xdiff/xdiff.h
+++ b/xdiff/xdiff.h
@@ -41,6 +41,8 @@ extern "C" {
#define XDF_IGNORE_BLANK_LINES (1 << 7)
+#define XDF_COMPACTION_HEURISTIC (1 << 8)
+
#define XDL_EMIT_FUNCNAMES (1 << 0)
#define XDL_EMIT_COMMON (1 << 1)
#define XDL_EMIT_FUNCCONTEXT (1 << 2)
diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c
index 748eeb991..b3c684887 100644
--- a/xdiff/xdiffi.c
+++ b/xdiff/xdiffi.c
@@ -400,6 +400,11 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1,
}
+static int is_blank_line(xrecord_t **recs, long ix, long flags)
+{
+ return xdl_blankline(recs[ix]->ptr, recs[ix]->size, flags);
+}
+
static int recs_match(xrecord_t **recs, long ixs, long ix, long flags)
{
return (recs[ixs]->ha == recs[ix]->ha &&
@@ -411,6 +416,7 @@ static int recs_match(xrecord_t **recs, long ixs, long ix, long flags)
int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
+ unsigned int blank_lines;
xrecord_t **recs = xdf->recs;
/*
@@ -444,6 +450,7 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
do {
grpsiz = ix - ixs;
+ blank_lines = 0;
/*
* If the line before the current change group, is equal to
@@ -478,6 +485,8 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
* the group.
*/
while (ix < nrec && recs_match(recs, ixs, ix, flags)) {
+ blank_lines += is_blank_line(recs, ix, flags);
+
rchg[ixs++] = 0;
rchg[ix++] = 1;
@@ -504,6 +513,23 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
rchg[--ix] = 0;
while (rchgo[--ixo]);
}
+
+ /*
+ * If a group can be moved back and forth, see if there is a
+ * blank line in the moving space. If there is a blank line,
+ * make sure the last blank line is the end of the group.
+ *
+ * As we already shifted the group forward as far as possible
+ * in the earlier loop, we need to shift it back only if at all.
+ */
+ if ((flags & XDF_COMPACTION_HEURISTIC) && blank_lines) {
+ while (ixs > 0 &&
+ !is_blank_line(recs, ix - 1, flags) &&
+ recs_match(recs, ixs - 1, ix - 1, flags)) {
+ rchg[--ixs] = 1;
+ rchg[--ix] = 0;
+ }
+ }
}
return 0;