From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796377876; b=xY7NdVNRpRiUtUTh9fFF4d+ZxRe/2+R4ffTQ/o89deZe1FQFLqZ+O4o9kPlLFDd76vYm CKRTdj7akDbLr3f2UFX9Vu3HgJ+VLo/c67Xnq0YZLUMlASMtdqkwSCQLC6Kt2+mRmGuyN Ku6htgoPMkDFBpruOFqrLxc8JzNYueOl3p02VeyTfshebjFKN3vJJ9RSAM/L3SbPmrurf P2hscGi1sDSyfhZ+WYWcyTDi/k/O4j0wscFKe/FDa2lM9HsMNgn/Xc2lwxlBWoy8Eb53p y+r0BrTOKh1UKX8AB4zbvCst1Ro5x4S37NbrQB7YAF1HVe9OHBNIPuSPk7IaRA+vqcQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796377876; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:From:Date: Subject:Fcc:Content-Type:Content-Transfer-Encoding:MIME-Version:To: Cc; bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=; b=hIF2gxU7/tAsURs6XRrcmCyT8tqOHc4K58lGdYj5ielT74uM7Nhr733SpOgs9deWCHru ZnRmf4BfwyfO6eQnZNKgrpKoDveHlvmsvEkcMohmTC1BRkpPE7BKApGT4peFIi2W8NyFC oKxxgecp7jQ1YPEj0ygfNa9DUu2c71Pm3cgjWcC5lcIwJfnSI39n97z1z8eceTP0EcNbO +n2Q+UU4/zSPjJUR+b2gUHQQsaErvQCd/KvtSXC6BPhlpwdMCjaSIYJK9IEmLdbpIELfC 0RQk5XZ45h7uEsiQTJOirf7lb9eqfiGSPx8plNFHc2J55txKjbQjVIbj5meh7arb9bw== 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=fXWjhKt1; arc=none smtp.remote-ip=209.85.215.175 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 B6720606A322 for ; Fri, 4 Dec 2026 09:51:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B6720606A322 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=fXWjhKt1 Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-cdb2dfcca94so389861a12.1 for ; Fri, 04 Dec 2026 01:51:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796377875; x=1796982675; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=; b=fXWjhKt1naF2GM2c6FB1liPhEhIWljxH+AxJJP+ExU3tTRiXMl/dZJnEsyk7AWlyyZ /DXJcT1Euntrrd5TecHBubEOKlYhbk+jqmkpFe5DXs2B2sYrb26UoHZ4ybQRW8Px+26K Xl8vPUFxENVjr+ERfekj9NTL3MsFLoWIiGFOdwd/7hHvRvzgQadPl+vI6+riEvqpOIz9 Hjb4zr28e7+AAE0bbfOE/9XQhER7O4MZsyxrg+VryWzazWUMl04AjQKvh53veyTMhmyJ +II3X6yHN4PFYYg1ftVsLwUX6LqmCBGrW8RoR17gdShZe2efDhMoFp+RRUkdKrBORtpK 4fXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796377875; x=1796982675; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=; b=XGZjiD+3cKlADPk4KVJgRhafKAqDyDxu16/ZpUckBZKDpxksDcbF+eyyBtV8XBgXaX 3K6IW8AMABfEX3uw90YMEXLTionrwwO8Gd0G/nx3PwELOFt07nJ82wXKW4pReofqnCxU exmld0fpAQD8zaHdFlW2VV7ZABwbvMIf5kDADdmMqfdWIeGMG5HzDl37cqOIJcRb6mWR C7lwwK4iGx/9LbBG4G5qo/Sie3nCYm5FfGYhch2WBAePbVaS7puL6MGzwQSmE9kgMHV+ GoW+UQwAGFcA+Wdd5LnIJLRbpVJk5Tgvim7OD3TPeEY/OX0MuDIWC528W49La9nZhM7N bJnw== X-Gm-Message-State: APuU4Z9rgaRavfhZCfKCIICW5PAey+lqYIKRgRK9JOzon/yduBht+Ko/ FQQlI4/5wzba7TN/9ZBJX5bumCq5HlG8z1iOZyHmxQ/OcmYa80sDzbAK+eGBPg== X-Gm-Gg: AZFdTZfHoxObGEn10OLvHIBocUMyVyhIB/6ECKl2RyV7YhZoDXpwBfKIjo9wq52gPZJ zfBaWvGWe7eWlw/hgrID2HU0CiaTnks1VfocKGCbe6utMUN6Vonze2bPUDvliVhFN7/pAqITBjV aYPbv9rvD05FU+FwI2Ho22redCm8JpAl67WAIoPSWj6CiMo8XOEp+2vBDy72QBpRNUYXQyfj40q Y9cDTDQuTGHduThvNx5Qsqy7AYi6+5Zn6MSPEynKJg7b1ekrRPsDhwds4qcCB99VmQdrbf9vJc0 HvuXqL0hxoFW/19vKpM4fwwu7RqFuootYJnHTd8xkF4kreRv/WW7O5FMOJuRs0GdFKYHcLY/zui VKRobRHOtkjuI7s+3UKy9JSWmZwzb93Xx+tyPZ205fZm3CcyLFArVI7tnfbZQ1DDcX5yR/yGaso vlDRdxillltmaSRLbPXWqWYxiyKy9GURBkzdihrERC8k3ZpPCO7C84ylsNEL3T5L4e9Tei9w5n/ 0QRuJW/kaEG1bruBJApXzBrJJTIDy6P0j3KXm5UkFJHh+pQpNB9XNsdxYUkRZp4RXvsOo0mz34a rPdH8tM8uWt3w5KCqr0LTmujpwoI07aXFw== X-Received: by 2002:a05:6a21:1f85:b0:3e2:9dee:42cd with SMTP id adf61e73a8af0-415619382famr1596579637.36.1796377874765; Fri, 04 Dec 2026 01:51:14 -0800 (PST) Received: from [127.0.0.1] ([172.149.32.178]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ce12356536dsm1006972a12.9.2026.12.04.01.51.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:51:14 -0800 (PST) Message-Id: From: "Harald Nordgren via GitGitGadget" Date: Fri, 04 Dec 2026 09:51:13 +0000 Subject: [PATCH] history: squash a branch whose base has moved on 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: "D. Ben Knoble" , Phillip Wood , Patrick Steinhardt , Harald Nordgren , Harald Nordgren From: Harald Nordgren "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 so that a side branch that forked before and was merged later is left out. Commits on a branch that forked from an ancestor of are not its descendants, so none of them are kept. Treat as the commit where forked from it, so that both spellings squash the same commits. A that is an ancestor of is its own fork point and such a range is squashed as before. Reported-by: D. Ben Knoble Signed-off-by: Harald Nordgren --- 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 `..` form, where __ 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. __ may also be a +branch that has gained commits since __ 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 has + * none in the range when the tip forked from one of its ancestors. Also walk + * from the merge bases of 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 "..". 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 '..' 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 '..' 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