-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
580 lines (514 loc) · 17.1 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
"strings"
"github.com/google/go-github/v32/github"
"github.com/pmatseykanets/gh-tools/auth"
gh "github.com/pmatseykanets/gh-tools/github"
"github.com/pmatseykanets/gh-tools/size"
"github.com/pmatseykanets/gh-tools/terminal"
"github.com/pmatseykanets/gh-tools/version"
"golang.org/x/oauth2"
)
func usage() {
usage := `Walk file hierarchies across GitHub repositories
Usage: gh-find [flags] [owner][/repo]
owner Repository owner (user or organization)
repo Repository name
Flags:
-archived Include archived repositories
-help, h Print this information and exit
-branch= The branch name if different from the default
-grep= The pattern to match the file contents. Implies
-type f
-list-details List details (file type, author, size, last commit date)
-max-depth Descend at most n directory levels
-max-grep-results= Limit the number of grep results
-max-repo-results= Limit the number of matched entries per repository
-max-results= Limit the number of matched entries
-min-depth= Descend at least n directory levels
-name= The pattern to match the last component of the pathname
-no-fork Don't include fork repositories
-no-grep= The pattern to reject the file contents. Implies
-type f
-no-matches List repositories with no matches. Implies
-max-results 0
-max-grep-results 1
-max-repo-results 1
-no-name= The pattern to reject the last component of the pathname
-no-path= The pattern to reject the pathname
-no-private Don't include private repositories
-no-public Don't include public repositories
-no-repo= The pattern to reject repository names
-path= The pattern to match the pathname
-repo= The pattern to match repository names
-size= Limit results based on the file size [+-]<d><u>
-token Prompt for an Access Token
-type= The entry type f - file, d - directory
-version Print the version and exit
`
fmt.Printf("gh-find version %s\n", version.Version)
fmt.Println(usage)
}
func main() {
if err := run(context.Background()); err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
}
const (
typeFile = "f"
typeDir = "d"
)
type sizePredicate struct {
op int // <0 - less than, 0 - equals, >0 greater than
value int64 // Size in bytes
}
func (p *sizePredicate) match(value int64) bool {
switch p.op {
case 0:
return value == p.value
case 1:
return value >= p.value
default:
return value <= p.value
}
}
type config struct {
owner string
repo string
repoRegexp *regexp.Regexp // The pattern to match respository names.
branch string // The branch name if different from the default.
ftype string // The entry type f - file, d - directory.
minDepth int // Descend at least n directory levels.
maxDepth int // Descend at most n directory levels.
maxResults int // Limit the number of matched entries.
maxRepoResults int // Limit the number of matched entries per repository.
nameRegexp []*regexp.Regexp // The pattern to match the last component of the pathname.
noNameRegexp []*regexp.Regexp // The pattern to reject the last component of the pathname.
pathRegexp []*regexp.Regexp // The pattern to match the pathname.
noPathRegexp []*regexp.Regexp // The pattern to reject the pathname.
grepRegexp *regexp.Regexp // The pattern to match the contents of matching files.
noGrepRegexp *regexp.Regexp // The pattern to reject the file contents.
token bool // Propmt for an access token.
size *sizePredicate // Limit results based on the file size [+-]<d><u>.
noMatches bool // List repositories with no matches.
maxGrepResults int // Limit the number of grep results.
listDetails bool // List details.
archived bool // Include archived repositories.
noPrivate bool // Don't include private repositories.
noPublic bool // Don't include public repositories.
noFork bool // Don't include fork repositories.
noRepoRegexp *regexp.Regexp // The pattern to reject repository names.
}
type finder struct {
gh *github.Client
config config
stdout io.WriteCloser
stderr io.WriteCloser
}
type stringList []string
func (l *stringList) String() string {
if l == nil {
return ""
}
return strings.Join(*l, ",")
}
func (l *stringList) Set(value string) error {
*l = append(*l, value)
return nil
}
func readConfig() (config, error) {
if len(os.Args) == 0 {
usage()
os.Exit(1)
}
config := config{}
var (
showVersion, showHelp bool
grep, noGrep, repo, noRepo, fsize string
name, path, noName, noPath stringList
err error
)
flag.BoolVar(&config.archived, "archived", config.archived, "Include archived repositories")
flag.StringVar(&config.branch, "branch", "", "The branch name if different from the default")
flag.BoolVar(&showHelp, "help", false, "Print this information and exit")
flag.StringVar(&grep, "grep", "", "The pattern to match the file contents")
flag.BoolVar(&config.listDetails, "list-details", config.listDetails, "List details (file type, author, size, last commit date)")
flag.IntVar(&config.maxDepth, "max-depth", 0, "Descend at most n directory levels")
flag.IntVar(&config.maxGrepResults, "max-grep-results", 0, "Limit the number of grep results.")
flag.IntVar(&config.maxResults, "max-results", 0, "Limit the number of matched entries")
flag.IntVar(&config.maxRepoResults, "max-repo-results", 0, "Limit the number of matched entries per repository")
flag.IntVar(&config.minDepth, "min-depth", 0, "Descend at least n directory levels")
flag.Var(&name, "name", "The pattern to match the last component of the pathname")
flag.BoolVar(&config.noFork, "no-fork", config.noFork, "Don't include fork repositories")
flag.StringVar(&noGrep, "no-grep", "", "The pattern to reject the file contents")
flag.BoolVar(&config.noMatches, "no-matches", config.noMatches, "List repositories with no matches")
flag.Var(&noName, "no-name", "The pattern to reject the last component of the pathname")
flag.Var(&noPath, "no-path", "The pattern to reject the pathname")
flag.BoolVar(&config.noPrivate, "no-private", config.noPrivate, "Don't include private repositories")
flag.BoolVar(&config.noPublic, "no-public", config.noPublic, "Don't include public repositories")
flag.StringVar(&noRepo, "no-repo", "", "The pattern to reject repository names")
flag.Var(&path, "path", "The pattern to match the pathname")
flag.StringVar(&repo, "repo", "", "The pattern to match repository names")
flag.StringVar(&fsize, "size", "", "Limit results based on the file size [+-]<d><u>")
flag.BoolVar(&config.token, "token", config.token, "Prompt for Access Token")
flag.StringVar(&config.ftype, "type", "", "File type f - file, d - directory")
flag.BoolVar(&showVersion, "version", showVersion, "Print version and exit")
flag.Usage = usage
flag.Parse()
if showHelp {
usage()
os.Exit(0)
}
if showVersion {
fmt.Printf("gh-find version %s\n", version.Version)
os.Exit(0)
}
parts := strings.Split(flag.Arg(0), "/")
nparts := len(parts)
if nparts > 0 {
config.owner = parts[0]
}
if nparts > 1 {
config.repo = parts[1]
}
if nparts > 2 {
return config, fmt.Errorf("invalid owner or repository name %s", flag.Arg(0))
}
if config.owner == "" {
return config, fmt.Errorf("owner is required")
}
if config.noPrivate && config.noPublic {
return config, fmt.Errorf("no-private and no-public are mutually exclusive")
}
config.nameRegexp = make([]*regexp.Regexp, len(name))
for i, n := range name {
if config.nameRegexp[i], err = regexp.Compile(n); err != nil {
return config, fmt.Errorf("invalid name pattern: %s: %s", n, err)
}
}
config.noNameRegexp = make([]*regexp.Regexp, len(noName))
for i, n := range noName {
if config.noNameRegexp[i], err = regexp.Compile(n); err != nil {
return config, fmt.Errorf("invalid no-name pattern: %s: %s", n, err)
}
}
config.pathRegexp = make([]*regexp.Regexp, len(path))
for i, n := range path {
if config.pathRegexp[i], err = regexp.Compile(n); err != nil {
return config, fmt.Errorf("invalid path pattern: %s: %s", n, err)
}
}
config.noPathRegexp = make([]*regexp.Regexp, len(noPath))
for i, n := range noPath {
if config.noPathRegexp[i], err = regexp.Compile(n); err != nil {
return config, fmt.Errorf("invalid no-path pattern: %s: %s", n, err)
}
}
if repo != "" {
if config.repoRegexp, err = regexp.Compile(repo); err != nil {
return config, fmt.Errorf("invalid repo pattern: %s", err)
}
}
if noRepo != "" {
if config.noRepoRegexp, err = regexp.Compile(noRepo); err != nil {
return config, fmt.Errorf("invalid no-repo pattern: %s", err)
}
}
switch t := config.ftype; t {
case "", typeFile, typeDir: // Empty or valid.
default:
return config, fmt.Errorf("invalid type: %s", t)
}
if grep != "" {
if config.grepRegexp, err = regexp.Compile(grep); err != nil {
return config, fmt.Errorf("invalid grep pattern: %s", err)
}
config.ftype = typeFile // Implies file type.
}
if noGrep != "" {
if config.noGrepRegexp, err = regexp.Compile(noGrep); err != nil {
return config, fmt.Errorf("invalid no-grep pattern: %s", err)
}
config.ftype = typeFile // Implies file type.
}
if config.maxDepth < 0 {
return config, fmt.Errorf("max-depth should be positive")
}
if config.minDepth < 0 {
return config, fmt.Errorf("min-depth should be positive")
}
if config.maxDepth > 0 && config.minDepth > 0 && config.maxDepth < config.minDepth {
return config, fmt.Errorf("min-depth should be less than max-depth")
}
if config.maxResults < 0 {
return config, fmt.Errorf("max-results should be positive")
}
if config.maxRepoResults < 0 {
return config, fmt.Errorf("max-repo-results should be positive")
}
if config.maxGrepResults < 0 {
return config, fmt.Errorf("max-grep-results should be positive")
}
if fsize != "" {
p := &sizePredicate{}
switch fsize[0] {
case '+':
p.op = 1
case '-':
p.op = -1
}
offset := 0
if p.op != 0 {
offset = 1
}
value, err := size.Parse(fsize[offset:])
if err != nil {
return config, fmt.Errorf("invalid size %s", fsize)
}
p.value = value
config.size = p
config.ftype = typeFile // Implies file type.
}
if config.noMatches {
// Implies no limit on max overall results.
config.maxResults = 0
// And there is no reason to look futher at the repo level
// if we have at least one entry match.
config.maxRepoResults = 1
// Or a at least one grep match.
config.maxGrepResults = 1
}
return config, nil
}
func run(ctx context.Context) error {
var err error
finder := &finder{
stdout: os.Stdout,
stderr: os.Stderr,
}
finder.config, err = readConfig()
if err != nil {
return err
}
var token string
if finder.config.token {
token, _ = terminal.PasswordPrompt("Access Token: ")
} else {
token = auth.GetToken()
}
if token == "" {
return fmt.Errorf("access token is required")
}
finder.gh = github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)))
return finder.find(ctx)
}
func (f *finder) find(ctx context.Context) error {
repos, err := gh.NewRepoFinder(f.gh).Find(ctx, gh.RepoFilter{
Owner: f.config.owner,
Repo: f.config.repo,
RepoRegexp: f.config.repoRegexp,
Archived: f.config.archived,
NoPrivate: f.config.noPrivate,
NoPublic: f.config.noPublic,
NoFork: f.config.noFork,
NoRepoRegexp: f.config.noRepoRegexp,
})
if err != nil {
return err
}
var (
branch, entryPath, basename string
level, matched, repoMatched int
repo, prevRepo *github.Repository
)
nextRepo:
for _, repo = range repos {
if prevRepo != nil && f.config.noMatches && repoMatched == 0 {
fmt.Fprintln(f.stdout, prevRepo.GetFullName())
}
prevRepo = repo
repoMatched = 0 // Reset per repository counter.
// Check the number of overall matched entries.
if f.config.maxResults > 0 && matched >= f.config.maxResults {
return nil
}
branch = f.config.branch
if branch == "" {
branch = repo.GetDefaultBranch()
}
tree, resp, err := f.gh.Git.GetTree(ctx, f.config.owner, repo.GetName(), branch, true)
if err != nil {
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusConflict {
// http.StatusConflict - Git Repository is empty.
continue
}
return err
}
if tree.GetTruncated() {
fmt.Fprintf(f.stderr, "WARNING: results were truncated for %s", repo.GetFullName())
}
nextEntry:
for _, entry := range tree.Entries {
// Check the number of overall matched entries.
if f.config.maxResults > 0 && matched >= f.config.maxResults {
return nil
}
// Check the number of per repository matched entries.
if f.config.maxRepoResults > 0 && repoMatched >= f.config.maxRepoResults {
continue nextRepo
}
entryPath = entry.GetPath()
level = levels(entryPath)
if f.config.minDepth > 0 && level < f.config.minDepth {
continue
}
if f.config.maxDepth > 0 && level > f.config.maxDepth {
continue
}
switch f.config.ftype {
case typeFile:
if entry.GetType() != "blob" {
continue
}
case typeDir:
if entry.GetType() != "tree" {
continue
}
}
// Check size.
if f.config.size != nil && !f.config.size.match(int64(entry.GetSize())) {
continue nextEntry
}
// Check for path rejects first.
if len(f.config.noPathRegexp) > 0 && matchAny(entryPath, f.config.noPathRegexp) {
continue nextEntry
}
// Then check for path matches.
if len(f.config.pathRegexp) > 0 && !matchAny(entryPath, f.config.pathRegexp) {
continue nextEntry
}
_, basename = path.Split(entryPath)
// Then check for name rejects.
if len(f.config.noNameRegexp) > 0 && matchAny(basename, f.config.noNameRegexp) {
continue nextEntry
}
// And finally check for name matches.
if len(f.config.nameRegexp) > 0 && !matchAny(basename, f.config.nameRegexp) {
continue nextEntry
}
// Check if we need to reject based on the contents of the file.
if f.config.noGrepRegexp != nil && entry.GetType() == "blob" {
results, err := f.grepContents(ctx, repo, branch, entry, 1)
if err != nil {
return err
}
if len(results.matches) > 0 {
continue nextEntry
}
}
if f.config.grepRegexp != nil && entry.GetType() == "blob" {
results, err := f.grepContents(ctx, repo, branch, entry, f.config.maxGrepResults)
if err != nil {
return err
}
if len(results.matches) > 0 {
matched++
repoMatched++
}
if !f.config.noMatches {
for _, match := range results.matches {
fmt.Fprintln(f.stdout, repo.GetFullName(), entry.GetPath(), match.lineno, match.line)
}
}
continue nextEntry
}
matched++
repoMatched++
if !f.config.noMatches {
if !f.config.listDetails {
fmt.Fprintln(f.stdout, repo.GetFullName(), entry.GetPath())
continue nextEntry
}
commit, err := f.getLastCommit(ctx, repo, branch, entry)
if err != nil {
return err
}
fmt.Fprintln(f.stdout, repo.GetFullName(), entryType(entry),
commit.Author.GetLogin(), entry.GetSize(),
commit.Commit.Author.GetDate().Format("Jan 2 15:04:05 2006"),
entry.GetPath(),
)
}
}
}
if prevRepo != nil && f.config.noMatches && repoMatched == 0 {
fmt.Fprintln(f.stdout, prevRepo.GetFullName())
}
return nil
}
func entryType(e *github.TreeEntry) string {
if e == nil {
return ""
}
switch e.GetType() {
case "tree":
return "d"
case "blob":
return "f"
default:
return ""
}
}
func (f *finder) getLastCommit(ctx context.Context, repo *github.Repository, branch string, entry *github.TreeEntry) (*github.RepositoryCommit, error) {
opts := &github.CommitsListOptions{
SHA: branch,
Path: entry.GetPath(),
ListOptions: github.ListOptions{
Page: 1,
PerPage: 1,
},
}
commits, resp, err := f.gh.Repositories.ListCommits(ctx, f.config.owner, repo.GetName(), opts)
if err != nil {
return nil, err
}
_ = resp
if len(commits) == 0 {
return nil, nil
}
return commits[0], nil
}
func (f *finder) grepContents(ctx context.Context, repo *github.Repository, branch string, entry *github.TreeEntry, limit int) (*grepResults, error) {
if f.config.grepRegexp == nil {
return nil, nil // There is nothing to do.
}
opts := &github.RepositoryContentGetOptions{Ref: branch}
contents, err := f.gh.Repositories.DownloadContents(ctx, f.config.owner, repo.GetName(), entry.GetPath(), opts)
if err != nil {
return nil, err
}
defer contents.Close()
return grep(contents, f.config.grepRegexp, limit)
}
func levels(path string) int {
return len(path) - len(strings.ReplaceAll(path, "/", "")) + 1
}
func matchAny(s string, regexes []*regexp.Regexp) bool {
for _, regex := range regexes {
if regex.MatchString(s) {
return true
}
}
return false
}