Skip to content

Commit

Permalink
[MER-2744] fix: look for correct git dir path. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichKramer authored Sep 7, 2023
1 parent 6360252 commit 7923ddf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
15 changes: 11 additions & 4 deletions cmd/av/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ var cachedRepo *git.Repo

func getRepo() (*git.Repo, error) {
if cachedRepo == nil {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd := exec.Command("git", "rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir")

if rootFlags.Directory != "" {
cmd.Dir = rootFlags.Directory
}
toplevel, err := cmd.Output()
paths, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(
err,
"failed to determine repo toplevel (are you running inside a Git repo?)",
"failed to find git directory (are you running inside a Git repo?)",
)
}
cachedRepo, err = git.OpenRepo(strings.TrimSpace(string(toplevel)))

dir, gitDir, found := strings.Cut(strings.TrimSpace(string(paths)), "\n" )
if !found {
return nil, errors.New("Unexpected format, not able to parse toplevel and common dir.")
}

cachedRepo, err = git.OpenRepo(dir, gitDir)
if err != nil {
return nil, errors.Wrap(err, "failed to open git repo")
}
Expand Down
6 changes: 4 additions & 2 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ var ErrRemoteNotFound = errors.Sentinel("this repository doesn't have a remote o

type Repo struct {
repoDir string
gitDir string
log logrus.FieldLogger
}

func OpenRepo(repoDir string) (*Repo, error) {
func OpenRepo(repoDir string, gitDir string) (*Repo, error) {
r := &Repo{
repoDir,
gitDir,
logrus.WithFields(logrus.Fields{"repo": path.Base(repoDir)}),
}

Expand All @@ -37,7 +39,7 @@ func (r *Repo) Dir() string {
}

func (r *Repo) GitDir() string {
return path.Join(r.repoDir, ".git")
return r.gitDir
}

func (r *Repo) AvDir() string {
Expand Down
3 changes: 2 additions & 1 deletion internal/git/gittest/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gittest
import (
"os"
"os/exec"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -49,7 +50,7 @@ func NewTempRepo(t *testing.T) *git.Repo {
err = remoteInit.Run()
require.NoError(t, err, "failed to initialize remote git repository")

repo, err := git.OpenRepo(dir)
repo, err := git.OpenRepo(dir, path.Join(dir, ".git"))
require.NoError(t, err, "failed to open repo")

settings := map[string]string{
Expand Down

0 comments on commit 7923ddf

Please sign in to comment.