2015-10-05 20:16:52 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-10-05 22:45:33 +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.
|
|
|
|
func Commits(commitrange string) ([]CommitEntry, error) {
|
2016-04-01 20:47:39 +00:00
|
|
|
cmdArgs := []string{"git", "log", `--pretty=format:%H`, 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-06 17:36:43 +00:00
|
|
|
cmd := exec.Command("git", "--no-pager", "show", "--check", commit)
|
|
|
|
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 {
|
2015-10-05 20:16:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-05 18:53:57 +00:00
|
|
|
c[v] = strings.TrimSpace(string(out))
|
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 {
|
|
|
|
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 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(output)), nil
|
|
|
|
}
|