Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hashicorp/go-getter
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.7.3
Choose a base ref
...
head repository: hashicorp/go-getter
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.7.4
Choose a head ref
  • 3 commits
  • 4 files changed
  • 3 contributors

Commits on Apr 22, 2023

  1. Make addAuthFromNetrc ignore ENOTDIR errors

    The function already returns early if the specified 'netrc' configuration points to a non existing file, but currently returns an error if the OS reports ENOTDIR:
    
    This will happen if the $HOME directory of the specified user points to a file instead a directory - something eg. 'void linux' does for user 'nobody':
    
    ```
    $ grep nobody /etc/passwd
    nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
    ```
    
    go-getter then attempts to open `/dev/null/.netrc` which fails with ENOTDIR - something that should just be threated the same way as a non existing file (in this case)
    adrian-bl committed Apr 22, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    5ccb39a View commit details

Commits on Dec 4, 2023

  1. Merge pull request #433 from adrian-bl/netrc-fix

    Make addAuthFromNetrc ignore ENOTDIR errors
    crw authored Dec 4, 2023
    Copy the full SHA
    975961f View commit details

Commits on Apr 15, 2024

  1. escape user provide string to git (#483)

    mcollao-hc authored Apr 15, 2024
    Copy the full SHA
    268c11c View commit details
Showing with 74 additions and 3 deletions.
  1. +2 −2 get_git.go
  2. +30 −0 get_git_test.go
  3. +2 −1 netrc.go
  4. +40 −0 netrc_test.go
4 changes: 2 additions & 2 deletions get_git.go
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR
args = append(args, "--depth", strconv.Itoa(depth))
args = append(args, "--branch", ref)
}
args = append(args, u.String(), dst)
args = append(args, "--", u.String(), dst)

cmd := exec.CommandContext(ctx, "git", args...)
setupGitEnv(cmd, sshKeyFile)
@@ -289,7 +289,7 @@ func findDefaultBranch(ctx context.Context, dst string) string {
// default branch. "master" is returned if no HEAD symref exists.
func findRemoteDefaultBranch(ctx context.Context, u *url.URL) string {
var stdoutbuf bytes.Buffer
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--symref", u.String(), "HEAD")
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--symref", "--", u.String(), "HEAD")
cmd.Stdout = &stdoutbuf
err := cmd.Run()
matches := lsRemoteSymRefRegexp.FindStringSubmatch(stdoutbuf.String())
30 changes: 30 additions & 0 deletions get_git_test.go
Original file line number Diff line number Diff line change
@@ -836,6 +836,36 @@ func TestGitGetter_subdirectory(t *testing.T) {
}
}

func TestGitGetter_BadRemoteUrl(t *testing.T) {

if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}

g := new(GitGetter)
dst := tempDir(t)

// try an option that exists
badUrl := "--no-refs"

u, err := url.Parse(badUrl)
if err != nil {
t.Fatal(err)
}

err = g.Get(dst, u)
if err == nil {
t.Fatalf("get succeeded; want error")
}

got := err.Error()
want := `repository '--no-refs' does not exist`
if !strings.Contains(got, want) {
t.Fatalf("wrong error\ngot: %s\nwant: %q", got, want)
}
}

// gitRepo is a helper struct which controls a single temp git repo.
type gitRepo struct {
t *testing.T
3 changes: 2 additions & 1 deletion netrc.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"net/url"
"os"
"runtime"
"syscall"

"github.com/bgentry/go-netrc/netrc"
"github.com/mitchellh/go-homedir"
@@ -38,7 +39,7 @@ func addAuthFromNetrc(u *url.URL) error {
// If the file is not a file, then do nothing
if fi, err := os.Stat(path); err != nil {
// File doesn't exist, do nothing
if os.IsNotExist(err) {
if serr, ok := err.(*os.PathError); ok && (os.IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
return nil
}

40 changes: 40 additions & 0 deletions netrc_test.go
Original file line number Diff line number Diff line change
@@ -61,3 +61,43 @@ func TestAddAuthFromNetrc_hasUsername(t *testing.T) {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotExist(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/_does_not_exist")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotADirectory(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/basic/parent-not-a-dir")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}