Git development
 help / color / mirror / Atom feed
* [PATCH] history: squash a branch whose base has moved on
@ 2026-12-04  9:51 Harald Nordgren via GitGitGadget
  2026-12-04 16:06 ` D. Ben Knoble
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-12-04  9:51 UTC (permalink / raw)
  To: git
  Cc: D. Ben Knoble, Phillip Wood, Patrick Steinhardt, Harald Nordgren,
	Harald Nordgren

From: Harald Nordgren <haraldnordgren@gmail.com>

"git history squash origin/main..HEAD" fails with "the revision range
is empty" once a fetch has brought new commits into origin/main, even
though "git rev-list origin/main..HEAD" still lists the commits on the
branch. The squash only goes through when the range is spelled as
"$(git merge-base origin/main HEAD)..HEAD".

The range is walked with --ancestry-path, which keeps only the
descendants of <base> so that a side branch that forked before <base>
and was merged later is left out. Commits on a branch that forked from
an ancestor of <base> are not its descendants, so none of them are
kept.

Treat <base> as the commit where <tip> forked from it, so that both
spellings squash the same commits. A <base> that is an ancestor of
<tip> is its own fork point and such a range is squashed as before.

Reported-by: D. Ben Knoble <ben.knoble@gmail.com>
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    history: squash a branch whose base has moved on
    
    Fix git history squash origin/main..HEAD failing with "the revision
    range is empty" after a fetch has moved origin/main past the commit the
    branch forked from.
    
    Reported in
    https://lore.kernel.org/git/CALnO6CCOyVX_Setv4fy3sUnccQn889dHhUa=NyoOLyXbe768XA@mail.gmail.com/

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2515%2FHaraldNordgren%2Fhistory-squash-diverged-base-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2515/HaraldNordgren/history-squash-diverged-base-v1
Pull-Request: https://github.com/git/git/pull/2515

 Documentation/git-history.adoc |  5 +++-
 builtin/history.c              | 50 +++++++++++++++++++++++++++++++---
 t/t3455-history-squash.sh      | 26 ++++++++++++++++++
 3 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
index 1fcc30150a..291920d61b 100644
--- a/Documentation/git-history.adoc
+++ b/Documentation/git-history.adoc
@@ -128,7 +128,10 @@ The range is given in the usual `<base>..<tip>` form, where _<base>_ is
 the commit just below the oldest commit to squash. For example, `git
 history squash HEAD~3..HEAD` folds the three most recent commits into
 one, and `git history squash HEAD~5..HEAD~2` squashes an interior range
-while leaving the two newest commits in place. Several revisions may be
+while leaving the two newest commits in place. _<base>_ may also be a
+branch that has gained commits since _<tip>_ forked from it, so `git
+history squash origin/main..HEAD` folds the commits made since the fork
+even after a fetch has updated `origin/main`. Several revisions may be
 given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is
 already on `topic`. Rev-list options may also be given, but any that would
 change how the range is walked are overridden with a warning.
diff --git a/builtin/history.c b/builtin/history.c
index 43ba76f5a1..c848526368 100644
--- a/builtin/history.c
+++ b/builtin/history.c
@@ -1318,10 +1318,42 @@ static int build_squash_message(struct repository *repo,
 				const struct strbuf *todo_buf,
 				struct strbuf *out);
 
+/*
+ * The walk keeps only the descendants of its bottom commits, and <base> has
+ * none in the range when the tip forked from one of its ancestors. Also walk
+ * from the merge bases of <base> and each tip so that such a range is not
+ * empty.
+ */
+static int add_merge_base_bottoms(struct repository *repo,
+				  struct rev_info *revs,
+				  struct commit *base)
+{
+	for (size_t i = 0; i < revs->cmdline.nr; i++) {
+		struct rev_cmdline_entry *arg = &revs->cmdline.rev[i];
+		struct commit_list *merge_bases = NULL;
+		struct commit *tip;
+
+		if (arg->flags & UNINTERESTING)
+			continue;
+		tip = lookup_commit_reference_gently(repo, &arg->item->oid, 1);
+		if (!tip)
+			continue;
+		if (repo_get_merge_bases(repo, base, tip, &merge_bases) < 0)
+			return -1;
+		for (struct commit_list *p = merge_bases; p; p = p->next)
+			commit_list_insert(p->item,
+					   &revs->ancestry_path_bottoms);
+		commit_list_free(merge_bases);
+	}
+	return 0;
+}
+
 static int setup_squash_revisions(struct repository *repo,
 				  int argc, const char **argv,
 				  struct rev_info *revs)
 {
+	bool has_base = false;
+
 	repo_init_revisions(repo, revs, NULL);
 	revs->reverse = 1;
 	revs->topo_order = 1;
@@ -1357,11 +1389,21 @@ static int setup_squash_revisions(struct repository *repo,
 	 * something, as in "<base>..<tip>". A revision range with no such
 	 * bottom commit cannot be squashed.
 	 */
-	for (size_t i = 0; i < revs->cmdline.nr; i++)
-		if (revs->cmdline.rev[i].flags & BOTTOM)
-			return 0;
+	for (size_t i = 0; i < revs->cmdline.nr; i++) {
+		struct rev_cmdline_entry *arg = &revs->cmdline.rev[i];
+		struct commit *base;
 
-	return error(_("not a '<base>..<tip>' revision range"));
+		if (!(arg->flags & BOTTOM))
+			continue;
+		has_base = true;
+		base = lookup_commit_reference_gently(repo, &arg->item->oid, 1);
+		if (base && add_merge_base_bottoms(repo, revs, base) < 0)
+			return -1;
+	}
+	if (!has_base)
+		return error(_("not a '<base>..<tip>' revision range"));
+
+	return 0;
 }
 
 /*
diff --git a/t/t3455-history-squash.sh b/t/t3455-history-squash.sh
index 237afdfa26..643d005984 100755
--- a/t/t3455-history-squash.sh
+++ b/t/t3455-history-squash.sh
@@ -264,6 +264,32 @@ test_expect_success 'squashes when the base is the root commit' '
 	test "$tip_tree" = "$(git rev-parse HEAD^{tree})"
 '
 
+test_expect_success 'squashes onto the fork point when the base has moved on' '
+	test_when_finished "rm -rf upstream downstream" &&
+	git init upstream &&
+	(
+		cd upstream &&
+		test_commit base
+	) &&
+	git clone --single-branch upstream downstream &&
+	(
+		cd upstream &&
+		test_commit upstream-moved
+	) &&
+	(
+		cd downstream &&
+		test_commit one &&
+		test_commit two &&
+		git fetch &&
+		git history squash --no-edit origin..HEAD &&
+		check_log_subjects HEAD <<-\EOF &&
+		one
+		base
+		EOF
+		test_cmp_rev two^{tree} HEAD^{tree}
+	)
+'
+
 test_expect_success 'squashing a mix of fixups' '
 	git reset --hard three &&
 	echo fix >file &&

base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 16:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:51 [PATCH] history: squash a branch whose base has moved on Harald Nordgren via GitGitGadget
2026-12-04 16:06 ` D. Ben Knoble

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox