/
git_local.go
361 lines (294 loc) · 12.1 KB
/
git_local.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
package gits
import (
"io"
"time"
"github.com/jenkins-x/jx/pkg/auth"
gitcfg "gopkg.in/src-d/go-git.v4/config"
)
// GitLocal provides a semi-fake Gitter - local operations delegate to GitCLI but remote operations are delegated to
// FakeGit. When using it in tests you must make sure you are operating on a temporary copy of a git repo NOT the
// real one on your disk (as it will get changed!).
// Faked out methods have the comment "Faked out"
type GitLocal struct {
GitCLI *GitCLI
GitFake *GitFake
}
// NewGitLocal creates a new GitLocal instance
func NewGitLocal() *GitLocal {
return &GitLocal{
GitCLI: NewGitCLI(),
GitFake: &GitFake{},
}
}
// FindGitConfigDir tries to find the `.git` directory either in the current directory or in parent directories
// Faked out
func (g *GitLocal) FindGitConfigDir(dir string) (string, string, error) {
return g.GitCLI.FindGitConfigDir(dir)
}
// Clone clones the given git URL into the given directory
// Faked out
func (g *GitLocal) Clone(url string, dir string) error {
return g.GitFake.Clone(url, dir)
}
// ShallowCloneBranch clones a single branch of the given git URL into the given directory
// Faked out
func (g *GitLocal) ShallowCloneBranch(url string, branch string, dir string) error {
return g.GitFake.ShallowCloneBranch(url, branch, dir)
}
// Pull pulls the Git repository in the given directory
// Faked out
func (g *GitLocal) Pull(dir string) error {
return g.GitFake.Pull(dir)
}
// PullRemoteBranches pulls the remote Git tags from the given given directory
// Faked out
func (g *GitLocal) PullRemoteBranches(dir string) error {
return g.GitFake.PullRemoteBranches(dir)
}
// DeleteRemoteBranch deletes the remote branch in the given given directory
// Faked out
func (g *GitLocal) DeleteRemoteBranch(dir string, remoteName string, branch string) error {
return g.GitFake.DeleteRemoteBranch(dir, remoteName, branch)
}
// CloneOrPull clones the given git URL or pull if it already exists
// Faked out
func (g *GitLocal) CloneOrPull(url string, dir string) error {
return g.GitFake.CloneOrPull(url, dir)
}
// PullUpstream pulls the remote upstream branch into master branch into the given directory
// Faked out
func (g *GitLocal) PullUpstream(dir string) error {
return g.GitFake.PullUpstream(dir)
}
// AddRemote adds a remote repository at the given URL and with the given name
func (g *GitLocal) AddRemote(dir string, name string, url string) error {
return g.GitCLI.AddRemote(dir, name, url)
}
// UpdateRemote updates the URL of the remote repository
func (g *GitLocal) UpdateRemote(dir, url string) error {
return g.GitCLI.UpdateRemote(dir, url)
}
// Stash stashes the current changes from the given directory
func (g *GitLocal) Stash(dir string) error {
return g.GitCLI.Stash(dir)
}
// CheckoutRemoteBranch checks out the given remote tracking branch
func (g *GitLocal) CheckoutRemoteBranch(dir string, branch string) error {
return g.GitCLI.CheckoutRemoteBranch(dir, branch)
}
// RemoteBranches returns the remote branches
func (g *GitLocal) RemoteBranches(dir string) ([]string, error) {
return g.GitCLI.RemoteBranches(dir)
}
// Checkout checks out the given branch
func (g *GitLocal) Checkout(dir string, branch string) error {
return g.GitCLI.Checkout(dir, branch)
}
// CheckoutOrphan checks out the given branch as an orphan
func (g *GitLocal) CheckoutOrphan(dir string, branch string) error {
return g.GitCLI.CheckoutOrphan(dir, branch)
}
// Init inits a git repository into the given directory
func (g *GitLocal) Init(dir string) error {
return g.GitCLI.Init(dir)
}
// Remove removes the given file from a Git repository located at the given directory
func (g *GitLocal) Remove(dir, fileName string) error {
return g.GitCLI.Remove(dir, fileName)
}
// RemoveForce removes the given file from a git repository located at the given directory
func (g *GitLocal) RemoveForce(dir, fileName string) error {
return g.GitCLI.RemoveForce(dir, fileName)
}
// CleanForce cleans a git repository located at a given directory
func (g *GitLocal) CleanForce(dir, fileName string) error {
return g.CleanForce(dir, fileName)
}
// Status returns the status of the git repository at the given directory
func (g *GitLocal) Status(dir string) error {
return g.GitCLI.Status(dir)
}
// Branch returns the current branch of the repository located at the given directory
func (g *GitLocal) Branch(dir string) (string, error) {
return g.GitCLI.Branch(dir)
}
// Push pushes the changes from the repository at the given directory
// Faked out
func (g *GitLocal) Push(dir string) error {
return g.GitFake.Push(dir)
}
// ForcePushBranch does a force push of the local branch into the remote branch of the repository at the given directory
// Faked out
func (g *GitLocal) ForcePushBranch(dir string, localBranch string, remoteBranch string) error {
return g.GitFake.ForcePushBranch(dir, localBranch, remoteBranch)
}
// PushMaster pushes the master branch into the origin
// Faked out
func (g *GitLocal) PushMaster(dir string) error {
return g.GitFake.PushMaster(dir)
}
// PushTag pushes the given tag into the origin
// Faked out
func (g *GitLocal) PushTag(dir string, tag string) error {
return g.GitFake.PushTag(dir, tag)
}
// Add does a git add for all the given arguments
func (g *GitLocal) Add(dir string, args ...string) error {
return g.GitCLI.Add(dir, args...)
}
// HasChanges indicates if there are any changes in the repository from the given directory
func (g *GitLocal) HasChanges(dir string) (bool, error) {
return g.GitCLI.HasChanges(dir)
}
// CommitIfChanges does a commit if there are any changes in the repository at the given directory
func (g *GitLocal) CommitIfChanges(dir string, message string) error {
return g.GitCLI.CommitIfChanges(dir, message)
}
// CommitDir commits all changes from the given directory
func (g *GitLocal) CommitDir(dir string, message string) error {
return g.GitCLI.CommitDir(dir, message)
}
// AddCommit perform an add and commit of the changes from the repository at the given directory with the given messages
func (g *GitLocal) AddCommit(dir string, msg string) error {
return g.GitCLI.AddCommit(dir, msg)
}
// CreatePushURL creates the Git repository URL with the username and password encoded for HTTPS based URLs
func (g *GitLocal) CreatePushURL(cloneURL string, userAuth *auth.UserAuth) (string, error) {
return g.GitCLI.CreatePushURL(cloneURL, userAuth)
}
// RepoName formats the repository names based on the organization
func (g *GitLocal) RepoName(org, repoName string) string {
return g.GitCLI.RepoName(org, repoName)
}
// Server returns the Git server of the repository at the given directory
func (g *GitLocal) Server(dir string) (string, error) {
return g.GitCLI.Server(dir)
}
// Info returns the git info of the repository at the given directory
func (g *GitLocal) Info(dir string) (*GitRepository, error) {
return g.GitCLI.Info(dir)
}
// ConvertToValidBranchName converts the given branch name into a valid git branch string
// replacing any dodgy characters
func (g *GitLocal) ConvertToValidBranchName(name string) string {
return g.GitCLI.ConvertToValidBranchName(name)
}
// FetchBranch fetches a branch
// Faked out
func (g *GitLocal) FetchBranch(dir string, repo string, refspec string) error {
return g.GitFake.FetchBranch(dir, repo, refspec)
}
// GetAuthorEmailForCommit returns the author email from commit message with the given SHA
func (g *GitLocal) GetAuthorEmailForCommit(dir string, sha string) (string, error) {
return g.GitCLI.GetAuthorEmailForCommit(dir, sha)
}
// SetRemoteURL sets the remote URL of the remote with the given name
func (g *GitLocal) SetRemoteURL(dir string, name string, gitURL string) error {
return g.GitCLI.SetRemoteURL(dir, name, gitURL)
}
// DiscoverRemoteGitURL discovers the remote git URL from the given git configuration
func (g *GitLocal) DiscoverRemoteGitURL(gitConf string) (string, error) {
return g.GitCLI.DiscoverRemoteGitURL(gitConf)
}
// DiscoverUpstreamGitURL discovers the upstream git URL from the given git configuration
func (g *GitLocal) DiscoverUpstreamGitURL(gitConf string) (string, error) {
return g.GitCLI.DiscoverUpstreamGitURL(gitConf)
}
// GetRemoteUrl returns the remote URL from the given git config
func (g *GitLocal) GetRemoteUrl(config *gitcfg.Config, name string) string {
return g.GitCLI.GetRemoteUrl(config, name)
}
// RemoteBranchNames returns all remote branch names with the given prefix
func (g *GitLocal) RemoteBranchNames(dir string, prefix string) ([]string, error) {
return g.GitCLI.RemoteBranchNames(dir, prefix)
}
// GetPreviousGitTagSHA returns the previous git tag from the repository at the given directory
func (g *GitLocal) GetPreviousGitTagSHA(dir string) (string, error) {
return g.GitCLI.GetPreviousGitTagSHA(dir)
}
// GetRevisionBeforeDate returns the revision before the given date
func (g *GitLocal) GetRevisionBeforeDate(dir string, t time.Time) (string, error) {
return g.GitCLI.GetRevisionBeforeDate(dir, t)
}
// GetRevisionBeforeDateText returns the revision before the given date in format "MonthName dayNumber year"
func (g *GitLocal) GetRevisionBeforeDateText(dir string, dateText string) (string, error) {
return g.GitCLI.GetRevisionBeforeDateText(dir, dateText)
}
// GetCurrentGitTagSHA return the SHA of the current git tag from the repository at the given directory
func (g *GitLocal) GetCurrentGitTagSHA(dir string) (string, error) {
return g.GitCLI.GetCurrentGitTagSHA(dir)
}
// GetLatestCommitMessage returns the latest git commit message
func (g *GitLocal) GetLatestCommitMessage(dir string) (string, error) {
return g.GitCLI.GetLatestCommitMessage(dir)
}
// FetchTags fetches all the tags
// Faked out
func (g *GitLocal) FetchTags(dir string) error {
return g.GitFake.FetchTags(dir)
}
// Tags returns all tags from the repository at the given directory
func (g *GitLocal) Tags(dir string) ([]string, error) {
return g.GitCLI.Tags(dir)
}
// CreateTag creates a tag with the given name and message in the repository at the given directory
func (g *GitLocal) CreateTag(dir string, tag string, msg string) error {
return g.GitCLI.CreateTag(dir, tag, msg)
}
// PrintCreateRepositoryGenerateAccessToken prints the access token URL of a Git repository
func (g *GitLocal) PrintCreateRepositoryGenerateAccessToken(server *auth.AuthServer, username string, o io.Writer) {
g.GitCLI.PrintCreateRepositoryGenerateAccessToken(server, username, o)
}
// IsFork indicates if the repository at the given directory is a fork
func (g *GitLocal) IsFork(dir string) (bool, error) {
return g.GitCLI.IsFork(dir)
}
// Version returns the git version
func (g *GitLocal) Version() (string, error) {
return g.GitCLI.Version()
}
// Username return the username from the git configuration
// Faked out
func (g *GitLocal) Username(dir string) (string, error) {
// Use GitFake as this is a global setting
return g.GitFake.Username(dir)
}
// SetUsername sets the username in the git configuration
// Faked out
func (g *GitLocal) SetUsername(dir string, username string) error {
// Use GitFake as this is a global setting
return g.GitFake.SetUsername(dir, username)
}
// Email returns the email from the git configuration
// Faked out
func (g *GitLocal) Email(dir string) (string, error) {
// Use GitFake as this is a global setting
return g.GitFake.Email(dir)
}
// SetEmail sets the given email in the git configuration
// Faked out
func (g *GitLocal) SetEmail(dir string, email string) error {
// Use GitFake as this is a global setting
return g.GitFake.SetEmail(dir, email)
}
// CreateBranch creates a branch with the given name in the Git repository from the given directory
func (g *GitLocal) CreateBranch(dir string, branch string) error {
return g.GitCLI.CreateBranch(dir, branch)
}
// Diff runs git diff
func (g *GitLocal) Diff(dir string) (string, error) {
return g.GitCLI.Diff(dir)
}
// FetchUnshallow does nothing
// Faked out
func (g *GitLocal) FetchUnshallow(dir string) error {
return g.GitFake.FetchUnshallow(dir)
}
// IsShallow runs git rev-parse --is-shalllow-repository in dir
func (g *GitLocal) IsShallow(dir string) (bool, error) {
return g.GitCLI.IsShallow(dir)
}
// CreateBranchFrom creates a new branch called branchName from startPoint
func (g *GitLocal) CreateBranchFrom(dir string, branchName string, startPoint string) error {
return g.GitCLI.CreateBranchFrom(dir, branchName, startPoint)
}