From: "Alessio Attilio via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
Alessio Attilio <attilio.alessio@protonmail.com>,
Alessio Attilio <attilio.alessio@protonmail.com>
Subject: [PATCH 1/4] xdiff: support transformed records in merge matching
Date: Mon, 30 Nov 2026 00:01:22 +0000 [thread overview]
Message-ID: <279ec378886f3ffce3690f7fcd3a8d5e3916d62a.1795996885.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2511.git.git.1795996885.gitgitgadget@gmail.com>
From: Alessio Attilio <attilio.alessio@protonmail.com>
The merge backend compares lines from both sides before deciding whether
changes overlap. Whitespace fixes can make otherwise identical lines
differ, so let xdiff compare transformed records while retaining the
original records for output.
Add an optional line transformation to xdiff's record preparation and use
the transformed bytes for hashing and matching. Keeping transformation
separate from the source records lets callers normalize only for matching
without changing unchanged text in the result.
Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
xdiff/xdiff.h | 4 ++++
xdiff/xmerge.c | 16 ++++++--------
xdiff/xprepare.c | 57 +++++++++++++++++++++++++++++++++++++++---------
xdiff/xtypes.h | 2 ++
xdiff/xutils.c | 14 ++++++++++++
xdiff/xutils.h | 2 ++
6 files changed, 76 insertions(+), 19 deletions(-)
diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h
index 8fa513fc4e..07c15c630e 100644
--- a/xdiff/xdiff.h
+++ b/xdiff/xdiff.h
@@ -73,6 +73,8 @@ typedef struct s_mmfile {
size_t size;
} mmfile_t;
+typedef int (*xdl_line_transform_fn)(const char *, long, int, char **, long *, void *);
+
typedef struct s_xpparam {
unsigned long flags;
@@ -83,6 +85,8 @@ typedef struct s_xpparam {
/* See Documentation/diff-options.adoc. */
char **anchors;
size_t anchors_nr;
+ xdl_line_transform_fn line_transform;
+ void *line_transform_data;
} xpparam_t;
typedef struct s_xdemitcb {
diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c
index 5a78b88fad..0e335e2658 100644
--- a/xdiff/xmerge.c
+++ b/xdiff/xmerge.c
@@ -94,15 +94,14 @@ static int xdl_cleanup_merge(xdmerge_t *c)
}
static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
- int line_count, long flags)
+ int line_count, xpparam_t const *xpp)
{
int i;
xrecord_t *rec1 = xe1->xdf2.recs + i1;
xrecord_t *rec2 = xe2->xdf2.recs + i2;
for (i = 0; i < line_count; i++) {
- int result = xdl_recmatch((const char *)rec1[i].ptr, (long)rec1[i].size,
- (const char *)rec2[i].ptr, (long)rec2[i].size, flags);
+ int result = xdl_recmatch_records(&rec1[i], &rec2[i], xpp);
if (!result)
return -1;
}
@@ -323,10 +322,9 @@ static size_t xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
return size;
}
-static int recmatch(xrecord_t *rec1, xrecord_t *rec2, unsigned long flags)
+static int recmatch(xrecord_t *rec1, xrecord_t *rec2, xpparam_t const *xpp)
{
- return xdl_recmatch((const char *)rec1->ptr, (long)rec1->size,
- (const char *)rec2->ptr, (long)rec2->size, flags);
+ return xdl_recmatch_records(rec1, rec2, xpp);
}
/*
@@ -342,7 +340,7 @@ static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t
continue;
while(m->chg1 && m->chg2 &&
- recmatch(&rec1[m->i1], &rec2[m->i2], xpp->flags)) {
+ recmatch(&rec1[m->i1], &rec2[m->i2], xpp)) {
m->chg1--;
m->chg2--;
m->i1++;
@@ -350,7 +348,7 @@ static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t
}
while (m->chg1 && m->chg2 &&
recmatch(&rec1[m->i1 + m->chg1 - 1],
- &rec2[m->i2 + m->chg2 - 1], xpp->flags)) {
+ &rec2[m->i2 + m->chg2 - 1], xpp)) {
m->chg1--;
m->chg2--;
}
@@ -581,7 +579,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
xscr1->chg2 != xscr2->chg2 ||
xdl_merge_cmp_lines(xe1, xscr1->i2,
xe2, xscr2->i2,
- xscr1->chg2, xpp->flags)) {
+ xscr1->chg2, xpp)) {
/* conflict */
int off = xscr1->i1 - xscr2->i1;
int ffo = off + xscr1->chg1 - xscr2->chg1;
diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c
index 11bada2608..3d6df505c3 100644
--- a/xdiff/xprepare.c
+++ b/xdiff/xprepare.c
@@ -56,8 +56,9 @@ typedef struct s_xdlclassifier {
-static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
- cf->flags = flags;
+static int xdl_init_classifier(xdlclassifier_t *cf, long size,
+ xpparam_t const *xpp) {
+ cf->flags = xpp->flags;
cf->hbits = xdl_hashbits((unsigned int) size);
cf->hsize = 1 << cf->hbits;
@@ -98,12 +99,15 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t
uint64_t line_hash) {
size_t hi;
xdlclass_t *rcrec;
+ const uint8_t *ptr = rec->match_ptr ? (uint8_t *)rec->match_ptr : rec->ptr;
+ size_t size = rec->match_ptr ? rec->match_size : rec->size;
hi = XDL_HASHLONG(line_hash, cf->hbits);
for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
if (rcrec->line_hash == line_hash &&
- xdl_recmatch((const char *)rcrec->ptr, (long)rcrec->size,
- (const char *)rec->ptr, (long)rec->size, cf->flags))
+ xdl_recmatch((const char *)rcrec->ptr,
+ (long)rcrec->size, (const char *)ptr,
+ (long)size, cf->flags))
break;
if (!rcrec) {
@@ -116,8 +120,8 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t
return -1;
cf->rcrecs[rcrec->idx] = rcrec;
rcrec->line_hash = line_hash;
- rcrec->ptr = rec->ptr;
- rcrec->size = rec->size;
+ rcrec->ptr = ptr;
+ rcrec->size = size;
rcrec->len1 = rcrec->len2 = 0;
rcrec->next = cf->rchash[hi];
cf->rchash[hi] = rcrec;
@@ -133,6 +137,9 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t
static void xdl_free_ctx(xdfile_t *xdf)
{
+ size_t i;
+ for (i = 0; i < xdf->nrec; i++)
+ free(xdf->recs[i].match_ptr);
xdl_free(xdf->reference_index);
xdl_free(xdf->changed - 1);
xdl_free(xdf->recs);
@@ -142,13 +149,14 @@ static void xdl_free_ctx(xdfile_t *xdf)
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
xdlclassifier_t *cf, xdfile_t *xdf) {
long bsize;
- uint64_t hav;
+ uint64_t hav = 0;
uint8_t const *blk, *cur, *top, *prev;
xrecord_t *crec;
xdf->reference_index = NULL;
xdf->changed = NULL;
xdf->recs = NULL;
+ xdf->nrec = 0;
if (!XDL_ALLOC_ARRAY(xdf->recs, narec))
goto abort;
@@ -156,13 +164,42 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
xdf->nrec = 0;
if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
for (top = blk + bsize; cur < top; ) {
+ long line_size;
+ int is_eof;
+ char *transformed = NULL;
+ long transformed_size = 0;
+ uint8_t const *transformed_ptr;
+
prev = cur;
- hav = xdl_hash_record(&cur, top, xpp->flags);
+ if (xpp->line_transform) {
+ const uint8_t *line_end = memchr(cur, '\n', top - cur);
+ line_size = line_end ? line_end + 1 - cur : top - cur;
+ is_eof = cur + line_size == top;
+ cur += line_size;
+ } else {
+ hav = xdl_hash_record(&cur, top, xpp->flags);
+ line_size = cur - prev;
+ is_eof = cur == top;
+ }
if (XDL_ALLOC_GROW(xdf->recs, (long)xdf->nrec + 1, narec))
goto abort;
crec = &xdf->recs[xdf->nrec++];
crec->ptr = prev;
- crec->size = cur - prev;
+ crec->size = line_size;
+ crec->match_ptr = NULL;
+ crec->match_size = 0;
+ if (xpp->line_transform) {
+ if (xpp->line_transform((const char *)prev, line_size,
+ is_eof, &transformed,
+ &transformed_size,
+ xpp->line_transform_data) < 0)
+ goto abort;
+ crec->match_ptr = transformed;
+ crec->match_size = (size_t)transformed_size;
+ transformed_ptr = (uint8_t *)transformed;
+ hav = xdl_hash_record_verbatim(&transformed_ptr,
+ transformed_ptr + transformed_size);
+ }
if (xdl_classify_record(pass, cf, crec, hav) < 0)
goto abort;
}
@@ -442,7 +479,7 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
enl1 = xdl_guess_lines(mf1, sample) + 1;
enl2 = xdl_guess_lines(mf2, sample) + 1;
- if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
+ if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp) < 0)
return -1;
if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
diff --git a/xdiff/xtypes.h b/xdiff/xtypes.h
index 50aee779be..714153a86e 100644
--- a/xdiff/xtypes.h
+++ b/xdiff/xtypes.h
@@ -42,6 +42,8 @@ typedef struct s_xrecord {
uint8_t const *ptr;
size_t size;
size_t minimal_perfect_hash;
+ char *match_ptr;
+ size_t match_size;
} xrecord_t;
typedef struct s_xdfile {
diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index 4215f646c5..5b1c0edc3d 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -249,6 +249,20 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
return 1;
}
+int xdl_recmatch_records(xrecord_t *rec1, xrecord_t *rec2,
+ xpparam_t const *xpp)
+{
+ const char *line1 = rec1->match_ptr ? rec1->match_ptr :
+ (const char *)rec1->ptr;
+ const char *line2 = rec2->match_ptr ? rec2->match_ptr :
+ (const char *)rec2->ptr;
+ size_t size1 = rec1->match_ptr ? rec1->match_size : rec1->size;
+ size_t size2 = rec2->match_ptr ? rec2->match_size : rec2->size;
+
+ return xdl_recmatch(line1, (long)size1, line2, (long)size2,
+ xpp->flags);
+}
+
uint64_t xdl_hash_record_with_whitespace(uint8_t const **data,
uint8_t const *top, uint64_t flags) {
uint64_t ha = 5381;
diff --git a/xdiff/xutils.h b/xdiff/xutils.h
index 58f9d74cda..51217d99c6 100644
--- a/xdiff/xutils.h
+++ b/xdiff/xutils.h
@@ -34,6 +34,8 @@ void *xdl_cha_alloc(chastore_t *cha);
long xdl_guess_lines(mmfile_t *mf, long sample);
int xdl_blankline(const char *line, long size, long flags);
int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
+int xdl_recmatch_records(xrecord_t *rec1, xrecord_t *rec2,
+ xpparam_t const *xpp);
uint64_t xdl_hash_record_verbatim(uint8_t const **data, uint8_t const *top);
uint64_t xdl_hash_record_with_whitespace(uint8_t const **data, uint8_t const *top, uint64_t flags);
static inline uint64_t xdl_hash_record(uint8_t const **data, uint8_t const *top, uint64_t flags)
--
gitgitgadget
next prev parent reply other threads:[~2026-11-30 0:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 0:01 [PATCH 0/4] rebase: support --whitespace=fix with --interactive Alessio Attilio via GitGitGadget
2026-11-30 0:01 ` Alessio Attilio via GitGitGadget [this message]
2026-11-30 0:01 ` [PATCH 2/4] merge-ort: fix whitespace in replayed changes Alessio Attilio via GitGitGadget
2026-11-30 0:01 ` [PATCH 3/4] rebase: route whitespace fixes through the selected backend Alessio Attilio via GitGitGadget
2026-11-30 0:01 ` [PATCH 4/4] rebase: test whitespace fixes with the merge backend Alessio Attilio via GitGitGadget
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=279ec378886f3ffce3690f7fcd3a8d5e3916d62a.1795996885.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=attilio.alessio@protonmail.com \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox