Skip to content

Commit

Permalink
feat: get author from git history
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Apr 4, 2024
1 parent f2748b7 commit 6a8269e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/output/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (o MarkdownOutput) generate(report []scanner.Report) ([]byte, error) {
res = append(res, []string{
item.File,
strconv.Itoa(item.LineNumber),
item.Author,
fmt.Sprintf("%s <%s>", item.Author, item.AuthorEmail),
item.Msg,
})
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/scanner/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package scanner

type Report struct {
File string `json:"file"`
LineNumber int `json:"lineNumber"`
Author string `json:"author,omitempty"`
Msg string `json:"message,omitempty"`
File string `json:"file"`
LineNumber int `json:"lineNumber"`
Author string `json:"author,omitempty"`
AuthorEmail string `json:"authorEmail,omitempty"`
Msg string `json:"message,omitempty"`
}
54 changes: 48 additions & 6 deletions pkg/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ type Scan struct {
config *config.Config
}

func (s *Scan) getAuthorForLine(filename string, lineNumber int, defaultAuthor string) (author, authorEmail string, err error) {
repo, err := git.PlainOpen(s.config.WorkingDirectory)
if err != nil {
if err == git.ErrRepositoryNotExists {
// Line not under Git control - return the default author
err = nil
author = defaultAuthor
return
}
return
}

l, err := repo.Log(&git.LogOptions{
FileName: &filename,
})
if err != nil {
return
}

commit, err := l.Next()
if err != nil {
return
}

blame, err := git.Blame(commit, filename)
if err != nil {
return
}

author = blame.Lines[lineNumber].AuthorName
authorEmail = blame.Lines[lineNumber].Author

return
}

func (s *Scan) FindFilesByGit() ([]string, error) {
files := make([]string, 0)

Expand Down Expand Up @@ -186,8 +221,14 @@ func (s *Scan) scanFileForTodo(filename string) ([]Report, error) {

l1.WithField("matches", matches).Debug("Found matches")

// @todo(sje): get the author from the Git history
author := strings.TrimSpace(matches[4])
repoFilename := strings.Replace(filename, s.config.WorkingDirectory, "", 1)
repoFilename = strings.TrimLeft(repoFilename, "/")

author, authorEmail, err := s.getAuthorForLine(repoFilename, lineNumber, strings.TrimSpace(matches[4]))
if err != nil {
l1.WithError(err).Error("Error getting author")
return nil, err
}
msg := strings.TrimSpace(matches[6])

// Remove working directory
Expand All @@ -197,10 +238,11 @@ func (s *Scan) scanFileForTodo(filename string) ([]Report, error) {

// @todo(sje): add the URL to the file/line number
res = append(res, Report{
File: cleanFilename,
LineNumber: lineNumber,
Author: author,
Msg: msg,
File: cleanFilename,
LineNumber: lineNumber,
Author: author,
AuthorEmail: authorEmail,
Msg: msg,
})
}
}
Expand Down
5 changes: 2 additions & 3 deletions toodaloo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
| File | Line Number | Author | Message |
| --- | --- | --- | --- |
| pkg/output/markdown.go | 30 | sje | implement report |
| pkg/scanner/scan.go | 189 | sje | get the author from the Git history |
| pkg/scanner/scan.go | 198 | sje | add the URL to the file/line number |
| pkg/output/markdown.go | 30 | Simon Emms <[email protected]> | implement report |
| pkg/scanner/scan.go | 239 | Simon Emms <[email protected]> | add the URL to the file/line number |

0 comments on commit 6a8269e

Please sign in to comment.