-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathgit.go
326 lines (307 loc) · 12.4 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright IBM Corporation 2023
*
* 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 vcs
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/konveyor/move2kube/common"
"github.com/konveyor/move2kube/qaengine"
"github.com/sirupsen/logrus"
)
// GitVCSRepo stores git repo config
type GitVCSRepo struct {
InputURL string
URL string
Branch string
Tag string
CommitHash string
PathWithinRepo string
GitRepository *git.Repository
GitRepoPath string
}
var (
// for https or ssh git repo urls
gitVCSRegex = regexp.MustCompile(`^git\+(https|ssh)://[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$`)
)
func isGitCommitHash(commithash string) bool {
gitCommitHashRegex := regexp.MustCompile(`^[a-fA-F0-9]{40}$`)
return gitCommitHashRegex.MatchString(commithash)
}
func isGitBranch(branch string) bool {
gitBranchRegex := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_\/]*$`)
return gitBranchRegex.MatchString(branch)
}
func isGitTag(tag string) bool {
gitTagRegex := regexp.MustCompile(`^v[0-9]+(\.[0-9]+)?(\.[0-9]+)?$`)
return gitTagRegex.MatchString(tag)
}
// getGitRepoStruct extracts information from the given git path and returns a struct
func getGitRepoStruct(vcsurl string) (*GitVCSRepo, error) {
// for format visit https://move2kube.konveyor.io/concepts/git-support
partsSplitByAt := strings.Split(vcsurl, "@")
if len(partsSplitByAt) > 2 {
return nil, fmt.Errorf("invalid git remote path provided. Should follow the format git+[ssh|https]://<URL>@[tag|commit hash|branch]:/path/in/the/repo but received : %s", vcsurl)
}
gitRepoStruct := GitVCSRepo{}
gitRepoStruct.InputURL = vcsurl
partsSplitByColon := strings.Split(partsSplitByAt[0], ":")
gitUrl := partsSplitByAt[0]
if len(partsSplitByColon) == 3 {
gitRepoStruct.PathWithinRepo = partsSplitByColon[2]
gitUrl = strings.Join([]string{partsSplitByColon[0], partsSplitByColon[1]}, ":")
}
if strings.HasPrefix(gitUrl, "git+https") {
hostNameRegex := regexp.MustCompile(`git\+https:\/\/(.*?)\/`)
matches := hostNameRegex.FindAllStringSubmatch(gitUrl, -1)
if len(matches) == 0 {
return nil, fmt.Errorf("failed to extract host name from the given vcs url %v", vcsurl)
}
hostName := matches[0][1]
gitRepoStruct.GitRepoPath = strings.TrimPrefix(gitUrl, "git+https://"+hostName+"/")
gitRepoStruct.URL = strings.TrimPrefix(gitUrl, "git+")
} else if strings.HasPrefix(gitUrl, "git+ssh") {
hostNameRegex := regexp.MustCompile(`git\+ssh:\/\/(.*?)\/`)
matches := hostNameRegex.FindAllStringSubmatch(gitUrl, -1)
if len(matches) == 0 {
return nil, fmt.Errorf("failed to extract host name from the given vcs url %v", vcsurl)
}
hostName := matches[0][1]
gitRepoStruct.GitRepoPath = strings.TrimPrefix(gitUrl, "git+ssh://"+hostName+"/")
gitRepoStruct.URL = "git@" + hostName + ":" + gitRepoStruct.GitRepoPath
} else {
return nil, fmt.Errorf("failed to have either of the prefixes git+https or git+ssh, got %v", gitUrl)
}
if len(partsSplitByAt) == 2 {
if isGitBranch(partsSplitByAt[1]) {
gitRepoStruct.Branch = partsSplitByAt[1]
} else if isGitCommitHash(partsSplitByAt[1]) {
gitRepoStruct.CommitHash = partsSplitByAt[1]
} else if isGitTag(partsSplitByAt[1]) {
gitRepoStruct.Tag = partsSplitByAt[1]
}
}
return &gitRepoStruct, nil
}
// isGitVCS checks if the given vcs url is a git repo url
func isGitVCS(vcsurl string) bool {
return gitVCSRegex.MatchString(vcsurl)
}
func pushGitVCS(remotePath, folderName string, maxSize int64) error {
if !common.IgnoreEnvironment {
logrus.Warnf("push to remote git repositories using credentials from the environment is not yet supported.")
}
remotePathSplitByAt := strings.Split(remotePath, "@")
remotePathSplitByColon := strings.Split(remotePathSplitByAt[0], ":")
isSSH := strings.HasPrefix(remotePath, "git+ssh")
isHTTPS := strings.HasPrefix(remotePath, "git+https")
gitFSPath, err := GetClonedPath(remotePath, folderName, false)
if err != nil {
return fmt.Errorf("failed to clone the repo. Error: %w", err)
}
if (isHTTPS && len(remotePathSplitByColon) > 2) || (isSSH && len(remotePathSplitByColon) > 2) {
gitFSPath = strings.TrimSuffix(gitFSPath, remotePathSplitByColon[len(remotePathSplitByColon)-1])
}
repo, err := git.PlainOpen(gitFSPath)
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to open the repository. Error %+v", err)}
}
worktree, err := repo.Worktree()
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to fetch a worktree. Error %+v", err)}
}
_, err = worktree.Add(".")
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to add files to staging. Error %+v", err)}
}
authorName := qaengine.FetchStringAnswer(common.JoinQASubKeys(common.GitKey, "name"), "Enter git author name : ", []string{}, "", nil)
authorEmail := qaengine.FetchStringAnswer(common.JoinQASubKeys(common.GitKey, "email"), "Enter git author email : ", []string{}, "", nil)
commit, err := worktree.Commit("add move2kube generated output artifacts", &git.CommitOptions{
Author: &object.Signature{
Name: authorName,
Email: authorEmail,
When: time.Now(),
},
})
logrus.Debugf("changes committed with commit hash : %+v", commit)
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to commit. Error : %+v", err)}
}
ref, err := repo.Head()
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to get head. Error : %+v", err)}
}
if isHTTPS {
username := qaengine.FetchStringAnswer(common.JoinQASubKeys(common.GitKey, "username"), "Enter git username : ", []string{}, "", nil)
password := qaengine.FetchPasswordAnswer(common.JoinQASubKeys(common.GitKey, "pass"), "Enter git password : ", []string{}, nil)
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("+%s:%s", ref.Name(), ref.Name())),
},
Auth: &http.BasicAuth{
Username: username,
Password: password,
},
})
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to push. Error : %+v", err)}
}
}
if isSSH {
authMethod, err := ssh.DefaultAuthBuilder("git")
if err != nil {
return fmt.Errorf("failed to get default auth builder. Error : %v", authMethod)
}
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("+%s:%s", ref.Name(), ref.Name())),
},
Auth: authMethod,
})
if err != nil {
return &FailedVCSPush{VCSPath: gitFSPath, Err: fmt.Errorf("failed to push. Error : %+v", err)}
}
}
return nil
}
// Clone clones a git repository with the given commit depth
// and path where it is to be cloned and returns the final path inside the repo
func (gvcsrepo *GitVCSRepo) Clone(cloneOptions VCSCloneOptions) (string, error) {
if cloneOptions.CloneDestinationPath == "" {
return "", fmt.Errorf("the path where the repository has to be cloned cannot be empty")
}
repoPath := filepath.Join(cloneOptions.CloneDestinationPath, gvcsrepo.GitRepoPath)
repoDirInfo, err := os.Stat(repoPath)
if err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("failed to stat the git repo clone destination path '%s'. error: %w", repoPath, err)
}
logrus.Debugf("the cloned git repo will be available at '%s'", repoPath)
} else {
if !cloneOptions.Overwrite {
if !repoDirInfo.IsDir() {
return "", fmt.Errorf("a file already exists at the git repo clone destination path '%s'", repoPath)
}
logrus.Infof("Assuming that the directory at '%s' is the cloned repo", repoPath)
return filepath.Join(repoPath, gvcsrepo.PathWithinRepo), nil
}
logrus.Infof("git repository clone will overwrite the files/directories at '%s'", repoPath)
if err := os.RemoveAll(repoPath); err != nil {
return "", fmt.Errorf("failed to remove the files/directories at '%s' . error: %w", repoPath, err)
}
}
logrus.Infof("Cloning the repository using git into '%s' . This might take some time.", cloneOptions.CloneDestinationPath)
// ------------
var repoDirWt, dotGitDir billy.Filesystem
repoDirWt = osfs.New(repoPath)
dotGitDir, _ = repoDirWt.Chroot(git.GitDirName)
fStorer := filesystem.NewStorage(dotGitDir, cache.NewObjectLRUDefault())
limitStorer := Limit(fStorer, cloneOptions.MaxSize)
// ------------
commitDepth := 1
if cloneOptions.CommitDepth != 0 {
commitDepth = cloneOptions.CommitDepth
}
if gvcsrepo.Branch != "" {
cloneOpts := git.CloneOptions{
URL: gvcsrepo.URL,
Depth: commitDepth,
SingleBranch: true,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", gvcsrepo.Branch)),
}
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
if err != nil {
logrus.Debugf("failed to clone the given branch '%s' . Will clone the entire repo and try again.", gvcsrepo.Branch)
cloneOpts := git.CloneOptions{
URL: gvcsrepo.URL,
Depth: commitDepth,
}
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
if err != nil {
return "", fmt.Errorf("failed to perform clone operation using git. Error: %w", err)
}
branch := fmt.Sprintf("refs/heads/%s", gvcsrepo.Branch)
b := plumbing.ReferenceName(branch)
w, err := gvcsrepo.GitRepository.Worktree()
if err != nil {
return "", fmt.Errorf("failed return a worktree for the repostiory. Error: %w", err)
}
if err := w.Checkout(&git.CheckoutOptions{Create: false, Force: false, Branch: b}); err != nil {
logrus.Debugf("failed to checkout the branch '%s', creating it...", b)
if err := w.Checkout(&git.CheckoutOptions{Create: true, Force: false, Branch: b}); err != nil {
return "", fmt.Errorf("failed checkout a new branch. Error : %+v", err)
}
}
}
} else if gvcsrepo.CommitHash != "" {
commitHash := plumbing.NewHash(gvcsrepo.CommitHash)
cloneOpts := git.CloneOptions{
URL: gvcsrepo.URL,
}
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
if err != nil {
return "", fmt.Errorf("failed to perform clone operation using git with options %+v. Error: %w", cloneOpts, err)
}
r, err := git.PlainOpen(repoPath)
if err != nil {
return "", fmt.Errorf("failed to open the git repository at the given path '%s' . Error: %w", repoPath, err)
}
w, err := r.Worktree()
if err != nil {
return "", fmt.Errorf("failed return a worktree for the repostiory %+v. Error: %w", r, err)
}
checkoutOpts := git.CheckoutOptions{Hash: commitHash}
if err := w.Checkout(&checkoutOpts); err != nil {
return "", fmt.Errorf("failed to checkout commit hash '%s' on work tree. Error: %w", commitHash, err)
}
} else if gvcsrepo.Tag != "" {
cloneOpts := git.CloneOptions{
URL: gvcsrepo.URL,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/tags/%s", gvcsrepo.Tag)),
}
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
if err != nil {
return "", fmt.Errorf("failed to perform clone operation using git with options %+v. Error: %w", cloneOpts, err)
}
} else {
cloneOpts := git.CloneOptions{
URL: gvcsrepo.URL,
Depth: commitDepth,
SingleBranch: true,
ReferenceName: "refs/heads/main",
}
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
if err != nil {
return "", fmt.Errorf("failed to perform clone operation using git with options %+v and %+v. Error: %w", cloneOpts, cloneOptions, err)
}
}
return filepath.Join(repoPath, gvcsrepo.PathWithinRepo), nil
}