git2go:列出具有最新提交者和提交日期的文件

huangapple go评论95阅读模式
英文:

git2go: Listing files with the latest committer and commit date

问题

我正在尝试使用git2go来输出一个存储库中文件的列表,包括它们的最新作者和最近的提交日期。使用tree.Walk循环遍历文件似乎很简单:

我无法确定的是,我应该采取哪种方法?在传递给tree.Walk的函数内部,我能否根据git.TreeEntry的有限信息确定提交?还是我需要单独构建一个包含关联文件的提交列表,并进行交叉引用?

英文:

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

得分: 0

这个日志示例展示了如何通过路径筛选revwalk。它涉及将每个提交与其父提交进行差异比较,并将路径作为路径规范参数传递。这并不是一件简单的事情。

英文:

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.

huangapple
  • 本文由 发表于 2017年3月3日 03:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/42564667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定