Skip to content

Commit 0634fda

Browse files
committed
fix: skip the owner validation during parse the git source info
Signed-off-by: chlins <[email protected]>
1 parent 6b5f9c9 commit 0634fda

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

pkg/source/git_libgit2.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,53 @@ package source
2020

2121
import (
2222
"fmt"
23+
"path/filepath"
24+
"strings"
2325

2426
git2go "github.com/libgit2/git2go/v34"
2527
)
2628

29+
const (
30+
// The error returned by libgit2 when the user is not the owner of the git repository.
31+
safeDirectoryNotFoundErrorMsg = "config value 'safe.directory' was not found"
32+
)
33+
34+
// isSafeDirectoryNotFoundError checks if the error is a safe.directory not found error.
35+
func isSafeDirectoryNotFoundError(err error) bool {
36+
if err != nil {
37+
return strings.Contains(err.Error(), safeDirectoryNotFoundErrorMsg)
38+
}
39+
40+
return false
41+
}
42+
2743
type git struct{}
2844

2945
func (g *git) Parse(workspace string) (*Info, error) {
46+
absWorkspace, err := filepath.Abs(workspace)
47+
if err != nil {
48+
return nil, fmt.Errorf("failed to get absolute path of workspace: %w", err)
49+
}
50+
3051
repo, err := git2go.OpenRepository(workspace)
3152
if err != nil {
32-
return nil, fmt.Errorf("failed to open repository at %s: %w", workspace, err)
53+
// Try to set safe.directory manually if it is not found, and try to open repository again.
54+
if isSafeDirectoryNotFoundError(err) {
55+
config, err := git2go.OpenDefault()
56+
if err != nil {
57+
return nil, fmt.Errorf("failed to open config: %w", err)
58+
}
59+
defer config.Free()
60+
61+
if err := config.SetString("safe.directory", absWorkspace); err != nil {
62+
return nil, fmt.Errorf("failed to set safe.directory: %w", err)
63+
}
64+
}
65+
66+
repo, err = git2go.OpenRepository(workspace)
67+
if err != nil {
68+
return nil, fmt.Errorf("failed to open repository at %s: %w", workspace, err)
69+
}
3370
}
3471
defer repo.Free()
3572

0 commit comments

Comments
 (0)