github.com/Microsoft/fabrikate@v0.0.0-20190420002442-bff75be28d02/core/git.go (about)

     1  package core
     2  
     3  import (
     4  	"os/exec"
     5  
     6  	"github.com/kyokomi/emoji"
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func CloneRepo(repo string, commit string, intoPath string, branch string) (err error) {
    11  	cloneArgs := []string{
    12  		"clone",
    13  		repo,
    14  	}
    15  
    16  	if len(commit) == 0 {
    17  		log.Println(emoji.Sprintf(":helicopter: component requested latest commit: fast cloning at --depth 1"))
    18  
    19  		cloneArgs = append(cloneArgs, "--depth", "1")
    20  	} else {
    21  		log.Println(emoji.Sprintf(":helicopter: component requested commit '%s': need full clone", commit))
    22  	}
    23  
    24  	if len(branch) != 0 {
    25  		log.Println(emoji.Sprintf(":helicopter: component requested branch '%s'", branch))
    26  		cloneArgs = append(cloneArgs, "--branch", branch)
    27  	}
    28  
    29  	cloneArgs = append(cloneArgs, intoPath)
    30  
    31  	if err = exec.Command("git", cloneArgs...).Run(); err != nil {
    32  		return err
    33  	}
    34  
    35  	if len(commit) != 0 {
    36  		log.Println(emoji.Sprintf(":helicopter: performing checkout at commit %s", commit))
    37  		checkoutCommit := exec.Command("git", "checkout", commit)
    38  		checkoutCommit.Dir = intoPath
    39  		if err = checkoutCommit.Run(); err != nil {
    40  			return err
    41  		}
    42  	}
    43  
    44  	return nil
    45  }