Skip to content

Commit a606fcd

Browse files
pushkarscriptsgitster
authored andcommitted
subtree: validate --prefix against commit in split
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d529f3a commit a606fcd

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

contrib/subtree/git-subtree.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ main () {
257257
test -e "$arg_prefix" &&
258258
die "fatal: prefix '$arg_prefix' already exists."
259259
;;
260+
split)
261+
# checked later against the commit, not the working tree
262+
;;
260263
*)
261264
test -e "$arg_prefix" ||
262265
die "fatal: '$arg_prefix' does not exist; use 'git subtree add'"
@@ -966,6 +969,12 @@ cmd_split () {
966969
else
967970
die "fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
968971
fi
972+
973+
# Now validate prefix against the commit, not the working tree
974+
if ! git cat-file -e "$rev:$dir" 2>/dev/null
975+
then
976+
die "fatal: '$dir' does not exist; use 'git subtree add'"
977+
fi
969978
repository=""
970979
if test "$#" = 2
971980
then

contrib/subtree/t/t7900-subtree.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ test_expect_success 'split requires path given by option --prefix must exist' '
368368
)
369369
'
370370

371+
test_expect_success 'split works when prefix exists in commit but not in working tree' '
372+
subtree_test_create_repo "$test_count" &&
373+
(
374+
cd "$test_count" &&
375+
376+
# create subtree
377+
mkdir pkg &&
378+
echo ok >pkg/file &&
379+
git add pkg &&
380+
git commit -m "add pkg" &&
381+
good=$(git rev-parse HEAD) &&
382+
383+
# remove it from working tree in later commit
384+
git rm -r pkg &&
385+
git commit -m "remove pkg" &&
386+
387+
# must still be able to split using the old commit
388+
git subtree split --prefix=pkg "$good" >out &&
389+
test -s out
390+
)
391+
'
392+
371393
test_expect_success 'split rejects flags for add' '
372394
subtree_test_create_repo "$test_count" &&
373395
subtree_test_create_repo "$test_count/sub proj" &&

0 commit comments

Comments
 (0)