Skip to content

Commit 2ecc394

Browse files
committed
fix(crafter): handle invalid merge branch config during repo open
Gracefully handle "branch config: invalid merge" errors from go-git when opening a repository, similar to the existing protection for remote listing. This prevents the crafter from crashing when the local git config has an invalid merge reference. Closes #3031 Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 1586bad commit 2ecc394

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

pkg/attestation/crafter/crafter.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ var errBranchInvalidMerge = errors.New("branch config: invalid merge")
310310

311311
// Returns the current directory git commit hash if possible
312312
// If we are not in a git repo it will return an empty string
313-
func gracefulGitRepoHead(path string) (*HeadCommit, error) {
313+
func gracefulGitRepoHead(path string, logger *zerolog.Logger) (*HeadCommit, error) {
314+
if logger == nil {
315+
l := zerolog.Nop()
316+
logger = &l
317+
}
318+
314319
repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{
315320
// walk up the directory tree until we find a git repo
316321
DetectDotGit: true,
@@ -329,6 +334,12 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
329334
return nil, nil
330335
}
331336

337+
// This error is not exposed by go-git so we do a string comparison
338+
if isBranchInvalidMergeError(err) {
339+
logger.Warn().Err(err).Msg("branch is invalid merge, skipping commit")
340+
return nil, nil
341+
}
342+
332343
return nil, fmt.Errorf("opening repository: %w", err)
333344
}
334345

@@ -361,8 +372,9 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
361372
// we do not care about that use-case, so we ignore the error
362373
// we compare by error string because go-git does not expose the error type
363374
// and errors.Is require the same instance of the error
364-
if err.Error() == errBranchInvalidMerge.Error() {
365-
return c, nil
375+
if isBranchInvalidMergeError(err) {
376+
logger.Warn().Err(err).Msg("branch is invalid merge, skipping remotes")
377+
return nil, nil
366378
}
367379

368380
return nil, fmt.Errorf("getting remotes: %w", err)
@@ -387,6 +399,10 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
387399
return c, nil
388400
}
389401

402+
func isBranchInvalidMergeError(err error) bool {
403+
return err.Error() == errBranchInvalidMerge.Error()
404+
}
405+
390406
// Clear any basic auth credentials from the remote URL
391407
func sanitizeRemoteURL(remoteURL string) (string, error) {
392408
uri, err := url.Parse(remoteURL)
@@ -409,7 +425,7 @@ func initialCraftingState(cwd string, opts *InitOpts) (*api.CraftingState, error
409425
return nil, errors.New("required init options not provided")
410426
}
411427
// Get git commit hash
412-
headCommit, err := gracefulGitRepoHead(cwd)
428+
headCommit, err := gracefulGitRepoHead(cwd, opts.Logger)
413429
if err != nil {
414430
return nil, fmt.Errorf("getting git commit hash: %w", err)
415431
}

pkg/attestation/crafter/crafter_unit_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/go-git/go-git/v5"
2828
"github.com/go-git/go-git/v5/config"
2929
"github.com/go-git/go-git/v5/plumbing/object"
30+
"github.com/rs/zerolog"
3031
"github.com/stretchr/testify/assert"
3132
"github.com/stretchr/testify/require"
3233
"github.com/stretchr/testify/suite"
@@ -190,7 +191,8 @@ func (s *crafterUnitSuite) TestGitRepoHead() {
190191
require.NoError(s.T(), err)
191192
}
192193

193-
got, err := gracefulGitRepoHead(path)
194+
l := zerolog.Nop()
195+
got, err := gracefulGitRepoHead(path, &l)
194196
if tc.wantErr {
195197
assert.Error(s.T(), err)
196198
return
@@ -358,7 +360,8 @@ func (s *crafterUnitSuite) TestGitRepoHeadWorktree() {
358360
out, err := cmd.CombinedOutput()
359361
require.NoError(s.T(), err, "git worktree add: %s", out)
360362

361-
got, err := gracefulGitRepoHead(worktreePath)
363+
l := zerolog.Nop()
364+
got, err := gracefulGitRepoHead(worktreePath, &l)
362365
require.NoError(s.T(), err)
363366
require.NotNil(s.T(), got)
364367

0 commit comments

Comments
 (0)