英文:
git2go: Listing files with the latest committer and commit date
问题
我正在尝试使用git2go来输出一个存储库中文件的列表,包括它们的最新作者和最近的提交日期。使用tree.Walk
循环遍历文件似乎很简单:
我无法确定的是,我应该采取哪种方法?在传递给tree.Walk
的函数内部,我能否根据git.TreeEntry
的有限信息确定提交?还是我需要单独构建一个包含关联文件的提交列表,并进行交叉引用?
英文:
I'm attempting to use git2go to output a list of files, along with their latest author and most recent commit date in a repository. Looping through the files with tree.Walk
seems to be straightforward:
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
}
What I can't work out is, which approach do I take? Can I, inside the function passed to tree.Walk
, work out the commit based on the limited information of the git.TreeEntry
? Or do I need to separately construct a list of commits along with associated files and somehow cross-reference them?
答案1
得分: 0
这个日志示例展示了如何通过路径筛选revwalk。它涉及将每个提交与其父提交进行差异比较,并将路径作为路径规范参数传递。这并不是一件简单的事情。
英文:
The log example shows how to filter revwalk by path. It involves diffing each commit to it's parent with the path as a pathspec argument. It's not trivial.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论