dou11655853 2017-03-02 19:51
浏览 80
已采纳

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?

  • 写回答

1条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?