简体   繁体   中英

git2go: Listing files with the latest committer and commit date

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM