dongxi2163 2014-08-01 09:15
浏览 116
已采纳

git2go中的SSH身份验证

I'm working on learning Go as my first compiled language (coming from php/python). My first project was a small POST hook listener for Bitbucket, which fetches and then checks out a Git repository via os/exec. I'm now trying to replace the os/exec calls with git2go. I'm running into a snag with the authentication, though. I have the following code:

package main

import (
    git "github.com/libgit2/git2go"
    "log"
)

func main() {

    _, Cred := git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
    log.Println(Cred.Type())
    gitH,err := git.OpenRepository(".")
    if (err != nil) {
        log.Fatalln(err)
    }
    remotes,err := gitH.ListRemotes()
    if (err != nil) {
        log.Fatalln(err)
    }
    log.Println(remotes)
    origin,err := gitH.LoadRemote("origin")
    if (err != nil) {
        log.Fatalln(err)
    }
    err = origin.Fetch(nil,"")
    if (err != nil) {
        log.Fatalln(err)
    }
}

When I run this I get authentication required but no callback set.

Looking at the docs, it looks like I need to add a call to origin.SetCallbacks() which expects a RemoteCallbacks struct. RemoteCallbacks has the function CredentialsCallback which returns an int and a Cred pointer. Since NewCredSshKey returns the same values, I tried adding the following:

var cb git.RemoteCallbacks
cb.CredentialsCallback = git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
origin.SetCallbacks(cb)

which gives the errors multiple-value git.NewCredSshKey() in single-value context and cannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks in function argument.

I think I'm completely misunderstanding how this works, and I haven't been able to find any examples using this library. Tips or pointers to some examples would be much appreciated.

展开全部

  • 写回答

1条回答 默认 最新

  • duancai7568 2014-08-01 09:51
    关注

    A Couple of things:

    CredentialsCallback needs to be set to a function that matches it's signature, not the output of such a function. However, the signature for NewCredSshKey isn't correct in the first place, only its return values match. The correct signature is:

    func(url string, username_from_url string, allowed_types CredType) (int, *Cred)
    

    The second error cannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks is because you need a pointer to a RemoteCallbacks.

    Either declare and initialize it as a pointer:

    cb := &git.RemoteCallbacks{}
    // or alternatively 
    // cb := new(git.RemoteCallbacks)
    

    or take the address of when passing it as an argument:

    origin.SetCallbacks(&cb)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 不能对数据库增删改但是可以查询
  • ¥15 在触控设备上启动TabTip.exe打不开键盘界面,怎么用代码启动进程打开界面
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
  • ¥50 如何快速查看手机目标app的主要服务器ip
  • ¥15 (标签-stm32|关键词-m3)
  • ¥15 matlab中频率调制法代码的解读
  • ¥15 ceph的对象、块、文件相关问题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部