2017-03-02 176 views
1

我试图使用git2go在存储库中输出文件列表及其最新作者和最近提交日期。通过与tree.Walk文件循环似乎是直截了当:git2go:使用最新的提交者和提交日期列出文件

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的有限信息计算出提交吗?或者我需要单独构建提交列表以及相关文件,并以某种方式交叉引用它们?

+0

revwalk help? [走路历史](http://ben.straub.cc/2013/10/02/revwalk/) – Mark

+0

Mark看起来很有希望,我会在早上适当调查 –

回答

0

log example显示了如何按路径过滤返回值。它涉及将每个提交与路径作为pathspec参数进行区分。这不是微不足道的。