From: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
To: git@vger.kernel.org
Cc: pushkarkumarsingh1970@gmail.com
Subject: [PATCH v4] subtree: validate --prefix against commit in split
Date: Tue, 3 Feb 2026 16:48:16 +0000 [thread overview]
Message-ID: <20260203164815.68258-2-pushkarkumarsingh1970@gmail.com> (raw)
In-Reply-To: <20260115175403.3971-3-pushkarkumarsingh1970@gmail.com>
git subtree split currently validates --prefix against the working tree.
This breaks when splitting an older commit or when the working tree does
not contain the subtree, even though the commit does.
For example:
git subtree split --prefix=pkg <commit>
fails if pkg was removed later, even though it exists in <commit>.
Fix this by validating the prefix against the specified commit using
git cat-file instead of the working tree.
Add a test to ensure this behavior does not regress.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
---
Changes since v3:
- Fix regression in existing subtree tests by checking prefix existence
with git cat-file instead of git ls-tree -d
- Preserve original error message to keep test 17 passing
contrib/subtree/git-subtree.sh | 9 +++++++++
contrib/subtree/t/t7900-subtree.sh | 22 ++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 17106d1a72..d7f9121f2f 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -257,6 +257,9 @@ main () {
test -e "$arg_prefix" &&
die "fatal: prefix '$arg_prefix' already exists."
;;
+ split)
+ # checked later against the commit, not the working tree
+ ;;
*)
test -e "$arg_prefix" ||
die "fatal: '$arg_prefix' does not exist; use 'git subtree add'"
@@ -966,6 +969,12 @@ cmd_split () {
else
die "fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
fi
+
+ # Now validate prefix against the commit, not the working tree
+ if ! git cat-file -e "$rev:$dir" 2>/dev/null
+ then
+ die "fatal: '$dir' does not exist; use 'git subtree add'"
+ fi
repository=""
if test "$#" = 2
then
diff --git a/contrib/subtree/t/t7900-subtree.sh b/contrib/subtree/t/t7900-subtree.sh
index 316dc5269e..e4f632f3af 100755
--- a/contrib/subtree/t/t7900-subtree.sh
+++ b/contrib/subtree/t/t7900-subtree.sh
@@ -368,6 +368,28 @@ test_expect_success 'split requires path given by option --prefix must exist' '
)
'
+test_expect_success 'split works when prefix exists in commit but not in working tree' '
+ subtree_test_create_repo "$test_count" &&
+ (
+ cd "$test_count" &&
+
+ # create subtree
+ mkdir pkg &&
+ echo ok >pkg/file &&
+ git add pkg &&
+ git commit -m "add pkg" &&
+ good=$(git rev-parse HEAD) &&
+
+ # remove it from working tree in later commit
+ git rm -r pkg &&
+ git commit -m "remove pkg" &&
+
+ # must still be able to split using the old commit
+ git subtree split --prefix=pkg "$good" >out &&
+ test -s out
+ )
+'
+
test_expect_success 'split rejects flags for add' '
subtree_test_create_repo "$test_count" &&
subtree_test_create_repo "$test_count/sub proj" &&
--
2.43.0
next prev parent reply other threads:[~2026-02-03 16:59 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-15 12:09 [PATCH] subtree: validate --prefix against commit in split Pushkar Singh
2026-01-15 12:24 ` [PATCH v2] " Pushkar Singh
2026-01-15 16:30 ` Junio C Hamano
2026-01-15 17:52 ` [PATCH v3] " Pushkar Singh
2026-02-02 18:54 ` Josh Steadmon
2026-02-02 19:10 ` Junio C Hamano
2026-02-02 21:07 ` Junio C Hamano
2026-02-03 15:30 ` [RFH] adding test coverage for contrib/ in CI jobs Junio C Hamano
2026-02-03 17:06 ` Re* " Junio C Hamano
2026-02-03 23:09 ` Junio C Hamano
2026-02-04 4:38 ` Colin Stagner
2026-02-04 19:55 ` Junio C Hamano
2026-02-05 6:05 ` Colin Stagner
2026-02-05 16:39 ` Junio C Hamano
2026-02-05 20:54 ` Junio C Hamano
2026-02-03 21:26 ` Junio C Hamano
2026-02-03 21:53 ` Jeff King
2026-02-03 16:48 ` Pushkar Singh [this message]
2026-02-03 17:37 ` [PATCH v4] subtree: validate --prefix against commit in split Junio C Hamano
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=20260203164815.68258-2-pushkarkumarsingh1970@gmail.com \
--to=pushkarkumarsingh1970@gmail.com \
--cc=git@vger.kernel.org \
/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