/
git.go
177 lines (148 loc) · 3.58 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
package yagu
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
func FindGitRepoAbsPath(dir string) (string, error) {
var err error
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return "", err
}
}
dir, err = filepath.Abs(dir)
if err != nil {
return "", err
}
found := false
for !found && dir != "/" {
try := filepath.Join(dir, ".git")
info, err := os.Stat(try)
if err == nil && info.IsDir() {
found = true
break
}
next := filepath.Clean(filepath.Join(dir, ".."))
dir = next
}
if !found {
return "", nil
}
return dir, nil
}
// FindRemoteRepoRootAndClone walks a path back until
// it finds a valid git repo, cloning it into a tmp dir
func FindRemoteRepoRootAndClone(repo, ver string) (root, dir string, err error) {
parts := strings.Split(repo, "/")
for i := len(parts); i > 1; i-- {
r := strings.Join(parts[:i], "/")
fmt.Println("trying...", r)
dir, err = CloneRepoIntoTmp("https://" + r, ver)
if err == nil {
return r, dir, nil
}
err = os.RemoveAll(dir)
if err != nil {
return r, dir, err
}
}
return "", "", fmt.Errorf("failed to find remote git repo from %s@%s", repo, ver)
}
func CloneRepoIntoTmp(srcUrl, srcVer string) (string, error) {
co, err := SetupGitOptions(srcUrl, srcVer)
if err != nil {
return "", err
}
// temp dir to clone to
dir, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
// Clones the repository into the worktree (fs) and storer all the .git
// content into the storer
_, err = git.PlainClone(dir, false, co)
if err != nil {
return dir, err
}
return dir, nil
}
func CloneRepoIntoDir(srcUrl, srcVer, dest string) error {
co, err := SetupGitOptions(srcUrl, srcVer)
if err != nil {
return err
}
// Clones the repository into the worktree (fs) and storer all the .git
// content into the storer
_, err = git.PlainClone(dest, false, co)
if err != nil {
return err
}
return nil
}
func SetupGitOptions(srcUrl, srcVer string) (*git.CloneOptions, error) {
co := &git.CloneOptions{
URL: srcUrl,
// Progress: os.Stdout,
// SingleBranch: true,
}
if srcVer != "" {
co.ReferenceName = plumbing.ReferenceName(srcVer)
}
err := SetupGitAuth(srcUrl, srcVer, co)
if err != nil {
return co, err
}
return co, nil
}
func SetupGitAuth(srcUrl, srcVer string, co *git.CloneOptions) error {
// GitHub variations
if strings.Contains(srcUrl, "github.com") {
if os.Getenv("GITHUB_TOKEN") != "" {
co.Auth = &http.BasicAuth{
Username: "github-token", // yes, this can be anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
}
return nil
}
}
// GitLab variations
if strings.Contains(srcUrl, "gitlab.com") {
if os.Getenv("GITLAB_TOKEN") != "" {
co.Auth = &http.TokenAuth{
Token: os.Getenv("GITLAB_TOKEN"),
}
return nil
}
if os.Getenv("GITLAB_USERNAME") != "" && os.Getenv("GITLAB_PASSWORD") != "" {
co.Auth = &http.BasicAuth{
Username: os.Getenv("GITLAB_USERNAME"),
Password: os.Getenv("GITLAB_PASSWORD"),
}
return nil
}
}
// BitBucket variations
if strings.Contains(srcUrl, "bitbucket.org") {
if os.Getenv("BITBUCKET_TOKEN") != "" {
co.Auth = &http.TokenAuth{
Token: os.Getenv("BITBUCKET_TOKEN"),
}
return nil
}
if os.Getenv("BITBUCKET_USERNAME") != "" && os.Getenv("BITBUCKET_PASSWORD") != "" {
co.Auth = &http.BasicAuth{
Username: os.Getenv("BITBUCKET_USERNAME"),
Password: os.Getenv("BITBUCKET_PASSWORD"),
}
return nil
}
}
return nil
}