4

I'm using the go project go-git as a git-client and want to fetch from a private git-repository hosted via gitea.

The appropiate function to do that is func (r *Remote) Fetch(o *FetchOptions) error, which expects an transport.AuthMethod object for authentication.

I tried the following:

repo, _ := git.PlainOpen("/path/to/project/folder")
err := repo.Fetch(&git.FetchOptions{
    Auth: http.NewBasicAuth("someUser", "andThePassword"),
})

...which simply returns:

invalid auth method

The same happens if I use

authenticator, _ := ssh.NewSSHAgentAuth("git")

from the package "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh".

And also if I use a certificate:

authenticator, _:= ssh.NewPublicKeysFromFile("gitea name", "/home/name/.ssh/id_rsa", "passphrase")

How do I find out which authentication method is supported, and are there existing implementations of transport.AuthMethod that I could use?

mcuadros
  • 4,098
  • 3
  • 37
  • 44
maja
  • 17,250
  • 17
  • 82
  • 125
  • Your usage looks correct to me, maybe there is a bug? There was a recent issue [#618](https://github.com/src-d/go-git/issues/618) on the project that might indicate a useful workaround: `&git.CloneOptions{ URL: "https://user:pass@host/repo.git" }` – orirawlings Nov 17 '17 at 22:24
  • @orirawlings Such an url can only be applied when cloning a new repository. When I already have the repository local, I open it with `PlainOpen`, and then use `Fetch`, for which I cannot specify the url anymore. (The url is taken from the `.git/config` file. And even if I modify the url there, it returns "invalid auth method" – maja Nov 18 '17 at 08:18
  • which kind of protocol are you using? HTTP or SSH? or maybe git? – mcuadros Nov 18 '17 at 18:03
  • @mcuadros I'm using http – maja Nov 19 '17 at 08:24
  • can you print "git remote -v"? – mcuadros Nov 20 '17 at 10:38
  • @mcuadros Here you go (I replaced the actual url): `origin https://companyname.com/project/name (fetch) origin https://companyname.com/project/name (push) ` – maja Nov 20 '17 at 14:39

2 Answers2

5

The problem here was, that I was using the wrong http-package to acquire the authenticator.

I used the BasicAuth() from the package net/http - however, the correct http package to use is gopkg.in/src-d/go-git.v4/plumbing/transport/http.

When changing the import and using the (compatible) BasicAuth-authenticator from there, it works flawlessly.

maja
  • 17,250
  • 17
  • 82
  • 125
0

This worked for me with gopkg.in/src-d/go-git.v4

func cloneGit() (err error) {
    username := "dude"
    password := "super-secret-Personal-access-token"
    repo := "github.build.company.com/org/repo-name.git"
    url := fmt.Sprintf("https://%s:%s@%s", username, password, repo)
    options := git.CloneOptions{
        URL:      url,
        Progress: os.Stdout,
    }
    r, err := git.PlainClone("./src", false, &options)

    if err != nil {
        return err
    }
    fmt.Println(r.Log)
    return err
}