]> git.ipfire.org Git - thirdparty/git.git/commitdiff
subtree: validate --prefix against commit in split
authorPushkar Singh <pushkarkumarsingh1970@gmail.com>
Thu, 15 Jan 2026 17:52:26 +0000 (17:52 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 15 Jan 2026 19:10:28 +0000 (11:10 -0800)
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 ls-tree instead of the working tree.

Add a test to ensure this behavior does not regress.

Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/subtree/git-subtree.sh
contrib/subtree/t/t7900-subtree.sh

index 17106d1a721519978ef78b63e9b7763e78813674..324ed381487dd895a5b46dff39e9595089177498 100755 (executable)
@@ -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 ls-tree -d "$rev" -- "$dir" >/dev/null
+       then
+               die "fatal: '$dir' does not exist in commit $rev"
+       fi
        repository=""
        if test "$#" = 2
        then
index 316dc5269e2b6f47dd01f28e854dd9a68dbde493..e4f632f3afdb1eafabe9dedb41a7f14664ab5a8f 100755 (executable)
@@ -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" &&