@@ -892,7 +892,7 @@ func TestGitGetter_BadGitConfig(t *testing.T) {
892892 err = g .update (ctx , dst , testGitToken , url , "main" , 1 )
893893 } else {
894894 // Clone a repository with a git config file
895- err = g .clone (ctx , dst , testGitToken , url , "main" , 1 )
895+ err = g .clone (ctx , dst , testGitToken , url , "main" , 1 , "" )
896896 if err != nil {
897897 t .Fatalf (err .Error ())
898898 }
@@ -950,7 +950,7 @@ func TestGitGetter_BadGitDirName(t *testing.T) {
950950 }
951951 } else {
952952 // Clone a repository with a git directory
953- err = g .clone (ctx , dst , testGitToken , url , "main" , 1 )
953+ err = g .clone (ctx , dst , testGitToken , url , "main" , 1 , "" )
954954 if err != nil {
955955 t .Fatalf (err .Error ())
956956 }
@@ -984,6 +984,77 @@ func TestGitGetter_BadGitDirName(t *testing.T) {
984984 }
985985}
986986
987+ func TestGitGetter_sparseCheckout (t * testing.T ) {
988+ if ! testHasGit {
989+ t .Skip ("git not found, skipping" )
990+ }
991+
992+ g := new (GitGetter )
993+ dst := tempDir (t )
994+
995+ repo := testGitRepo (t , "sparse-checkout" )
996+ repo .commitFile ("subdir1/file1.txt" , "hello" )
997+ repo .commitFile ("subdir2/file2.txt" , "world" )
998+
999+ q := repo .url .Query ()
1000+ q .Add ("subdir" , "subdir1" )
1001+ repo .url .RawQuery = q .Encode ()
1002+
1003+ if err := g .Get (dst , repo .url ); err != nil {
1004+ t .Fatalf ("err: %s" , err )
1005+ }
1006+
1007+ // Verify the file in subdir1 exists
1008+ mainPath := filepath .Join (dst , "subdir1/file1.txt" )
1009+ if _ , err := os .Stat (mainPath ); err != nil {
1010+ t .Fatalf ("err: %s" , err )
1011+ }
1012+
1013+ // Verify the file in subdir2 does not exist
1014+ mainPath = filepath .Join (dst , "subdir2/file2.txt" )
1015+ if _ , err := os .Stat (mainPath ); err == nil {
1016+ t .Fatalf ("expected subdir2 file to not exist" )
1017+ }
1018+ }
1019+
1020+ func TestGitGetter_sparseCheckoutWithCommitID (t * testing.T ) {
1021+ if ! testHasGit {
1022+ t .Skip ("git not found, skipping" )
1023+ }
1024+
1025+ g := new (GitGetter )
1026+ dst := tempDir (t )
1027+
1028+ repo := testGitRepo (t , "sparse-checkout-commit-id" )
1029+ repo .commitFile ("subdir1/file1.txt" , "hello" )
1030+ repo .commitFile ("subdir2/file2.txt" , "world" )
1031+ commitID , err := repo .latestCommit ()
1032+ if err != nil {
1033+ t .Fatal (err )
1034+ }
1035+
1036+ q := repo .url .Query ()
1037+ q .Add ("ref" , commitID )
1038+ q .Add ("subdir" , "subdir1" )
1039+ repo .url .RawQuery = q .Encode ()
1040+
1041+ if err := g .Get (dst , repo .url ); err != nil {
1042+ t .Fatalf ("err: %s" , err )
1043+ }
1044+
1045+ // Verify the file in subdir1 exists
1046+ mainPath := filepath .Join (dst , "subdir1/file1.txt" )
1047+ if _ , err := os .Stat (mainPath ); err != nil {
1048+ t .Fatalf ("err: %s" , err )
1049+ }
1050+
1051+ // Verify the file in subdir2 does not exist
1052+ mainPath = filepath .Join (dst , "subdir2/file2.txt" )
1053+ if _ , err := os .Stat (mainPath ); err == nil {
1054+ t .Fatalf ("expected subdir2 file to not exist" )
1055+ }
1056+ }
1057+
9871058// gitRepo is a helper struct which controls a single temp git repo.
9881059type gitRepo struct {
9891060 t * testing.T
@@ -1035,6 +1106,13 @@ func (r *gitRepo) git(args ...string) {
10351106// commitFile writes and commits a text file to the repo.
10361107func (r * gitRepo ) commitFile (file , content string ) {
10371108 path := filepath .Join (r .dir , file )
1109+
1110+ // Ensure the directory structure exists
1111+ dir := filepath .Dir (path )
1112+ if err := os .MkdirAll (dir , 0755 ); err != nil {
1113+ r .t .Fatal (err )
1114+ }
1115+
10381116 if err := ioutil .WriteFile (path , []byte (content ), 0600 ); err != nil {
10391117 r .t .Fatal (err )
10401118 }
0 commit comments