forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_fake.go
385 lines (325 loc) · 8.28 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
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"
)
type GitRemote struct {
Name string
URL string
}
type GitTag struct {
Name string
Message string
}
type GitFake struct {
Remotes []GitRemote
Branches []string
BranchesRemote []string
CurrentBranch string
AccessTokenURL string
RepoInfo GitRepositoryInfo
Fork bool
GitVersion string
UserInfo GitUser
Commits []GitCommit
Changes bool
GitTags []GitTag
Revision string
}
func (g *GitFake) FindGitConfigDir(dir string) (string, string, error) {
return dir, dir, nil
}
func (g *GitFake) ToGitLabels(names []string) []GitLabel {
labels := []GitLabel{}
for _, n := range names {
labels = append(labels, GitLabel{Name: n})
}
return labels
}
func (g *GitFake) PrintCreateRepositoryGenerateAccessToken(server *auth.AuthServer, username string, o io.Writer) {
fmt.Fprintf(o, "Access token URL: %s\n\n", g.AccessTokenURL)
}
func (g *GitFake) Status(dir string) error {
return nil
}
func (g *GitFake) Server(dir string) (string, error) {
return g.RepoInfo.HostURL(), nil
}
func (g *GitFake) Info(dir string) (*GitRepositoryInfo, error) {
return &g.RepoInfo, nil
}
func (g *GitFake) IsFork(gitProvider GitProvider, gitInfo *GitRepositoryInfo, dir string) (bool, error) {
return g.Fork, nil
}
func (g *GitFake) Version() (string, error) {
return g.GitVersion, nil
}
func (g *GitFake) RepoName(org string, repoName string) string {
if org != "" {
return org + "/" + repoName
}
return repoName
}
func (g *GitFake) Username(dir string) (string, error) {
return g.UserInfo.Name, nil
}
func (g *GitFake) SetUsername(dir string, username string) error {
g.UserInfo.Name = username
return nil
}
func (g *GitFake) Email(dir string) (string, error) {
return g.UserInfo.Email, nil
}
func (g *GitFake) SetEmail(dir string, email string) error {
g.UserInfo.Email = email
return nil
}
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")
}
func (g *GitFake) Init(dir string) error {
return nil
}
func (g *GitFake) Clone(url string, directory string) error {
return nil
}
func (g *GitFake) Push(dir string) error {
return nil
}
func (g *GitFake) PushMaster(dir string) error {
return nil
}
func (g *GitFake) PushTag(dir string, tag string) error {
return nil
}
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
}
func (g *GitFake) ForcePushBranch(dir string, localBranch string, remoteBranch string) error {
return nil
}
func (g *GitFake) CloneOrPull(url string, directory string) error {
return nil
}
func (g *GitFake) Pull(dir string) error {
return nil
}
func (g *GitFake) PullRemoteBranches(dir string) error {
return nil
}
func (g *GitFake) PullUpstream(dir string) error {
return nil
}
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)
}
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
}
func (g *GitFake) UpdateRemote(dir string, url string) error {
return g.SetRemoteURL(dir, "origin", url)
}
func (g *GitFake) DeleteRemoteBranch(dir string, remoteName string, branch string) error {
return nil
}
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
}
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
}
func (g *GitFake) RemoteBranches(dir string) ([]string, error) {
return g.BranchesRemote, nil
}
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
}
func (g *GitFake) GetRemoteUrl(config *gitcfg.Config, name string) string {
if len(g.Remotes) == 0 {
return ""
}
return g.Remotes[0].URL
}
func (g *GitFake) Branch(dir string) (string, error) {
return g.CurrentBranch, nil
}
func (g *GitFake) CreateBranch(dir string, branch string) error {
g.Branches = append(g.Branches, branch)
return nil
}
func (g *GitFake) CheckoutRemoteBranch(dir string, branch string) error {
g.CurrentBranch = branch
g.Branches = append(g.Branches, branch)
return nil
}
func (g *GitFake) Checkout(dir string, branch string) error {
g.CurrentBranch = branch
return nil
}
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()
}
func (g *GitFake) Stash(dir string) error {
return nil
}
func (g *GitFake) Remove(dir string, fileName string) error {
return nil
}
func (g *GitFake) Add(dir string, args ...string) error {
return nil
}
func (g *GitFake) CommitIfChanges(dir string, message string) error {
commit := GitCommit{
SHA: "",
Message: message,
Author: &g.UserInfo,
URL: g.RepoInfo.URL,
Branch: g.CurrentBranch,
Committer: &g.UserInfo,
}
g.Commits = append(g.Commits, commit)
return nil
}
func (g *GitFake) CommitDir(dir string, message string) error {
return g.CommitIfChanges(dir, message)
}
func (g *GitFake) AddCommmit(dir string, msg string) error {
return g.CommitIfChanges(dir, msg)
}
func (g *GitFake) HasChanges(dir string) (bool, error) {
return g.Changes, nil
}
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
}
func (g *GitFake) GetCurrentGitTagSHA(dir string) (string, error) {
len := len(g.Commits)
if len < 1 {
return "", errors.New("no previous commit found")
}
return g.Commits[len-1].SHA, nil
}
func (g *GitFake) FetchTags(dir string) error {
return nil
}
func (g *GitFake) Tags(dir string) ([]string, error) {
tags := []string{}
for _, tag := range g.GitTags {
tags = append(tags, tag.Name)
}
return tags, nil
}
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
}
func (g *GitFake) GetRevisionBeforeDate(dir string, t time.Time) (string, error) {
return g.Revision, nil
}
func (g *GitFake) GetRevisionBeforeDateText(dir string, dateText string) (string, error) {
return g.Revision, nil
}