-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
git.go
187 lines (159 loc) · 3.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
package main
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/kevinburke/ssh_config"
"github.com/muesli/gitty/vcs"
"github.com/muesli/gitty/vcs/gitea"
"github.com/muesli/gitty/vcs/github"
"github.com/muesli/gitty/vcs/gitlab"
)
type Client interface {
Issues(owner string, name string) ([]vcs.Issue, error)
PullRequests(owner string, name string) ([]vcs.PullRequest, error)
Repository(owner string, name string) (vcs.Repo, error)
Repositories(owner string) ([]vcs.Repo, error)
Branches(owner string, name string) ([]vcs.Branch, error)
History(repo vcs.Repo, max int, since time.Time) ([]vcs.Commit, error)
GetUsername() (string, error)
IssueURL(owner string, name string, number int) string
}
func tokenForHost(host string) string {
token := os.Getenv("GITTY_TOKENS")
tokens := strings.Split(token, ";")
for _, t := range tokens {
if !strings.Contains(t, "=") {
continue
}
s := strings.Split(t, "=")
k, v := s[0], s[1]
if !strings.EqualFold(k, host) {
continue
}
return strings.TrimSpace(v)
}
// fallback for old tokens
if host == "github.com" {
token = os.Getenv("GITTY_TOKEN")
if len(token) > 0 {
return token
}
token = os.Getenv("GITHUB_TOKEN")
if len(token) > 0 {
return token
}
}
return ""
}
func guessClient(host string) (Client, error) {
token := tokenForHost(host)
if len(token) == 0 {
return nil, fmt.Errorf("please set a GITTY_TOKENS env var for host " + host)
}
if strings.EqualFold(host, "github.com") {
return github.NewClient(token)
}
if strings.EqualFold(host, "gitlab.com") {
return gitlab.NewClient(host, token, true)
}
if strings.EqualFold(host, "gitea.com") {
return gitea.NewClient(host, token, true)
}
if strings.EqualFold(host, "codeberg.org") {
return gitea.NewClient(host, token, true)
}
if strings.Contains(host, "invent.kde.org") {
return gitlab.NewClient(host, token, true)
}
var client Client
var err error
client, err = gitlab.NewClient(host, token, false)
if err == nil {
return client, nil
}
client, err = gitea.NewClient(host, token, false)
if err == nil {
return client, nil
}
// fmt.Println(err)
return nil, fmt.Errorf("not a recognized git provider")
}
func originURL(path string) (string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return "", err
}
remotes, err := r.Remotes()
if err != nil {
return "", err
}
var u string
var rn string
for _, v := range remotes {
if (v.Config().Name == "origin" && rn != "origin") ||
rn == "" {
rn = v.Config().Name
u = v.Config().URLs[0]
}
}
if u == "" {
return "", fmt.Errorf("no remote found")
}
return cleanupURL(u)
}
func cleanupURL(arg string) (string, error) {
var sshURL bool
if strings.Contains(arg, "://") {
u, err := url.Parse(arg)
if err == nil {
host, _, err := net.SplitHostPort(u.Host)
if err == nil {
// strip port
u.Host = host
arg = u.String()
}
}
arg = strings.Split(arg, "://")[1]
} else {
sshURL = true
arg = strings.ReplaceAll(arg, ":", "/")
}
arg = "https://" + arg
u, err := url.Parse(arg)
if err != nil {
return "", err
}
// SSH URLs support stanzas we need to resolve.
if sshURL {
nh := ssh_config.Get(u.Hostname(), "Hostname")
if nh != "" {
u.Host = nh
}
}
u.Path = strings.TrimSuffix(u.Path, ".git")
u.User = nil
u.Scheme = "https"
return u.String(), nil
}
// parseRepo returns host, owner and repository name from a given path or URL.
func parseRepo(arg string) (string, string, string, error) {
u, err := originURL(arg)
if err != nil {
// not a local repo
u, err = cleanupURL(arg)
if err != nil {
return "", "", "", err
}
}
p := strings.Split(u, "/")
if len(p) < 5 {
return "", "", "", fmt.Errorf("does not look like a valid path or URL")
}
host, owner, name := p[2], p[3], p[4]
return host, owner, name, nil
}