forked from sourcegraph/srclib
/
api_cmds.go
692 lines (613 loc) · 17.6 KB
/
api_cmds.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package src
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/kr/fs"
"sourcegraph.com/sourcegraph/go-sourcegraph/sourcegraph"
"sourcegraph.com/sourcegraph/rwvfs"
"sourcegraph.com/sourcegraph/srclib/buildstore"
"sourcegraph.com/sourcegraph/srclib/config"
"sourcegraph.com/sourcegraph/srclib/dep"
"sourcegraph.com/sourcegraph/srclib/graph"
"sourcegraph.com/sourcegraph/srclib/plan"
"sourcegraph.com/sourcegraph/srclib/unit"
)
func init() {
c, err := CLI.AddCommand("api",
"API",
"",
&apiCmd,
)
if err != nil {
log.Fatal(err)
}
/* START APIDescribeCmdDoc OMIT
This command is used by editor plugins to retrieve information about
the identifier at a specific position in a file.
It will hit Sourcegraph's API to get a definition's examples. With the
flag `--no-examples`, this command does not hit Sourcegraph's API.
END APIDescribeCmdDoc OMIT */
_, err = c.AddCommand("describe",
"display documentation for the def under the cursor",
"Returns information about the definition referred to by the cursor's current position in a file.",
&apiDescribeCmd,
)
if err != nil {
log.Fatal(err)
}
/* START APIListCmdDoc OMIT
This command will return a list of all the definitions,
references, and docs in a file. It can be used for finding all
uses of a reference in a file.
END APIListCmdDoc OMIT */
_, err = c.AddCommand("list",
"list all defs, refs, and docs in a given file",
"Return a list of all definitions, references, and docs that are in the current file.",
&apiListCmd,
)
if err != nil {
log.Fatal(err)
}
/* START APIDepsCmdDoc OMIT
This command returns a list of all resolved and unresolved
dependencies for the current repository.
END APIDepsCmdDoc OMIT */
_, err = c.AddCommand("deps",
"list all resolved and unresolved dependencies",
`Return a list of all resolved and unresolved dependencies that are in the current repository.`,
&apiDepsCmd,
)
if err != nil {
log.Fatal(err)
}
/* START APIUnitsCmdDoc OMIT
This command returns a list of all of the source units in the current
repository.
END APIUnitsCmdDoc OMIT */
_, err = c.AddCommand("units",
"list all source unit information",
"Return a list of all source units that are in the current repository.",
&apiUnitsCmd,
)
if err != nil {
log.Fatal(err)
}
}
type APICmd struct{}
var apiCmd APICmd
func (c *APICmd) Execute(args []string) error { return nil }
type APIDescribeCmd struct {
File string `long:"file" required:"yes" value-name:"FILE"`
StartByte uint32 `long:"start-byte" required:"yes" value-name:"BYTE"`
NoExamples bool `long:"no-examples" describe:"don't show examples from Sourcegraph.com"`
}
type APIListCmd struct {
File string `long:"file" required:"yes" value-name:"FILE"`
NoRefs bool `long:"no-refs"`
NoDefs bool `long:"no-defs"`
NoDocs bool `long:"no-docs"`
}
type APIDepsCmd struct {
Args struct {
Dir Directory `name:"DIR" default:"." description:"root directory of target project"`
} `positional-args:"yes"`
}
type APIUnitsCmd struct {
Args struct {
Dir Directory `name:"DIR" default:"." description:"root directory of target project"`
} `positional-args:"yes"`
}
var apiDescribeCmd APIDescribeCmd
var apiListCmd APIListCmd
var apiDepsCmd APIDepsCmd
var apiUnitsCmd APIUnitsCmd
type commandContext struct {
repo *Repo
relativeFile string
buildStore buildstore.RepoBuildStore
commitFS rwvfs.WalkableFileSystem
}
// prepareCommandContext prepare the context for the the "src"
// command. If file is "." or ends in a "/", then it is a directory. It is safe
// for "file" to have multiple trailing slashes. prepareCommandContext
// creates commandContext, changes the process' working directory to
// file's directory and ensures that a build has been made. It is
// meant to be used by user-facing commands.
func prepareCommandContext(file string) (commandContext, error) {
var (
err error
c commandContext
isDir bool
)
if file == "" {
return commandContext{}, errors.New("prepareCommandContext: file cannot be empty")
}
if file == "." || file[len(file)-1] == os.PathSeparator {
isDir = true
}
file, err = filepath.Abs(file)
if err != nil {
return commandContext{}, err
}
// filepath.Abs returns a cleaned file path, so we need to add
// the path separator to it to presrve filepath.Dir's
// semantics.
if isDir {
file += string(os.PathSeparator)
}
repo, err := OpenRepo(filepath.Dir(file))
if err != nil {
return commandContext{}, err
}
c.repo = repo
rel, err := filepath.Rel(repo.RootDir, file)
if err != nil {
return commandContext{}, err
}
c.relativeFile = rel
if err := os.Chdir(repo.RootDir); err != nil {
return commandContext{}, err
}
buildStore, err := buildstore.LocalRepo(repo.RootDir)
if err != nil {
return commandContext{}, err
}
c.buildStore = buildStore
c.commitFS = buildStore.Commit(repo.CommitID)
if err := ensureBuild(buildStore, repo); err != nil {
if err := buildstore.RemoveAllDataForCommit(buildStore, repo.CommitID); err != nil {
log.Println(err)
}
return commandContext{}, err
}
return c, nil
}
// ensureBuild invokes the build process on the given repository
func ensureBuild(buildStore buildstore.RepoBuildStore, repo *Repo) error {
configOpt := config.Options{
Repo: repo.URI(),
Subdir: ".",
}
toolchainExecOpt := ToolchainExecOpt{ExeMethods: "program"}
// Config repository if not yet built.
exists, err := buildstore.BuildDataExistsForCommit(buildStore, repo.CommitID)
if err != nil {
return err
}
if !exists {
configCmd := &ConfigCmd{
Options: configOpt,
ToolchainExecOpt: toolchainExecOpt,
Quiet: true,
}
if err := configCmd.Execute(nil); err != nil {
return err
}
}
// Always re-make.
//
// TODO(sqs): optimize this
makeCmd := &MakeCmd{
Options: configOpt,
ToolchainExecOpt: toolchainExecOpt,
Quiet: true,
}
if err := makeCmd.Execute(nil); err != nil {
return err
}
// Always re-import.
i := &StoreImportCmd{
ImportOpt: ImportOpt{
Repo: repo.CloneURL,
CommitID: repo.CommitID,
},
Quiet: true,
}
if err := i.Execute(nil); err != nil {
return err
}
return nil
}
func getSourceUnits(commitFS rwvfs.WalkableFileSystem, repo *Repo) []string {
var unitFiles []string
unitSuffix := buildstore.DataTypeSuffix(unit.SourceUnit{})
w := fs.WalkFS(".", commitFS)
for w.Step() {
if strings.HasSuffix(w.Path(), unitSuffix) {
unitFiles = append(unitFiles, w.Path())
}
}
return unitFiles
}
// getSourceUnitsWithFile gets a list of all source units that contain
// the given file.
func getSourceUnitsWithFile(buildStore buildstore.RepoBuildStore, repo *Repo, filename string) ([]*unit.SourceUnit, error) {
filename = filepath.Clean(filename)
// TODO(sqs): This whole lookup is totally inefficient. The storage format
// is not optimized for lookups.
commitFS := buildStore.Commit(repo.CommitID)
unitFiles := getSourceUnits(commitFS, repo)
// Find all source unit definition files.
unitSuffix := buildstore.DataTypeSuffix(unit.SourceUnit{})
w := fs.WalkFS(".", commitFS)
for w.Step() {
if strings.HasSuffix(w.Path(), unitSuffix) {
unitFiles = append(unitFiles, w.Path())
}
}
// Find which source units the file belongs to.
var units []*unit.SourceUnit
for _, unitFile := range unitFiles {
var u *unit.SourceUnit
f, err := commitFS.Open(unitFile)
if err != nil {
return nil, err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&u); err != nil {
return nil, fmt.Errorf("%s: %s", unitFile, err)
}
for _, f2 := range u.Files {
if filepath.Clean(f2) == filename {
units = append(units, u)
break
}
}
}
return units, nil
}
// START APIListCmdOutput OMIT
type apiListCmdOutput struct {
Defs []*graph.Def `json:",omitempty"`
Refs []*graph.Ref `json:",omitempty"`
Docs []*graph.Doc `json:",omitempty"`
}
// END APIListCmdOutput OMIT
func (c *APIListCmd) Execute(args []string) error {
context, err := prepareCommandContext(c.File)
if err != nil {
return err
}
file := context.relativeFile
units, err := getSourceUnitsWithFile(context.buildStore, context.repo, file)
if err != nil {
return err
}
if GlobalOpt.Verbose {
if len(units) > 0 {
ids := make([]string, len(units))
for i, u := range units {
ids[i] = string(u.ID())
}
log.Printf("File %s is in %d source units %v.", file, len(units), ids)
} else {
log.Printf("File %s is not in any source units.", file)
}
}
// Grab all the data for the file.
var output apiListCmdOutput
for _, u := range units {
var g graph.Output
graphFile := plan.SourceUnitDataFilename("graph", u)
f, err := context.commitFS.Open(graphFile)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&g); err != nil {
return fmt.Errorf("%s: %s", graphFile, err)
}
if !c.NoRefs {
for _, ref := range g.Refs {
if file == ref.File {
output.Refs = append(output.Refs, ref)
}
}
}
if !c.NoDefs {
for _, def := range g.Defs {
if file == def.File {
output.Defs = append(output.Defs, def)
}
}
}
if !c.NoDocs {
for _, doc := range g.Docs {
if file == doc.File {
output.Docs = append(output.Docs, doc)
}
}
}
}
if err := json.NewEncoder(os.Stdout).Encode(output); err != nil {
return err
}
return nil
}
/* START APIDescribeCmdOutput OMIT
The output is defined in
[api_cmds.go](https://github.com/sourcegraph/srclib/blob/e5295dfcd719535ff9cbb37a2771337d44fe5953/src/api_cmds.go#L190-L193),
as the JSON representation of the following struct.
The Def and Example structs are defined as follows in the Sourcegraph API.
[[.code "src/api_cmds.go" "APIDescribeCmdOutputQuickHack"]]
[[.code "https://raw.githubusercontent.com/sourcegraph/go-sourcegraph/6937daba84bf2d0f919191fd74e5193171b4f5d5/sourcegraph/defs.go" 105 113]]
[[.code "graph/def.pb.go" "Def "]]
[[.code "https://raw.githubusercontent.com/sourcegraph/go-sourcegraph/6937daba84bf2d0f919191fd74e5193171b4f5d5/sourcegraph/defs.go" 236 252]]
[[.code "graph/ref.pb.go" "Ref"]]
END APIDescribeCmdOutput OMIT */
// START APIDescribeCmdOutputQuickHack OMIT
type apiDescribeCmdOutput struct {
Def *sourcegraph.Def
Examples []*sourcegraph.Example
}
// END APIDescribeCmdOutputQuickHack OMIT
func (c *APIDescribeCmd) Execute(args []string) error {
context, err := prepareCommandContext(c.File)
if err != nil {
return err
}
file := context.relativeFile
units, err := getSourceUnitsWithFile(context.buildStore, context.repo, file)
if err != nil {
return err
}
if GlobalOpt.Verbose {
if len(units) > 0 {
ids := make([]string, len(units))
for i, u := range units {
ids[i] = string(u.ID())
}
log.Printf("Position %s:%d is in %d source units %v.", file, c.StartByte, len(units), ids)
} else {
log.Printf("Position %s:%d is not in any source units.", file, c.StartByte)
}
}
// Find the ref(s) at the character position.
var ref *graph.Ref
var nearbyRefs []*graph.Ref // Find nearby refs to help with debugging.
OuterLoop:
for _, u := range units {
var g graph.Output
graphFile := plan.SourceUnitDataFilename("graph", u)
f, err := context.commitFS.Open(graphFile)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&g); err != nil {
return fmt.Errorf("%s: %s", graphFile, err)
}
for _, ref2 := range g.Refs {
if file == ref2.File {
if c.StartByte >= ref2.Start && c.StartByte <= ref2.End {
ref = ref2
if ref.DefUnit == "" {
ref.DefUnit = u.Name
}
if ref.DefUnitType == "" {
ref.DefUnitType = u.Type
}
break OuterLoop
} else if GlobalOpt.Verbose && abs(int(ref2.Start)-int(c.StartByte)) < 25 {
nearbyRefs = append(nearbyRefs, ref2)
}
}
}
}
if ref == nil {
if GlobalOpt.Verbose {
log.Printf("No ref found at %s:%d.", file, c.StartByte)
if len(nearbyRefs) > 0 {
log.Printf("However, nearby refs were found in the same file:")
for _, nref := range nearbyRefs {
log.Printf("Ref at bytes %d-%d to %v", nref.Start, nref.End, nref.DefKey())
}
}
f, err := os.Open(file)
if err == nil {
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("Error reading source file: %s.", err)
}
start := c.StartByte
if start < 0 || int(start) > len(b)-1 {
log.Fatalf("Start byte %d is out of file bounds.", c.StartByte)
}
end := c.StartByte + 50
if int(end) > len(b)-1 {
end = uint32(len(b) - 1)
}
log.Printf("Surrounding source is:\n\n%s", b[start:end])
} else {
log.Printf("Error opening source file to show surrounding source: %s.", err)
}
}
fmt.Println(`{}`)
return nil
}
// ref.DefRepo is *not* guaranteed to be non-empty, as
// repo.URI() will return the empty string if the repo's
// CloneURL is empty or malformed.
if ref.DefRepo == "" {
ref.DefRepo = context.repo.URI()
}
var resp apiDescribeCmdOutput
// Now find the def for this ref.
defInCurrentRepo := ref.DefRepo == context.repo.URI()
if defInCurrentRepo {
// Def is in the current repo.
var g graph.Output
graphFile := plan.SourceUnitDataFilename("graph", &unit.SourceUnit{Name: ref.DefUnit, Type: ref.DefUnitType})
f, err := context.commitFS.Open(graphFile)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&g); err != nil {
return fmt.Errorf("%s: %s", graphFile, err)
}
for _, def2 := range g.Defs {
if def2.Path == ref.DefPath {
resp.Def = &sourcegraph.Def{Def: *def2}
break
}
}
if resp.Def != nil {
for _, doc := range g.Docs {
if doc.Path == ref.DefPath {
resp.Def.DocHTML = doc.Data
}
}
// If Def is in the current Repo, transform that path to be an absolute path
resp.Def.File = filepath.Join(context.repo.RootDir, resp.Def.File)
}
if resp.Def == nil && GlobalOpt.Verbose {
log.Printf("No definition found with path %q in unit %q type %q.", ref.DefPath, ref.DefUnit, ref.DefUnitType)
}
}
// spec is only valid for remote requests if ref.DefRepo is
// non-empty.
var spec sourcegraph.DefSpec
var specValid bool
if ref.DefRepo != "" {
specValid = true
spec = sourcegraph.DefSpec{
Repo: string(ref.DefRepo),
UnitType: ref.DefUnitType,
Unit: ref.DefUnit,
Path: string(ref.DefPath),
}
}
if specValid {
apiclient := NewAPIClientWithAuthIfPresent()
var wg sync.WaitGroup
if resp.Def == nil {
// Def is not in the current repo. Try looking it up using the
// Sourcegraph API.
wg.Add(1)
go func() {
defer wg.Done()
var err error
resp.Def, _, err = apiclient.Defs.Get(spec, &sourcegraph.DefGetOptions{Doc: true})
if err != nil && GlobalOpt.Verbose {
log.Printf("Couldn't fetch definition %v: %s.", spec, err)
}
}()
}
if !c.NoExamples {
wg.Add(1)
go func() {
defer wg.Done()
var err error
resp.Examples, _, err = apiclient.Defs.ListExamples(spec, &sourcegraph.DefListExamplesOptions{
Formatted: true,
ListOptions: sourcegraph.ListOptions{PerPage: 4},
})
if err != nil && GlobalOpt.Verbose {
log.Printf("Couldn't fetch examples for %v: %s.", spec, err)
}
}()
}
wg.Wait()
}
if err := json.NewEncoder(os.Stdout).Encode(resp); err != nil {
return err
}
return nil
}
func abs(n int) int {
if n < 0 {
return -1 * n
}
return n
}
/* START APIDepsCmdOutput OMIT
This command returns a dep.Resolution slice.
[[.code "dep/resolve.go" "Resolution"]]
END APIDepsCmdOutput OMIT */
func (c *APIDepsCmd) Execute(args []string) error {
// HACK(samertm): append a backslash to Dir to assure that it's parsed
// as a directory, but Directory should have an unmarshalling
// method that does this.
context, err := prepareCommandContext(c.Args.Dir.String())
if err != nil {
return err
}
var depSlice []*dep.Resolution
// TODO: Make DataTypeSuffix work with type of depSlice
depSuffix := buildstore.DataTypeSuffix([]*dep.ResolvedDep{})
depCache := make(map[string]struct{})
foundDepresolve := false
w := fs.WalkFS(".", context.commitFS)
for w.Step() {
depfile := w.Path()
if strings.HasSuffix(depfile, depSuffix) {
foundDepresolve = true
var deps []*dep.Resolution
f, err := context.commitFS.Open(depfile)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&deps); err != nil {
return fmt.Errorf("%s: %s", depfile, err)
}
for _, d := range deps {
key, err := d.RawKeyId()
if err != nil {
return err
}
if _, ok := depCache[key]; !ok {
depCache[key] = struct{}{}
depSlice = append(depSlice, d)
}
}
}
}
if foundDepresolve == false {
return errors.New("No dependency information found. Try running `src config` first.")
}
return json.NewEncoder(os.Stdout).Encode(depSlice)
}
/* START APIUnitsCmdOutput OMIT
This command returns a unit.SourceUnit slice.
[[.code "unit/source_unit.go" "SourceUnit"]]
END APIUnitsCmdOutput OMIT */
func (c *APIUnitsCmd) Execute(args []string) error {
context, err := prepareCommandContext(c.Args.Dir.String())
if err != nil {
return err
}
var unitSlice []unit.SourceUnit
unitSuffix := buildstore.DataTypeSuffix(unit.SourceUnit{})
foundUnit := false
w := fs.WalkFS(".", context.commitFS)
for w.Step() {
unitFile := w.Path()
if strings.HasSuffix(unitFile, unitSuffix) {
var unit unit.SourceUnit
foundUnit = true
f, err := context.commitFS.Open(unitFile)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&unit); err != nil {
return fmt.Errorf("%s: %s", unitFile, err)
}
unitSlice = append(unitSlice, unit)
}
}
if foundUnit == false {
return errors.New("No source units found. Try running `src config` first.")
}
return json.NewEncoder(os.Stdout).Encode(unitSlice)
}