如何在GO中使用libgit2运行git log命令?

时间:2016-05-18 03:19:10

标签: git go libgit2

我有兴趣从go运行git log命令。我看到C#版本支持这个(https://github.com/libgit2/libgit2sharp/wiki/git-log)。 GO version是否也支持类似的git log命令?我做了一些快速搜索"查询"和"过滤"关键字,我没有找到任何东西。

2 个答案:

答案 0 :(得分:2)

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
})

答案 1 :(得分:1)

您至少可以使用允许访问提交父级的Commit.Parent method来模拟git日志(来自commit.go)。

除此之外,我没有看到直接调用git log。