1
0
Fork 0
mirror of https://github.com/vbatts/git-validation.git synced 2024-12-28 16:26:31 +00:00

git: add verbose output of the commands run

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2015-10-05 18:45:33 -04:00
parent 03bda4bcb2
commit 07a982ff94
2 changed files with 42 additions and 16 deletions

View file

@ -8,13 +8,19 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"github.com/Sirupsen/logrus"
) )
// Commits returns a set of commits. // 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 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. // If commitrange is a single commit, all ancestor commits up through the hash provided.
func Commits(commitrange string) ([]CommitEntry, error) { func Commits(commitrange string) ([]CommitEntry, error) {
output, err := exec.Command("git", "log", prettyFormat+formatCommit, commitrange).Output() cmdArgs := []string{"git", "log", prettyFormat + formatCommit, commitrange}
if Verbose {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -34,6 +40,9 @@ func Commits(commitrange string) ([]CommitEntry, error) {
type CommitEntry map[string]string type CommitEntry map[string]string
var ( var (
// Verbose output of commands run
Verbose = false
prettyFormat = `--pretty=format:` prettyFormat = `--pretty=format:`
formatSubject = `%s` formatSubject = `%s`
formatBody = `%b` formatBody = `%b`
@ -50,7 +59,11 @@ var (
// LogCommit assembles the full information on a commit from its commit hash // LogCommit assembles the full information on a commit from its commit hash
func LogCommit(commit string) (*CommitEntry, error) { func LogCommit(commit string) (*CommitEntry, error) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := exec.Command("git", "log", "-1", prettyFormat+formatMap, commit) cmdArgs := []string{"git", "log", "-1", prettyFormat + formatMap, commit}
if Verbose {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Stdout = buf cmd.Stdout = buf
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -88,7 +101,11 @@ func LogCommit(commit string) (*CommitEntry, error) {
// FetchHeadCommit returns the hash of FETCH_HEAD // FetchHeadCommit returns the hash of FETCH_HEAD
func FetchHeadCommit() (string, error) { func FetchHeadCommit() (string, error) {
output, err := exec.Command("git", "rev-parse", "--verify", "FETCH_HEAD").Output() cmdArgs := []string{"git", "rev-parse", "--verify", "FETCH_HEAD"}
if Verbose {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -97,7 +114,11 @@ func FetchHeadCommit() (string, error) {
// HeadCommit returns the hash of HEAD // HeadCommit returns the hash of HEAD
func HeadCommit() (string, error) { func HeadCommit() (string, error) {
output, err := exec.Command("git", "rev-parse", "--verify", "HEAD").Output() cmdArgs := []string{"git", "rev-parse", "--verify", "HEAD"}
if Verbose {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil { if err != nil {
return "", err return "", err
} }

27
main.go
View file

@ -21,6 +21,10 @@ var (
func main() { func main() {
flag.Parse() flag.Parse()
if *flVerbose {
git.Verbose = true
}
if *flListRules { if *flListRules {
for _, r := range validate.RegisteredRules { for _, r := range validate.RegisteredRules {
fmt.Printf("%q -- %s\n", r.Name, r.Description) fmt.Printf("%q -- %s\n", r.Name, r.Description)
@ -39,10 +43,13 @@ func main() {
} else { } else {
var err error var err error
commitrange, err = git.FetchHeadCommit() commitrange, err = git.FetchHeadCommit()
if err != nil {
commitrange, err = git.HeadCommit()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
}
c, err := git.Commits(commitrange) c, err := git.Commits(commitrange)
if err != nil { if err != nil {
@ -56,20 +63,18 @@ func main() {
results = append(results, vr...) results = append(results, vr...)
if _, fail := vr.PassFail(); fail == 0 { if _, fail := vr.PassFail(); fail == 0 {
fmt.Println("PASS") fmt.Println("PASS")
if *flVerbose {
for _, r := range vr {
if r.Pass {
fmt.Printf(" - %s\n", r.Msg)
}
}
}
} else { } else {
fmt.Println("FAIL") fmt.Println("FAIL")
// default, only print out failed validations
for _, r := range vr {
if !r.Pass {
fmt.Printf(" - %s\n", r.Msg)
} }
for _, r := range vr {
if *flVerbose {
if r.Pass {
fmt.Printf("ok %s\n", r.Msg)
} else {
fmt.Printf("not ok %s\n", r.Msg)
}
} else if !r.Pass {
fmt.Printf("not ok %s\n", r.Msg)
} }
} }
} }