Skip to content

Commit

Permalink
add ability to save blame to file
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Nov 4, 2023
1 parent 771e0c3 commit 1b22b47
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
3 changes: 2 additions & 1 deletion internal/experience/experience.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func (e *ExperienceCalculator) CalculateExperience(rootPath string, exclusions [
return nil, err
}

log.Println("Blamed files: ", len(blames))
log.Println("Blamed files: ", len(blames.Files))
blames.ToFile("blames.txt")

Check failure on line 46 in internal/experience/experience.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `blames.ToFile` is not checked (errcheck)
return nil, nil

Check failure on line 47 in internal/experience/experience.go

View workflow job for this annotation

GitHub Actions / Lint

return both the `nil` error and invalid value: use a sentinel error instead (nilnil)
}

Expand Down
43 changes: 36 additions & 7 deletions internal/git/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package git
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -29,6 +31,30 @@ func NewBlamer(repository *git.Repository) *Blamer {
}
}

type BlameFiles struct {
Files []BlameFile
}

func (b *BlameFiles) ToFile(outputFile string) error {

file, err := os.Create(outputFile)
if err != nil {
return err
}
defer file.Close()

for _, blameFile := range b.Files {
for _, line := range blameFile.Lines {
_, err := file.WriteString(fmt.Sprintf("%s,%d,%s,%s\n", blameFile.Path, line.LineNumber, line.Author.Name, line.Author.Email))
if err != nil {
return err
}
}
}

return nil
}

type BlameFile struct {
Lines []BlameLine
Path string
Expand Down Expand Up @@ -86,7 +112,7 @@ func gitBlameFile(filePath string) ([]BlameLine, error) {
return blameLines, nil
}

func (b *Blamer) BlamAllFiles() ([]BlameFile, error) {
func (b *Blamer) BlamAllFiles() (*BlameFiles, error) {
files, err := FindAllTrackedFiles(b.repository)
if err != nil {
return nil, err
Expand Down Expand Up @@ -142,11 +168,14 @@ func (b *Blamer) BlamAllFiles() ([]BlameFile, error) {
blameFiles = append(blameFiles, bf)
}

// for err := range errChan {
// if err != nil {
// return nil, err
// }
// }
for err := range errChan {
if err != nil {
return nil, err
}
}

return &BlameFiles{
Files: blameFiles,
}, nil

return blameFiles, nil
}
52 changes: 48 additions & 4 deletions internal/git/blame_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package git

import (
"io/ioutil"

Check failure on line 4 in internal/git/blame_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"os"
"testing"
)

Expand All @@ -19,14 +21,56 @@ func TestBlame(t *testing.T) {
t.Fatal("failed to blame file. Error:", err)
}

if len(blameRes[0].Lines) == 0 {
t.Fatal("Should be larger than 0 lines, was", len(blameRes[0].Lines))
if len(blameRes.Files[0].Lines) == 0 {
t.Fatal("Should be larger than 0 lines, was", len(blameRes.Files[0].Lines))
}

if blameRes[0].Lines[0].Author.Name == "" {
if blameRes.Files[0].Lines[0].Author.Name == "" {
t.Fatal("Author should not be empty")
}
if blameRes[0].Lines[0].Author.Email == "" {
if blameRes.Files[0].Lines[0].Author.Email == "" {
t.Fatal("Email should not be empty")
}
}

func TestToFile(t *testing.T) {

tempFile, err := os.CreateTemp("", "test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempFile.Name()) // clean up

blamefiles := BlameFiles{
Files: []BlameFile{
{
Lines: []BlameLine{
{
Author: Author{
Email: "[email protected]",
Name: "Example",
},
LineNumber: 1,
},
},
Path: "example.txt",
},
},
}

err = blamefiles.ToFile(tempFile.Name())
if err != nil {
t.Fatal("failed to write to file. Error:", err)
}

content, err := ioutil.ReadFile(tempFile.Name())
if err != nil {
t.Fatal(err)
}

expected := "example.txt,1,Example,[email protected]\n"
if string(content) != expected {
t.Errorf("Expected %s, got %s", expected, content)
}

}

0 comments on commit 1b22b47

Please sign in to comment.