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(+)
Show changes to 2 files +34 −0
contrib/subtree/git-subtree.sh, contrib/subtree/t/t7900-subtree.sh
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