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 2/4] merge-ort: fix whitespace in replayed changes
Date: Mon, 30 Nov 2026 00:01:23 +0000 [thread overview]
Message-ID: <5ddfdca00e646bdc87b0dd09ffbee2c9398f9b1e.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 apply backend passes --whitespace=fix to git apply, which fixes
introduced lines while applying each commit. The merge backend instead
replays commits through a three-way merge, so it needs to identify the
resulting changes and fix them after merging.
Use the path's whitespace rules to compare and repair changed lines in
the merge result. Leave lines inherited from our side untouched, and
handle files added by the replayed commit as having an empty base.
Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
merge-ll.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++-
merge-ll.h | 5 ++
merge-ort.c | 52 +++++++++++++++
merge-ort.h | 1 +
4 files changed, 239 insertions(+), 1 deletion(-)
diff --git a/merge-ll.c b/merge-ll.c
index 996a38113f..a47aa7cb24 100644
--- a/merge-ll.c
+++ b/merge-ll.c
@@ -18,6 +18,7 @@
#include "strbuf.h"
#include "gettext.h"
#include "tempfile.h"
+#include "ws.h"
struct ll_merge_driver;
@@ -101,6 +102,152 @@ static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv UN
return ret;
}
+struct whitespace_fix_data {
+ unsigned char *changed;
+ int line_nr;
+};
+
+struct whitespace_transform_data {
+ unsigned ws_rule;
+};
+
+static int transform_whitespace_record(const char *line, long len, int is_eof,
+ char **transformed,
+ long *transformed_len, void *data)
+{
+ struct whitespace_transform_data *transform = data;
+ struct strbuf fixed = STRBUF_INIT;
+ size_t size;
+
+ if (is_eof && (transform->ws_rule & WS_BLANK_AT_EOF) &&
+ ws_blank_line(line, len)) {
+ *transformed = xstrdup("");
+ *transformed_len = 0;
+ return 0;
+ }
+ ws_fix_copy(&fixed, line, len,
+ transform->ws_rule & ~WS_BLANK_AT_EOF, NULL);
+ *transformed = strbuf_detach(&fixed, &size);
+ *transformed_len = size;
+ return 0;
+}
+
+static int mark_whitespace_fix_hunk(void *data_, long old_begin UNUSED,
+ long old_nr UNUSED, long new_begin,
+ long new_nr, const char *func UNUSED,
+ long funclen UNUSED)
+{
+ struct whitespace_fix_data *data = data_;
+ long start = new_nr ? new_begin - 1 : new_begin;
+ long i;
+
+ for (i = start; i < start + new_nr && i < data->line_nr; i++)
+ if (data->changed && i >= 0)
+ data->changed[i] = 1;
+ return 0;
+}
+
+static int no_op_diff_line(void *data UNUSED, mmfile_t *mb UNUSED,
+ int nbuf UNUSED)
+{
+ return 0;
+}
+
+static size_t trim_blank_lines_at_eof(mmfile_t *file)
+{
+ size_t end = file->size;
+
+ while (end) {
+ size_t start = end;
+ if (file->ptr[start - 1] == '\n')
+ start--;
+ while (start && file->ptr[start - 1] != '\n')
+ start--;
+ if (!ws_blank_line(file->ptr + start, end - start))
+ break;
+ end = start;
+ }
+ return end;
+}
+
+static mmfile_t *trimmed_copy(mmfile_t *file, size_t size, mmfile_t *copy)
+{
+ if (size == file->size)
+ return file;
+ copy->ptr = xmalloc(size ? size : 1);
+ memcpy(copy->ptr, file->ptr, size);
+ copy->size = size;
+ return copy;
+}
+
+int ll_merge_fix_whitespace(mmfile_t *result, mmfile_t *ours,
+ unsigned ws_rule)
+{
+ struct whitespace_fix_data data = { 0 };
+ struct strbuf fixed = STRBUF_INIT;
+ xpparam_t xpp = { 0 };
+ xdemitconf_t xecfg = { .ctxlen = 0 };
+ xdemitcb_t ecb = {
+ .out_hunk = mark_whitespace_fix_hunk,
+ .out_line = no_op_diff_line,
+ .priv = &data,
+ };
+ size_t pos = 0, cut = result->size;
+ int ret = -1, i = 0;
+
+ for (pos = 0; pos < result->size; pos++)
+ if (result->ptr[pos] == '\n')
+ i++;
+ if (result->size && result->ptr[result->size - 1] != '\n')
+ i++;
+ data.line_nr = i;
+ CALLOC_ARRAY(data.changed, data.line_nr);
+ if (xdi_diff(ours, result, &xpp, &xecfg, &ecb))
+ goto out;
+
+ if (ws_rule & WS_BLANK_AT_EOF) {
+ int line = data.line_nr;
+ pos = result->size;
+ while (line > 0 && data.changed[line - 1]) {
+ size_t start = pos;
+ if (start > 0 && result->ptr[start - 1] == '\n')
+ start--;
+ while (start > 0 && result->ptr[start - 1] != '\n')
+ start--;
+ if (!ws_blank_line(result->ptr + start, pos - start))
+ break;
+ cut = start;
+ pos = start;
+ line--;
+ }
+ }
+
+ strbuf_init(&fixed, result->size);
+ pos = 0;
+ for (i = 0; i < data.line_nr; i++) {
+ size_t end = pos;
+ while (end < cut && result->ptr[end] != '\n')
+ end++;
+ if (end < cut)
+ end++;
+ if (data.changed[i])
+ ws_fix_copy(&fixed, result->ptr + pos, end - pos,
+ ws_rule, NULL);
+ else
+ strbuf_add(&fixed, result->ptr + pos, end - pos);
+ pos = end;
+ }
+ if (pos < cut)
+ strbuf_add(&fixed, result->ptr + pos, cut - pos);
+ free(result->ptr);
+ result->ptr = strbuf_detach(&fixed, &result->size);
+ ret = 0;
+out:
+ strbuf_release(&fixed);
+ free(data.changed);
+ return ret;
+}
+
static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused,
mmfile_t *result,
const char *path,
@@ -112,8 +259,14 @@ static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unuse
{
enum ll_merge_result ret;
xmparam_t xmp;
+ mmfile_t orig_copy = { 0 }, src1_copy = { 0 }, src2_copy = { 0 };
+ mmfile_t *merge_orig = orig, *merge_src1 = src1, *merge_src2 = src2;
+ size_t src1_size;
int status;
+ struct whitespace_transform_data transform_data;
assert(opts);
+ src1_size = src1->size;
+ transform_data.ws_rule = opts->whitespace_rule;
if (orig->size > MAX_XDIFF_SIZE ||
src1->size > MAX_XDIFF_SIZE ||
@@ -133,6 +286,19 @@ static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unuse
xmp.level = XDL_MERGE_ZEALOUS;
xmp.favor = opts->variant;
xmp.xpp.flags = opts->xdl_opts;
+ if (opts->whitespace_fix) {
+ xmp.xpp.line_transform = transform_whitespace_record;
+ xmp.xpp.line_transform_data = &transform_data;
+ if (opts->whitespace_rule & WS_BLANK_AT_EOF) {
+ merge_orig = trimmed_copy(orig,
+ trim_blank_lines_at_eof(orig), &orig_copy);
+ merge_src1 = trimmed_copy(src1,
+ trim_blank_lines_at_eof(src1), &src1_copy);
+ merge_src2 = trimmed_copy(src2,
+ trim_blank_lines_at_eof(src2), &src2_copy);
+ src1_size = merge_src1->size;
+ }
+ }
if (opts->conflict_style >= 0)
xmp.style = opts->conflict_style;
else if (git_xmerge_style >= 0)
@@ -142,8 +308,22 @@ static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unuse
xmp.ancestor = orig_name;
xmp.file1 = name1;
xmp.file2 = name2;
- status = xdl_merge(orig, src1, src2, &xmp, result);
+ status = xdl_merge(merge_orig, merge_src1, merge_src2, &xmp, result);
ret = (status > 0) ? LL_MERGE_CONFLICT : status;
+ if (opts->whitespace_fix && status >= 0) {
+ if (src1_size < src1->size) {
+ size_t suffix_size = src1->size - src1_size;
+ result->ptr = xrealloc(result->ptr, result->size + suffix_size);
+ memcpy(result->ptr + result->size,
+ src1->ptr + src1_size, suffix_size);
+ result->size += suffix_size;
+ }
+ if (ll_merge_fix_whitespace(result, src1, opts->whitespace_rule))
+ ret = LL_MERGE_ERROR;
+ }
+ free(orig_copy.ptr);
+ free(src1_copy.ptr);
+ free(src2_copy.ptr);
return ret;
}
diff --git a/merge-ll.h b/merge-ll.h
index f95332c682..5a81aa2d65 100644
--- a/merge-ll.h
+++ b/merge-ll.h
@@ -83,6 +83,8 @@ struct ll_merge_options {
/* Extra xpparam_t flags as defined in xdiff/xdiff.h. */
long xdl_opts;
+ unsigned whitespace_fix : 1;
+ unsigned whitespace_rule;
};
#define LL_MERGE_OPTIONS_INIT { .conflict_style = -1 }
@@ -108,6 +110,9 @@ enum ll_merge_result ll_merge(mmfile_t *result_buf,
struct index_state *istate,
const struct ll_merge_options *opts);
+int ll_merge_fix_whitespace(mmfile_t *result, mmfile_t *ours,
+ unsigned ws_rule);
+
int ll_merge_marker_size(struct index_state *istate, const char *path);
int is_conflict_marker_line(const char *line, unsigned long len, int marker_size);
int has_conflict_markers(struct index_state *istate, const char *path);
diff --git a/merge-ort.c b/merge-ort.c
index 1383f7b256..912df3cb9e 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -52,6 +52,7 @@
#include "trace2.h"
#include "tree.h"
#include "unpack-trees.h"
+#include "ws.h"
#include "xdiff-interface.h"
/*
@@ -1260,6 +1261,11 @@ static void collect_rename_info(struct merge_options *opt,
}
}
+static int fix_blob_whitespace(struct merge_options *opt,
+ const struct object_id *base_oid,
+ struct object_id *result_oid,
+ const char *path);
+
static int collect_merge_info_callback(int n,
unsigned long mask,
unsigned long dirmask,
@@ -1385,6 +1391,11 @@ static int collect_merge_info_callback(int n,
* destinations within side2.
*/
if (side1_matches_mbase && filemask == 0x07) {
+ if (opt->whitespace_fix && S_ISREG(names[2].mode)) {
+ if (fix_blob_whitespace(opt, &names[0].oid, &names[2].oid,
+ fullpath))
+ return -1;
+ }
/* use side2 version as resolution */
if (setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
names, names+2, side2_null, 0,
@@ -2128,6 +2139,10 @@ static int merge_3way(struct merge_options *opt,
ll_opts.renormalize = opt->renormalize;
ll_opts.extra_marker_size = extra_marker_size;
ll_opts.xdl_opts = opt->xdl_opts;
+ ll_opts.whitespace_fix = opt->whitespace_fix;
+ if (ll_opts.whitespace_fix)
+ ll_opts.whitespace_rule = whitespace_rule(opt->repo->index,
+ path);
ll_opts.conflict_style = opt->conflict_style;
if (opt->priv->call_depth) {
@@ -2180,6 +2195,30 @@ static int merge_3way(struct merge_options *opt,
return merge_status;
}
+static int fix_blob_whitespace(struct merge_options *opt,
+ const struct object_id *base_oid,
+ struct object_id *result_oid,
+ const char *path)
+{
+ mmfile_t base = { 0 }, result;
+ struct object_id fixed_oid;
+ int ret;
+
+ if (!is_null_oid(base_oid))
+ read_mmblob(&base, opt->repo->objects, base_oid);
+ read_mmblob(&result, opt->repo->objects, result_oid);
+ ret = ll_merge_fix_whitespace(&result, &base,
+ whitespace_rule(opt->repo->index, path));
+ if (!ret)
+ ret = odb_write_object(opt->repo->objects, result.ptr, result.size,
+ OBJ_BLOB, &fixed_oid);
+ if (!ret)
+ oidcpy(result_oid, &fixed_oid);
+ free(base.ptr);
+ free(result.ptr);
+ return ret;
+}
+
static int handle_content_merge(struct merge_options *opt,
const char *path,
const struct version_info *o,
@@ -4222,6 +4261,12 @@ static int process_entry(struct merge_options *opt,
if (ci->merged.is_null)
ci->merged.clean = 1;
oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
+ if (side == 2 && opt->whitespace_fix &&
+ S_ISREG(ci->merged.result.mode)) {
+ if (fix_blob_whitespace(opt, &ci->stages[0].oid,
+ &ci->merged.result.oid, path))
+ return -1;
+ }
assert(othermask == 2 || othermask == 4);
assert(ci->merged.is_null ==
@@ -4428,6 +4473,11 @@ static int process_entry(struct merge_options *opt,
int side = (ci->filemask == 4) ? 2 : 1;
ci->merged.result.mode = ci->stages[side].mode;
oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
+ if (side == 2 && opt->whitespace_fix &&
+ S_ISREG(ci->merged.result.mode) &&
+ fix_blob_whitespace(opt, null_oid(opt->repo->hash_algo),
+ &ci->merged.result.oid, path))
+ return -1;
ci->merged.clean = !ci->df_conflict && !ci->path_conflict;
} else if (ci->filemask == 1) {
/* Deleted on both sides */
@@ -5598,6 +5648,8 @@ int parse_merge_opt(struct merge_options *opt, const char *s)
DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
else if (!strcmp(s, "ignore-cr-at-eol"))
DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
+ else if (!strcmp(s, "whitespace=fix"))
+ opt->whitespace_fix = 1;
else if (!strcmp(s, "renormalize"))
opt->renormalize = 1;
else if (!strcmp(s, "no-renormalize"))
diff --git a/merge-ort.h b/merge-ort.h
index 6045579825..e446600fee 100644
--- a/merge-ort.h
+++ b/merge-ort.h
@@ -67,6 +67,7 @@ struct merge_options {
/* xdiff-related options (patience, ignore whitespace, ours/theirs) */
long xdl_opts;
+ unsigned whitespace_fix : 1;
int conflict_style;
enum {
MERGE_VARIANT_NORMAL = 0,
--
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 ` [PATCH 1/4] xdiff: support transformed records in merge matching Alessio Attilio via GitGitGadget
2026-11-30 0:01 ` Alessio Attilio via GitGitGadget [this message]
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=5ddfdca00e646bdc87b0dd09ffbee2c9398f9b1e.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