From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795996892; b=oVjbHoc8wCuh4dTNdIqtb8Qaj6co765F+5o5TnDGEitUXglBfoGWm/USI0C1oc+Yfj4L yavpvweDZa5zjEiGM6BBy+L8Msa0HxkpKjuo3uuHXdu66LAHAfvtxKiYVG8vphf7hE8vm Fb5ZtL8peusWAjCx5hzOw0TNs23Qd5CBTXjaCjsQwXbU568dXd3x9ILdFK5WYdGkpp9Ch TnNnzgXFRp8p5DksIjUAVPxamfqrBeHraEle/NketUNkRtdhArELaHRwUM8ODu8rfbjTX MCqUsizI/e7PABJ7h87MhzLFRhgzqGWxYrGZxsEjDBLfOI4vmW1lzZwwJUjH8sct22w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795996892; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To: References:From:Date:Subject:Fcc:Content-Type: Content-Transfer-Encoding:MIME-Version:To:Cc; bh=WO+OTriZByMV2NX5WoFCXQE0teoAYngL19a5NfyThV4=; b=Sv62ahn7XFfRqqfn7Zo7S5yRQQtGSH/14Fesz6ZOWwRgKDggmzH8LSyVyFziloy1Vpv7 dC811jaM/CEdXCUDLwD6M3gOzpmFEbS5DkHETWRYVQGtYxQ64beFdKaewQ8gwqHQPcXNq Kc7e98d60MG0JZjUStD0UtG5hubJUyNEauGaZ7bOiztFa+YWFkwrl40jtIMI8miEU5jyE LO4EIL7gVfYk8SKBukCYAqhAOpLrOgIeY8vIO0FvqyQ5yu3PZRcbqquC//aF6VdddslKd ehAXqPBf9Fekrla3JuRc7l22OOl+Nr2t+og8jIqFdSH4ro0PjcP7ipkeETmPMS+VAFg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GnSWTzHp; arc=none smtp.remote-ip=209.85.160.174 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E40E606A32E for ; Mon, 30 Nov 2026 00:01:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E40E606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GnSWTzHp Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-55d2b4a99d4so95903981cf.1 for ; Sun, 29 Nov 2026 16:01:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795996891; x=1796601691; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=WO+OTriZByMV2NX5WoFCXQE0teoAYngL19a5NfyThV4=; b=GnSWTzHpki/NrJFZeO78HJ+LZq2IBj1w75IAQ+xZNhup/wjvED4oHIsSW7NHRI/xdg /jxI5vekHHpzXZ31Tj0EZ6IocBoz5j8atcMXi8gYnzKrApnr7OSRE212H8EXAnIvcTv6 mHk+ikKR1J+VvrnHXdYKC3PBSqVdVskjn6ylpk6CFj1nKRevvclE84reBX5tMeQ5QSc4 qkVFvkxtwbicMPpAMXqG5r3kKC5KJhfGHuVopvfKyEbVxRVwCQArgj9RbyXHoZFlKHjy n+BRw60XicIUZKohOzhvLD0PdV6Lu/5UFjYYNEkelBqwrOPO7LeV0xn+13naLFbPjj/0 q2Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795996891; x=1796601691; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=WO+OTriZByMV2NX5WoFCXQE0teoAYngL19a5NfyThV4=; b=ra767ePB4oL1LrxvqjKOwCxNoXGuW/OgUJro4EL3NNmqinrlKZXV94+vaYYbx1ntQg n7Bx8cXvt3UR4LIghToXGR91a+U4pONuHcWJmwlMXfoJpQxySJgTiMYl1e3TZlWoSXIa +falacakF6f0wrwtodFA58P2R3nVh804kLkCM9BPjj8eV0hO9b7Vh3jyIx304ysoPXa7 ssRUrSNItpsddFmXpHQBasj3hHz5D5thh+pmPrsGOwZdgFg/1E+iQbRfshSRyS0Qv5jo 6oAQxLTpfd8sK5dxbM8sm0djAYcV6W6Cf+Jrv5nZjECDrMZJdvtDiew5oTpO2sGkB8EJ rwFw== X-Gm-Message-State: APuU4Z9H15aY9iEpfItuxjBkNvyxb91oF3hpkyo6ua6Kh7gTfmeuXQHN zr2zaKXN2NK2lMefC5U/3A3UtNqDT3xRB2p3bw35v6X2S40n/NI+StGkfjE4l7Iq X-Gm-Gg: AZFdTZdLCH0zrfBByI8dmL7+okQ7w3v41D/E3tnqtnNJfUCqepJxBFULwLo9HiOMzjb oGA4tHyLMG4X+Gvojd7MLMLESSU5hxa4kNOjmSp0U6L1YDTiBKKwlVc+ILn3MMzFsbpH1uej8v4 Xslsiwqmll9M1EXKwDibQK7pGvx+6rK8Dr5gsjVhmVjAtaeg6RLIjGPGb84I/mFgWw6PgVeJfUW xtV3xmUBQ7tuGGM31u8Rlf0MG2fWK3yKU4bF1pdPzE8Zn8etUke2DOsfElfUm3wJxsEgeQKKh75 uVo/oe9hhwt8874pn/UyovqZh8wipR6Z4CXtvU6d2iIVzZkKJGBHL1QdtddbEmFhXsL+mA2yxya IQmVMUcFZ355NN82jsUjhcGmxYifekToKoz/wsY+ydP00ZAeNos7kPwwnkjLoi9F573xiH8G+jX /5aOpwONuEQbhWrgUKeqkk57Mo3YMPEFT3zsWb7AvdtmE4sET2de6HbSuRA2EhznlyIKsIZ1xhg Hhc7o65DSSPdcnJA9JKk0K+qIfRn8KDjefyS20n6oiqON5fynpYundDOYsifSi8gfZEa8FuWvj7 FCId/GaOmfrRdemlhCnhJyEm3ql7qzU= X-Received: by 2002:a05:622a:903:b0:533:33c6:e635 with SMTP id d75a77b69052e-55d7690fe9bmr613793891cf.8.1795996891208; Sun, 29 Nov 2026 16:01:31 -0800 (PST) Received: from [127.0.0.1] ([20.64.247.97]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-55d78451407sm270750751cf.2.2026.11.29.16.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 16:01:30 -0800 (PST) Message-Id: <5ddfdca00e646bdc87b0dd09ffbee2c9398f9b1e.1795996885.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Alessio Attilio via GitGitGadget" Date: Mon, 30 Nov 2026 00:01:23 +0000 Subject: [PATCH 2/4] merge-ort: fix whitespace in replayed changes Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Alessio Attilio , Alessio Attilio From: Alessio Attilio 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 --- 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