如何在 GO 中使用 libgit2 运行 git 记录命令?
How to run git log commands using libgit2 in GO?
我对 运行 git 来自 go 的日志命令感兴趣。我看到 C# 版本支持这个(https://github.com/libgit2/libgit2sharp/wiki/git-log) . Does the GO version 也支持类似的 git 日志命令?我快速搜索了 "query" 和 "filter" 关键字但没有找到任何东西。
您至少可以使用 Commit.Parent
method which allows to access to the parent of a commit (from commit.go
).
模拟 git 日志
除此之外,我没有看到直接调用 git 日志。
RevWalk 就是您要找的。
repo, err := git.OpenRepository("path/to/repository")
log.Println(err)
w, err := repo.Walk() // returns a RevWalk instance for this repo
log.Println(err)
您可以配置返回的 RewWalk
实例。
err = w.PushHead() // instruct to start from the head commit
log.Println(err)
如果你想记录不同的分支,你可以使用 PushRef。还有其他配置选项来配置日志的起点和终点。查看这些文档。
使用Iterate 方法遍历提交列表。您需要向它传递一个函数,该函数将为列表中的每个提交调用。
w.Iterate(func(c *git.Commit) bool {
fmt.Println(c.Message())
return true // return false when you want to stop iterating
})
我对 运行 git 来自 go 的日志命令感兴趣。我看到 C# 版本支持这个(https://github.com/libgit2/libgit2sharp/wiki/git-log) . Does the GO version 也支持类似的 git 日志命令?我快速搜索了 "query" 和 "filter" 关键字但没有找到任何东西。
您至少可以使用 Commit.Parent
method which allows to access to the parent of a commit (from commit.go
).
除此之外,我没有看到直接调用 git 日志。
RevWalk 就是您要找的。
repo, err := git.OpenRepository("path/to/repository")
log.Println(err)
w, err := repo.Walk() // returns a RevWalk instance for this repo
log.Println(err)
您可以配置返回的 RewWalk
实例。
err = w.PushHead() // instruct to start from the head commit
log.Println(err)
如果你想记录不同的分支,你可以使用 PushRef。还有其他配置选项来配置日志的起点和终点。查看这些文档。
使用Iterate 方法遍历提交列表。您需要向它传递一个函数,该函数将为列表中的每个提交调用。
w.Iterate(func(c *git.Commit) bool {
fmt.Println(c.Message())
return true // return false when you want to stop iterating
})