git2go是一个用go代码操作git的库,需要依赖于第三方库libgit2
在 https://github.com/libgit2/libgit2 下载libgit2的代码进行安装:
然后下载git2go:
下面是测试的代码:
安装libgit2前,先安装相应的包:
yum -y install libssh2-devel
在 https://github.com/libgit2/libgit2 下载libgit2的代码进行安装:
mkdir build && cd build cmake .. cmake --build . --target install
然后下载git2go:
go get -d github.com/libgit2/git2go-d表示只把代码下载不安装
进行git2go的目录,执行:
git checkout next git submodule update --init # get libgit2 make install
下面是测试的代码:
package main import ( git "github.com/libgit2/git2go" "log" ) func credentialsCallback(url string, username string, allowedTypes git.CredType) (git.ErrorCode, *git.Cred) { ret, cred := git.NewCredSshKey("git", "/var/www/testgo/git/id_rsa.pub", "/var/www/testgo/git/id_rsa", "") return git.ErrorCode(ret), &cred } func certificateCheckCallback(cert *git.Certificate, valid bool, hostname string) git.ErrorCode { return 0 } func main() { cloneOptions := &git.CloneOptions{} cloneOptions.FetchOptions = &git.FetchOptions{ RemoteCallbacks: git.RemoteCallbacks{ CredentialsCallback: credentialsCallback, CertificateCheckCallback: certificateCheckCallback, }, } repo, err := git.Clone("git@github.com:test/test.git", "/tmp/code", cloneOptions) if err != nil { log.Panic(err) } log.Print(repo) }
[文章作者]曾健生
[作者邮箱]h6k65@126.com
[作者QQ]190678908
[博客] http://blog.csdn.net/newjueqi
有疑问加站长微信联系(非本文作者)