-
Notifications
You must be signed in to change notification settings - Fork 2
/
clone.go
221 lines (185 loc) · 5.43 KB
/
clone.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
package main
import (
"bytes"
"fmt"
sshconfig "github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"gopkg.in/src-d/go-git.v4/storage/memory"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"strings"
)
func getCredentialHelper(url string) string {
cmd := exec.Command("git", "config", "--get-urlmatch", "credential.helper", url)
helper, err := cmd.Output()
if err == nil {
return strings.TrimSpace(string(helper))
}
if exiterr, ok := err.(*exec.ExitError); ok {
if exiterr.ExitCode() != 1 {
log.Fatalf("ERROR: %s returned exitcode %d", cmd.String(), exiterr.ExitCode())
}
} else {
log.Fatalf("ERROR: %s failed %s", cmd.String(), err)
}
return ""
}
func getPassword(repositoryUrl string) transport.AuthMethod {
u, err := url.Parse(repositoryUrl)
if err != nil {
log.Fatalf("ERROR: url '%s' could not be parsed, %s", repositoryUrl, err)
}
if os.Getenv("GIT_ASKPASS") == "" && getCredentialHelper(repositoryUrl) == "" {
// No credential helper specified, not passing in credentials
return nil
}
user := u.User.Username()
password, _ := u.User.Password()
cmd := exec.Command("git", "credential", "fill")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatalf("ERROR: internal error on getPassword %s", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatalf("ERROR: internal error on getPassword %s", err)
}
go func() {
defer stdin.Close()
input := fmt.Sprintf("protocol=%s\nhost=%s\nusername=%s\npath=%s\n", u.Scheme, u.Host, user, u.Path)
io.WriteString(stdin, input)
}()
out, err := cmd.Output()
if err != nil {
io.Copy(os.Stderr, stderr)
log.Fatalf("ERROR: git credential fill failed, %s", err)
}
for _, line := range strings.Split(string(out), "\n") {
value := strings.SplitN(line, "=", 2)
if value[0] == "username" {
user = value[1]
}
if value[0] == "password" {
password = value[1]
}
}
return &githttp.BasicAuth{Username: user, Password: password}
}
func identityFileAuthentication(user string, host string) (auth transport.AuthMethod, err error) {
var keyFile = sshconfig.Get(host, "IdentityFile")
if user == "" {
user = sshconfig.Get(host, "User")
}
if _, err = os.Stat(keyFile); os.IsNotExist(err) {
return nil, nil
}
key, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("failed to read %s, %s", keyFile, err)
}
signer, parseError := ssh.ParsePrivateKey(key)
if parseError == nil {
return &gitssh.PublicKeys{User: user, Signer: signer}, nil
}
if missingErr, ok := parseError.(*ssh.PassphraseMissingError); ok {
return sshAgentAuthentication(user, host, keyFile, missingErr.PublicKey)
}
if publicKey, _, _, _, publicKeyParseError := ssh.ParseAuthorizedKey(key); publicKeyParseError == nil {
return sshAgentAuthentication(user, host, keyFile, publicKey)
}
return nil, fmt.Errorf("ERROR: failed to read private key from '%s', %s", keyFile, parseError)
}
func sshAgentAuthentication(user, host, keyFile string, key ssh.PublicKey) (auth transport.AuthMethod, err error) {
if user == "" {
user = sshconfig.Get(host, "User")
}
sshAuthSocket := os.Getenv("SSH_AUTH_SOCK")
if sshAuthSocket == "" {
return nil, nil
}
publicKeys, err := gitssh.NewSSHAgentAuth(user)
if err != nil {
return nil, fmt.Errorf("ERROR: failed to connect ssh agent, %s", err)
}
signers, err := publicKeys.Callback()
if err != nil {
return nil, fmt.Errorf("ERROR: failed to obtain keys from ssh agent, %s", err)
}
for _, signer := range signers {
if bytes.Compare(signer.PublicKey().Marshal(), key.Marshal()) == 0 {
return &gitssh.PublicKeys{User: user, Signer: signer}, nil
}
}
if auth == nil && err == nil {
log.Printf("WARNING: key for identity file %s not available in ssh agent.", keyFile)
}
return nil, nil
}
func GetAuth(url string) (auth transport.AuthMethod, plainOpen bool, err error) {
if MatchesScheme(url) {
if os.Getenv("GIT_ASKPASS") != "" || getCredentialHelper(url) != "" {
auth = getPassword(url)
}
return auth, false, nil
}
if MatchesScpLike(url) {
user, host, _, _ := FindScpLikeComponents(url)
if auth, err = identityFileAuthentication(user, host); err != nil {
return
}
} else {
auth = nil
plainOpen = true
}
return
}
func Clone(url string, progress io.Writer, readOnly bool) (r *git.Repository, err error) {
var plainOpen bool
var auth transport.AuthMethod
if auth, plainOpen, err = GetAuth(url); err != nil {
return nil, err
}
if plainOpen {
if !readOnly {
r, err = git.PlainOpenWithOptions(url, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return nil, err
}
} else {
r, err = git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: url,
Progress: progress,
Depth: 2,
})
}
} else {
r, err = git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: url,
Progress: progress,
Auth: auth,
Depth: 2,
})
if err != nil {
return nil, err
}
err = r.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
Depth: 1,
Auth: auth,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return nil, fmt.Errorf("ERROR: failed to fetch all branches from %s, %s", url, err)
}
}
return r, nil
}