用go代码操作git

git2go是一个用go代码操作git的库,需要依赖于第三方库libgit2

安装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("[email protected]:test/test.git", "/tmp/code", cloneOptions)
            if err != nil {
                    log.Panic(err)
            }
    log.Print(repo)
}

[文章作者]曾健生

[作者邮箱][email protected]

[作者QQ]190678908

[博客]  http://blog.csdn.net/newjueqi

http://blog.sina.com.cn/h6k65





你可能感兴趣的:(用go代码操作git)