forked from google/zoekt
/
main.go
334 lines (295 loc) · 8.22 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This binary fetches all repos of a user or organization and clones
// them. It is strongly recommended to get a personal API token from
// https://github.com/settings/tokens, save the token in a file, and
// point the --token option to it.
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/google/go-github/v27/github"
"golang.org/x/oauth2"
"github.com/henrik242/zoekt/gitindex"
)
type topicsFlag []string
func (f *topicsFlag) String() string {
return strings.Join(*f, ",")
}
func (f *topicsFlag) Set(value string) error {
*f = append(*f, value)
return nil
}
type reposFilters struct {
topics []string
excludeTopics []string
noArchived *bool
}
func main() {
dest := flag.String("dest", "", "destination directory")
githubURL := flag.String("url", "", "GitHub Enterprise url. If not set github.com will be used as the host.")
org := flag.String("org", "", "organization to mirror")
user := flag.String("user", "", "user to mirror")
token := flag.String("token",
filepath.Join(os.Getenv("HOME"), ".github-token"),
"file holding API token.")
forks := flag.Bool("forks", false, "also mirror forks.")
deleteRepos := flag.Bool("delete", false, "delete missing repos")
namePattern := flag.String("name", "", "only clone repos whose name matches the given regexp.")
excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
topics := topicsFlag{}
flag.Var(&topics, "topic", "only clone repos whose have one of given topics. You can add multiple topics by setting this more than once.")
excludeTopics := topicsFlag{}
flag.Var(&excludeTopics, "exclude_topic", "don't clone repos whose have one of given topics. You can add multiple topics by setting this more than once.")
noArchived := flag.Bool("no_archived", false, "mirror only projects that are not archived")
flag.Parse()
if *dest == "" {
log.Fatal("must set --dest")
}
if *githubURL == "" && *org == "" && *user == "" {
log.Fatal("must set either --org or --user when github.com is used as host")
}
var host string
var apiBaseURL string
var client *github.Client
if *githubURL != "" {
rootURL, err := url.Parse(*githubURL)
if err != nil {
log.Fatal(err)
}
host = rootURL.Host
apiPath, err := url.Parse("/api/v3/")
if err != nil {
log.Fatal(err)
}
apiBaseURL = rootURL.ResolveReference(apiPath).String()
client, err = github.NewEnterpriseClient(apiBaseURL, apiBaseURL, nil)
if err != nil {
log.Fatal(err)
}
} else {
host = "github.com"
apiBaseURL = "https://github.com/"
client = github.NewClient(nil)
}
destDir := filepath.Join(*dest, host)
if err := os.MkdirAll(destDir, 0o755); err != nil {
log.Fatal(err)
}
if *token != "" {
content, err := os.ReadFile(*token)
if err != nil {
log.Fatal(err)
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: strings.TrimSpace(string(content)),
})
tc := oauth2.NewClient(context.Background(), ts)
if *githubURL != "" {
client, err = github.NewEnterpriseClient(apiBaseURL, apiBaseURL, tc)
if err != nil {
log.Fatal(err)
}
} else {
client = github.NewClient(tc)
}
}
reposFilters := reposFilters{
topics: topics,
excludeTopics: excludeTopics,
noArchived: noArchived,
}
var repos []*github.Repository
var err error
if *org != "" {
repos, err = getOrgRepos(client, *org, reposFilters)
} else if *user != "" {
repos, err = getUserRepos(client, *user, reposFilters)
} else {
log.Printf("no user or org specified, cloning all repos.")
repos, err = getUserRepos(client, "", reposFilters)
}
if err != nil {
log.Fatal(err)
}
if !*forks {
trimmed := repos[:0]
for _, r := range repos {
if r.Fork == nil || !*r.Fork {
trimmed = append(trimmed, r)
}
}
repos = trimmed
}
filter, err := gitindex.NewFilter(*namePattern, *excludePattern)
if err != nil {
log.Fatal(err)
}
{
trimmed := repos[:0]
for _, r := range repos {
if filter.Include(*r.Name) {
trimmed = append(trimmed, r)
}
}
repos = trimmed
}
if err := cloneRepos(destDir, repos); err != nil {
log.Fatalf("cloneRepos: %v", err)
}
if *deleteRepos {
if err := deleteStaleRepos(*dest, filter, repos, *org+*user); err != nil {
log.Fatalf("deleteStaleRepos: %v", err)
}
}
}
func deleteStaleRepos(destDir string, filter *gitindex.Filter, repos []*github.Repository, user string) error {
var baseURL string
if len(repos) > 0 {
baseURL = *repos[0].HTMLURL
} else {
return nil
}
u, err := url.Parse(baseURL)
if err != nil {
return err
}
u.Path = user
names := map[string]struct{}{}
for _, r := range repos {
u, err := url.Parse(*r.HTMLURL)
if err != nil {
return err
}
names[filepath.Join(u.Host, u.Path+".git")] = struct{}{}
}
if err := gitindex.DeleteRepos(destDir, u, names, filter); err != nil {
log.Fatalf("deleteRepos: %v", err)
}
return nil
}
func hasIntersection(s1, s2 []string) bool {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
if hash[e] {
return true
}
}
return false
}
func filterRepositories(repos []*github.Repository, include []string, exclude []string, noArchived bool) (filteredRepos []*github.Repository) {
for _, repo := range repos {
if noArchived && *repo.Archived {
continue
}
if (len(include) == 0 || hasIntersection(include, repo.Topics)) &&
!hasIntersection(exclude, repo.Topics) {
filteredRepos = append(filteredRepos, repo)
}
}
return
}
func getOrgRepos(client *github.Client, org string, reposFilters reposFilters) ([]*github.Repository, error) {
var allRepos []*github.Repository
opt := &github.RepositoryListByOrgOptions{}
for {
repos, resp, err := client.Repositories.ListByOrg(context.Background(), org, opt)
if err != nil {
return nil, err
}
if len(repos) == 0 {
break
}
opt.Page = resp.NextPage
repos = filterRepositories(repos, reposFilters.topics, reposFilters.excludeTopics, *reposFilters.noArchived)
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
}
return allRepos, nil
}
func getUserRepos(client *github.Client, user string, reposFilters reposFilters) ([]*github.Repository, error) {
var allRepos []*github.Repository
opt := &github.RepositoryListOptions{}
for {
repos, resp, err := client.Repositories.List(context.Background(), user, opt)
if err != nil {
return nil, err
}
if len(repos) == 0 {
break
}
opt.Page = resp.NextPage
repos = filterRepositories(repos, reposFilters.topics, reposFilters.excludeTopics, *reposFilters.noArchived)
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
}
return allRepos, nil
}
func itoa(p *int) string {
if p != nil {
return strconv.Itoa(*p)
}
return ""
}
func cloneRepos(destDir string, repos []*github.Repository) error {
for _, r := range repos {
host, err := url.Parse(*r.HTMLURL)
if err != nil {
return err
}
archived := false
if r.Archived != nil {
archived = *r.Archived
}
config := map[string]string{
"zoekt.web-url-type": "github",
"zoekt.web-url": *r.HTMLURL,
"zoekt.name": filepath.Join(host.Hostname(), *r.FullName),
"zoekt.github-stars": itoa(r.StargazersCount),
"zoekt.github-watchers": itoa(r.WatchersCount),
"zoekt.github-subscribers": itoa(r.SubscribersCount),
"zoekt.github-forks": itoa(r.ForksCount),
"zoekt.archived": marshalBool(archived),
}
dest, err := gitindex.CloneRepo(destDir, *r.FullName, *r.CloneURL, config)
if err != nil {
return err
}
if dest != "" {
fmt.Println(dest)
}
}
return nil
}
func marshalBool(b bool) string {
if b {
return "1"
}
return "0"
}