0

Using go-git/v5 and trying to clone over https as follows:

    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
        URL:           repo,
        ReferenceName: plumbing.ReferenceName(branch),
        Depth:         1,
        SingleBranch:  true,
        Auth:          &http.TokenAuth{Token: string(token)},
    })

where token is a string of the form ghp_XXXXXXXXX (my personal GH access token)

and repo equals to my private repo https://github.com/pkaramol/arepo

The error is

"net/http: invalid header field value \"Bearer ghp_XXXXXXXXX`\\n\" for key Authorization"

I have also trying using basic auth with my username and the token as password

    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
        URL:           repo,
        ReferenceName: plumbing.ReferenceName(branch),
        Depth:         1,
        SingleBranch:  true,
        Auth:          &http.BasicAuth{Username: "pkaramol", Password: token},
    })

Now the error becomes:

authentication required

What is the proper way of cloning over https?

The token has repo scope fwiw

edit:

the fs is instantiated as follows

fs := memfs.New()

the http package used is the following

"github.com/go-git/go-git/v5/plumbing/transport/http"
pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • Your `BasicAuth` example works fine for me. Can you edit your post with the exact imports you're using the and surrounding code (`fs` instantiation for example)? – Omer Tuchfeld Dec 04 '21 at 13:49
  • added some updates, let me know if it is not enough – pkaramol Dec 04 '21 at 13:55
  • Yeah we're aligned, it works for me. Something's wrong with your token would be my guess. I assume you tried creating a new token. How do instantiate the token? I simply do `token := "ghp_XXXXXXXXXXXXXXXXXXXXXXX"` – Omer Tuchfeld Dec 04 '21 at 13:57
  • I'll post my code as an answer so you can compare – Omer Tuchfeld Dec 04 '21 at 13:59

1 Answers1

1

This should work:

package main

import (
    "os"
    "fmt"

    "github.com/go-git/go-billy/v5/memfs"
    "github.com/go-git/go-git/v5/plumbing"
    "github.com/go-git/go-git/v5/plumbing/transport/http"
    "github.com/go-git/go-git/v5/storage/memory"

    git "github.com/go-git/go-git/v5"
)

func main() {
    token := "ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

    fs := memfs.New()

    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
        URL:           "https://github.com/username/reponame",
        ReferenceName: plumbing.ReferenceName("refs/heads/main"),
        Depth:         1,
        SingleBranch:  true,
        Auth:          &http.BasicAuth{Username: "username", Password: token},
        Progress:      os.Stdout,
    })

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

    fmt.Println("Done")
}
Omer Tuchfeld
  • 2,886
  • 1
  • 17
  • 24
  • 2
    duh....there was a `\n` char in my token variable (I am reading it from a file). thanks for getting into the trouble of answering it, I am accepting your answer – pkaramol Dec 04 '21 at 14:07