(commitLog string)
| 75 | } |
| 76 | |
| 77 | func parseLocalCommitStack(commitLog string) ([]Commit, bool) { |
| 78 | var commits []Commit |
| 79 | |
| 80 | commitHashRegex := regexp.MustCompile(`^commit ([a-f0-9]{40})`) |
| 81 | commitIDRegex := regexp.MustCompile(`commit-id\:\s*([a-f0-9]{8})`) |
| 82 | |
| 83 | // The list of commits from the command line actually starts at the |
| 84 | // most recent commit. In order to reverse the list we use a |
| 85 | // custom prepend function instead of append |
| 86 | prepend := func(l []Commit, c Commit) []Commit { |
| 87 | l = append(l, Commit{}) |
| 88 | copy(l[1:], l) |
| 89 | l[0] = c |
| 90 | return l |
| 91 | } |
| 92 | |
| 93 | // commitScanOn is set to true when the commit hash is matched |
| 94 | // and turns false when the commit-id is matched. |
| 95 | // Commit messages always start with a hash and end with a commit-id. |
| 96 | // The commit subject and body are always between the hash the commit-id. |
| 97 | commitScanOn := false |
| 98 | |
| 99 | subjectIndex := 0 |
| 100 | var scannedCommit Commit |
| 101 | |
| 102 | lines := strings.Split(commitLog, "\n") |
| 103 | log.Debug().Int("lines", len(lines)).Msg("parseLocalCommitStack") |
| 104 | for index, line := range lines { |
| 105 | |
| 106 | // match commit hash : start of a new commit |
| 107 | matches := commitHashRegex.FindStringSubmatch(line) |
| 108 | if matches != nil { |
| 109 | log.Debug().Interface("matches", matches).Msg("parseLocalCommitStack :: commitHashMatch") |
| 110 | if commitScanOn { |
| 111 | // missing the commit-id |
| 112 | log.Debug().Msg("parseLocalCommitStack :: missing commit id") |
| 113 | return nil, false |
| 114 | } |
| 115 | commitScanOn = true |
| 116 | scannedCommit = Commit{ |
| 117 | CommitHash: matches[1], |
| 118 | } |
| 119 | subjectIndex = index + 4 |
| 120 | } |
| 121 | |
| 122 | // match commit id : last thing in the commit |
| 123 | matches = commitIDRegex.FindStringSubmatch(line) |
| 124 | if matches != nil { |
| 125 | log.Debug().Interface("matches", matches).Msg("parseLocalCommitStack :: commitIdMatch") |
| 126 | scannedCommit.CommitID = matches[1] |
| 127 | scannedCommit.Body = strings.TrimSpace(scannedCommit.Body) |
| 128 | |
| 129 | if strings.HasPrefix(scannedCommit.Subject, "WIP") { |
| 130 | scannedCommit.WIP = true |
| 131 | } |
| 132 | |
| 133 | commits = prepend(commits, scannedCommit) |
| 134 | commitScanOn = false |
no outgoing calls