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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题