git: file content at ref

This commit is contained in:
Anirudh Oppiliappan 2022-12-11 14:18:25 +05:30
parent 6857a2f002
commit ab30497e16
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
1 changed files with 19 additions and 0 deletions

View File

@ -64,6 +64,25 @@ func FilesAtRef(r *git.Repository, hash plumbing.Hash, path string) ([]NiceTree,
return files, nil return files, nil
} }
func FileContentAtRef(r *git.Repository, hash plumbing.Hash, path string) (string, error) {
c, err := r.CommitObject(hash)
if err != nil {
return "", fmt.Errorf("commit object: %w", err)
}
tree, err := c.Tree()
if err != nil {
return "", fmt.Errorf("file tree: %w", err)
}
file, err := tree.File(path)
if err != nil {
return "", err
}
return file.Contents()
}
func makeNiceTree(es []object.TreeEntry) []NiceTree { func makeNiceTree(es []object.TreeEntry) []NiceTree {
nts := []NiceTree{} nts := []NiceTree{}
for _, e := range es { for _, e := range es {