kubesphere.io/s2irun@v3.2.1+incompatible/pkg/scm/downloaders/git/clone.go (about) 1 package git 2 3 import ( 4 "path/filepath" 5 6 "github.com/golang/glog" 7 8 "github.com/kubesphere/s2irun/pkg/api" 9 "github.com/kubesphere/s2irun/pkg/api/constants" 10 "github.com/kubesphere/s2irun/pkg/scm/git" 11 "github.com/kubesphere/s2irun/pkg/utils/fs" 12 ) 13 14 // Clone knows how to clone a Git repository. 15 type Clone struct { 16 git.Git 17 fs.FileSystem 18 } 19 20 // Download downloads the application source code from the Git repository 21 // and checkout the Ref specified in the config. 22 func (c *Clone) Download(config *api.Config) (*git.SourceInfo, error) { 23 targetSourceDir := filepath.Join(config.WorkingDir, constants.Source) 24 config.WorkingSourceDir = targetSourceDir 25 26 RevisionId := config.RevisionId 27 if RevisionId == "" { 28 RevisionId = "HEAD" 29 } 30 31 if len(config.ContextDir) > 0 { 32 targetSourceDir = filepath.Join(config.WorkingDir, constants.ContextTmp) 33 glog.V(9).Infof("Downloading %q (%q) ...", config.Source, config.ContextDir) 34 } else { 35 glog.V(9).Infof("Downloading %q ...", config.Source) 36 } 37 38 if !config.IgnoreSubmodules { 39 glog.V(2).Infof("Cloning sources into %q", targetSourceDir) 40 } else { 41 glog.V(2).Infof("Cloning sources (ignoring submodules) into %q", targetSourceDir) 42 } 43 44 cloneConfig := git.CloneConfig{Quiet: false} 45 err := c.Clone(config.Source, targetSourceDir, cloneConfig) 46 if err != nil { 47 glog.V(0).Infof("error: git clone failed: %v", err) 48 return nil, err 49 } 50 51 err = c.Checkout(targetSourceDir, RevisionId) 52 if err != nil { 53 return nil, err 54 } 55 56 glog.V(0).Infof("Checked out to %q", RevisionId) 57 if !config.IgnoreSubmodules { 58 err = c.SubmoduleUpdate(targetSourceDir, true, true) 59 if err != nil { 60 return nil, err 61 } 62 glog.V(0).Infof("Updated submodules for %q", RevisionId) 63 } 64 65 info := c.GetInfo(targetSourceDir) 66 if len(config.ContextDir) > 0 { 67 originalTargetDir := filepath.Join(config.WorkingDir, constants.Source) 68 c.RemoveDirectory(originalTargetDir) 69 path := filepath.Join(targetSourceDir, config.ContextDir) 70 err := c.CopyContents(path, originalTargetDir) 71 if err != nil { 72 return nil, err 73 } 74 c.RemoveDirectory(targetSourceDir) 75 } 76 77 if len(config.ContextDir) > 0 { 78 info.ContextDir = config.ContextDir 79 } 80 81 return info, nil 82 }