Go的git库

huangapple go评论96阅读模式
英文:

git library for Go

问题

作为一个宠物项目,我想开发一个使用git作为存储后端的笔记应用。(我怀疑这个还不存在,鉴于这个人的博客文章:http://jarofgreen.co.uk/2012/08/how-about-a-mobile-note-app-backed-by-git/)

现在,我想把这个当作一个机会来玩一下Go。然而,我似乎找不到任何(甚至是最微小的)Go的git库。
实际上有吗?

显然,我对Go的了解是不存在的,所以编写libgit的绑定似乎不是一个有趣的开始方式...(而且我可能会转而使用我也不懂的ruby)

英文:

As a pet project, I want to develop a note taking app using git as storage backend. (I suspect this doesn't exist yet, given this guy's blog post: http://jarofgreen.co.uk/2012/08/how-about-a-mobile-note-app-backed-by-git/ )

Now, I'd like to take this as an opportunity to play around with Go a bit. However, I cannot seem to find any (not even the tiniest approach to) git library for Go.
Is there actually any?

Obviously my knowledge of Go is non-existant, so writing bindings for libgit doesn't seem a fun way to start... (and I would probably resort to ruby, which I don't know either)

答案1

得分: 47

我会说git2go是在Go中使用的git绑定库。它定期更新,并由运行libgit2的人维护。

如果你正在寻找一个纯粹用Go编写的git实现,go-git是最成熟和活跃的选择。

英文:

I'd say git2go is the git bindings library to use in Go. It is updated regularly and maintained by the people running libgit2.

If you are looking for a git implementation purely written in Go, go-git the most mature and active option.

答案2

得分: 16

自几年前开始,我和我的团队正在使用Go语言编写一个纯粹的git实现,它避免了任何C/C++依赖,使其更加灵活和易于扩展。

https://github.com/go-git/go-git

go-git的目标是达到libgit2或jgit的完整性,目前已经覆盖了大部分的底层读操作和一些主要的写操作,但缺少了主要的高级操作,比如合并操作。

英文:

Since a few years ago, my team and I, we were coding a pure git implementation in Go, it avoids to have any c/c++ dependency and make it more flexible and easy to extend.

https://github.com/go-git/go-git

> go-git aims to reach the completeness of libgit2 or jgit, nowadays covers the majority of the plumbing read operations and some of the main write operations, but lacks the main porcelain operations such as merges.

答案3

得分: 14

你可以使用Go标准库中的os/exec包,直接调用git命令。

英文:

You can just shell out to git command using os/exec package from Go standard library.

答案4

得分: 7

维克多提出的确实是Git开发者所设想的“官方”脚本方式。Git的命令被分为两个广泛的组别,专门用于此目的: “plumbing”命令是低级别的,主要用于其他程序使用;“porcelain”命令用于与用户交互,并调用plumbing命令来完成工作。查看/usr/lib/git-core目录(在您的系统上可能不同)可以了解Git有多少plumbing命令。

另一方面,Go通过其cgo功能支持与共享库链接。因此,您可以尝试使用libgit2进行封装。据我所知,libgit2与Git本身还不完全相同,但它能够读取/写入Git存储库,进行分支等操作-这对于您的任务应该足够了。

好的,在我写完所有这些之后,我滚动到了libgit2网站上的“Bindings”条目,发现了go-git

英文:

What Victor proposed is indeed the "official" way to "script" Git as envisioned by its developers. Git's commands are divided in the two broad groups specifically for this purpose: the "plumbing" commands are low-level and inteneded mostly to be used by other programs; the "porcelain" command are intended to interact with the user, and call plumbing commands to do their work. Look inside the /usr/lib/git-core directory (might be different on your system) to get the idea of how many plumbing commands Git has.

On the other hand, Go supports linking with shared libraries via its cgo facility. You hence could try wrapping libgit2 with it. AFAIK, libgit2 is not yet fully on par with the Git itself, but it is able to read/write Git repositories, do branching etc — supposedly it will be enough for your task.

Okay, after I wrote all that, I scrolled down the "Bindings" entry on the libgit2's site and found go-git...

答案5

得分: 3

在GoDoc上搜索“git”会找到一些项目。有一个libgit2的包装器,底部有一个未完成的Go语言Git实现。

英文:

A search for "git" on GoDoc turns up some projects. There's a libgit2 wrapper, and at the bottom is an unfinished Git implementation in Go.

答案6

得分: 2

golang-gitoperations模块提供了一个基于os/exec的接口,用于与命令行工具进行交互。

英文:

The golang-gitoperations module provides an os/exec based interface to the command line tool.

答案7

得分: 0

然后,Go库fluxcd/go-git-providers可能会很有用。

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() {
	// 创建一个新的客户端
	ctx := context.Background()
	c, err := github.NewClient()
	checkErr(err)

	// 将URL解析为OrgRepositoryRef
	ref, err := gitprovider.ParseOrgRepositoryURL("https://github.com/fluxcd/flux")
	checkErr(err)

	// 获取有关flux存储库的公共信息。
	repo, err := c.OrgRepositories().Get(ctx, *ref)
	checkErr(err)

	// 使用.Get()获取高级gitprovider.OrganizationInfo结构
	repoInfo := repo.Get()
	// 将内部对象转换为*gogithub.Repository以访问自定义数据
	internalRepo := repo.APIObject().(*gogithub.Repository)

	fmt.Printf("Description: %s. Homepage: %s", *repoInfo.Description, internalRepo.GetHomepage())
	// 输出:Description: The GitOps Kubernetes operator. Homepage: https://docs.fluxcd.io
}
英文:

> use git as storage backend

Then the Go library fluxcd/go-git-providers can come in handy.

> go-git-providers is a general-purpose Go client for interacting with Git providers' APIs (e.g. GitHub, GitLab, Bitbucket).

Example:

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
}

huangapple
  • 本文由 发表于 2012年11月24日 00:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/13532389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定