2017-11-17 283 views
0

我使用go項目go-git作爲git-client,並想從通過gitea託管的私有git存儲庫中獲取。go-git:如何在遠程服務器上進行身份驗證?

執行此操作的相應功能是func (r *Remote) Fetch(o *FetchOptions) error,它需要transport.AuthMethod對象進行身份驗證。

我試過如下:

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

...這只是返回:

無效的授權方法

如果我使用

authenticator, _ := ssh.NewSSHAgentAuth("git") 
同樣的情況,

從包"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"

並且如果我使用證書:

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

如何找出哪個身份驗證方法的支持,並在那裏的transport.AuthMethod,我可以用現有的實現?

+0

您的使用看起來正確的我,也許有一個bug?該項目最近有一個問題[#618](https://github.com/src-d/go-git/issues/618),可能表明有用的解決方法:'&git.CloneOptions {URL:「https: //user:[email protected]/repo.git「}' – orirawlings

+0

@orirawlings這樣的url只能在克隆新存儲庫時應用。當我已經擁有本地版本庫時,我用'PlainOpen'打開它,然後使用'Fetch',爲此我無法再指定URL。 (URL是從'.git/config'文件中獲得的,即使我在那裏修改了url,它也會返回「invalid auth method」 – maja

+0

你使用哪種協議?HTTP或者SSH?或者git? – mcuadros

回答

0

這裏的問題是,我使用了錯誤的http-package來獲取authenticator。

我使用了包net/http中的BasicAuth() - 但是,要使用的正確http包是gopkg.in/src-d/go-git.v4/plumbing/transport/http

更改導入並從那裏使用(兼容)BasicAuth-authenticator時,它可以完美地工作。

0

這個工作對我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:%[email protected]%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 
} 
相關問題