@@ -567,6 +567,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
567567 }
568568 }
569569
570+ cd := checkoutDir
570571 if gs .src .KeepGitDir && subdir == "." {
571572 checkoutDirGit := filepath .Join (checkoutDir , ".git" )
572573 if err := os .MkdirAll (checkoutDir , 0711 ); err != nil {
@@ -624,7 +625,6 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
624625 }
625626 gitDir = checkoutDirGit
626627 } else {
627- cd := checkoutDir
628628 if subdir != "." {
629629 cd , err = os .MkdirTemp (cd , "checkout" )
630630 if err != nil {
@@ -636,41 +636,42 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
636636 if err != nil {
637637 return nil , errors .Wrapf (err , "failed to checkout remote %s" , urlutil .RedactCredentials (gs .src .Remote ))
638638 }
639- if subdir != "." {
640- d , err := os .Open (filepath .Join (cd , subdir ))
641- if err != nil {
642- return nil , errors .Wrapf (err , "failed to open subdir %v" , subdir )
643- }
644- defer func () {
645- if d != nil {
646- d .Close ()
647- }
648- }()
649- names , err := d .Readdirnames (0 )
650- if err != nil {
651- return nil , err
652- }
653- for _ , n := range names {
654- if err := os .Rename (filepath .Join (cd , subdir , n ), filepath .Join (checkoutDir , n )); err != nil {
655- return nil , err
656- }
657- }
658- if err := d .Close (); err != nil {
659- return nil , err
660- }
661- d = nil // reset defer
662- if err := os .RemoveAll (cd ); err != nil {
663- return nil , err
664- }
665- }
666639 }
667640
668- git = git .New (gitutil .WithWorkTree (checkoutDir ), gitutil .WithGitDir (gitDir ))
641+ git = git .New (gitutil .WithWorkTree (cd ), gitutil .WithGitDir (gitDir ))
669642 _ , err = git .Run (ctx , "submodule" , "update" , "--init" , "--recursive" , "--depth=1" )
670643 if err != nil {
671644 return nil , errors .Wrapf (err , "failed to update submodules for %s" , urlutil .RedactCredentials (gs .src .Remote ))
672645 }
673646
647+ if subdir != "." {
648+ d , err := os .Open (filepath .Join (cd , subdir ))
649+ if err != nil {
650+ return nil , errors .Wrapf (err , "failed to open subdir %v" , subdir )
651+ }
652+ defer func () {
653+ if d != nil {
654+ d .Close ()
655+ }
656+ }()
657+ names , err := d .Readdirnames (0 )
658+ if err != nil {
659+ return nil , err
660+ }
661+ for _ , n := range names {
662+ if err := os .Rename (filepath .Join (cd , subdir , n ), filepath .Join (checkoutDir , n )); err != nil {
663+ return nil , err
664+ }
665+ }
666+ if err := d .Close (); err != nil {
667+ return nil , err
668+ }
669+ d = nil // reset defer
670+ if err := os .RemoveAll (cd ); err != nil {
671+ return nil , err
672+ }
673+ }
674+
674675 if idmap := mount .IdentityMapping (); idmap != nil {
675676 uid , gid := idmap .RootPair ()
676677 err := filepath .WalkDir (gitDir , func (p string , _ os.DirEntry , _ error ) error {
0 commit comments