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?