Skip to content

Commit e03eae6

Browse files
committed
Fix GetFile support for GitHub getter
This change fixes support for getting single files from GitHub. While this worked fine for other Git repositories, it did not work for GitHub, because the getter tried to clone a wrong URL. For example, if a GitHub URL of "https://github.com/owner/repo/subdir/file.txt" would be given to "GetFile", it would clone "https://github.com/owner/repo/subdir" instead of cloning "https://github.com/owner/repo" and extracting "subdir/file.txt".
1 parent 4e45866 commit e03eae6

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

get_git.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,15 @@ func (g *GitGetter) GetFile(ctx context.Context, req *Request) error {
141141

142142
// Get the filename, and strip the filename from the URL so we can
143143
// just get the repository directly.
144-
filename := filepath.Base(req.u.Path)
145-
req.u.Path = filepath.Dir(req.u.Path)
144+
var filename string
145+
if req.u.Host == "github.com" {
146+
tokens := strings.SplitN(req.u.Path[1:], "/", 3)
147+
req.u.Path = "/" + tokens[0] + "/" + tokens[1]
148+
filename = tokens[2]
149+
} else {
150+
filename = filepath.Base(req.u.Path)
151+
req.u.Path = filepath.Dir(req.u.Path)
152+
}
146153
dst := req.Dst
147154
req.Dst = td
148155

get_git_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,61 @@ func TestGitGetter_GetFile(t *testing.T) {
313313
testing_helper.AssertContents(t, dst, "hello")
314314
}
315315

316+
func TestGitGetter_githubGetWithFileMode(t *testing.T) {
317+
if !testHasGit {
318+
t.Skip("git not found, skipping")
319+
}
320+
321+
ctx := context.Background()
322+
dst := testing_helper.TempTestFile(t)
323+
defer os.RemoveAll(filepath.Dir(dst))
324+
325+
req := Request{
326+
Src: "git::https://github.com/arikkfir/go-getter/testdata/basic/foo/main.tf?ref=master",
327+
Dst: dst,
328+
GetMode: ModeFile,
329+
}
330+
331+
var result *GetResult
332+
var err error
333+
var c Client
334+
if result, err = c.Get(ctx, &req); err != nil {
335+
t.Fatalf("err: %s", err)
336+
} else if result.Dst != dst {
337+
t.Errorf("result.Dst != dst (%s != %s)", result.Dst, dst)
338+
}
339+
340+
// Verify the main file exists
341+
if _, err := os.Stat(dst); err != nil {
342+
t.Fatalf("err: %s", err)
343+
}
344+
testing_helper.AssertContents(t, dst, "# Hello\n")
345+
}
346+
347+
func TestGitGetter_githubGetFile(t *testing.T) {
348+
if !testHasGit {
349+
t.Skip("git not found, skipping")
350+
}
351+
352+
ctx := context.Background()
353+
dst := testing_helper.TempTestFile(t)
354+
defer os.RemoveAll(filepath.Dir(dst))
355+
356+
var result *GetResult
357+
var err error
358+
if result, err = GetFile(ctx, dst, "git::https://github.com/arikkfir/go-getter/testdata/basic/foo/main.tf?ref=master"); err != nil {
359+
t.Fatalf("err: %s", err)
360+
} else if result.Dst != dst {
361+
t.Errorf("result.Dst != dst (%s != %s)", result.Dst, dst)
362+
}
363+
364+
// Verify the main file exists
365+
if _, err := os.Stat(dst); err != nil {
366+
t.Fatalf("err: %s", err)
367+
}
368+
testing_helper.AssertContents(t, dst, "# Hello\n")
369+
}
370+
316371
func TestGitGetter_gitVersion(t *testing.T) {
317372
if !testHasGit {
318373
t.Skip("git not found, skipping")

0 commit comments

Comments
 (0)