@@ -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.
1717func 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:..."
4483var FieldNames = map [string ]string {
0 commit comments