@@ -394,11 +394,14 @@ func TestForcePush(t *testing.T) {
394394
395395func TestSwitchBranch (t * testing.T ) {
396396 tests := []struct {
397- name string
398- setupFunc func (g * WithT , path string ) string
399- changeRepo func (g * WithT , c * Client ) string
400- branch string
401- singleBranch bool
397+ name string
398+ setupFunc func (g * WithT , path string ) string
399+ changeRepo func (g * WithT , c * Client ) string
400+ branch string
401+ singleBranch bool
402+ sparseCheckoutDirectories []string
403+ expectedFiles []string
404+ expectedNoFiles []string
402405 }{
403406 {
404407 name : "switch to a branch ahead of the current branch" ,
@@ -537,6 +540,37 @@ func TestSwitchBranch(t *testing.T) {
537540 setupFunc : nil ,
538541 branch : "new" ,
539542 },
543+ {
544+ name : "switch branch preserves sparse checkout directories" ,
545+ setupFunc : func (g * WithT , repoURL string ) string {
546+ tmp := t .TempDir ()
547+ repo , err := extgogit .PlainClone (tmp , false , & extgogit.CloneOptions {
548+ URL : repoURL ,
549+ ReferenceName : plumbing .NewBranchReferenceName (git .DefaultBranch ),
550+ RemoteName : git .DefaultRemote ,
551+ })
552+ g .Expect (err ).ToNot (HaveOccurred ())
553+
554+ err = createBranch (repo , "sparse-branch" )
555+ g .Expect (err ).ToNot (HaveOccurred ())
556+
557+ // Create files in different directories
558+ _ , err = commitFile (repo , "dir1/file1" , "content in dir1" , time .Now ())
559+ g .Expect (err ).ToNot (HaveOccurred ())
560+ cc , err := commitFile (repo , "dir2/file2" , "content in dir2" , time .Now ())
561+ g .Expect (err ).ToNot (HaveOccurred ())
562+
563+ err = repo .Push (& extgogit.PushOptions {
564+ RemoteName : git .DefaultRemote ,
565+ })
566+ g .Expect (err ).ToNot (HaveOccurred ())
567+ return cc .String ()
568+ },
569+ branch : "sparse-branch" ,
570+ sparseCheckoutDirectories : []string {"dir1" },
571+ expectedFiles : []string {"dir1/file1" },
572+ expectedNoFiles : []string {"dir2/file2" },
573+ },
540574 }
541575
542576 for _ , tt := range tests {
@@ -571,6 +605,7 @@ func TestSwitchBranch(t *testing.T) {
571605 ggc , err := NewClient (tmp , nil )
572606 g .Expect (err ).ToNot (HaveOccurred ())
573607 ggc .repository = repo
608+ ggc .sparseCheckoutDirectories = tt .sparseCheckoutDirectories
574609
575610 if tt .changeRepo != nil {
576611 expectedHash = tt .changeRepo (g , ggc )
@@ -583,6 +618,18 @@ func TestSwitchBranch(t *testing.T) {
583618 g .Expect (err ).ToNot (HaveOccurred ())
584619 g .Expect (ref .Name ().Short ()).To (Equal (tt .branch ))
585620 g .Expect (ref .Hash ().String ()).To (Equal (expectedHash ))
621+
622+ // Verify sparse checkout: included files should exist
623+ for _ , file := range tt .expectedFiles {
624+ _ , err := os .Stat (filepath .Join (tmp , file ))
625+ g .Expect (err ).ToNot (HaveOccurred (), "file %s should exist with sparse checkout" , file )
626+ }
627+
628+ // Verify sparse checkout: excluded files should not exist
629+ for _ , noFile := range tt .expectedNoFiles {
630+ _ , err := os .Stat (filepath .Join (tmp , noFile ))
631+ g .Expect (os .IsNotExist (err )).To (BeTrue (), "file %s should not exist with sparse checkout" , noFile )
632+ }
586633 })
587634 }
588635}
0 commit comments