2017-03-02 53 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參數進行區分。這不是微不足道的。

相關問題