-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathclient.go
467 lines (401 loc) · 12 KB
/
client.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
Copyright 2022 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogit
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"path/filepath"
"time"
extgogit "github.com/fluxcd/go-git/v5"
"github.com/fluxcd/go-git/v5/config"
"github.com/fluxcd/go-git/v5/plumbing"
"github.com/fluxcd/go-git/v5/plumbing/cache"
"github.com/fluxcd/go-git/v5/plumbing/object"
"github.com/fluxcd/go-git/v5/storage"
"github.com/fluxcd/go-git/v5/storage/filesystem"
"github.com/fluxcd/go-git/v5/storage/memory"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/git/gogit/fs"
"github.com/fluxcd/pkg/git/repository"
)
// ClientName is the string representation of Client.
const ClientName = "go-git"
// Client implements repository.Client.
type Client struct {
*repository.DiscardCloser
path string
repository *extgogit.Repository
authOpts *git.AuthOptions
storer storage.Storer
worktreeFS billy.Filesystem
forcePush bool
credentialsOverHTTP bool
useDefaultKnownHosts bool
singleBranch bool
}
var _ repository.Client = &Client{}
type ClientOption func(*Client) error
// NewClient returns a new GoGitClient.
func NewClient(path string, authOpts *git.AuthOptions, clientOpts ...ClientOption) (*Client, error) {
securePath, err := git.SecurePath(path)
if err != nil {
return nil, fmt.Errorf("invalid path %s: %w", path, err)
}
g := &Client{
path: securePath,
authOpts: authOpts,
// Default to single branch as it is the most performant option.
singleBranch: true,
}
if len(clientOpts) == 0 {
clientOpts = append(clientOpts, WithDiskStorage())
}
for _, clientOpt := range clientOpts {
if err := clientOpt(g); err != nil {
return nil, err
}
}
if g.storer == nil {
return nil, errors.New("unable to create client with a nil storer")
}
if g.worktreeFS == nil {
return nil, errors.New("unable to create client with a nil worktree filesystem")
}
return g, nil
}
func WithStorer(s storage.Storer) ClientOption {
return func(c *Client) error {
c.storer = s
return nil
}
}
func WithWorkTreeFS(wt billy.Filesystem) ClientOption {
return func(c *Client) error {
c.worktreeFS = wt
return nil
}
}
// WithSingleBranch indicates whether only the references of a single
// branch will be fetched during cloning operations.
// For read-only clones, and for single branch write operations,
// a single branch is advised for performance reasons.
//
// For write operations that require multiple branches, for example,
// cloning from main and pushing into a feature branch, this should be
// disabled. Otherwise a second fetch will be required to get the state
// of the target branch, which won't work against some Git servers due
// to MULTI_ACK not being implemented in go-git.
//
// By default this is enabled.
func WithSingleBranch(singleBranch bool) ClientOption {
return func(c *Client) error {
c.singleBranch = singleBranch
return nil
}
}
func WithDiskStorage() ClientOption {
return func(c *Client) error {
wt := fs.New(c.path)
dot := fs.New(filepath.Join(c.path, extgogit.GitDirName))
c.storer = filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
c.worktreeFS = wt
return nil
}
}
func WithMemoryStorage() ClientOption {
return func(c *Client) error {
c.storer = memory.NewStorage()
c.worktreeFS = memfs.New()
return nil
}
}
// WithForcePush enables the use of force push for all push operations
// back to the Git repository.
// By default this is disabled.
func WithForcePush() ClientOption {
return func(c *Client) error {
c.forcePush = true
return nil
}
}
// WithInsecureCredentialsOverHTTP enables credentials being used over
// HTTP. This is not recommended for production environments.
func WithInsecureCredentialsOverHTTP() ClientOption {
return func(c *Client) error {
c.credentialsOverHTTP = true
return nil
}
}
// WithFallbackToDefaultKnownHosts enables falling back to the default known_hosts
// of the machine if the provided auth options don't provide it.
func WithFallbackToDefaultKnownHosts() ClientOption {
return func(c *Client) error {
c.useDefaultKnownHosts = true
return nil
}
}
func (g *Client) Init(ctx context.Context, url, branch string) error {
if err := g.validateUrl(url); err != nil {
return err
}
if g.repository != nil {
return nil
}
r, err := extgogit.Init(g.storer, g.worktreeFS)
if err != nil {
return err
}
if _, err = r.CreateRemote(&config.RemoteConfig{
Name: extgogit.DefaultRemoteName,
URLs: []string{url},
}); err != nil {
return err
}
branchRef := plumbing.NewBranchReferenceName(branch)
if err = r.CreateBranch(&config.Branch{
Name: branch,
Remote: extgogit.DefaultRemoteName,
Merge: branchRef,
}); err != nil {
return err
}
// PlainInit assumes the initial branch to always be master, we can
// overwrite this by setting the reference of the Storer to a new
// symbolic reference (as there are no commits yet) that points
// the HEAD to our new branch.
if err = r.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, branchRef)); err != nil {
return err
}
g.repository = r
return nil
}
func (g *Client) Clone(ctx context.Context, url string, cloneOpts repository.CloneOptions) (*git.Commit, error) {
if err := g.validateUrl(url); err != nil {
return nil, err
}
checkoutStrat := cloneOpts.CheckoutStrategy
switch {
case checkoutStrat.Commit != "":
return g.cloneCommit(ctx, url, checkoutStrat.Commit, cloneOpts)
case checkoutStrat.Tag != "":
return g.cloneTag(ctx, url, checkoutStrat.Tag, cloneOpts)
case checkoutStrat.SemVer != "":
return g.cloneSemVer(ctx, url, checkoutStrat.SemVer, cloneOpts)
default:
branch := checkoutStrat.Branch
if branch == "" {
branch = git.DefaultBranch
}
return g.cloneBranch(ctx, url, branch, cloneOpts)
}
}
func (g *Client) validateUrl(u string) error {
ru, err := url.Parse(u)
if err != nil {
return fmt.Errorf("cannot parse url: %w", err)
}
if g.credentialsOverHTTP {
return nil
}
httpOrEmpty := ru.Scheme == string(git.HTTP) || ru.Scheme == ""
if httpOrEmpty && ru.User != nil {
return errors.New("URL cannot contain credentials when using HTTP")
}
if httpOrEmpty && g.authOpts != nil &&
(g.authOpts.Username != "" || g.authOpts.Password != "") {
return errors.New("basic auth cannot be sent over HTTP")
}
return nil
}
func (g *Client) writeFile(path string, reader io.Reader) error {
if g.repository == nil {
return git.ErrNoGitRepository
}
f, err := g.worktreeFS.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, reader)
return err
}
func (g *Client) Commit(info git.Commit, commitOpts ...repository.CommitOption) (string, error) {
if g.repository == nil {
return "", git.ErrNoGitRepository
}
options := &repository.CommitOptions{}
for _, o := range commitOpts {
o(options)
}
for path, content := range options.Files {
if err := g.writeFile(path, content); err != nil {
return "", err
}
}
wt, err := g.repository.Worktree()
if err != nil {
return "", err
}
status, err := wt.Status()
if err != nil {
return "", err
}
var changed bool
for file := range status {
_, _ = wt.Add(file)
changed = true
}
if !changed {
head, err := g.repository.Head()
if err != nil {
return "", err
}
return head.Hash().String(), git.ErrNoStagedFiles
}
opts := &extgogit.CommitOptions{
Author: &object.Signature{
Name: info.Author.Name,
Email: info.Author.Email,
When: time.Now(),
},
}
if options.Signer != nil {
opts.SignKey = options.Signer
}
commit, err := wt.Commit(info.Message, opts)
if err != nil {
return "", err
}
return commit.String(), nil
}
func (g *Client) Push(ctx context.Context) error {
if g.repository == nil {
return git.ErrNoGitRepository
}
authMethod, err := transportAuth(g.authOpts, g.useDefaultKnownHosts)
if err != nil {
return fmt.Errorf("failed to construct auth method with options: %w", err)
}
return g.repository.PushContext(ctx, &extgogit.PushOptions{
Force: g.forcePush,
RemoteName: extgogit.DefaultRemoteName,
Auth: authMethod,
Progress: nil,
CABundle: caBundle(g.authOpts),
})
}
// SwitchBranch switches the current branch to the given branch name.
//
// No new references are fetched from the remote during the process,
// this is to ensure that the same flow can be used across all Git
// servers, regardless of them requiring MULTI_ACK or not. Once MULTI_ACK
// is implemented in go-git, this can be revisited.
//
// If more than one remote branch state is required, create the gogit
// client using WithSingleBranch(false). This will fetch all remote
// branches as part of the initial clone. Note that this is fully
// compatible with shallow clones.
//
// The following cases are handled:
// - Branch does not exist results in one being created using HEAD
// of the worktree.
// - Branch exists only remotely, results in a local branch being
// created tracking the remote HEAD.
// - Branch exists only locally, results in a checkout to the
// existing branch.
// - Branch exists locally and remotely, the local branch will take
// precendece.
//
// To override a remote branch with the state from the current branch,
// (i.e. image automation controller), use WithForcePush(true) in
// combination with WithSingleBranch(true). This will ignore the
// remote branch's existence.
func (g *Client) SwitchBranch(ctx context.Context, branchName string) error {
if g.repository == nil {
return git.ErrNoGitRepository
}
wt, err := g.repository.Worktree()
if err != nil {
return fmt.Errorf("failed to load worktree: %w", err)
}
// Assumes both local and remote branches exists until proven otherwise.
remote, local := true, true
remRefName := plumbing.NewRemoteReferenceName(extgogit.DefaultRemoteName, branchName)
remRef, err := g.repository.Reference(remRefName, true)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
remote = false
} else if err != nil {
return fmt.Errorf("could not fetch remote reference '%s': %w", branchName, err)
}
refName := plumbing.NewBranchReferenceName(branchName)
_, err = g.repository.Reference(refName, true)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
local = false
} else if err != nil {
return fmt.Errorf("could not fetch local reference '%s': %w", branchName, err)
}
create := false
// If the remote branch exists, but not the local branch, create a local
// reference to the remote's HEAD.
if remote && !local {
branchRef := plumbing.NewHashReference(refName, remRef.Hash())
err = g.repository.Storer.SetReference(branchRef)
if err != nil {
return fmt.Errorf("could not create reference to remote HEAD '%s': %w", branchRef.Hash().String(), err)
}
} else if !remote && !local {
// If the target branch does not exist locally or remotely, create a new
// branch using the current worktree HEAD.
create = true
}
err = wt.Checkout(&extgogit.CheckoutOptions{
Branch: refName,
Create: create,
})
if err != nil {
return fmt.Errorf("could not checkout to branch '%s': %w", branchName, err)
}
return nil
}
func (g *Client) IsClean() (bool, error) {
if g.repository == nil {
return false, git.ErrNoGitRepository
}
wt, err := g.repository.Worktree()
if err != nil {
return false, err
}
status, err := wt.Status()
if err != nil {
return false, err
}
return status.IsClean(), nil
}
func (g *Client) Head() (string, error) {
if g.repository == nil {
return "", git.ErrNoGitRepository
}
head, err := g.repository.Head()
if err != nil {
return "", err
}
return head.Hash().String(), nil
}
func (g *Client) Path() string {
return g.path
}