-
Notifications
You must be signed in to change notification settings - Fork 18
/
lookup.go
62 lines (48 loc) · 1.13 KB
/
lookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gitclient
import "gopkg.in/libgit2/git2go.v24"
//go:generate counterfeiter . FileLookup
type FileLookup interface {
FileContents(repoPath string, branch string, filePath string) ([]byte, error)
}
func NewFileLookup() FileLookup {
return &fileLookup{}
}
type fileLookup struct{}
func (f *fileLookup) FileContents(repoPath string, branchName string, path string) ([]byte, error) {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
defer repo.Free()
branch, err := repo.LookupBranch("origin/"+branchName, git.BranchRemote)
if err != nil {
return nil, err
}
defer branch.Free()
ref, err := branch.Resolve()
if err != nil {
return nil, err
}
defer ref.Free()
refTarget := branch.Target()
commit, err := repo.LookupCommit(refTarget)
if err != nil {
return nil, err
}
defer commit.Free()
tree, err := commit.Tree()
if err != nil {
return nil, err
}
defer tree.Free()
treeEntry, err := tree.EntryByPath(path)
if err != nil {
return nil, err
}
blob, err := repo.LookupBlob(treeEntry.Id)
if err != nil {
return nil, err
}
defer blob.Free()
return blob.Contents(), nil
}