Skip to content

Commit 6f5b21b

Browse files
authored
Merge pull request #60 from vbatts/fix_revlist
commits: add a fallback if rev-list fails due to shallow depth
2 parents 767ae83 + a041fe8 commit 6f5b21b

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

git/commits.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ import (
1515
// If commitrange is a single commit, all ancestor commits up through the hash provided.
1616
// If commitrange is an empty commit range, then nil is returned.
1717
func Commits(commitrange string) ([]CommitEntry, error) {
18+
// TEST if it has _enough_ of a range from rev-list
19+
commitrange, err := checkRevList(commitrange)
20+
if err != nil {
21+
logrus.Errorf("failed to validate the git commit range: %s", err)
22+
return nil, err
23+
}
1824
cmdArgs := []string{"git", "rev-list", commitrange}
1925
if debug() {
2026
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
2127
}
2228
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
2329
if err != nil {
24-
logrus.Errorf("mm[git] cmd: %q", strings.Join(cmdArgs, " "))
30+
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
2531
return nil, err
2632
}
2733
if len(output) == 0 {
@@ -39,6 +45,39 @@ func Commits(commitrange string) ([]CommitEntry, error) {
3945
return commits, nil
4046
}
4147

48+
// Since the commitrange requested may be longer than the depth being cloned in CI,
49+
// check for an error, if so do a git log to get the oldest available commit for a reduced range.
50+
func checkRevList(commitrange string) (string, error) {
51+
cmdArgs := []string{"git", "rev-list", commitrange}
52+
if debug() {
53+
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
54+
}
55+
_, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
56+
if err == nil {
57+
// no issues, return now
58+
return commitrange, nil
59+
}
60+
cmdArgs = []string{"git", "log", "--pretty=oneline"}
61+
if debug() {
62+
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
63+
}
64+
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
65+
if err != nil {
66+
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
67+
return "", err
68+
}
69+
// This "output" is now the list of available commits and short description.
70+
// We want the last commit hash only.. (i.e. `| tail -n1 | awk '{ print $1 }'`)
71+
chunks := strings.Split(strings.TrimSpace(string(output)), "\n")
72+
if len(chunks) == 1 {
73+
return strings.Split(chunks[0], " ")[0], nil
74+
}
75+
last := chunks[len(chunks)-1]
76+
lastCommit := strings.Split(last, " ")[0]
77+
78+
return fmt.Sprintf("%s..HEAD", lastCommit), nil
79+
}
80+
4281
// FieldNames are for the formating and rendering of the CommitEntry structs.
4382
// Keys here are from git log pretty format "format:..."
4483
var FieldNames = map[string]string{

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"os"
87
"strings"
98

9+
"github.com/sirupsen/logrus"
1010
_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
1111
_ "github.com/vbatts/git-validation/rules/dco"
1212
_ "github.com/vbatts/git-validation/rules/messageregexp"
@@ -62,7 +62,7 @@ func main() {
6262
rules = validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters(*flRun))
6363
}
6464
if os.Getenv("DEBUG") != "" {
65-
log.Printf("%#v", rules) // XXX maybe reduce this list
65+
logrus.Printf("%#v", rules) // XXX maybe reduce this list
6666
}
6767

6868
var commitRange = *flCommitRange
@@ -80,13 +80,14 @@ func main() {
8080
}
8181
}
8282

83+
logrus.Infof("using commit range: %s", commitRange)
8384
runner, err := validate.NewRunner(*flDir, rules, commitRange, *flVerbose)
8485
if err != nil {
85-
log.Fatal(err)
86+
logrus.Fatal(err)
8687
}
8788

8889
if err := runner.Run(); err != nil {
89-
log.Fatal(err)
90+
logrus.Fatal(err)
9091
}
9192
_, fail := runner.Results.PassFail()
9293
if fail > 0 {

0 commit comments

Comments
 (0)