diff --git a/TODO.md b/TODO.md index c4cdd0cf8..2a8f4e357 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,13 @@ +## Fix: git worktree HEAD resolution + +- [x] Slice 1: Enable `EnableDotGitCommonDir` in `gitview.New()` so HEAD resolves in worktrees + - [x] Test: `New()` succeeds when called from a git worktree path + - [x] Test: `BranchName()` returns correct branch when called from a worktree + - [x] Test: `GetCommitInfoFromCommitSHA("HEAD", ...)` works from a worktree + ## kosli evaluate trail - [x] Slice 1: Skeleton `evaluate` parent + `evaluate trail` fetches trail JSON diff --git a/internal/gitview/gitView.go b/internal/gitview/gitView.go index a2c75f19f..dc8ffcd1b 100644 --- a/internal/gitview/gitView.go +++ b/internal/gitview/gitView.go @@ -49,7 +49,9 @@ const redactedCommitInfoValue = "**REDACTED**" // repository is bare or a normal one. If the path doesn't contain a valid // repository ErrRepositoryNotExists is returned func New(repositoryRoot string) (*GitView, error) { - repository, err := git.PlainOpen(repositoryRoot) + repository, err := git.PlainOpenWithOptions(repositoryRoot, &git.PlainOpenOptions{ + EnableDotGitCommonDir: true, + }) if err != nil { return nil, fmt.Errorf("failed to open git repository at %s: %v", repositoryRoot, err) } diff --git a/internal/gitview/gitView_test.go b/internal/gitview/gitView_test.go index 212ffb431..2ff79040f 100644 --- a/internal/gitview/gitView_test.go +++ b/internal/gitview/gitView_test.go @@ -3,6 +3,7 @@ package gitview import ( "fmt" "os" + "os/exec" "path/filepath" "testing" @@ -54,6 +55,31 @@ func (suite *GitViewTestSuite) TestNewGitView() { require.Error(suite.T(), err) } +func (suite *GitViewTestSuite) TestNewGitViewFromWorktree() { + dirPath := filepath.Join(suite.tmpDir, "repoName") + _, _, err := initializeRepoAndCommit(dirPath, 1) + require.NoError(suite.T(), err) + + worktreePath := filepath.Join(suite.tmpDir, "myWorktree") + cmd := exec.Command("git", "worktree", "add", "-b", "worktree-branch", worktreePath) + cmd.Dir = dirPath + output, err := cmd.CombinedOutput() + require.NoError(suite.T(), err, "git worktree add failed: %s", string(output)) + + gv, err := New(worktreePath) + require.NoError(suite.T(), err) + require.NotNil(suite.T(), gv) + + branchName, err := gv.BranchName() + require.NoError(suite.T(), err) + require.Equal(suite.T(), "worktree-branch", branchName) + + commitInfo, err := gv.GetCommitInfoFromCommitSHA("HEAD", true, []string{}) + require.NoError(suite.T(), err) + require.NotEmpty(suite.T(), commitInfo.Sha1) + require.Equal(suite.T(), "worktree-branch", commitInfo.Branch) +} + func (suite *GitViewTestSuite) TestCommitsBetween() { for i, t := range []struct { name string