Skip to content

Commit 134c3aa

Browse files
committed
fix: revert go-git to v5.16.5 and handle extension errors gracefully
go-git v5.17.0 introduced strict repository extension validation (go-git/go-git#1861) with a case-sensitivity bug that rejects the worktreeConfig extension, breaking attestation init in repos using git worktree. Reverts to v5.16.5 and adds defensive handling in gracefulGitRepoHead so unsupported extension errors degrade gracefully instead of failing the attestation. Closes #2966 Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent d88676d commit 134c3aa

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ require (
353353
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
354354
github.com/fsnotify/fsnotify v1.9.0 // indirect
355355
github.com/fsouza/fake-gcs-server v1.47.6
356-
github.com/go-git/go-git/v5 v5.17.1
356+
github.com/go-git/go-git/v5 v5.16.5
357357
github.com/go-kratos/aegis v0.2.0 // indirect
358358
github.com/go-logr/logr v1.4.3 // indirect
359359
github.com/go-logr/stdr v1.2.2 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ github.com/go-git/go-billy/v5 v5.8.0/go.mod h1:RpvI/rw4Vr5QA+Z60c6d6LXH0rYJo0uD5
449449
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
450450
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
451451
github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY=
452-
github.com/go-git/go-git/v5 v5.17.1 h1:WnljyxIzSj9BRRUlnmAU35ohDsjRK0EKmL0evDqi5Jk=
453-
github.com/go-git/go-git/v5 v5.17.1/go.mod h1:pW/VmeqkanRFqR6AljLcs7EA7FbZaN5MQqO7oZADXpo=
452+
github.com/go-git/go-git/v5 v5.16.5 h1:mdkuqblwr57kVfXri5TTH+nMFLNUxIj9Z7F5ykFbw5s=
453+
github.com/go-git/go-git/v5 v5.16.5/go.mod h1:QOMLpNf1qxuSY4StA/ArOdfFR2TrKEjJiye2kel2m+M=
454454
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
455455
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
456456
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

pkg/attestation/crafter/crafter.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ type CommitRemote struct {
312312
// This error is not exposed by go-git
313313
var errBranchInvalidMerge = errors.New("branch config: invalid merge")
314314

315+
// isGitExtensionError returns true if the error is related to unsupported git
316+
// repository format extensions (e.g. worktreeConfig). go-git v5.17.0 introduced
317+
// strict validation that rejects repos with extensions it doesn't support.
318+
// See https://github.com/go-git/go-git/pull/1861
319+
func isGitExtensionError(err error) bool {
320+
if err == nil {
321+
return false
322+
}
323+
msg := err.Error()
324+
return strings.Contains(msg, "does not support extension") ||
325+
strings.Contains(msg, "unknown extension") ||
326+
strings.Contains(msg, "repositoryformatversion not supported")
327+
}
328+
315329
// Returns the current directory git commit hash if possible
316330
// If we are not in a git repo it will return an empty string
317331
func gracefulGitRepoHead(path string) (*HeadCommit, error) {
@@ -321,7 +335,8 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
321335
})
322336

323337
if err != nil {
324-
if errors.Is(err, git.ErrRepositoryNotExists) {
338+
if errors.Is(err, git.ErrRepositoryNotExists) ||
339+
isGitExtensionError(err) {
325340
return nil, nil
326341
}
327342

pkg/attestation/crafter/crafter_unit_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ func (s *crafterUnitSuite) TestGitRepoHead() {
152152
name: "not a repository",
153153
wantNoCommit: true,
154154
},
155+
{
156+
name: "repo with unsupported extension degrades gracefully",
157+
repoProvider: func(repoPath string) (*HeadCommit, error) {
158+
// Init a repo and add a worktreeConfig extension to trigger
159+
// go-git's strict extension validation (added in v5.17.0)
160+
if _, err := git.PlainInit(repoPath, false); err != nil {
161+
return nil, err
162+
}
163+
164+
// Write the extension directly into the git config file
165+
gitConfigPath := filepath.Join(repoPath, ".git", "config")
166+
f, err := os.OpenFile(gitConfigPath, os.O_APPEND|os.O_WRONLY, 0o600)
167+
if err != nil {
168+
return nil, err
169+
}
170+
defer f.Close()
171+
if _, err := f.WriteString("[extensions]\n\tworktreeConfig = true\n"); err != nil {
172+
return nil, err
173+
}
174+
175+
return nil, nil
176+
},
177+
wantNoCommit: true,
178+
},
155179
}
156180

157181
for _, tc := range testCases {

0 commit comments

Comments
 (0)