diff --git a/internal/gitview/gitView.go b/internal/gitview/gitView.go index ae13f6133..8b682e6e9 100644 --- a/internal/gitview/gitView.go +++ b/internal/gitview/gitView.go @@ -1,6 +1,7 @@ package gitview import ( + "errors" "fmt" "net/url" "strings" @@ -45,6 +46,16 @@ type GitView struct { const redactedCommitInfoValue = "**REDACTED**" +// TODO: remove this guard once go-git supports sha256 (v6) and the Kosli backend accepts sha256 commits. + +// ErrUnsupportedObjectFormat is returned by New for a repository using the sha256 object format. +var ErrUnsupportedObjectFormat = errors.New("the repository uses the sha256 object format, which the Kosli CLI does not support yet") + +func isUnsupportedObjectFormat(err error) bool { + return (errors.Is(err, git.ErrUnsupportedExtensionRepositoryFormatVersion) || errors.Is(err, git.ErrUnknownExtension)) && + strings.Contains(err.Error(), "objectformat") +} + // New opens a git repository from the given path. It detects if the // repository is bare or a normal one. If the path doesn't contain a valid // repository ErrRepositoryNotExists is returned @@ -52,6 +63,9 @@ func New(repositoryRoot string) (*GitView, error) { repository, err := git.PlainOpenWithOptions(repositoryRoot, &git.PlainOpenOptions{ EnableDotGitCommonDir: true, }) + if isUnsupportedObjectFormat(err) { + return nil, fmt.Errorf("failed to open git repository at %s: %w (%v)", repositoryRoot, ErrUnsupportedObjectFormat, err) + } 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 63fc1bbbd..5c465ee3b 100644 --- a/internal/gitview/gitView_test.go +++ b/internal/gitview/gitView_test.go @@ -51,6 +51,18 @@ func (suite *GitViewTestSuite) TestNewGitView() { _, err = New(filepath.Join(suite.tmpDir, "non-existing")) require.Error(suite.T(), err) + require.NotErrorIs(suite.T(), err, ErrUnsupportedObjectFormat) +} + +func (suite *GitViewTestSuite) TestNewGitViewSha256Repo() { + dirPath := filepath.Join(suite.tmpDir, "sha256Repo") + cmd := exec.Command("git", "init", "--object-format=sha256", dirPath) + output, err := cmd.CombinedOutput() + require.NoError(suite.T(), err, "git init failed: %s", string(output)) + + _, err = New(dirPath) + require.ErrorIs(suite.T(), err, ErrUnsupportedObjectFormat) + require.Contains(suite.T(), err.Error(), "sha256") } func (suite *GitViewTestSuite) TestNewGitViewFromWorktree() {