-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-git.go
134 lines (122 loc) · 2.72 KB
/
go-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
//go:build gogit
package main
import (
"log"
"net/url"
"os"
"path/filepath"
"strings"
git "github.com/go-git/go-git/v5"
plumbing "github.com/go-git/go-git/v5/plumbing"
transport "github.com/go-git/go-git/v5/plumbing/transport"
http "github.com/go-git/go-git/v5/plumbing/transport/http"
ssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
)
func netrcAuth(importPath string) transport.AuthMethod {
u, err := url.Parse("https://" + importPath)
if err != nil {
return nil
}
f, err := os.Open(filepath.Join(os.Getenv("HOME"), ".netrc"))
if err != nil {
return nil
}
defer f.Close()
username, password, err := netrc(f, u.Host)
if err != nil {
return nil
}
if username == "" && password == "" {
return nil
}
return &http.BasicAuth{Username: username, Password: password}
}
func sshAuth() transport.AuthMethod {
auth, _ := ssh.NewSSHAgentAuth("git")
return auth
}
func auth(url string) (transport.AuthMethod, string) {
auth := netrcAuth(url)
schema := "https://"
if auth == nil {
auth = sshAuth()
schema = "ssh://"
}
return auth, schema
}
type gitRepo struct {
url string
repo *git.Repository
}
func gitOpenDir(url, dir string) (repo, error) {
r, err := git.PlainOpen(dir)
if err != nil {
return nil, err
}
return &gitRepo{url: url, repo: r}, nil
}
func gitCloneDir(url, dir string) (repo, error) {
auth, schema := auth(url)
r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: schema + url + ".git",
Auth: auth,
})
if err != nil {
return nil, err
}
return &gitRepo{url: url, repo: r}, nil
}
func (r *gitRepo) Checkout(rev string) error {
w, err := r.repo.Worktree()
if err != nil {
return err
}
if rev == "" || rev == latestRev {
ref, err := r.repo.Head()
if err != nil {
return err
}
rev = ref.Hash().String()
log.Println("Using HEAD revision", rev)
} else {
tagrefs, err := r.repo.Tags()
if err != nil {
return err
}
found := false
tagrefs.ForEach(func(t *plumbing.Reference) error {
if !found && strings.TrimPrefix(t.Name().String(), "refs/tags/") == rev {
found = true
rev = t.Hash().String()
annotated, err := r.repo.TagObject(t.Hash())
if err == nil {
rev = annotated.Target.String()
}
log.Println("Using tag ", t.Name().String(), "revision", rev)
}
return nil
})
}
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(rev),
})
if err != nil {
return err
}
return nil
}
func (r *gitRepo) Fetch() error {
w, err := r.repo.Worktree()
if err != nil {
return err
}
auth, _ := auth(r.url)
if err := w.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
}); err != nil && err != git.NoErrAlreadyUpToDate {
// Ignore if pull fails, try our best to work offline
return err
}
return nil
}