Skip to content

Commit

Permalink
Ensure git is always given an absolute repo path
Browse files Browse the repository at this point in the history
Add simple unit test for readFile

Ensure all repo paths are absolute
  • Loading branch information
felixjung committed Oct 25, 2024
1 parent 32a921a commit e0143d6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 15 deletions.
6 changes: 5 additions & 1 deletion cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ func GetConsoleCommand() *cobra.Command {
}

if f.IsDir() {
repo := p
repo, err := absoluteRepoPath(p)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
p = args[1]
f, err = os.Stat(filepath.Join(repo, p))
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ func GetHTMLReportCommand() *cobra.Command {
}

if f.IsDir() {
repo := p
repo, err := absoluteRepoPath(p)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
p = args[1]
f, err = os.Stat(filepath.Join(repo, p))
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions cmd/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"
"os"
"path"
"path/filepath"
)

func absoluteRepoPath(p string) (string, error) {
if !path.IsAbs(p) {
wd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("get working dir: %v", err)
}
return filepath.Join(wd, p), nil
}

return p, nil
}
26 changes: 26 additions & 0 deletions cmd/paths_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAbsoluteRepoPath(t *testing.T) {
t.Run("makes relative path absolute", func(t *testing.T) {
p := "repo"

have, err := absoluteRepoPath(p)
assert.NoError(t, err)
assert.True(t, filepath.IsAbs(have))
})

t.Run("returns absolute path", func(t *testing.T) {
p := "/home/user/repo"

have, err := absoluteRepoPath(p)
assert.NoError(t, err)
assert.Equal(t, p, have)
})
}
11 changes: 4 additions & 7 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,10 @@ func GetReportCommand() *cobra.Command {
}

if f.IsDir() {
repo := p
if !path.IsAbs(repo) {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("get working dir: %v", err)
}
repo = filepath.Join(wd, repo)
repo, err := absoluteRepoPath(p)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
p = args[1]
f, err = os.Stat(filepath.Join(repo, p))
Expand Down
7 changes: 5 additions & 2 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ func GetSummaryCommand() *cobra.Command {
}

if f.IsDir() {

repo := p
repo, err := absoluteRepoPath(p)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
p = args[1]
f, err = os.Stat(filepath.Join(repo, p))
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions git/read_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func PopulateHistoryWithChanges(commitHistory []*model.Commit, limit int, limitT
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]*model.Commit, []error) {

for c := range commitHistory {
var err []error
var err error
commitHistory[c].Data, err = readFile(commitHistory[c].RepoDirectory, commitHistory[c].Hash, commitHistory[c].FilePath)
if err != nil {
return nil, err
return nil, []error{err}
}
model.SendProgressUpdate("population",
fmt.Sprintf("Extracting %d bytes extracted from commit '%s'",
Expand Down Expand Up @@ -293,15 +293,15 @@ func ExtractPathAndFile(location string) (string, string) {

// readFile reads the specified file at the specified commit hash from the
// specified git repository.
func readFile(repoDir, hash, filePath string) ([]byte, []error) {
func readFile(repoDir, hash, filePath string) ([]byte, error) {
cmd := exec.Command(GIT, NOPAGER, SHOW, fmt.Sprintf("%s:%s", hash, filePath))
var ou, er bytes.Buffer
cmd.Stdout = &ou
cmd.Stderr = &er
cmd.Dir = repoDir
err := cmd.Run()
if err != nil {
return nil, []error{fmt.Errorf("read file from git: %v", err)}
return nil, fmt.Errorf("read file from git: %v", err)
}

return ou.Bytes(), nil
Expand Down
6 changes: 6 additions & 0 deletions git/read_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ func TestGetTopLevel(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, str)
}

func TestReadFile(t *testing.T) {
contentRaw, err := readFile("../", "HEAD", "./git/read_local.go")
assert.NoError(t, err)
assert.NotEmpty(t, contentRaw)
}

0 comments on commit e0143d6

Please sign in to comment.