2015-10-05 20:16:52 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2017-04-26 21:29:57 +00:00
|
|
|
"fmt"
|
2015-10-05 20:16:52 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-10-05 22:45:33 +00:00
|
|
|
|
2018-09-25 18:10:05 +00:00
|
|
|
version "github.com/hashicorp/go-version"
|
2017-11-27 17:02:43 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-10-05 20:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Commits returns a set of commits.
|
|
|
|
// If commitrange is a git still range 12345...54321, then it will be isolated set of commits.
|
|
|
|
// If commitrange is a single commit, all ancestor commits up through the hash provided.
|
2018-11-08 13:13:57 +00:00
|
|
|
// If commitrange is an empty commit range, then nil is returned.
|
2015-10-05 20:16:52 +00:00
|
|
|
func Commits(commitrange string) ([]CommitEntry, error) {
|
2020-09-16 15:41:22 +00:00
|
|
|
cmdArgs := []string{"git", "rev-list", commitrange}
|
2015-10-06 14:44:04 +00:00
|
|
|
if debug() {
|
2015-10-05 22:45:33 +00:00
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
|
|
|
}
|
|
|
|
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
|
2015-10-05 20:16:52 +00:00
|
|
|
if err != nil {
|
2017-03-21 20:17:56 +00:00
|
|
|
logrus.Errorf("mm[git] cmd: %q", strings.Join(cmdArgs, " "))
|
2015-10-05 20:16:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-08 13:13:57 +00:00
|
|
|
if len(output) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-10-05 20:16:52 +00:00
|
|
|
commitHashes := strings.Split(strings.TrimSpace(string(output)), "\n")
|
|
|
|
commits := make([]CommitEntry, len(commitHashes))
|
|
|
|
for i, commitHash := range commitHashes {
|
|
|
|
c, err := LogCommit(commitHash)
|
|
|
|
if err != nil {
|
|
|
|
return commits, err
|
|
|
|
}
|
|
|
|
commits[i] = *c
|
|
|
|
}
|
|
|
|
return commits, nil
|
|
|
|
}
|
|
|
|
|
2016-04-01 20:20:18 +00:00
|
|
|
// FieldNames are for the formating and rendering of the CommitEntry structs.
|
|
|
|
// Keys here are from git log pretty format "format:..."
|
|
|
|
var FieldNames = map[string]string{
|
|
|
|
"%h": "abbreviated_commit",
|
|
|
|
"%p": "abbreviated_parent",
|
|
|
|
"%t": "abbreviated_tree",
|
|
|
|
"%aD": "author_date",
|
|
|
|
"%aE": "author_email",
|
|
|
|
"%aN": "author_name",
|
|
|
|
"%b": "body",
|
|
|
|
"%H": "commit",
|
|
|
|
"%N": "commit_notes",
|
|
|
|
"%cD": "committer_date",
|
|
|
|
"%cE": "committer_email",
|
|
|
|
"%cN": "committer_name",
|
|
|
|
"%e": "encoding",
|
|
|
|
"%P": "parent",
|
|
|
|
"%D": "refs",
|
|
|
|
"%f": "sanitized_subject_line",
|
|
|
|
"%GS": "signer",
|
|
|
|
"%GK": "signer_key",
|
|
|
|
"%s": "subject",
|
|
|
|
"%G?": "verification_flag",
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:10:05 +00:00
|
|
|
func gitVersion() (string, error) {
|
|
|
|
cmd := exec.Command("git", "version")
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
buf, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Fields(string(buf))[2], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/vbatts/git-validation/issues/37
|
|
|
|
var versionWithExcludes = "1.9.5"
|
|
|
|
|
|
|
|
func gitVersionNewerThan(otherV string) (bool, error) {
|
|
|
|
gv, err := gitVersion()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
v1, err := version.NewVersion(gv)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
v2, err := version.NewVersion(otherV)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return v2.Equal(v1) || v2.LessThan(v1), nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:33:31 +00:00
|
|
|
// Check warns if changes introduce whitespace errors.
|
|
|
|
// Returns non-zero if any issues are found.
|
|
|
|
func Check(commit string) ([]byte, error) {
|
2017-04-26 21:29:57 +00:00
|
|
|
args := []string{
|
|
|
|
"--no-pager", "log", "--check",
|
|
|
|
fmt.Sprintf("%s^..%s", commit, commit),
|
|
|
|
}
|
2018-03-06 06:48:24 +00:00
|
|
|
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
|
2018-09-25 18:10:05 +00:00
|
|
|
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gitNewEnough {
|
|
|
|
excludeList := strings.Split(excludeEnvList, ":")
|
|
|
|
for _, exclude := range excludeList {
|
2019-09-10 20:33:44 +00:00
|
|
|
if exclude == "" {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-25 18:10:05 +00:00
|
|
|
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
|
|
|
|
}
|
2018-03-06 06:48:24 +00:00
|
|
|
}
|
2017-04-26 21:29:57 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command("git", args...)
|
2016-04-06 17:36:43 +00:00
|
|
|
if debug() {
|
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
|
|
|
|
}
|
2016-04-06 16:33:31 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Output()
|
|
|
|
}
|
|
|
|
|
2016-04-05 14:11:34 +00:00
|
|
|
// Show returns the diff of a commit.
|
|
|
|
//
|
|
|
|
// NOTE: This could be expensive for very large commits.
|
|
|
|
func Show(commit string) ([]byte, error) {
|
2016-04-06 17:36:43 +00:00
|
|
|
cmd := exec.Command("git", "--no-pager", "show", commit)
|
|
|
|
if debug() {
|
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
|
|
|
|
}
|
2016-04-05 14:11:34 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
2016-04-06 16:33:31 +00:00
|
|
|
return cmd.Output()
|
2016-04-05 14:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 20:20:18 +00:00
|
|
|
// CommitEntry represents a single commit's information from `git`.
|
|
|
|
// See also FieldNames
|
2015-10-05 20:16:52 +00:00
|
|
|
type CommitEntry map[string]string
|
|
|
|
|
|
|
|
// LogCommit assembles the full information on a commit from its commit hash
|
|
|
|
func LogCommit(commit string) (*CommitEntry, error) {
|
|
|
|
c := CommitEntry{}
|
2016-04-01 20:47:39 +00:00
|
|
|
for k, v := range FieldNames {
|
2016-04-06 17:36:43 +00:00
|
|
|
cmd := exec.Command("git", "--no-pager", "log", "-1", `--pretty=format:`+k+``, commit)
|
|
|
|
if debug() {
|
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
|
|
|
|
}
|
2016-04-01 20:47:39 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
2016-04-05 18:53:57 +00:00
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2017-03-21 20:17:56 +00:00
|
|
|
logrus.Errorf("[git] cmd: %q", strings.Join(cmd.Args, " "))
|
2015-10-05 20:16:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-03 22:25:59 +00:00
|
|
|
commitMessage := strings.ReplaceAll(string(out), "\r\n", "\n")
|
|
|
|
c[v] = strings.TrimSpace(commitMessage)
|
2015-10-05 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:44:04 +00:00
|
|
|
func debug() bool {
|
|
|
|
return len(os.Getenv("DEBUG")) > 0
|
|
|
|
}
|
|
|
|
|
2015-10-05 20:16:52 +00:00
|
|
|
// FetchHeadCommit returns the hash of FETCH_HEAD
|
|
|
|
func FetchHeadCommit() (string, error) {
|
2016-04-06 17:36:43 +00:00
|
|
|
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "FETCH_HEAD"}
|
2015-10-06 14:44:04 +00:00
|
|
|
if debug() {
|
2015-10-05 22:45:33 +00:00
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
|
|
|
}
|
|
|
|
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
|
2015-10-05 20:16:52 +00:00
|
|
|
if err != nil {
|
2017-03-21 20:17:56 +00:00
|
|
|
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
2015-10-05 20:16:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(output)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadCommit returns the hash of HEAD
|
|
|
|
func HeadCommit() (string, error) {
|
2016-04-06 17:36:43 +00:00
|
|
|
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "HEAD"}
|
2015-10-06 14:44:04 +00:00
|
|
|
if debug() {
|
2015-10-05 22:45:33 +00:00
|
|
|
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
|
|
|
}
|
|
|
|
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
|
2015-10-05 20:16:52 +00:00
|
|
|
if err != nil {
|
2017-03-21 20:17:56 +00:00
|
|
|
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
2015-10-05 20:16:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(output)), nil
|
|
|
|
}
|