Skip to content

Commit ba38344

Browse files
authored
fix: remote git taskfile cloning and directory includes (#2602)
1 parent 4e963f8 commit ba38344

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

‎taskfile/node_git.go‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/go-task/task/v3/errors"
1616
"github.com/go-task/task/v3/internal/execext"
1717
"github.com/go-task/task/v3/internal/filepathext"
18+
"github.com/go-task/task/v3/internal/fsext"
1819
)
1920

2021
// An GitNode is a node that reads a Taskfile from a remote location via Git.
@@ -104,13 +105,13 @@ func (node *GitNode) buildURL() string {
104105
// Get the base URL
105106
baseURL := node.url.String()
106107

107-
ref := node.ref
108-
if ref == "" {
109-
ref = "HEAD"
110-
}
111108
// Always use git:: prefix for git URLs (following Terraform's pattern)
112109
// This forces go-getter to use git protocol
113-
return fmt.Sprintf("git::%s?ref=%s&depth=1", baseURL, ref)
110+
if node.ref != "" {
111+
return fmt.Sprintf("git::%s?ref=%s&depth=1", baseURL, node.ref)
112+
}
113+
// When no ref is specified, omit it entirely to let git clone the default branch
114+
return fmt.Sprintf("git::%s?depth=1", baseURL)
114115
}
115116

116117
// getOrCloneRepo returns the path to a cached git repository.
@@ -164,11 +165,16 @@ func (node *GitNode) ReadContext(ctx context.Context) ([]byte, error) {
164165
}
165166

166167
// Build path to Taskfile in the cached repo
167-
taskfilePath := node.path
168-
if taskfilePath == "" {
169-
taskfilePath = "Taskfile.yml"
168+
// If node.path is empty, search in repo root; otherwise search in the specified path
169+
// fsext.SearchPath handles both files and directories (searching for DefaultTaskfiles)
170+
searchPath := repoDir
171+
if node.path != "" {
172+
searchPath = filepath.Join(repoDir, node.path)
173+
}
174+
filePath, err := fsext.SearchPath(searchPath, DefaultTaskfiles)
175+
if err != nil {
176+
return nil, err
170177
}
171-
filePath := filepath.Join(repoDir, taskfilePath)
172178

173179
// Read file from cached repo
174180
b, err := os.ReadFile(filePath)
@@ -230,7 +236,7 @@ func (node *GitNode) repoCacheKey() string {
230236

231237
ref := node.ref
232238
if ref == "" {
233-
ref = "HEAD"
239+
ref = "_default_" // Placeholder for the remote's default branch
234240
}
235241

236242
return filepath.Join(node.url.Host, repoPath, ref)

‎taskfile/node_git_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ func TestGitNode_buildURL(t *testing.T) {
127127
expectedURL: "git::https://github.com/foo/bar.git?ref=v1.0.0&depth=1",
128128
},
129129
{
130-
name: "HTTPS without ref (uses remote HEAD)",
130+
name: "HTTPS without ref (uses remote default branch)",
131131
entrypoint: "https://github.com/foo/bar.git//Taskfile.yml",
132-
expectedURL: "git::https://github.com/foo/bar.git?ref=HEAD&depth=1",
132+
expectedURL: "git::https://github.com/foo/bar.git?depth=1",
133133
},
134134
{
135135
name: "SSH with directory path",
@@ -198,20 +198,20 @@ func TestRepoCacheKey_DifferentRepos(t *testing.T) {
198198
assert.NotEqual(t, key1, key2, "Different repos should generate different cache keys")
199199
}
200200

201-
func TestRepoCacheKey_NoRefVsHead(t *testing.T) {
201+
func TestRepoCacheKey_NoRefVsExplicitRef(t *testing.T) {
202202
t.Parallel()
203203

204-
// No ref (defaults to HEAD) vs explicit HEAD should have SAME cache key
204+
// No ref (uses default branch) vs explicit ref should have DIFFERENT cache keys
205205
node1, err := NewGitNode("https://github.com/foo/bar.git//file.yml", "", false)
206206
require.NoError(t, err)
207207

208-
node2, err := NewGitNode("https://github.com/foo/bar.git//file.yml?ref=HEAD", "", false)
208+
node2, err := NewGitNode("https://github.com/foo/bar.git//file.yml?ref=main", "", false)
209209
require.NoError(t, err)
210210

211211
key1 := node1.repoCacheKey()
212212
key2 := node2.repoCacheKey()
213213

214-
assert.Equal(t, key1, key2, "No ref and explicit HEAD should generate same cache key")
214+
assert.NotEqual(t, key1, key2, "No ref and explicit ref should generate different cache keys")
215215
}
216216

217217
func TestRepoCacheKey_SSHvsHTTPS(t *testing.T) {

0 commit comments

Comments
 (0)
⌂ 换网址