-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
git.go
306 lines (265 loc) · 6.79 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
package gogit
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/store"
"github.com/blang/semver"
"github.com/pkg/errors"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
var (
// Stdout is exported for mocking / redirecting
Stdout io.Writer = os.Stdout
)
// Git is a go-git.v4 based git client
type Git struct {
path string
repo *git.Repository
wt *git.Worktree
}
// Open tries to open an existing git repo on disk
func Open(path string) (*Git, error) {
r, err := git.PlainOpen(path)
if err != nil {
return nil, err
}
w, err := r.Worktree()
if err != nil {
return nil, err
}
return &Git{
path: path,
repo: r,
wt: w,
}, nil
}
// Clone tries to clone an git repo from a remote
func Clone(ctx context.Context, repo, path string) (*Git, error) {
r, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: repo,
Progress: Stdout,
})
if err != nil {
return nil, err
}
w, err := r.Worktree()
if err != nil {
return nil, err
}
return &Git{
path: path,
repo: r,
wt: w,
}, nil
}
// Init creates a new git repo on disk
func Init(ctx context.Context, path string) (*Git, error) {
g := &Git{
path: path,
}
if !g.IsInitialized() {
r, err := git.PlainInit(path, false)
if err != nil {
return nil, errors.Wrapf(err, "Failed to initialize git: %s", err)
}
g.repo = r
wt, err := g.repo.Worktree()
if err != nil {
return nil, errors.Wrapf(err, "Failed to get worktree: %s", err)
}
g.wt = wt
}
// add current content of the store
if err := g.Add(ctx, g.path); err != nil {
return g, errors.Wrapf(err, "failed to add '%s' to git", g.path)
}
// commit if there is something to commit
if !g.HasStagedChanges(ctx) {
out.Debug(ctx, "No staged changes")
return g, nil
}
if err := g.Commit(ctx, "Add current content of password store"); err != nil {
return nil, errors.Wrapf(err, "failed to commit changes to git")
}
return g, nil
}
// Version just returns the (static) go-git version
func (g *Git) Version(context.Context) semver.Version {
return semver.Version{
Major: 4,
Build: []string{"go-git"},
}
}
// IsInitialized returns true if this is a valid repo
func (g *Git) IsInitialized() bool {
_, err := git.PlainOpen(g.path)
return err == nil
}
// Add adds any number of files to git
func (g *Git) Add(ctx context.Context, files ...string) error {
if len(files) < 1 || (len(files) == 1 && files[0] == g.path) {
return g.addAll()
}
for _, file := range files {
if strings.HasPrefix(file, g.path) {
file = strings.TrimPrefix(file, g.path+string(filepath.Separator))
}
_, err := g.wt.Add(file)
if err != nil {
return errors.Wrapf(err, "failed to add file '%s': %s", file, err)
}
}
return nil
}
func (g *Git) addAll() error {
files := make([]string, 0, 10)
err := filepath.Walk(g.path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && path != g.path {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if info.Name() == "." || info.Name() == ".." {
return nil
}
files = append(files, strings.TrimPrefix(path, g.path+string(filepath.Separator)))
return nil
})
if err != nil {
return errors.Wrapf(err, "failed to walk '%s': %s", g.path, err)
}
for _, file := range files {
_, err := g.wt.Add(file)
if err != nil {
return errors.Wrapf(err, "failed to add file '%s': %s", file, err)
}
}
return nil
}
// HasStagedChanges retures true if there are changes which can be committed
func (g *Git) HasStagedChanges(ctx context.Context) bool {
st, err := g.wt.Status()
if err != nil {
out.Red(ctx, "Error: Unable to get status: %s", err)
return false
}
return !st.IsClean()
}
// Commit creates a new commit
func (g *Git) Commit(ctx context.Context, msg string) error {
if !g.HasStagedChanges(ctx) {
return store.ErrGitNothingToCommit
}
_, err := g.wt.Commit(msg, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: os.Getenv("GIT_AUTHOR_NAME"),
Email: os.Getenv("GIT_AUTHOR_EMAIL"),
When: time.Now(),
},
})
return err
}
// PushPull will first pull from the remote and then push any changes
func (g *Git) PushPull(ctx context.Context, op, remote, branch string) error {
if err := g.Pull(ctx, remote, branch); err != nil {
if op == "pull" {
return err
}
out.Yellow(ctx, "Failed to pull before git push: %s", err)
}
if op == "pull" {
return nil
}
return g.Push(ctx, remote, branch)
}
// Pull will pull any changes
func (g *Git) Pull(ctx context.Context, remote, branch string) error {
if !g.IsInitialized() {
return store.ErrGitNotInit
}
if remote == "" {
remote = "origin"
}
if branch == "" {
branch = "master"
}
cfg, err := g.repo.Config()
if err != nil {
return errors.Wrapf(err, "Failed to get git config: %s", err)
}
if _, found := cfg.Remotes[remote]; !found {
return store.ErrGitNoRemote
}
return g.wt.PullContext(ctx, &git.PullOptions{
RemoteName: remote,
ReferenceName: plumbing.ReferenceName(branch),
Progress: Stdout,
})
}
// Push will push any changes to the remote
func (g *Git) Push(ctx context.Context, remote, branch string) error {
if !g.IsInitialized() {
return store.ErrGitNotInit
}
if remote == "" {
remote = "origin"
}
cfg, err := g.repo.Config()
if err != nil {
return errors.Wrapf(err, "Failed to get git config: %s", err)
}
if _, found := cfg.Remotes[remote]; !found {
return store.ErrGitNoRemote
}
return g.repo.PushContext(ctx, &git.PushOptions{
RemoteName: remote,
Progress: Stdout,
})
}
// Cmd is not supported and will go away eventually
func (g *Git) Cmd(context.Context, string, ...string) error {
return fmt.Errorf("not supported")
}
// InitConfig is not yet implemented
func (g *Git) InitConfig(context.Context, string, string) error {
return fmt.Errorf("not supported")
}
// AddRemote adds a new remote
func (g *Git) AddRemote(ctx context.Context, remote, url string) error {
_, err := g.repo.CreateRemote(&config.RemoteConfig{
Name: remote,
URLs: []string{url},
})
return err
}
// RemoveRemote removes a remote
func (g *Git) RemoveRemote(ctx context.Context, remote string) error {
return g.repo.DeleteRemote(remote)
}
// Name returns go-git
func (g *Git) Name() string {
return "go-git"
}
// Revisions is not implemented
func (g *Git) Revisions(context.Context, string) ([]backend.Revision, error) {
return nil, fmt.Errorf("not yet implemented for %s", g.Name())
}
// GetRevision is not implemented
func (g *Git) GetRevision(context.Context, string, string) ([]byte, error) {
return nil, fmt.Errorf("not yet implemented for %s", g.Name())
}