[PATCH 3/4] rebase: route whitespace fixes through the selected backend
- From
- Alessio Attilio via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 30, 2026, 00:01 UTC
- Message-ID
- <46631ec7de84667a7047fc4401b2f156ed64b2cb.1795996885.git.gitgitgadget@gmail.com>
- 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