我尝试使用git2go在存储库中输出文件列表及其最新作者和最近的提交日期。使用tree.Walk
循环遍历文件似乎很简单:
package main
import (
"time"
"gopkg.in/libgit2/git2go.v25"
)
// FileItem contains enough file information to build list
type FileItem struct {
AbsoluteFilename string `json:"absolute_filename"`
Filename string `json:"filename"`
Path string `json:"path"`
Author string `json:"author"`
Time time.Time `json:"updated_at"`
}
func check(err error) {
// ...
}
func getFiles(path string) (files []FileItem) {
repository, err := git.OpenRepository(path)
check(err)
head, err := repository.Head()
check(err)
headCommit, err := repository.LookupCommit(head.Target())
check(err)
tree, err := headCommit.Tree()
check(err)
err = tree.Walk(func(td string, te *git.TreeEntry) int {
if te.Type == git.ObjectBlob {
files = append(files, FileItem{
Filename: te.Name,
Path: td,
Author: "Joey", // should be last committer
Time: time.Now(), // should be last commit time
})
}
return 0
})
check(err)
return
}
我能解决的是,我采取哪种方法?我可以在传递给tree.Walk
的函数内部根据git.TreeEntry
的有限信息来计算提交吗?或者我是否需要单独构建提交列表以及相关文件并以某种方式交叉引用它们?