-
Notifications
You must be signed in to change notification settings - Fork 785
/
git_fake.go
482 lines (414 loc) · 11.3 KB
/
git_fake.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package gits
import (
"bytes"
"errors"
"fmt"
"io"
"net/url"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/auth"
gitcfg "gopkg.in/src-d/go-git.v4/config"
)
// GitRemote a remote url
type GitRemote struct {
Name string
URL string
}
// GitTag git tag
type GitTag struct {
Name string
Message string
}
// GitFake provides a fake Gitter
type GitFake struct {
Remotes []GitRemote
Branches []string
BranchesRemote []string
CurrentBranch string
AccessTokenURL string
RepoInfo GitRepository
Fork bool
GitVersion string
GitUser GitUser
Commits []GitCommit
Changes bool
GitTags []GitTag
Revision string
serverURL string
}
// NewGitFake creates a new fake Gitter
func NewGitFake() Gitter {
return &GitFake{}
}
// FindGitConfigDir finds the git config dir
func (g *GitFake) FindGitConfigDir(dir string) (string, string, error) {
return dir, dir, nil
}
// PrintCreateRepositoryGenerateAccessToken prints the generate access token URL
func (g *GitFake) PrintCreateRepositoryGenerateAccessToken(server *auth.AuthServer, username string, o io.Writer) {
fmt.Fprintf(o, "Access token URL: %s\n\n", g.AccessTokenURL)
}
// Status check the status
func (g *GitFake) Status(dir string) error {
return nil
}
// Server returns the server URL
func (g *GitFake) Server(dir string) (string, error) {
return g.RepoInfo.HostURL(), nil
}
// Info returns the git repo info
func (g *GitFake) Info(dir string) (*GitRepository, error) {
return &g.RepoInfo, nil
}
// IsFork returns trie if this repo is a fork
func (g *GitFake) IsFork(dir string) (bool, error) {
return g.Fork, nil
}
// Version returns the git version
func (g *GitFake) Version() (string, error) {
return g.GitVersion, nil
}
// RepoName returns the repo name
func (g *GitFake) RepoName(org string, repoName string) string {
if org != "" {
return org + "/" + repoName
}
return repoName
}
// Username returns the current user name
func (g *GitFake) Username(dir string) (string, error) {
return g.GitUser.Name, nil
}
// SetUsername sets the username
func (g *GitFake) SetUsername(dir string, username string) error {
g.GitUser.Name = username
return nil
}
// Email returns the current user git email address
func (g *GitFake) Email(dir string) (string, error) {
return g.GitUser.Email, nil
}
// SetEmail sets the git email address
func (g *GitFake) SetEmail(dir string, email string) error {
g.GitUser.Email = email
return nil
}
// GetAuthorEmailForCommit returns the author email for a commit
func (g *GitFake) GetAuthorEmailForCommit(dir string, sha string) (string, error) {
for _, commit := range g.Commits {
if commit.SHA == sha {
return commit.Author.Email, nil
}
}
return "", errors.New("No commit found with given SHA")
}
// Init initialises git in a dir
func (g *GitFake) Init(dir string) error {
return nil
}
// Clone clones the repo to the given dir
func (g *GitFake) Clone(url string, directory string) error {
return nil
}
// ShallowCloneBranch shallow clone of a branch
func (g *GitFake) ShallowCloneBranch(url string, branch string, directory string) error {
return nil
}
// Push performs a git push
func (g *GitFake) Push(dir string) error {
return nil
}
// PushMaster pushes to master
func (g *GitFake) PushMaster(dir string) error {
return nil
}
// PushTag pushes a tag
func (g *GitFake) PushTag(dir string, tag string) error {
return nil
}
// CreatePushURL creates a Push URL
func (g *GitFake) CreatePushURL(cloneURL string, userAuth *auth.UserAuth) (string, error) {
u, err := url.Parse(cloneURL)
if err != nil {
return cloneURL, nil
}
if userAuth.Username != "" || userAuth.ApiToken != "" {
u.User = url.UserPassword(userAuth.Username, userAuth.ApiToken)
return u.String(), nil
}
return cloneURL, nil
}
// ForcePushBranch force push a branch
func (g *GitFake) ForcePushBranch(dir string, localBranch string, remoteBranch string) error {
return nil
}
// CloneOrPull performs a clone or pull
func (g *GitFake) CloneOrPull(url string, directory string) error {
return nil
}
// Pull git pulls
func (g *GitFake) Pull(dir string) error {
return nil
}
// PullRemoteBranches pull remote branches
func (g *GitFake) PullRemoteBranches(dir string) error {
return nil
}
// PullUpstream pulls upstream
func (g *GitFake) PullUpstream(dir string) error {
return nil
}
// AddRemote adds a remote
func (g *GitFake) AddRemote(dir string, name string, url string) error {
r := GitRemote{
Name: name,
URL: url,
}
g.Remotes = append(g.Remotes, r)
return nil
}
func (g *GitFake) findRemote(name string) (*GitRemote, error) {
for _, remote := range g.Remotes {
if remote.Name == name {
return &remote, nil
}
}
return nil, fmt.Errorf("no remote found with name '%s'", name)
}
// SetRemoteURL sets a remote URL
func (g *GitFake) SetRemoteURL(dir string, name string, gitURL string) error {
remote, err := g.findRemote(name)
if err != nil {
r := GitRemote{
Name: name,
URL: gitURL,
}
g.Remotes = append(g.Remotes, r)
return nil
}
remote.URL = gitURL
return nil
}
// UpdateRemote updates a remote
func (g *GitFake) UpdateRemote(dir string, url string) error {
return g.SetRemoteURL(dir, "origin", url)
}
// DeleteRemoteBranch deletes a remote branch
func (g *GitFake) DeleteRemoteBranch(dir string, remoteName string, branch string) error {
return nil
}
// DiscoverRemoteGitURL discover the remote git URL
func (g *GitFake) DiscoverRemoteGitURL(gitConf string) (string, error) {
origin, err := g.findRemote("origin")
if err != nil {
upstream, err := g.findRemote("upstream")
if err != nil {
return "", err
}
return upstream.URL, nil
}
return origin.URL, nil
}
// DiscoverUpstreamGitURL discover the upstream git URL
func (g *GitFake) DiscoverUpstreamGitURL(gitConf string) (string, error) {
upstream, err := g.findRemote("upstream")
if err != nil {
origin, err := g.findRemote("origin")
if err != nil {
return "", err
}
return origin.URL, nil
}
return upstream.URL, nil
}
// RemoteBranches list the remote branches
func (g *GitFake) RemoteBranches(dir string) ([]string, error) {
return g.BranchesRemote, nil
}
// RemoteBranchNames list the remote branch names
func (g *GitFake) RemoteBranchNames(dir string, prefix string) ([]string, error) {
remoteBranches := []string{}
for _, remoteBranch := range g.BranchesRemote {
remoteBranch = strings.TrimSpace(strings.TrimPrefix(remoteBranch, "* "))
if prefix != "" {
remoteBranch = strings.TrimPrefix(remoteBranch, prefix)
}
remoteBranches = append(remoteBranches, remoteBranch)
}
return remoteBranches, nil
}
// GetRemoteUrl get the remote URL
func (g *GitFake) GetRemoteUrl(config *gitcfg.Config, name string) string {
if len(g.Remotes) == 0 {
return ""
}
return g.Remotes[0].URL
}
// Branch returns the current branch
func (g *GitFake) Branch(dir string) (string, error) {
return g.CurrentBranch, nil
}
// CreateBranch creates a branch
func (g *GitFake) CreateBranch(dir string, branch string) error {
g.Branches = append(g.Branches, branch)
return nil
}
// CheckoutRemoteBranch checkout remote branch
func (g *GitFake) CheckoutRemoteBranch(dir string, branch string) error {
g.CurrentBranch = branch
g.Branches = append(g.Branches, branch)
return nil
}
// Checkout checkout the branch
func (g *GitFake) Checkout(dir string, branch string) error {
g.CurrentBranch = branch
return nil
}
// CheckoutOrphan checkout the orphan
func (g *GitFake) CheckoutOrphan(dir string, branch string) error {
g.CurrentBranch = branch
return nil
}
// ConvertToValidBranchName converts the name to a valid branch name
func (g *GitFake) ConvertToValidBranchName(name string) string {
name = strings.TrimSuffix(name, "/")
name = strings.TrimSuffix(name, ".lock")
var buffer bytes.Buffer
last := ' '
for _, ch := range name {
if ch <= 32 {
ch = replaceInvalidBranchChars
}
switch ch {
case '~':
ch = replaceInvalidBranchChars
case '^':
ch = replaceInvalidBranchChars
case ':':
ch = replaceInvalidBranchChars
case ' ':
ch = replaceInvalidBranchChars
case '\n':
ch = replaceInvalidBranchChars
case '\r':
ch = replaceInvalidBranchChars
case '\t':
ch = replaceInvalidBranchChars
}
if ch != replaceInvalidBranchChars || last != replaceInvalidBranchChars {
buffer.WriteString(string(ch))
}
last = ch
}
return buffer.String()
}
// FetchBranch fetch branch
func (g *GitFake) FetchBranch(dir string, repo string, refspec string) error {
return nil
}
// Stash git stash
func (g *GitFake) Stash(dir string) error {
return nil
}
// Remove a file from git
func (g *GitFake) Remove(dir string, fileName string) error {
return nil
}
// RemoveForce remove force
func (g *GitFake) RemoveForce(dir string, fileName string) error {
return nil
}
// CleanForce clean force
func (g *GitFake) CleanForce(dir string, fileName string) error {
return nil
}
// Add add files to git
func (g *GitFake) Add(dir string, args ...string) error {
return nil
}
// CommitIfChanges git commit if there are changes
func (g *GitFake) CommitIfChanges(dir string, message string) error {
commit := GitCommit{
SHA: "",
Message: message,
Author: &g.GitUser,
URL: g.RepoInfo.URL,
Branch: g.CurrentBranch,
Committer: &g.GitUser,
}
g.Commits = append(g.Commits, commit)
return nil
}
// CommitDir commit a dir
func (g *GitFake) CommitDir(dir string, message string) error {
return g.CommitIfChanges(dir, message)
}
// AddCommit add a commit
func (g *GitFake) AddCommit(dir string, msg string) error {
return g.CommitIfChanges(dir, msg)
}
// HasChanges returns true if has changes in git
func (g *GitFake) HasChanges(dir string) (bool, error) {
return g.Changes, nil
}
// GetPreviousGitTagSHA returns the previous git tag SHA
func (g *GitFake) GetPreviousGitTagSHA(dir string) (string, error) {
len := len(g.Commits)
if len < 2 {
return "", errors.New("no previous commit found")
}
return g.Commits[len-2].SHA, nil
}
// GetCurrentGitTagSHA returns the current git tag sha
func (g *GitFake) GetCurrentGitTagSHA(dir string) (string, error) {
len := len(g.Commits)
if len < 1 {
return "", errors.New("no current commit found")
}
return g.Commits[len-1].SHA, nil
}
// GetLatestCommitMessage returns the last commit message
func (g *GitFake) GetLatestCommitMessage(dir string) (string, error) {
len := len(g.Commits)
if len < 1 {
return "", errors.New("no current commit found")
}
return g.Commits[len-1].Message, nil
}
// FetchTags fetches tags
func (g *GitFake) FetchTags(dir string) error {
return nil
}
// Tags lists the tags
func (g *GitFake) Tags(dir string) ([]string, error) {
tags := []string{}
for _, tag := range g.GitTags {
tags = append(tags, tag.Name)
}
return tags, nil
}
// CreateTag creates a tag
func (g *GitFake) CreateTag(dir string, tag string, msg string) error {
t := GitTag{
Name: tag,
Message: msg,
}
g.GitTags = append(g.GitTags, t)
return nil
}
// GetRevisionBeforeDate get the revision before the date
func (g *GitFake) GetRevisionBeforeDate(dir string, t time.Time) (string, error) {
return g.Revision, nil
}
// GetRevisionBeforeDateText get the revision before the date text
func (g *GitFake) GetRevisionBeforeDateText(dir string, dateText string) (string, error) {
return g.Revision, nil
}
// Diff performs a git diff
func (g *GitFake) Diff(dir string) (string, error) {
return "", nil
}
func (g *GitFake) notFound() error {
return fmt.Errorf("Not found")
}