Skip to content

Commit

Permalink
Fix GetFile support for GitHub getter
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
arikkfir committed Apr 11, 2022
1 parent 4e45866 commit e03eae6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
11 changes: 9 additions & 2 deletions get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ func (g *GitGetter) GetFile(ctx context.Context, req *Request) error {

// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
filename := filepath.Base(req.u.Path)
req.u.Path = filepath.Dir(req.u.Path)
var filename string
if req.u.Host == "github.com" {
tokens := strings.SplitN(req.u.Path[1:], "/", 3)
req.u.Path = "/" + tokens[0] + "/" + tokens[1]
filename = tokens[2]
} else {
filename = filepath.Base(req.u.Path)
req.u.Path = filepath.Dir(req.u.Path)
}
dst := req.Dst
req.Dst = td

Expand Down
55 changes: 55 additions & 0 deletions get_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,61 @@ func TestGitGetter_GetFile(t *testing.T) {
testing_helper.AssertContents(t, dst, "hello")
}

func TestGitGetter_githubGetWithFileMode(t *testing.T) {
if !testHasGit {
t.Skip("git not found, skipping")
}

ctx := context.Background()
dst := testing_helper.TempTestFile(t)
defer os.RemoveAll(filepath.Dir(dst))

req := Request{
Src: "git::https://github.com/arikkfir/go-getter/testdata/basic/foo/main.tf?ref=master",
Dst: dst,
GetMode: ModeFile,
}

var result *GetResult
var err error
var c Client
if result, err = c.Get(ctx, &req); err != nil {
t.Fatalf("err: %s", err)
} else if result.Dst != dst {
t.Errorf("result.Dst != dst (%s != %s)", result.Dst, dst)
}

// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
testing_helper.AssertContents(t, dst, "# Hello\n")
}

func TestGitGetter_githubGetFile(t *testing.T) {
if !testHasGit {
t.Skip("git not found, skipping")
}

ctx := context.Background()
dst := testing_helper.TempTestFile(t)
defer os.RemoveAll(filepath.Dir(dst))

var result *GetResult
var err error
if result, err = GetFile(ctx, dst, "git::https://github.com/arikkfir/go-getter/testdata/basic/foo/main.tf?ref=master"); err != nil {
t.Fatalf("err: %s", err)
} else if result.Dst != dst {
t.Errorf("result.Dst != dst (%s != %s)", result.Dst, dst)
}

// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
testing_helper.AssertContents(t, dst, "# Hello\n")
}

func TestGitGetter_gitVersion(t *testing.T) {
if !testHasGit {
t.Skip("git not found, skipping")
Expand Down

0 comments on commit e03eae6

Please sign in to comment.