summaryrefslogtreecommitdiffstats
path: root/git/git.go
diff options
context:
space:
mode:
authorLibravatar Anirudh Oppiliappan <x@icyphox.sh>2022-12-11 14:18:25 +0530
committerLibravatar Anirudh Oppiliappan <x@icyphox.sh>2022-12-11 14:18:25 +0530
commitab30497e169cd3bfcd122e668c3cdde6bd48305b (patch)
treef0cc06bbcfff2e1a0418d8e6b8b80501a117882c /git/git.go
parent6857a2f002358c47f588c8417482a5afd01725d0 (diff)
downloadlegit-ab30497e169cd3bfcd122e668c3cdde6bd48305b.tar.gz
legit-ab30497e169cd3bfcd122e668c3cdde6bd48305b.tar.bz2
legit-ab30497e169cd3bfcd122e668c3cdde6bd48305b.zip
git: file content at ref
Diffstat (limited to '')
-rw-r--r--git/git.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/git/git.go b/git/git.go
index 13e703c..c7f72d3 100644
--- a/git/git.go
+++ b/git/git.go
@@ -64,6 +64,25 @@ func FilesAtRef(r *git.Repository, hash plumbing.Hash, path string) ([]NiceTree,
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 {
nts := []NiceTree{}
for _, e := range es {