From e0143d6e367a243d41ef155ffcc818b91ecc417e Mon Sep 17 00:00:00 2001 From: Felix Jung Date: Fri, 25 Oct 2024 13:17:44 +0200 Subject: [PATCH] Ensure git is always given an absolute repo path Add simple unit test for readFile Ensure all repo paths are absolute --- cmd/console.go | 6 +++++- cmd/html_report.go | 6 +++++- cmd/paths.go | 20 ++++++++++++++++++++ cmd/paths_test.go | 26 ++++++++++++++++++++++++++ cmd/report.go | 11 ++++------- cmd/summary.go | 7 +++++-- git/read_local.go | 8 ++++---- git/read_local_test.go | 6 ++++++ 8 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 cmd/paths.go create mode 100644 cmd/paths_test.go diff --git a/cmd/console.go b/cmd/console.go index ef48265..59063f3 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -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 { diff --git a/cmd/html_report.go b/cmd/html_report.go index 23d966b..04068e3 100644 --- a/cmd/html_report.go +++ b/cmd/html_report.go @@ -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 { diff --git a/cmd/paths.go b/cmd/paths.go new file mode 100644 index 0000000..48d96b2 --- /dev/null +++ b/cmd/paths.go @@ -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 +} diff --git a/cmd/paths_test.go b/cmd/paths_test.go new file mode 100644 index 0000000..d1798a1 --- /dev/null +++ b/cmd/paths_test.go @@ -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) + }) +} diff --git a/cmd/report.go b/cmd/report.go index 9acdf90..028ce5e 100644 --- a/cmd/report.go +++ b/cmd/report.go @@ -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)) diff --git a/cmd/summary.go b/cmd/summary.go index 73299be..b839583 100644 --- a/cmd/summary.go +++ b/cmd/summary.go @@ -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 { diff --git a/git/read_local.go b/git/read_local.go index e07d71f..bf9bd5d 100644 --- a/git/read_local.go +++ b/git/read_local.go @@ -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'", @@ -293,7 +293,7 @@ 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 @@ -301,7 +301,7 @@ func readFile(repoDir, hash, filePath string) ([]byte, []error) { 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 diff --git a/git/read_local_test.go b/git/read_local_test.go index 6f2ae03..7c454c8 100644 --- a/git/read_local_test.go +++ b/git/read_local_test.go @@ -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) +}