/
utils.go
180 lines (169 loc) · 4.34 KB
/
utils.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
package gtc
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"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/pkg/errors"
)
func (c *Client) addFile(path string, fileBlob []byte) error {
filePath := fmt.Sprintf("%s/%s", c.opt.DirPath, path)
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return err
}
return ioutil.WriteFile(filePath, fileBlob, 0644)
}
func (c *Client) CommitFiles(files map[string][]byte, message string) error {
for path, blob := range files {
if err := c.addFile(path, blob); err != nil {
return err
}
if err := c.Add(path); err != nil {
return err
}
}
if clean, err := c.IsClean(); err == nil && clean {
return nil
} else if err != nil {
return err
}
return c.Commit(message)
}
func (c *Client) GetHash(base string, referRemote bool) (string, error) {
ref := plumbing.NewBranchReferenceName(base)
if referRemote {
ref = plumbing.NewRemoteReferenceName("origin", base)
}
if h, err := c.r.ResolveRevision(plumbing.Revision(ref)); err == nil {
return h.String(), nil
}
if h, err := c.r.ResolveRevision(plumbing.Revision(plumbing.NewTagReferenceName(base))); err == nil {
return h.String(), nil
}
if o, err := c.r.Object(plumbing.CommitObject, plumbing.NewHash(base)); err == nil && !o.ID().IsZero() {
return base, nil
}
return "", errors.New("invalid base reference")
}
func (c *Client) GetLatestTagReference(referRemote bool) (*plumbing.Reference, error) {
if referRemote {
if err := c.Fetch(); err != nil {
return nil, errors.Wrap(err, "failed to fetch")
}
w, err := c.r.Worktree()
if err != nil {
return nil, err
}
if err := w.Checkout(&git.CheckoutOptions{Branch: plumbing.NewRemoteReferenceName("origin", c.opt.Revision), Force: true}); err != nil {
return nil, errors.Wrap(err, "failed to checkout remote branch")
}
}
tags, err := c.r.Tags()
if err != nil {
return nil, err
}
latestTagDate := time.Unix(0, 0)
var latestTagReference *plumbing.Reference = nil
err = tags.ForEach(func(ref *plumbing.Reference) error {
commit, err := c.r.CommitObject(ref.Hash())
if err != nil {
return err
}
if latestTagDate.Before(commit.Author.When) {
latestTagDate = commit.Author.When
latestTagReference = ref
}
return nil
})
if err != nil {
return nil, err
}
if latestTagReference == nil {
return nil, errors.New("no tag was found")
}
return latestTagReference, nil
}
func (c *Client) ReadFiles(paths, ignoreFile, ignoreDir []string, absolutePath bool) (map[string][]byte, error) {
result := map[string][]byte{}
for _, path := range paths {
buf, err := readFiles(fmt.Sprintf("%s/%s", c.opt.DirPath, path), ignoreFile, ignoreDir)
if err != nil {
return nil, err
}
for k, v := range buf {
if absolutePath {
result[k] = v
} else {
result[strings.Replace(k, fmt.Sprintf("%s/", c.opt.DirPath), "", -1)] = v
}
}
}
return result, nil
}
func readFiles(path string, ignoreFile, ignoreDir []string) (map[string][]byte, error) {
if ignoreFile == nil {
ignoreFile = []string{}
}
if ignoreDir == nil {
ignoreDir = []string{}
}
ret := map[string][]byte{}
s, err := os.Stat(path)
if err != nil {
return ret, nil
}
if !s.IsDir() {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
ret[path] = b
return ret, nil
}
err = filepath.Walk(path, func(path string, info os.FileInfo, e error) error {
if info.IsDir() {
for _, s := range ignoreDir {
if info.Name() == s {
return filepath.SkipDir
}
}
return nil
}
for _, s := range ignoreFile {
if strings.Contains(info.Name(), s) {
return nil
}
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
ret[path] = b
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}
func (c *Client) AddClientAsSubmodule(name string, subc Client) error {
return c.SubmoduleAdd(name, subc.opt.OriginURL, subc.opt.Revision, &subc.opt.Auth)
}
func (c *Client) MirrorBranch(src, dst string) error {
refs := fmt.Sprintf("refs/remotes/origin/%s:refs/heads/%s", src, dst)
rs := config.RefSpec(refs)
if err := c.r.Push(&git.PushOptions{
RemoteName: "origin",
Auth: c.opt.Auth.AuthMethod,
RefSpecs: []config.RefSpec{rs},
Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate {
return err
}
return nil
}