-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgitfs.go
158 lines (136 loc) · 3.58 KB
/
gitfs.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
package gitfs
import (
"fmt"
"path"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/storage/memory"
fs "sigs.k8s.io/kustomize/kyaml/filesys"
)
// gitFS is an internal implementation of the Kustomize
// filesystem abstraction.
type gitFS struct {
tree *object.Tree
}
// New creates and returns a go-git storage adapter.
func New(t *object.Tree) fs.FileSystem {
return &gitFS{tree: t}
}
// NewInMemoryFromOptions clones a Git repository into memory.
func NewInMemoryFromOptions(opts *git.CloneOptions) (fs.FileSystem, error) {
clone, err := git.Clone(memory.NewStorage(), nil, opts)
if err != nil {
return nil, err
}
ref, err := clone.Head()
if err != nil {
return nil, err
}
commit, err := clone.CommitObject(ref.Hash())
if err != nil {
return nil, err
}
tree, err := commit.Tree()
if err != nil {
return nil, err
}
return New(tree), nil
}
// IsDir implements fs.FileSystem.
func (g gitFS) IsDir(name string) bool {
// If it exists as a file, it's not a directory.
_, err := g.tree.File(name)
if err == nil {
return false
}
// Git doesn't store directories.
//
// If we can find a file with a prefix of the name we're looking for, then
// the name is a directory.
//
// TODO: make this a bit more efficent, cache found dirs?
isDir := false
err = g.tree.Files().ForEach(func(f *object.File) error {
if strings.HasPrefix(f.Name, name) {
isDir = true
return storer.ErrStop
}
return nil
})
// TODO: not a lot of choice here, there's no scope for returning an error.
if err != nil {
panic(err)
}
return isDir
}
// CleanedAbs implements fs.FileSystem.
func (g gitFS) CleanedAbs(p string) (fs.ConfirmedDir, string, error) {
if g.IsDir(p) {
return fs.ConfirmedDir(p), "", nil
}
d := path.Dir(p)
f := path.Base(p)
return fs.ConfirmedDir(d), f, nil
}
// ReadFile implements fs.FileSystem.
func (g gitFS) ReadFile(name string) ([]byte, error) {
f, err := g.tree.File(name)
if err != nil {
return nil, err
}
b, err := f.Contents()
if err != nil {
return nil, err
}
return []byte(b), nil
}
// Create implements fs.FileSystem.
func (g gitFS) Create(name string) (fs.File, error) {
return nil, errNotSupported("Create")
}
// MkDir implements fs.FileSystem.
func (g gitFS) Mkdir(name string) error {
return errNotSupported("MkDir")
}
// MkDirAll implements fs.FileSystem.
func (g gitFS) MkdirAll(name string) error {
return errNotSupported("MkdirAll")
}
// RemoveAll implements fs.FileSystem.
func (g gitFS) RemoveAll(name string) error {
return errNotSupported("RemoveAll")
}
// Open implements fs.FileSystem.
func (g gitFS) Open(name string) (fs.File, error) {
return nil, errNotSupported("Open")
}
// Exists implements fs.FileSystem.
func (g gitFS) Exists(name string) bool {
return false
}
// Glob implements fs.FileSystem.
func (g gitFS) Glob(pattern string) ([]string, error) {
return nil, errNotSupported("Glob")
}
// WriteFile implements fs.FileSystem.
func (g gitFS) WriteFile(name string, data []byte) error {
return errNotSupported("WriteFile")
}
// Walk implementation for fs.FileSystem
func (g gitFS) Walk(path string, walkFn filepath.WalkFunc) error {
return errNotSupported("Walk")
}
// ReadDir implementation for fs.FileSystem
func (g gitFS) ReadDir(path string) ([]string, error) {
return []string{}, errNotSupported("ReadDir")
}
func errNotSupported(s string) error {
return notSupported(s)
}
type notSupported string
func (f notSupported) Error() string {
return fmt.Sprintf("feature %#v not supported", string(f))
}