github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/pkg/scm/downloaders/git/clone.go (about)

     1  package git
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  
     8  	"github.com/golang/glog"
     9  	"github.com/openshift/source-to-image/pkg/api"
    10  	"github.com/openshift/source-to-image/pkg/scm/git"
    11  	"github.com/openshift/source-to-image/pkg/util/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, api.Source)
    24  	config.WorkingSourceDir = targetSourceDir
    25  
    26  	ref := config.Source.URL.Fragment
    27  	if ref == "" {
    28  		ref = "HEAD"
    29  	}
    30  
    31  	if len(config.ContextDir) > 0 {
    32  		targetSourceDir = filepath.Join(config.WorkingDir, api.ContextTmp)
    33  		glog.V(1).Infof("Downloading %q (%q) ...", config.Source, config.ContextDir)
    34  	} else {
    35  		glog.V(1).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: true}
    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, ref)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	glog.V(1).Infof("Checked out %q", ref)
    56  	if !config.IgnoreSubmodules {
    57  		err = c.SubmoduleUpdate(targetSourceDir, true, true)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		glog.V(1).Infof("Updated submodules for %q", ref)
    62  	}
    63  
    64  	// Record Git's knowledge about file permissions
    65  	if runtime.GOOS == "windows" {
    66  		filemodes, err := c.LsTree(filepath.Join(targetSourceDir, config.ContextDir), ref, true)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		for _, filemode := range filemodes {
    71  			c.Chmod(filepath.Join(targetSourceDir, config.ContextDir, filemode.Name()), os.FileMode(filemode.Mode())&os.ModePerm)
    72  		}
    73  	}
    74  
    75  	info := c.GetInfo(targetSourceDir)
    76  	if len(config.ContextDir) > 0 {
    77  		originalTargetDir := filepath.Join(config.WorkingDir, api.Source)
    78  		c.RemoveDirectory(originalTargetDir)
    79  		path := filepath.Join(targetSourceDir, config.ContextDir)
    80  		err := c.CopyContents(path, originalTargetDir)
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  		c.RemoveDirectory(targetSourceDir)
    85  	}
    86  
    87  	if len(config.ContextDir) > 0 {
    88  		info.ContextDir = config.ContextDir
    89  	}
    90  
    91  	return info, nil
    92  }