From: Eric Wendland <eric@wendland.dev>
To: git@vger.kernel.org
Cc: gitster@pobox.com, ask+git@howdoi.land,
levraiphilippeblain@gmail.com, ps@pks.im, l.s.r@web.de,
Eric Wendland <eric@wendland.dev>
Subject: [PATCH] subtree: allow duplicate cache entries with same value
Date: Wed, 29 Apr 2026 16:15:51 +0200 [thread overview]
Message-ID: <20260429141550.167194-2-eric@wendland.dev> (raw)
When a subtree is added, removed, and later re-added at the same path,
'git subtree split' fails with:
fatal: cache for <hash> already exists!
This happens because 'find_existing_splits()' scans history and calls
'cache_set()' for each prior split. When the same subtree commit was
added twice at different mainline commits, both map to the same subtree
commit. The second 'cache_set()' call tries to write the same mapping
again, but the old code treated any existing cache entry as a fatal error.
Fix 'cache_set()' to silently succeed when the existing cache entry
already contains the same value we're trying to write.
Add a regression test for this scenario.
Signed-off-by: Eric Wendland <eric@wendland.dev>
Closes: <1d8fa22c-eb13-4cdb-8499-e19f0dea6b42@tionis.dev>
---
contrib/subtree/git-subtree.sh | 5 +++++
contrib/subtree/t/t7900-subtree.sh | 29 +++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 791fd8260c..f0f2a47d53 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -343,6 +343,11 @@ cache_set () {
test "$oldrev" != "latest_new" &&
test -e "$cachedir/$oldrev"
then
+ read oldcache <"$cachedir/$oldrev"
+ if test "$oldcache" = "$newrev"
+ then
+ return
+ fi
die "fatal: cache for $oldrev already exists!"
fi
echo "$newrev" >"$cachedir/$oldrev"
diff --git a/contrib/subtree/t/t7900-subtree.sh b/contrib/subtree/t/t7900-subtree.sh
index 18d2b56448..85fe4362fb 100755
--- a/contrib/subtree/t/t7900-subtree.sh
+++ b/contrib/subtree/t/t7900-subtree.sh
@@ -528,6 +528,35 @@ do
'
done
+test_expect_success 'split after add, remove, and re-add at same path' '
+ subtree_test_create_repo "$test_count" &&
+ subtree_test_create_repo "$test_count/sub proj" &&
+ test_create_commit "$test_count" main1 &&
+ test_create_commit "$test_count/sub proj" sub1 &&
+ (
+ cd "$test_count" &&
+ git fetch ./"sub proj" HEAD &&
+ git subtree add --prefix="sub dir" FETCH_HEAD
+ ) &&
+ test_create_commit "$test_count" "sub dir"/main-sub1 &&
+ (
+ cd "$test_count" &&
+ git rm -rf "sub dir" &&
+ git commit -m "remove sub dir"
+ ) &&
+ (
+ cd "$test_count" &&
+ git fetch ./"sub proj" HEAD &&
+ git subtree add --prefix="sub dir" FETCH_HEAD
+ ) &&
+ test_create_commit "$test_count" "sub dir"/main-sub2 &&
+ (
+ cd "$test_count" &&
+ git subtree split --prefix="sub dir" --branch=subproj-br &&
+ test "$(git rev-parse --verify subproj-br)"
+ )
+'
+
# Usually,
#
# git subtree merge -P subA --squash f00...
--
2.47.3
reply other threads:[~2026-04-29 14:20 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260429141550.167194-2-eric@wendland.dev \
--to=eric@wendland.dev \
--cc=ask+git@howdoi.land \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=levraiphilippeblain@gmail.com \
--cc=ps@pks.im \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox