diff --git a/git/commits.go b/git/commits.go index 7516815..1e2c484 100644 --- a/git/commits.go +++ b/git/commits.go @@ -8,13 +8,19 @@ import ( "os" "os/exec" "strings" + + "github.com/Sirupsen/logrus" ) // 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) { - 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 { return nil, err } @@ -34,6 +40,9 @@ func Commits(commitrange string) ([]CommitEntry, error) { type CommitEntry map[string]string var ( + // Verbose output of commands run + Verbose = false + prettyFormat = `--pretty=format:` formatSubject = `%s` formatBody = `%b` @@ -50,7 +59,11 @@ var ( // LogCommit assembles the full information on a commit from its commit hash func LogCommit(commit string) (*CommitEntry, error) { 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.Stderr = os.Stderr @@ -88,7 +101,11 @@ func LogCommit(commit string) (*CommitEntry, error) { // FetchHeadCommit returns the hash of FETCH_HEAD 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 { return "", err } @@ -97,7 +114,11 @@ func FetchHeadCommit() (string, error) { // HeadCommit returns the hash of HEAD 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 { return "", err } diff --git a/main.go b/main.go index 5bfa236..ec022f2 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,10 @@ var ( func main() { flag.Parse() + if *flVerbose { + git.Verbose = true + } + if *flListRules { for _, r := range validate.RegisteredRules { fmt.Printf("%q -- %s\n", r.Name, r.Description) @@ -40,7 +44,10 @@ func main() { var err error commitrange, err = git.FetchHeadCommit() if err != nil { - log.Fatal(err) + commitrange, err = git.HeadCommit() + if err != nil { + log.Fatal(err) + } } } @@ -56,20 +63,18 @@ func main() { results = append(results, vr...) if _, fail := vr.PassFail(); fail == 0 { fmt.Println("PASS") - if *flVerbose { - for _, r := range vr { - if r.Pass { - fmt.Printf(" - %s\n", r.Msg) - } - } - } } else { 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) } } }