-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
269 lines (234 loc) · 5.82 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
package main
import (
"encoding/hex"
"fmt"
//"net"
"os"
"path/filepath"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
gittransport "github.com/go-git/go-git/v5/plumbing/transport"
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"golang.org/x/crypto/ssh"
//gitobject "github.com/go-git/go-git/v5/plumbing/object"
//gitconfig "github.com/go-git/go-git/v5/config"
)
func gitAuth(repoAddr string) (gittransport.AuthMethod, error) {
var isSshProtocal bool
for {
addr := strings.ToLower(repoAddr)
if strings.HasPrefix(addr, "https://") {
break
}
isSshProtocal = strings.HasPrefix(addr, "ssh://")
if isSshProtocal {
break
}
if i := strings.IndexByte(addr, '@'); i >= 0 {
// also check ':' ?
isSshProtocal = true
break
}
break
}
if isSshProtocal {
var homeDir, err = os.UserHomeDir()
if err != nil {
return nil, err
}
var potentialKeys = make([]string, 0, 2)
sshPath := filepath.Join(homeDir, ".ssh")
files, err := os.ReadDir(sshPath)
if err == nil {
for _, f := range files {
if f.IsDir() {
continue
}
name := f.Name()
if !strings.HasPrefix(name, "known_hosts") && !strings.HasSuffix(name, ".pub") {
potentialKeys = append(potentialKeys, filepath.Join(sshPath, name))
}
}
}
fmt.Println()
var sshKeyFilePath string
switch len(potentialKeys) {
case 0:
fmt.Println(`Need a ssh key to authenticate to remote server.`)
for strings.TrimSpace(sshKeyFilePath) == "" {
fmt.Print(`Specify the key file here: `)
_, err = fmt.Scanln(&sshKeyFilePath)
if err != nil && !strings.Contains(err.Error(), "unexpected newline") {
return nil, err
}
sshKeyFilePath = strings.TrimSpace(sshKeyFilePath)
}
case 1:
fmt.Printf(`Need a ssh key to authenticate to remote server.
Specify the key file here (Enter for %s): `, potentialKeys[0])
_, err = fmt.Scanln(&sshKeyFilePath)
if err != nil && !strings.Contains(err.Error(), "unexpected newline") {
return nil, err
}
sshKeyFilePath = strings.TrimSpace(sshKeyFilePath)
if sshKeyFilePath == "" {
sshKeyFilePath = potentialKeys[0]
}
case 2:
fmt.Println(`Need a ssh key to authenticate to remote server.
The key file might be one of (but not limited to) the following ones:`)
for _, f := range potentialKeys {
fmt.Printf("* %s\n", f)
}
fmt.Println()
for strings.TrimSpace(sshKeyFilePath) == "" {
fmt.Print(`Specify the key file here: `)
_, err = fmt.Scanln(&sshKeyFilePath)
if err != nil && !strings.Contains(err.Error(), "unexpected newline") {
return nil, err
}
sshKeyFilePath = strings.TrimSpace(sshKeyFilePath)
}
}
sshKey, err := os.ReadFile(sshKeyFilePath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey([]byte(sshKey))
if err != nil {
if _, ok := err.(*ssh.PassphraseMissingError); !ok {
return nil, err
}
fmt.Print(`Passphase: `)
var passphase string
_, err = fmt.Scanln(&passphase)
if err != nil {
return nil, err
}
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(sshKey), []byte(passphase))
if err != nil {
return nil, err
}
}
//hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// return nil
//}
return &gitssh.PublicKeys{
User: "git",
Signer: signer,
// Since go-git v5.5.0, this field is gone.
//HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
// HostKeyCallback: hostKeyCallback,
//},
}, nil
}
return nil, nil
}
func gitClone(repoAddr, toDir string) error {
var auth, err = gitAuth(repoAddr)
if err != nil {
return err
}
_, err = git.PlainClone(toDir, false,
&git.CloneOptions{
Auth: auth,
URL: repoAddr,
Progress: os.Stdout,
},
)
if err != nil {
return err
}
return nil
}
func gitPull(repoDir string) error {
var repo, err = git.PlainOpen(repoDir)
if err != nil {
return err
}
remote, err := repo.Remote("origin")
if err != nil {
return err
}
repoAddr := remote.Config().URLs[0]
auth, err := gitAuth(repoAddr)
if err != nil {
return err
}
worktree, err := repo.Worktree()
if err != nil {
return err
}
var o = git.PullOptions{
Auth: auth,
Force: true,
}
err = worktree.Pull(&o)
if err != nil && err.Error() == "already up-to-date" {
err = nil
}
return err
}
func gitFetch(repoDir string) error {
var repo, err = git.PlainOpen(repoDir)
if err != nil {
return err
}
remote, err := repo.Remote("origin")
if err != nil {
return err
}
repoAddr := remote.Config().URLs[0]
auth, err := gitAuth(repoAddr)
if err != nil {
return err
}
var o = git.FetchOptions{
Auth: auth,
Force: true,
}
return repo.Fetch(&o)
}
func gitWorktree(repoDir string) (*git.Worktree, error) {
var repo, err = git.PlainOpen(repoDir)
if err != nil {
return nil, err
}
return repo.Worktree()
}
func gitCheckout(repoDir string, opt *git.CheckoutOptions) error {
worktree, err := gitWorktree(repoDir)
if err != nil {
return err
}
return worktree.Checkout(opt)
}
func gitListTagsAndRemoteBranches(repoDir string) (tags map[string]string, bras map[string]string, err error) {
repo, err := git.PlainOpen(repoDir)
if err != nil {
return
}
iter, err := repo.References()
if err != nil {
return
}
const TagRefPrefix = "refs/tags/"
const BranchRefPrefix = "refs/remotes/origin/"
tags = make(map[string]string, 1024)
bras = make(map[string]string, 128)
iter.ForEach(func(ref *plumbing.Reference) error {
switch name := string(ref.Name()); {
case strings.HasPrefix(name, TagRefPrefix):
var hash = ref.Hash()
tags[name[len(TagRefPrefix):]] = hex.EncodeToString(hash[:])
case strings.HasPrefix(name, BranchRefPrefix):
var hash = ref.Hash()
bras[name[len(BranchRefPrefix):]] = hex.EncodeToString(hash[:])
default:
//fmt.Println(ref.String())
}
return nil
})
return
}