github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/git/git.go (about)

     1  // Package git provides an integration with the git command
     2  package git
     3  
     4  import (
     5  	"errors"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/apex/log"
    10  )
    11  
    12  // IsRepo returns true if current folder is a git repository
    13  func IsRepo() bool {
    14  	out, err := Run("rev-parse", "--is-inside-work-tree")
    15  	return err == nil && strings.TrimSpace(out) == "true"
    16  }
    17  
    18  // Run runs a git command and returns its output or errors
    19  func Run(args ...string) (string, error) {
    20  	// TODO: use exex.CommandContext here and refactor.
    21  	/* #nosec */
    22  	var cmd = exec.Command("git", args...)
    23  	log.WithField("args", args).Debug("running git")
    24  	bts, err := cmd.CombinedOutput()
    25  	log.WithField("output", string(bts)).
    26  		Debug("git result")
    27  	if err != nil {
    28  		return "", errors.New(string(bts))
    29  	}
    30  	return string(bts), nil
    31  }
    32  
    33  // Clean the output
    34  func Clean(output string, err error) (string, error) {
    35  	output = strings.Replace(strings.Split(output, "\n")[0], "'", "", -1)
    36  	if err != nil {
    37  		err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
    38  	}
    39  	return output, err
    40  }