dou11655853
dou11655853
2017-03-02 19:51
浏览 59

git2go:列出具有最新提交者和提交日期的文件

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用git2go输出文件列表以及它们的最新作者和最新提交日期 在存储库中。 用 tree.Walk 遍历文件似乎很简单:

 包main 
 
import(
“时间” 
 
  “ gopkg.in/libgit2/git2go.v25"
)

// FileItem包含足以构建列表的文件信息
type FileItem结构{
 AbsoluteFilename字符串`json:” absolute_filename“`
 Filename字符串`json  :“ filename”`
路径字符串`json:“ path”`
作者字符串`json:“ author”`
时间time.Time`json:“ updated_at”`
} 
 
func check(err 错误){
 // ... 
} 
 
func getFiles(路径字符串)(文件[] FileItem){
 
存储库,错误:= git.OpenRepository(路径)
检查(错误)  
 
 head,err:=仓库.Head()
 check(err)
 
 headCommit,err:=仓库.LookupCommit(head.Target())
 check(err)
 
树 ,err:= headCommit.Tree()
 check(err)
 
 err = tree.Walk(func(td string,te * git.TreeEntry)int {
 
如果te.Type == git。  ObjectBlob {
 
文件=  append(files,FileItem {
文件名:te.Name,
路径:td,
作者:“ Joey”,//应该是最后一个提交者
时间:time.Now(),//应该是最后一次提交 时间
 
}} 
 
} 
返回0 
})
 check(err)
 
返回
} 
   
 
 

我无法解决的是,我应该采取哪种方法? 我可以在传递给 tree.Walk 的函数中,基于 git.TreeEntry 的有限信息来计算提交吗? 还是我需要分别构建提交列表和相关文件,并以某种方式交叉引用它们?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhansong5785
    dongzhansong5785 2017-03-03 19:50
    已采纳

    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.

    点赞 评论

相关推荐