Skip to content

Commit a827553

Browse files
committed
feat: apply insteadOf rules from global config
When accessing a remote taskfile, it is unexpected and in some cases necessary to respect these settings. Since go-git doesn't support this yet, this workaround fixes it on our end. Signed-off-by: Robert Günzler <r@gnzler.io>
1 parent 048d927 commit a827553

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

taskfile/node_git.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
giturls "github.com/chainguard-dev/git-urls"
1212
"github.com/go-git/go-billy/v5/memfs"
1313
"github.com/go-git/go-git/v5"
14+
"github.com/go-git/go-git/v5/config"
1415
"github.com/go-git/go-git/v5/plumbing"
1516
"github.com/go-git/go-git/v5/storage/memory"
1617

@@ -72,11 +73,28 @@ func (node *GitNode) Read() ([]byte, error) {
7273
return node.ReadContext(context.Background())
7374
}
7475

75-
func (node *GitNode) ReadContext(_ context.Context) ([]byte, error) {
76+
func (node *GitNode) ReadContext(ctx context.Context) ([]byte, error) {
77+
cfg, err := config.LoadConfig(config.GlobalScope)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
// Workaround for git.Clone not considering git-config:
83+
var longestInsteadOfMatch *config.URL
84+
for _, u := range cfg.URLs {
85+
if strings.HasPrefix(node.url.String(), u.InsteadOf) &&
86+
(longestInsteadOfMatch == nil || len(longestInsteadOfMatch.InsteadOf) < len(u.InsteadOf)) {
87+
longestInsteadOfMatch = u
88+
}
89+
}
90+
// According to spec apply only the longest match.
91+
url := longestInsteadOfMatch.ApplyInsteadOf(node.url.String())
92+
7693
fs := memfs.New()
7794
storer := memory.NewStorage()
78-
_, err := git.Clone(storer, fs, &git.CloneOptions{
79-
URL: node.url.String(),
95+
96+
_, err = git.CloneContext(ctx, storer, fs, &git.CloneOptions{
97+
URL: url,
8098
ReferenceName: plumbing.ReferenceName(node.ref),
8199
SingleBranch: true,
82100
Depth: 1,

0 commit comments

Comments
 (0)