Merge pull request #1462 from git-pkgs/detect-dot-git-bare

pkg/gitinterface: open go-git with DetectDotGit:false
This commit is contained in:
Aditya Sirish A Yelgundhalli 2026-07-23 12:50:27 -04:00 committed by GitHub
commit 1ded25b6fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View file

@ -153,7 +153,11 @@ func isBareGitDir(path string) bool {
// GetGoGitRepository returns the go-git representation of a repository. We use
// this in certain signing and verifying workflows.
func (r *Repository) GetGoGitRepository() (*git.Repository, error) {
return git.PlainOpenWithOptions(r.gitDirPath, &git.PlainOpenOptions{DetectDotGit: true})
// gitDirPath is already the resolved git directory (set via
// `git rev-parse --git-dir` in LoadRepository), so DetectDotGit must be
// false: with it true, go-git looks for a .git entry inside this path,
// which a bare repository does not have, and returns ErrRepositoryNotExists.
return git.PlainOpenWithOptions(r.gitDirPath, &git.PlainOpenOptions{DetectDotGit: false})
}
// GetGitDir returns the GIT_DIR path for the repository.

View file

@ -229,4 +229,34 @@ func TestRepositoryObjectFormat(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, ObjectFormatSHA256, loaded.GetObjectFormat())
})
t.Run("GetGoGitRepository", func(t *testing.T) {
for _, bare := range []bool{false, true} {
name := "worktree"
if bare {
name = "bare"
}
t.Run(name, func(t *testing.T) {
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, bare)
ggr, err := repo.GetGoGitRepository()
require.NoError(t, err, "GetGoGitRepository on %s repo", name)
_, err = ggr.Head()
// Empty repo: ErrReferenceNotFound is fine; what matters is the
// repo opened. Anything else (esp. ErrRepositoryNotExists) is a
// failure to open the storage.
if err != nil {
assert.ErrorContains(t, err, "reference not found")
}
})
}
})
t.Run("GetGoGitRepository on .git-suffixed bare repo", func(t *testing.T) {
// Forge-style bare repos are conventionally named <name>.git.
dir := filepath.Join(t.TempDir(), "demo.git")
repo := CreateTestGitRepository(t, dir, true)
_, err := repo.GetGoGitRepository()
require.NoError(t, err)
})
}