This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
git.go
388 lines (325 loc) · 8.08 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package git
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/yaml.v3"
"github.com/pkg/errors"
"github.com/caos/orbos/mntr"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
gogit "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
const (
writeCheckTag = "writecheck"
)
type Client struct {
monitor mntr.Monitor
ctx context.Context
committer string
email string
auth *gitssh.PublicKeys
repo *gogit.Repository
fs billy.Filesystem
workTree *gogit.Worktree
progress io.Writer
repoURL string
cloned bool
}
func New(ctx context.Context, monitor mntr.Monitor, committer, email string) *Client {
newClient := &Client{
ctx: ctx,
committer: committer,
email: email,
monitor: monitor,
}
if monitor.IsVerbose() {
newClient.progress = os.Stdout
}
return newClient
}
func (g *Client) GetURL() string {
return g.repoURL
}
func (g *Client) Configure(repoURL string, deploykey []byte) error {
signer, err := ssh.ParsePrivateKey(deploykey)
if err != nil {
return errors.Wrap(err, "parsing deployment key failed")
}
g.repoURL = repoURL
g.monitor = g.monitor.WithField("repository", repoURL)
g.auth = &gitssh.PublicKeys{
User: "git",
Signer: signer,
}
// TODO: Fix
g.auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
return nil
}
func (g *Client) Check() error {
if !g.cloned {
return nil
}
if err := g.readCheck(); err != nil {
return err
}
return g.writeCheck()
}
func (g *Client) readCheck() error {
rem := gogit.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{g.repoURL},
})
// We can then use every Remote functions to retrieve wanted information
_, err := rem.List(&gogit.ListOptions{
Auth: g.auth,
})
if err != nil {
return errors.Wrap(err, "Read check failed")
}
g.monitor.Info("Read check success")
return nil
}
func (g *Client) writeCheck() error {
head, err := g.repo.Head()
if err != nil {
return errors.Wrap(err, "Failed to get head")
}
localWriteCheckTag := strings.Join([]string{writeCheckTag, g.committer}, "-")
ref, createErr := g.repo.CreateTag(localWriteCheckTag, head.Hash(), nil)
if createErr == gogit.ErrTagExists {
if ref, err = g.repo.Tag(localWriteCheckTag); err != nil {
return err
}
}
if createErr != nil {
return errors.Wrap(createErr, "Write-check failed")
}
if pushErr := g.repo.Push(&gogit.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec("+" + ref.Name() + ":" + ref.Name()),
},
Auth: g.auth,
}); pushErr != nil && pushErr == gogit.NoErrAlreadyUpToDate {
return errors.Wrap(pushErr, "Write-check failed")
}
g.monitor.Debug("Write check tag created")
if deleteErr := g.repo.DeleteTag(localWriteCheckTag); deleteErr != nil && deleteErr != gogit.ErrTagNotFound {
return errors.Wrap(err, "Write-check cleanup delete tag failed")
}
if err := g.repo.Push(&gogit.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec(":" + ref.Name()),
},
Auth: g.auth,
}); err != nil {
return errors.Wrap(err, "Write-check cleanup failed")
}
g.monitor.Debug("Write check tag cleaned up")
g.monitor.Info("Write check success")
return nil
}
func (g *Client) Clone() (err error) {
for i := 0; i < 10; i++ {
if err = g.clone(); err == nil {
return nil
}
time.Sleep(time.Second)
}
return err
}
func (g *Client) clone() error {
g.fs = memfs.New()
g.monitor.Debug("Cloning")
var err error
g.repo, err = gogit.CloneContext(g.ctx, memory.NewStorage(), g.fs, &gogit.CloneOptions{
URL: g.repoURL,
Auth: g.auth,
SingleBranch: true,
Depth: 1,
Progress: g.progress,
})
if err != nil {
return errors.Wrapf(err, "cloning repository from %s failed", g.repoURL)
}
g.monitor.Debug("Cloned")
g.workTree, err = g.repo.Worktree()
if err != nil {
panic(err)
}
g.cloned = true
return nil
}
func (g *Client) Read(path string) []byte {
readmonitor := g.monitor.WithFields(map[string]interface{}{
"path": path,
})
readmonitor.Debug("Reading file")
file, err := g.fs.Open(path)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
return make([]byte, 0)
}
panic(err)
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
if readmonitor.IsVerbose() {
readmonitor.Debug("File read")
fmt.Println(string(fileBytes))
}
return fileBytes
}
func (g *Client) ReadYamlIntoStruct(path string, struc interface{}) error {
data := g.Read(path)
return errors.Wrapf(yaml.Unmarshal(data, struc), "Error while unmarshaling yaml %s to struct", path)
}
func (g *Client) ExistsFolder(path string) (bool, error) {
monitor := g.monitor.WithFields(map[string]interface{}{
"path": path,
})
monitor.Debug("Reading folder")
_, err := g.fs.ReadDir(path)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
return false, nil
}
return false, errors.Wrapf(err, "opening %s from worktree failed", path)
}
return true, nil
}
func (g *Client) EmptyFolder(path string) (bool, error) {
monitor := g.monitor.WithFields(map[string]interface{}{
"path": path,
})
monitor.Debug("Reading folder")
files, err := g.fs.ReadDir(path)
if err != nil {
return false, errors.Wrapf(err, "opening %s from worktree failed", path)
}
if len(files) == 0 {
return true, nil
}
return false, nil
}
func (g *Client) ReadFolder(path string) (map[string][]byte, []string, error) {
monitor := g.monitor.WithFields(map[string]interface{}{
"path": path,
})
monitor.Debug("Reading folder")
dirBytes := make(map[string][]byte, 0)
files, err := g.fs.ReadDir(path)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
return make(map[string][]byte, 0), nil, nil
}
return nil, nil, errors.Wrapf(err, "opening %s from worktree failed", path)
}
subdirs := make([]string, 0)
for _, file := range files {
if !file.IsDir() {
filePath := filepath.Join(path, file.Name())
fileBytes := g.Read(filePath)
dirBytes[file.Name()] = fileBytes
} else {
subdirs = append(subdirs, file.Name())
}
}
if monitor.IsVerbose() {
monitor.Debug("Folder read")
fmt.Println(dirBytes)
}
return dirBytes, subdirs, nil
}
type File struct {
Path string
Content []byte
}
func (g *Client) StageAndCommit(msg string, files ...File) (bool, error) {
if g.stage(files...) {
return false, nil
}
return true, g.Commit(msg)
}
func (g *Client) UpdateRemote(msg string, files ...File) error {
if err := g.Clone(); err != nil {
return errors.Wrap(err, "recloning before committing changes failed")
}
changed, err := g.StageAndCommit(msg, files...)
if err != nil {
return err
}
if !changed {
g.monitor.Info("No changes")
return nil
}
return g.Push()
}
func (g *Client) stage(files ...File) bool {
for _, f := range files {
updatemonitor := g.monitor.WithFields(map[string]interface{}{
"path": f.Path,
})
updatemonitor.Debug("Overwriting local index")
file, err := g.fs.Create(f.Path)
if err != nil {
panic(err)
}
//noinspection GoDeferInLoop
defer file.Close()
if _, err := io.Copy(file, bytes.NewReader(f.Content)); err != nil {
panic(err)
}
_, err = g.workTree.Add(f.Path)
if err != nil {
panic(err)
}
}
status, err := g.workTree.Status()
if err != nil {
panic(err)
}
return status.IsClean()
}
func (g *Client) Commit(msg string) error {
if _, err := g.workTree.Commit(msg, &gogit.CommitOptions{
Author: &object.Signature{
Name: g.committer,
Email: g.email,
When: time.Now(),
},
}); err != nil {
return errors.Wrap(err, "committing changes failed")
}
g.monitor.Debug("Changes commited")
return nil
}
func (g *Client) Push() error {
err := g.repo.PushContext(g.ctx, &gogit.PushOptions{
RemoteName: "origin",
// RefSpecs: refspecs,
Auth: g.auth,
Progress: g.progress,
})
if err != nil {
return errors.Wrap(err, "pushing repository failed")
}
g.monitor.Info("Repository pushed")
return nil
}