-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
97 lines (79 loc) · 1.95 KB
/
store.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
package gitstorage
import (
"fmt"
"io/ioutil"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
)
func (c *Client) getHead() (string, error) {
pb, err := c.Repository.Head()
if err != nil {
return "", errors.Wrap(err, "unable to obtain worktree")
}
return pb.Hash().String(), nil
}
func (c *Client) objects() ([]string, error) {
var visited = []string{}
err := c.FileTreeReader.Walk(func(filepath string, isDir bool, err error) error {
if !isDir {
visited = append(visited, filepath)
}
return nil
})
if err != nil {
return []string{}, err
}
return visited, nil
}
func (c *Client) createFile(path, data string) error {
newFile, err := c.Filesystem.Create(path)
if err != nil {
return errors.Wrap(err, "unable to create file")
}
newFile.Write([]byte(data))
newFile.Close()
return nil
}
func (c *Client) deleteFile(path string) error {
return c.Filesystem.Remove(path)
}
func (c *Client) add(path string) (string, error) {
hash, err := c.Worktree.Add(path)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("unable to add path %s", path))
}
return hash.String(), nil
}
func (c *Client) commit() (string, error) {
s := object.Signature{
Name: "test",
Email: "gogs@gogs.com",
}
hash, err := c.Worktree.Commit("commit", &git.CommitOptions{Author: &s})
if err != nil {
return "", errors.Wrap(err, "unable to commit")
}
return hash.String(), nil
}
func (c *Client) get(path string) (string, error) {
fs, err := c.Filesystem.Open(path)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("unable to open file with path %s", path))
}
b, err := ioutil.ReadAll(fs)
if err != nil {
return "", errors.Wrap(err, "unable to read content")
}
return string(b), nil
}
func (c *Client) push(path string) error {
op := git.PushOptions{
Auth: c.BasicAuth,
}
err := c.Repository.Push(&op)
if err != nil {
return errors.Wrap(err, "unable to push")
}
return nil
}