作为一个宠物项目,我想开发一个使用git作为存储后端的笔记应用程序。 (我怀疑这还没有,鉴于这个人的博客文章:http://jarofgreen.co.uk/2012/08/how-about-a-mobile-note-app-backed-by-git/)
现在,我想借此机会与Go玩一下。但是,我似乎无法找到Go的任何(甚至是最小的方法)git库。 实际上有吗?
显然我对Go的知识是不存在的,所以为libgit编写绑定似乎并不是一种有趣的开始...(我可能会使用ruby,我也不知道)
答案 0 :(得分:35)
答案 1 :(得分:10)
您可以使用Go标准库中的os/exec包来发送到git
命令。
答案 2 :(得分:6)
Victor提出的确实是开发人员所设想的“编写”Git“官方”方式。 Git的命令分为两大类,专门用于此目的:“管道”命令是低级的,并且主要用于其他程序; “瓷器”命令旨在与用户交互,并调用管道命令来完成他们的工作。查看/usr/lib/git-core
目录(可能在您的系统上有所不同),以了解Git有多少管道命令。
另一方面,Go支持通过其cgo
工具与共享库进行链接。因此,您可以尝试用它包装libgit2
。 AFAIK,libgit2
尚未完全与Git本身相提并论,但它能够读/写Git存储库,进行分支等 - 据说它足以完成您的任务。
好的,在我写完所有内容后,我向下滚动了libgit2
网站上的“Bindings”条目,找到go-git
...
答案 3 :(得分:6)
几年前,我和我的团队,我们在Go中编写了一个纯粹的git实现,它避免了任何c / c ++依赖,并使其更灵活,更容易扩展。
https://github.com/src-d/go-git
go-git旨在达到libgit2或jgit的完整性,现在涵盖了大部分管道读取操作和一些主要的写操作,但缺少主要的瓷器操作,如合并。
答案 4 :(得分:3)
search for "git" on GoDoc出现了一些项目。有一个libgit2包装器,底部是an unfinished Git implementation in Go。
答案 5 :(得分:2)
golang-gitoperations 模块为命令行工具提供了一个基于 os/exec 的接口。
答案 6 :(得分:0)
使用git作为存储后端
然后Go库the FAQ entry on positional-only parameters会派上用场。
go-git-providers
是通用Go客户端,用于与Git提供者的API(例如GitHub,GitLab,Bitbucket)进行交互。
示例:
package github_test
import (
"context"
"fmt"
"github.com/fluxcd/go-git-providers/github"
"github.com/fluxcd/go-git-providers/gitprovider"
gogithub "github.com/google/go-github/v32/github"
)
func ExampleOrgRepositoriesClient_Get() {
// Create a new client
ctx := context.Background()
c, err := github.NewClient()
checkErr(err)
// Parse the URL into an OrgRepositoryRef
ref, err := gitprovider.ParseOrgRepositoryURL("https://github.com/fluxcd/flux")
checkErr(err)
// Get public information about the flux repository.
repo, err := c.OrgRepositories().Get(ctx, *ref)
checkErr(err)
// Use .Get() to aquire a high-level gitprovider.OrganizationInfo struct
repoInfo := repo.Get()
// Cast the internal object to a *gogithub.Repository to access custom data
internalRepo := repo.APIObject().(*gogithub.Repository)
fmt.Printf("Description: %s. Homepage: %s", *repoInfo.Description, internalRepo.GetHomepage())
// Output: Description: The GitOps Kubernetes operator. Homepage: https://docs.fluxcd.io
}