/
git.go
142 lines (114 loc) · 3.23 KB
/
git.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package git
import (
"context"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/git/bounded"
"github.com/docker/go-units"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"io"
"path"
"syscall"
)
var (
ErrRetrievalFailed = errors.New("failed to retrieve a Git repository")
)
type Git struct {
worktree *git.Worktree
}
func New(ctx context.Context, url string, revision string) (*Git, error) {
const (
cacheBytes = 1 * units.MiB
storageBytes = 4 * units.MiB
storageFiles = 4096
filesystemBytes = 4 * units.MiB
filesystemFiles = 4096
)
boundedCache := cache.NewObjectLRU(cacheBytes)
boundedStorage := filesystem.NewStorage(bounded.NewFilesystem(storageBytes, storageFiles), boundedCache)
boundedFilesystem := bounded.NewFilesystem(filesystemBytes, filesystemFiles)
// Clone the repository
repo, err := git.CloneContext(ctx, boundedStorage, boundedFilesystem, &git.CloneOptions{
URL: url,
})
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrRetrievalFailed, err)
}
// Checkout the working tree to the specified revision
worktree, err := repo.Worktree()
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrRetrievalFailed, err)
}
// Without this ResolveRevision() would only work for default branch (e.g. master)
if err := repo.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, fmt.Errorf("%w: %v", ErrRetrievalFailed, err)
}
hash, err := repo.ResolveRevision(plumbing.Revision(revision))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrRetrievalFailed, err)
}
if err := worktree.Checkout(&git.CheckoutOptions{
Hash: *hash,
}); err != nil {
return nil, fmt.Errorf("%w: %v", ErrRetrievalFailed, err)
}
return &Git{worktree: worktree}, nil
}
func (g Git) Stat(ctx context.Context, path string) (*fs.FileInfo, error) {
stat, err := g.worktree.Filesystem.Stat(path)
if err != nil {
return nil, err
}
return &fs.FileInfo{IsDir: stat.IsDir()}, nil
}
func (g Git) Get(ctx context.Context, path string) ([]byte, error) {
if err := g.ensureDirectory(path, false); err != nil {
return nil, err
}
file, err := g.worktree.Filesystem.Open(path)
if err != nil {
return nil, err
}
fileBytes, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return fileBytes, nil
}
func (g Git) ReadDir(ctx context.Context, path string) ([]string, error) {
if err := g.ensureDirectory(path, true); err != nil {
return nil, err
}
infos, err := g.worktree.Filesystem.ReadDir(path)
if err != nil {
return nil, err
}
var entries []string
for _, info := range infos {
entries = append(entries, info.Name())
}
return entries, nil
}
func (g Git) Join(elem ...string) string {
return path.Join(elem...)
}
func (g Git) ensureDirectory(path string, directory bool) error {
stat, err := g.worktree.Filesystem.Stat(path)
if err != nil {
return err
}
if directory && !stat.IsDir() {
return syscall.ENOTDIR
}
if !directory && stat.IsDir() {
return fs.ErrNormalizedIsADirectory
}
return nil
}