All of lore.kernel.org
 help / color / mirror / Atom feed
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 3/4] rebase: route whitespace fixes through the selected backend
Date: Mon, 30 Nov 2026 00:01:24 +0000	[thread overview]
Message-ID: <46631ec7de84667a7047fc4401b2f156ed64b2cb.1795996885.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2511.git.git.1795996885.gitgitgadget@gmail.com>

From: Alessio Attilio <attilio.alessio@protonmail.com>

--whitespace has historically implied the apply backend because its
value was forwarded directly to git apply.  With whitespace=fix
available in the merge backend, keep apply-specific actions on that
backend while allowing fix to reach merge and interactive rebases.

Validate the action before selecting a backend, pass fix to the merge
strategy when needed, and document the backend-specific behavior.  Keep
other whitespace actions unsupported with the merge backend.  Update
the incompatibility tests so the newly supported combinations are
expected to succeed.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
 Documentation/git-rebase.adoc          |  6 +--
 builtin/rebase.c                       | 55 +++++++++++++++++++++++---
 contrib/completion/git-completion.bash |  2 +-
 t/t3422-rebase-incompatible-options.sh | 17 +++++++-
 4 files changed, 69 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-rebase.adoc b/Documentation/git-rebase.adoc
index 2f0c6fbbd1..1d4c729e3f 100644
--- a/Documentation/git-rebase.adoc
+++ b/Documentation/git-rebase.adoc
@@ -466,9 +466,9 @@ merge backend;;
 	other side had no changes that conflicted.
 
 --whitespace=<option>::
-	This flag is passed to the `git apply` program
-	(see linkgit:git-apply[1]) that applies the patch.
-	Implies `--apply`.
+	In the apply backend, this flag is passed to the `git apply` program
+	(see linkgit:git-apply[1]) that applies the patch and implies `--apply`.
+	The merge backend supports only `fix`.
 +
 See also <<INCOMPATIBLE_OPTIONS,INCOMPATIBLE OPTIONS>> below.
 
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 535db60181..3512fe2e14 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -405,6 +405,30 @@ static int is_merge(struct rebase_options *opts)
 	return opts->type == REBASE_MERGE;
 }
 
+static int rebase_whitespace_uses_apply_backend(
+	const struct rebase_options *opts, const char *strategy_opt)
+{
+	if (opts->type == REBASE_APPLY)
+		return 1;
+	if (opts->type != REBASE_UNSPECIFIED)
+		return 0;
+	return !strcmp(opts->default_backend, "apply") &&
+		!(opts->flags & REBASE_INTERACTIVE_EXPLICIT) &&
+		!(opts->root && !opts->onto_name) &&
+		opts->rebase_merges != 1 &&
+		opts->config_rebase_merges != 1 &&
+		!opts->strategy &&
+		!strategy_opt &&
+		!opts->strategy_opts.nr &&
+		!opts->exec.nr &&
+		opts->empty == EMPTY_UNSPECIFIED &&
+		opts->autosquash != 1 &&
+		opts->update_refs != 1 &&
+		opts->config_update_refs != 1 &&
+		!opts->trailer_args.nr &&
+		opts->reapply_cherry_picks != 1;
+}
+
 static void imply_merge(struct rebase_options *opts, const char *option)
 {
 	switch (opts->type) {
@@ -1119,6 +1143,8 @@ int cmd_rebase(int argc,
 	struct strbuf buf = STRBUF_INIT;
 	struct object_id branch_base;
 	int ignore_whitespace = 0;
+	int whitespace_uses_apply_backend = 0;
+	const char *whitespace = NULL;
 	const char *gpg_sign = NULL;
 	struct object_id squash_onto;
 	char *squash_onto_name = NULL;
@@ -1166,8 +1192,8 @@ int cmd_rebase(int argc,
 				  N_("passed to 'git apply'"), 0),
 		OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
 			 N_("ignore changes in whitespace")),
-		OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
-				  N_("action"), N_("passed to 'git apply'"), 0),
+		OPT_STRING(0, "whitespace", &whitespace, N_("action"),
+			   N_("specify how to handle whitespace errors")),
 		OPT_BIT('f', "force-rebase", &options.flags,
 			N_("cherry-pick all commits, even if unchanged"),
 			REBASE_FORCE),
@@ -1488,6 +1514,16 @@ int cmd_rebase(int argc,
 	}
 	if (options.committer_date_is_author_date || options.ignore_date)
 		options.flags |= REBASE_FORCE;
+	if (whitespace) {
+		if (strcmp(whitespace, "fix") && strcmp(whitespace, "warn") &&
+		    strcmp(whitespace, "nowarn") &&
+		    strcmp(whitespace, "error") && strcmp(whitespace, "error-all"))
+			die("Invalid whitespace option: '%s'", whitespace);
+		if (!strcmp(whitespace, "fix")) {
+			allow_preemptive_ff = 0;
+			options.flags |= REBASE_FORCE;
+		}
+	}
 
 	for (i = 0; i < options.git_am_opts.nr; i++) {
 		const char *option = options.git_am_opts.v[i], *p;
@@ -1499,10 +1535,6 @@ int cmd_rebase(int argc,
 				if (!isdigit(*(p++)))
 					die(_("switch `C' expects a "
 					      "numerical value"));
-		} else if (skip_prefix(option, "--whitespace=", &p)) {
-			if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
-			    strcmp(p, "error") && strcmp(p, "error-all"))
-				die("Invalid whitespace option: '%s'", p);
 		}
 	}
 
