mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:21:50 +00:00
This PR consolidates and simplifies the client code a lot, in anticipation of moving more of the logic to the server-side. This allows us to have a better overview of what is actually used, and keeps the client interface as simple as possible. Most notable changes: - `Head` has been removed in favor of `GetDefaultBranch`. Currently, this is 1 gRPC -> 2 gRPC but with specialized endpoints, we should get it down to 1 again. They were otherwise the same, and I even noticed there's a slight bug in the `Head` implementation that didn't really handle the "empty repo" case. - `GetCommit` no longer exposes the option to also ensure the revision exists. 99% of call sites disabled this feature and I opted for an explicit call to EnsureRevision instead. This will keep the endpoint performant and not cause latency spikes. - `ListBranches` no longer takes options. They were unused. - The `BatchLog` endpoint has been deprecated. - `CommitsExist` has been removed, it's been the only method that makes use of the batch log method and accounts for probably 1500 of the deleted lines in this PR. We're now making individual requests instead. *We should monitor this, it doesn't seem like we're making crazy request counts on dotcom, our largest instance. And we have much worse offenders, like the indexserver endpoint that runs rev-parse for 3M repos.*. I do believe that with the new API, we're able to do better generic batching implementations though that aren't this specific, that also make use of gRPC streaming instead of creating very large responses and latency. - `CommitDate` has been removed in favor of `GetCommit` which returns the date as well, one less endpoint. - `GetCommits` has been unexported from the client API, it was only used internally. - `Addrs` has been unexported. - The unused properties `RefGlob` and `ExcludeRefGlob` have been removed from `RevisionSpecifier`, they were unused.
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package codenav
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/sourcegraph/sourcegraph/internal/codeintel/codenav/shared"
|
|
uploadsshared "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads/shared"
|
|
"github.com/sourcegraph/sourcegraph/lib/codeintel/precise"
|
|
)
|
|
|
|
func monikersToString(vs []precise.QualifiedMonikerData) string {
|
|
strs := make([]string, 0, len(vs))
|
|
for _, v := range vs {
|
|
strs = append(strs, fmt.Sprintf("%s:%s:%s:%s:%s", v.Kind, v.Scheme, v.Manager, v.Identifier, v.Version))
|
|
}
|
|
|
|
return strings.Join(strs, ", ")
|
|
}
|
|
|
|
func sliceContains(slice []string, str string) bool {
|
|
for _, el := range slice {
|
|
if el == str {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func uploadIDsToString(vs []uploadsshared.Dump) string {
|
|
ids := make([]string, 0, len(vs))
|
|
for _, v := range vs {
|
|
ids = append(ids, strconv.Itoa(v.ID))
|
|
}
|
|
|
|
return strings.Join(ids, ", ")
|
|
}
|
|
|
|
func sortRanges(ranges []shared.Range) []shared.Range {
|
|
sort.Slice(ranges, func(i, j int) bool {
|
|
iStart := ranges[i].Start
|
|
jStart := ranges[j].Start
|
|
|
|
if iStart.Line < jStart.Line {
|
|
// iStart comes first
|
|
return true
|
|
} else if iStart.Line > jStart.Line {
|
|
// jStart comes first
|
|
return false
|
|
}
|
|
// otherwise, starts on same line
|
|
|
|
if iStart.Character < jStart.Character {
|
|
// iStart comes first
|
|
return true
|
|
} else if iStart.Character > jStart.Character {
|
|
// jStart comes first
|
|
return false
|
|
}
|
|
// otherwise, starts at same character
|
|
|
|
iEnd := ranges[i].End
|
|
jEnd := ranges[j].End
|
|
|
|
if jEnd.Line < iEnd.Line {
|
|
// ranges[i] encloses ranges[j] (we want smaller first)
|
|
return false
|
|
} else if jStart.Line < jEnd.Line {
|
|
// ranges[j] encloses ranges[i] (we want smaller first)
|
|
return true
|
|
}
|
|
// otherwise, ends on same line
|
|
|
|
if jStart.Character < jEnd.Character {
|
|
// ranges[j] encloses ranges[i] (we want smaller first)
|
|
return true
|
|
}
|
|
|
|
return false
|
|
})
|
|
|
|
return ranges
|
|
}
|
|
|
|
func dedupeRanges(ranges []shared.Range) []shared.Range {
|
|
if len(ranges) == 0 {
|
|
return ranges
|
|
}
|
|
|
|
dedup := ranges[:1]
|
|
for _, s := range ranges[1:] {
|
|
if s != dedup[len(dedup)-1] {
|
|
dedup = append(dedup, s)
|
|
}
|
|
}
|
|
return dedup
|
|
}
|
|
|
|
type linemap struct {
|
|
positions []int
|
|
}
|
|
|
|
func newLinemap(source string) linemap {
|
|
// first line starts at offset 0
|
|
l := linemap{positions: []int{0}}
|
|
for i, char := range source {
|
|
if char == '\n' {
|
|
l.positions = append(l.positions, i+1)
|
|
}
|
|
}
|
|
// as we want the offset of the line _following_ a symbol's line,
|
|
// we need to add one extra here for when symbols exist on the final line
|
|
lastNewline := l.positions[len(l.positions)-1]
|
|
lenToEnd := len(source[lastNewline:])
|
|
l.positions = append(l.positions, lastNewline+lenToEnd+1)
|
|
return l
|
|
}
|