Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "edit file" and "browse repo" links #883

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ type SearchResult struct {
// RepoURLs holds a repo => template string map.
RepoURLs map[string]string

// RepoEditURLs holds a repo => template string map, for editing.
RepoEditURLs map[string]string

// RepoBrowseURLs holds a repo => string map, for browsing the repo.
RepoBrowseURLs map[string]string

// FragmentNames holds a repo => template string map, for
// the line number fragment.
LineFragments map[string]string
Expand All @@ -530,6 +536,20 @@ func (sr *SearchResult) SizeBytes() (sz uint64) {
sz += stringHeaderBytes + uint64(len(v))
}

// RepoEditURLs
sz += mapHeaderBytes
for k, v := range sr.RepoEditURLs {
sz += stringHeaderBytes + uint64(len(k))
sz += stringHeaderBytes + uint64(len(v))
}

// RepoBrowseURLs
sz += mapHeaderBytes
for k, v := range sr.RepoBrowseURLs {
sz += stringHeaderBytes + uint64(len(k))
sz += stringHeaderBytes + uint64(len(v))
}

// LineFragments
sz += mapHeaderBytes
for k, v := range sr.LineFragments {
Expand Down Expand Up @@ -584,6 +604,10 @@ type Repository struct {
// {{.Version}}, {{.Path}}
FileURLTemplate string

// The repository URL for editing a file. Has access to
// {{.Branch}}, {{.Path}}
FileEditURLTemplate string

// The URL fragment to add to a file URL for line numbers. has
// access to {{.LineNumber}}. The fragment should include the
// separator, generally '#' or ';'.
Expand Down Expand Up @@ -734,6 +758,10 @@ func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
mutated = true
r.FileURLTemplate = x.FileURLTemplate
}
if r.FileEditURLTemplate != x.FileEditURLTemplate {
mutated = true
r.FileEditURLTemplate = x.FileEditURLTemplate
}
if r.LineFragmentTemplate != x.LineFragmentTemplate {
mutated = true
r.LineFragmentTemplate = x.LineFragmentTemplate
Expand Down
14 changes: 9 additions & 5 deletions api_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,15 @@ func (p *Progress) ToProto() *proto.Progress {
}
}

func SearchResultFromStreamProto(p *proto.StreamSearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
func SearchResultFromStreamProto(p *proto.StreamSearchResponse, repoURLs, repoEditURLs, repoBrowseURLs, lineFragments map[string]string) *SearchResult {
if p == nil {
return nil
}

return SearchResultFromProto(p.GetResponseChunk(), repoURLs, lineFragments)
return SearchResultFromProto(p.GetResponseChunk(), repoURLs, repoEditURLs, repoBrowseURLs, lineFragments)
}

func SearchResultFromProto(p *proto.SearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
func SearchResultFromProto(p *proto.SearchResponse, repoURLs, repoEditURLs, repoBrowseURLs, lineFragments map[string]string) *SearchResult {
if p == nil {
return nil
}
Expand All @@ -370,8 +370,10 @@ func SearchResultFromProto(p *proto.SearchResponse, repoURLs, lineFragments map[

Files: files,

RepoURLs: repoURLs,
LineFragments: lineFragments,
RepoURLs: repoURLs,
RepoEditURLs: repoEditURLs,
RepoBrowseURLs: repoBrowseURLs,
LineFragments: lineFragments,
}
}

Expand Down Expand Up @@ -442,6 +444,7 @@ func RepositoryFromProto(p *proto.Repository) Repository {
SubRepoMap: subRepoMap,
CommitURLTemplate: p.GetCommitUrlTemplate(),
FileURLTemplate: p.GetFileUrlTemplate(),
FileEditURLTemplate: p.GetFileEditUrlTemplate(),
LineFragmentTemplate: p.GetLineFragmentTemplate(),
priority: p.GetPriority(),
RawConfig: p.GetRawConfig(),
Expand Down Expand Up @@ -484,6 +487,7 @@ func (r *Repository) ToProto() *proto.Repository {
SubRepoMap: subRepoMap,
CommitUrlTemplate: r.CommitURLTemplate,
FileUrlTemplate: r.FileURLTemplate,
FileEditUrlTemplate: r.FileEditURLTemplate,
LineFragmentTemplate: r.LineFragmentTemplate,
Priority: r.priority,
RawConfig: r.RawConfig,
Expand Down
16 changes: 13 additions & 3 deletions api_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,19 @@ func TestProtoRoundtrip(t *testing.T) {
t.Run("unary", func(t *testing.T) {
f := func(f1 *SearchResult) bool {
var repoURLs map[string]string
var repoEditURLs map[string]string
var repoBrowseURLs map[string]string
var lineFragments map[string]string

if f1 != nil {
repoURLs = f1.RepoURLs
repoEditURLs = f1.RepoEditURLs
repoBrowseURLs = f1.RepoBrowseURLs
lineFragments = f1.LineFragments
}

p1 := f1.ToProto()
f2 := SearchResultFromProto(p1, repoURLs, lineFragments)
f2 := SearchResultFromProto(p1, repoURLs, repoEditURLs, repoBrowseURLs, lineFragments)

return reflect.DeepEqual(f1, f2)
}
Expand All @@ -157,15 +161,19 @@ func TestProtoRoundtrip(t *testing.T) {
t.Run("stream", func(t *testing.T) {
f := func(f1 *SearchResult) bool {
var repoURLs map[string]string
var repoEditURLs map[string]string
var repoBrowseURLs map[string]string
var lineFragments map[string]string

if f1 != nil {
repoURLs = f1.RepoURLs
repoEditURLs = f1.RepoEditURLs
repoBrowseURLs = f1.RepoBrowseURLs
lineFragments = f1.LineFragments
}

p1 := f1.ToStreamProto()
f2 := SearchResultFromStreamProto(p1, repoURLs, lineFragments)
f2 := SearchResultFromStreamProto(p1, repoURLs, repoEditURLs, repoBrowseURLs, lineFragments)

return reflect.DeepEqual(f1, f2)
}
Expand Down Expand Up @@ -242,6 +250,7 @@ func TestProtoRoundtrip(t *testing.T) {
},
CommitURLTemplate: "committemplate",
FileURLTemplate: "fileurltemplate",
FileEditURLTemplate: "fileedittemplate",
LineFragmentTemplate: "linefragmenttemplate",
priority: 10,
RawConfig: map[string]string{
Expand Down Expand Up @@ -377,6 +386,7 @@ func (*Repository) Generate(rng *rand.Rand, _ int) reflect.Value {
SubRepoMap: map[string]*Repository{},
CommitURLTemplate: gen(r.CommitURLTemplate, rng),
FileURLTemplate: gen(r.FileURLTemplate, rng),
FileEditURLTemplate: gen(r.FileEditURLTemplate, rng),
LineFragmentTemplate: gen(r.LineFragmentTemplate, rng),
priority: gen(r.priority, rng),
RawConfig: gen(r.RawConfig, rng),
Expand Down Expand Up @@ -423,7 +433,7 @@ var (
}()

// The non-proto struct representation of the search result
exampleSearchResultGo = SearchResultFromProto(exampleSearchResultProto, nil, nil)
exampleSearchResultGo = SearchResultFromProto(exampleSearchResultProto, nil, nil, nil, nil)
)

func BenchmarkGobRoundtrip(b *testing.B) {
Expand Down
8 changes: 5 additions & 3 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ func TestSizeBytesSearchResult(t *testing.T) {
SubRepositoryPath: "", // 16 bytes
Version: "", // 16 bytes
}},
RepoURLs: nil, // 48 bytes
LineFragments: nil, // 48 bytes
RepoURLs: nil, // 48 bytes
RepoEditURLs: nil, // 48 bytes
RepoBrowseURLs: nil, // 48 bytes
LineFragments: nil, // 48 bytes
}

var wantBytes uint64 = 725
var wantBytes uint64 = 821
if sr.SizeBytes() != wantBytes {
t.Fatalf("want %d, got %d", wantBytes, sr.SizeBytes())
}
Expand Down
10 changes: 10 additions & 0 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ func addRepo(res *SearchResult, repo *Repository) {
}
res.RepoURLs[repo.Name] = repo.FileURLTemplate

if res.RepoEditURLs == nil {
res.RepoEditURLs = map[string]string{}
}
res.RepoEditURLs[repo.Name] = repo.FileEditURLTemplate

if res.RepoBrowseURLs == nil {
res.RepoBrowseURLs = map[string]string{}
}
res.RepoBrowseURLs[repo.Name] = repo.URL

if res.LineFragments == nil {
res.LineFragments = map[string]string{}
}
Expand Down
7 changes: 6 additions & 1 deletion gitindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
// helper to generate u.JoinPath as a template
varVersion := ".Version"
varPath := ".Path"
varBranch := ".Branch"
urlJoinPath := func(elem ...string) string {
elem = append([]string{u.String()}, elem...)
var parts []string
for _, e := range elem {
if e == varVersion || e == varPath {
if e == varVersion || e == varPath || e == varBranch {
parts = append(parts, e)
} else {
parts = append(parts, strconv.Quote(e))
Expand All @@ -120,6 +121,7 @@ func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
// eg. https://github.com/hanwen/go-fuse/blob/notify/genversion.sh#L10
repo.CommitURLTemplate = urlJoinPath("commit", varVersion)
repo.FileURLTemplate = urlJoinPath("blob", varVersion, varPath)
repo.FileEditURLTemplate = urlJoinPath("edit", varBranch, varPath)
repo.LineFragmentTemplate = "#L{{.LineNumber}}"
case "cgit":
// http://git.savannah.gnu.org/cgit/lilypond.git/tree/elisp/lilypond-mode.el?h=dev/philh&id=b2ca0fefe3018477aaca23b6f672c7199ba5238e#n100
Expand All @@ -142,12 +144,14 @@ func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
// https://<bitbucketserver-host>/projects/<project>/repos/<repo>/browse/<file>?at=5be7ca73b898bf17a08e607918accfdeafe1e0bc
repo.CommitURLTemplate = urlJoinPath("commits", varVersion)
repo.FileURLTemplate = urlJoinPath(varPath) + "?at={{.Version}}"
repo.FileEditURLTemplate = urlJoinPath(varPath) + "?at={{.Branch}}&mode=edit"
repo.LineFragmentTemplate = "#{{.LineNumber}}"
case "gitlab":
// https://gitlab.com/gitlab-org/omnibus-gitlab/-/commit/b152c864303dae0e55377a1e2c53c9592380ffed
// https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/aad04155b3f6fc50ede88aedaee7fc624d481149/files/gitlab-config-template/gitlab.rb.template
repo.CommitURLTemplate = urlJoinPath("-/commit", varVersion)
repo.FileURLTemplate = urlJoinPath("-/blob", varVersion, varPath)
repo.FileEditURLTemplate = urlJoinPath("-/edit", varBranch, varPath)
repo.LineFragmentTemplate = "#L{{.LineNumber}}"
case "gitea":
repo.CommitURLTemplate = urlJoinPath("commit", varVersion)
Expand All @@ -157,6 +161,7 @@ func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
// When /src/{{.Version}} is used it will redirect to /src/commit/{{.Version}},
// but the query parameters are obmitted.
repo.FileURLTemplate = urlJoinPath("src/commit", varVersion, varPath) + "?display=source"
repo.FileEditURLTemplate = urlJoinPath("_edit", varBranch, varPath)
repo.LineFragmentTemplate = "#L{{.LineNumber}}"
default:
return fmt.Errorf("URL scheme type %q unknown", typ)
Expand Down
Loading
Loading