@@ -1544,6 +1576,11 @@ int cmd_rebase(int argc,
 	if (options.exec.nr)
 		imply_merge(&options, "--exec");
 
+	whitespace_uses_apply_backend =
+		rebase_whitespace_uses_apply_backend(&options, strategy_opt);
+	if (whitespace && whitespace_uses_apply_backend)
+		strvec_pushf(&options.git_am_opts, "--whitespace=%s", whitespace);
+
 	if (options.type == REBASE_APPLY) {
 		if (ignore_whitespace)
 			strvec_push(&options.git_am_opts,
@@ -1555,6 +1592,12 @@ int cmd_rebase(int argc,
 			strvec_push(&options.git_am_opts, "--ignore-date");
 	} else {
 		/* REBASE_MERGE */
+		if (whitespace && !whitespace_uses_apply_backend &&
+		    !strcmp(whitespace, "fix"))
+			string_list_append(&options.strategy_opts, "whitespace=fix");
+		else if (whitespace && !whitespace_uses_apply_backend)
+			die(_("--whitespace=%s is not supported with the merge backend"),
+			    whitespace);
 		if (ignore_whitespace) {
 			string_list_append(&options.strategy_opts,
 					   "ignore-space-change");
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index a7f1da040a..187de33708 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1091,7 +1091,7 @@ __git_compute_merge_strategies ()
 __git_merge_strategy_options="ours theirs subtree subtree= patience
 	histogram diff-algorithm= ignore-space-change ignore-all-space
 	ignore-space-at-eol renormalize no-renormalize no-renames
-	find-renames find-renames= rename-threshold="
+	find-renames find-renames= rename-threshold= whitespace=fix"
 
 __git_complete_revlist_file ()
 {
diff --git a/t/t3422-rebase-incompatible-options.sh b/t/t3422-rebase-incompatible-options.sh
index d6830b82e0..f955309ce8 100755
--- a/t/t3422-rebase-incompatible-options.sh
+++ b/t/t3422-rebase-incompatible-options.sh
@@ -125,7 +125,22 @@ test_rebase_am_only () {
 }
 
 # Check options which imply --apply
-test_rebase_am_only --whitespace=fix
+test_expect_success '--whitespace=fix works with --merge' '
+	git checkout B^0 &&
+	git rebase --whitespace=fix --merge A &&
+	test_seq 1 9 >expect &&
+	echo "q qfoo();" >>expect &&
+	test_cmp expect foo
+'
+
+test_expect_success '--whitespace=fix works with --interactive' '
+	git checkout B^0 &&
+	GIT_SEQUENCE_EDITOR=: git rebase --whitespace=fix --interactive A &&
+	test_seq 1 9 >expect &&
+	echo "q qfoo();" >>expect &&
+	test_cmp expect foo
+'
+
 test_rebase_am_only -C4
 # Also check an explicit --apply
 test_rebase_am_only --apply
-- 
gitgitgadget


  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 ` [PATCH 2/4] merge-ort: fix whitespace in replayed changes Alessio Attilio via GitGitGadget
2026-11-30  0:01 ` Alessio Attilio via GitGitGadget [this message]
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=46631ec7de84667a7047fc4401b2f156ed64b2cb.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.