-
Notifications
You must be signed in to change notification settings - Fork 3
/
localgit.go
172 lines (136 loc) · 3.94 KB
/
localgit.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package localgit
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"golang.org/x/exp/slices"
"github.com/easyp-tech/server/internal/providers/content"
"github.com/easyp-tech/server/internal/providers/filter"
"github.com/easyp-tech/server/internal/providers/localgit/namedlocks"
"github.com/easyp-tech/server/internal/shake256"
)
const (
minNumberOfFiles = 1024
)
type namedLocks interface {
Lock(name string) *namedlocks.Unlocker
}
type store struct {
rootDir string
repos []filter.Repo
l namedLocks
}
func (s *store) Name() string {
return fmt.Sprintf("local git %q", s.rootDir)
}
func (s *store) Check(owner, repoName string) bool {
if s.rootDir == "" {
return false
}
fileStat, err := os.Stat(filepath.Join(s.rootDir, owner, repoName))
if err != nil {
return false
}
return fileStat.IsDir()
}
// New returns new instance of store.
func New(
rootDir string,
repos []filter.Repo,
l namedLocks,
) *store {
return &store{
rootDir: rootDir,
repos: repos,
l: l,
}
}
// Get implements storage.Store.
func (s *store) GetMeta(_ context.Context, owner, repoName, commit string) (content.Meta, error) {
dirName := path.Join(s.rootDir, owner, repoName)
l := s.l.Lock(dirName)
defer l.Unlock()
defaultBranch, commit, err := getRepoSwitchedCommit(dirName, commit)
if err != nil {
return content.Meta{DefaultBranch: defaultBranch, Commit: commit}, //nolint:exhaustruct
fmt.Errorf("investigating %q/%q:%q: %w", owner, repoName, commit, err)
}
return content.Meta{DefaultBranch: defaultBranch, Commit: commit}, nil //nolint:exhaustruct
}
// Get implements storage.Store.
func (s *store) GetFiles(_ context.Context, owner, repoName, commit string) ([]content.File, error) {
var (
dirName = path.Join(s.rootDir, owner, repoName)
repo = filter.FindRepo(owner, repoName, s.repos)
)
l := s.l.Lock(dirName)
defer l.Unlock()
if _, _, err := getRepoSwitchedCommit(dirName, commit); err != nil {
return nil, fmt.Errorf("investigating %q/%q:%q: %w", owner, repoName, commit, err)
}
files, err := enumerateProto(dirName, repo)
if err != nil {
return files, fmt.Errorf("enumerating %q/%q:%q: %w", owner, repoName, commit, err)
}
//nolint:godox
return files, nil
}
func getRepoSwitchedCommit(dirName, commit string) (string, string, error) {
r, err := git.PlainOpen(dirName)
if err != nil {
return "", "", fmt.Errorf("opening git: %w", err)
}
defaultBranch, err := r.Reference(plumbing.NewRemoteHEADReferenceName("origin"), true)
if err != nil {
return "", "", fmt.Errorf("resolving default branch: %w", err)
}
if commit == "" || commit == "main" {
commit = defaultBranch.Hash().String()
}
w, err := r.Worktree()
if err != nil {
return "", "", fmt.Errorf("getting work tree: %w", err)
}
if err = w.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(commit)}); err != nil { //nolint:exhaustruct
return "", "", fmt.Errorf("checking out %q: %w", commit, err)
}
return defaultBranch.Name().Short(), commit, nil
}
func enumerateProto(dirName string, repo filter.Repo) ([]content.File, error) {
res := make([]content.File, 0, minNumberOfFiles)
fsys := os.DirFS(dirName)
err := fs.WalkDir(
fsys,
".",
func(path string, info fs.DirEntry, err error) error {
if err != nil || info.IsDir() {
return nil //nolint:nilerr
}
newPath, ok := repo.Check(path)
if !ok {
return nil
}
data, err := fs.ReadFile(fsys, path)
if err != nil {
return fmt.Errorf("reading %q: %w", path, err)
}
hash, err := shake256.SHA3Shake256(data)
if err != nil {
return fmt.Errorf("hashing %q: %w", path, err)
}
res = append(res, content.File{Path: newPath, Data: data, Hash: hash})
return nil
},
)
if err != nil {
return nil, fmt.Errorf("walking %q: %w", dirName, err)
}
slices.SortFunc(res, func(a, b content.File) int { return strings.Compare(a.Path, b.Path) })
return res, nil
}