github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/scm/scm.go (about)

     1  package scm
     2  
     3  import (
     4  	"github.com/kubesphere/s2irun/pkg/build"
     5  	"github.com/kubesphere/s2irun/pkg/errors"
     6  	"github.com/kubesphere/s2irun/pkg/scm/downloaders/binary"
     7  	"github.com/kubesphere/s2irun/pkg/scm/downloaders/empty"
     8  	"github.com/kubesphere/s2irun/pkg/scm/downloaders/file"
     9  	gitdownloader "github.com/kubesphere/s2irun/pkg/scm/downloaders/git"
    10  	"github.com/kubesphere/s2irun/pkg/scm/git"
    11  	"github.com/kubesphere/s2irun/pkg/utils/cmd"
    12  	"github.com/kubesphere/s2irun/pkg/utils/fs"
    13  	utilglog "github.com/kubesphere/s2irun/pkg/utils/glog"
    14  )
    15  
    16  var glog = utilglog.StderrLog
    17  
    18  // DownloaderForSource determines what SCM plugin should be used for downloading
    19  // the sources from the repository.
    20  func DownloaderForSource(fs fs.FileSystem, s *git.URL, forceCopy bool) (build.Downloader, error) {
    21  	glog.V(9).Infof("DownloadForSource %s", s)
    22  
    23  	if s == nil {
    24  		return &empty.Noop{}, nil
    25  	}
    26  	if s.Type == git.URLTypeBinary {
    27  		return &binary.File{FileSystem: fs}, nil
    28  	}
    29  	if s.IsLocal() {
    30  		if forceCopy {
    31  			return &file.File{FileSystem: fs}, nil
    32  		}
    33  
    34  		isLocalNonBareGitRepo, err := git.IsLocalNonBareGitRepository(fs, s.LocalPath())
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  		if !isLocalNonBareGitRepo {
    39  			return &file.File{FileSystem: fs}, nil
    40  		}
    41  
    42  		isEmpty, err := git.LocalNonBareGitRepositoryIsEmpty(fs, s.LocalPath())
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  		if isEmpty {
    47  			return nil, errors.NewEmptyGitRepositoryError(s.LocalPath())
    48  		}
    49  
    50  		if !git.HasGitBinary() {
    51  			return &file.File{FileSystem: fs}, nil
    52  		}
    53  	}
    54  
    55  	return &gitdownloader.Clone{Git: git.New(fs, cmd.NewCommandRunner()), FileSystem: fs}, nil
    56  }