golang纯Go实现的高度可扩展Git操作插件库go-git的使用
Golang纯Go实现的高度可扩展Git操作插件库go-git的使用
项目介绍
go-git是一个用纯Go编写的高度可扩展的git实现库。它可以用于通过惯用的Go API在低级(plumbing)或高级(porcelain)操作git仓库。它还支持多种类型的存储,如内存文件系统或自定义实现,这要归功于Storer接口。
自2015年以来,它一直在积极开发,并被Keybase、Gitea、Pulumi以及许多其他库和工具广泛使用。
安装
推荐安装go-git的方式:
import "github.com/go-git/go-git/v6" // 启用go modules时(GO111MODULE=on或在GOPATH外)
import "github.com/go-git/go-git" // 禁用go modules时
使用示例
基础示例
模拟标准git clone
命令的基本示例:
// 克隆给定仓库到指定目录
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
一样:
// 在内存中克隆给定仓库,创建远程、本地分支并获取对象
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)
// 从HEAD获取历史记录,就像这个命令:
Info("git log")
// 获取HEAD指向的分支
ref, err := r.Head()
CheckIfError(err)
// 获取提交历史
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// 遍历提交并打印
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
输出:
commit ded8054fd0c3994453e9c8aacaf48d118d42991e
Author: Santiago M. Mola <santi@mola.io>
Date: Sat Nov 12 21:18:41 2016 +0100
index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)
commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
Author: Santiago M. Mola <santi@mola.io>
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.
...
项目状态
在经历了与src-d组织的法律问题、四个月没有更新以及需要进行硬分叉的要求后,该项目现在已恢复正常。
该项目目前由个人贡献者积极维护,包括几位原始作者,但也得到了一家新公司gitsight的支持,其中go-git是其大规模使用的关键组件。
与git的比较
go-git旨在与git完全兼容,所有高级操作都实现为与git完全相同的工作方式。
git是一个庞大的项目,有数千名贡献者多年的开发,这使得go-git难以实现所有功能。您可以在兼容性文档中找到go-git与git的比较。
贡献
非常欢迎贡献,如果您有兴趣,请查看我们的贡献指南。
许可证
Apache License Version 2.0
更多关于golang纯Go实现的高度可扩展Git操作插件库go-git的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复