go-git/go-git
GitHub: go-git/go-git
纯 Go 实现的高度可扩展 Git 库,支持内存存储和自定义后端,适合集成到各类工具中进行 Git 操作。
Stars: 7300 | Forks: 905

[](https://pkg.go.dev/github.com/go-git/go-git/v6) [](https://github.com/go-git/go-git/actions) [](https://goreportcard.com/report/github.com/go-git/go-git) [](https://scorecard.dev/viewer/?uri=github.com/go-git/go-git)
*go-git* 是一个高度可扩展的、用 **纯 Go** 编写的 git 实现库。
它可以通过符合 Go 语言习惯的 API,在低层(plumbing)或高层(porcelain)对 git 仓库进行操作。得益于 [`Storer`](https://pkg.go.dev/github.com/go-git/go-git/v6/plumbing/storer) 接口,它还支持多种存储类型,例如内存文件系统或自定义实现。
该项目自 2015 年起积极开发,并被 [Keybase](https://keybase.io/blog/encrypted-git-for-everyone)、[Gitea](https://gitea.io/en-us/) 或 [Pulumi](https://github.com/search?q=org%3Apulumi+go-git&type=Code) 广泛使用,许多其他库和工具也在使用它。
## 项目状态
在经历了 [`src-d`](https://github.com/src-d) 组织的[法律问题](https://github.com/src-d/go-git/issues/1295#issuecomment-592965250)、四个月的更新停滞以及强制硬分叉的需求之后,该项目**现已回归正常**。
该项目目前由个人贡献者积极维护,其中包括几位原作者,同时也得到了一家新公司 [gitsight](https://github.com/gitsight) 的支持,`go-git` 是其大规模使用的关键组件。
## 与 git 的比较
*go-git* 旨在与 [git](https://github.com/git/git) 完全兼容,所有的 *porcelain* 操作的实现都与 *git* 完全一致。
*git* 是一个庞大的项目,由数千名贡献者历经多年开发,这使得 *go-git* 难以实现所有功能。您可以在[兼容性文档](COMPATIBILITY.md)中找到 *go-git* 与 *git* 的比较。
## 安装
推荐的 *go-git* 安装方式是:
```
import "github.com/go-git/go-git/v6" // with go modules enabled (GO111MODULE=on or outside GOPATH)
import "github.com/go-git/go-git" // with go modules disabled
```
## 示例
### 基本示例
一个模拟标准 `git clone` 命令的基本示例
```
// Clone the given repository to the given directory
Info("git clone https://github.com/go-git/go-git")
_, err := git.PlainClone("/tmp/foo", &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
Progress: os.Stdout,
})
CheckIfError(err)
```
输出:
```
Counting objects: 4924, done.
Compressing objects: 100% (1333/1333), done.
Total 4924 (delta 530), reused 6 (delta 6), pack-reused 3533
```
### 内存示例
将仓库克隆到内存中并打印 HEAD 的历史记录,就像 `git log` 一样
```
// Clones the given repository in memory, creating the remote, the local
// branches and fetching the objects, exactly as:
Info("git clone https://github.com/go-git/go-billy")
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/go-git/go-billy",
})
CheckIfError(err)
// Gets the HEAD history from HEAD, just like this command:
Info("git log")
// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)
// ... retrieves the commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
```
输出:
```
commit ded8054fd0c3994453e9c8aacaf48d118d42991e
Author: Santiago M. Mola
Date: Sat Nov 12 21:18:41 2016 +0100
index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)
commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
Author: Santiago M. Mola
Date: Fri Nov 11 13:23:22 2016 +0100
readwriter: fix bug when writing index. (#10)
When using ReadWriter on an existing siva file, absolute offset for
index entries was not being calculated correctly.
...
```
您可以在 [examples](_examples) 文件夹中找到此[示例](_examples/log/main.go)以及许多其他示例。
## 贡献
非常欢迎[贡献](https://github.com/go-git/go-git/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22),如果您有兴趣,请查看我们的[贡献指南](CONTRIBUTING.md)。
## 许可证
Apache License Version 2.0,见 [LICENSE](LICENSE)
标签:DevOps工具, EVTX分析, Git, Gitea组件, Go语言, Keybase, 代码仓库操作, 内存文件系统, 安全可观测性, 底层原语, 开源库, 搜索引擎爬虫, 日志审计, 源代码管理, 版本控制, 程序破解, 纯Go库, 高层接口, 高度可扩展