Skip to content

Commit

Permalink
Add more information to the repository list page.
Browse files Browse the repository at this point in the history
Now the repository list shows:

 * last indexing time
 * link to repository upstream
 * branches indexed (linked)

Change-Id: I9d076c1cc5e1978ddaf48e06a36cd7916d0c9b34
  • Loading branch information
hanwen committed Sep 14, 2016
1 parent 9d796b7 commit 4e11405
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 154 deletions.
25 changes: 24 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,31 @@ type SearchResult struct {
LineFragments map[string]string
}

// RepositoryBranch describes an indexed branch, which is a name
// combined with a version.
type RepositoryBranch struct {
Name string
Version string
}

// Repository holds repository metadata.
type Repository struct {
Name string
URL string

// The branches indexed in this repo.
Branches []RepositoryBranch

// Timestamp of indexing
IndexTime time.Time

// URL template to link to the commit of a branch
CommitURLTemplate string
}

// RepoList holds a set of Repository metadata.
type RepoList struct {
Repos []string
Repos []*Repository
}

type Searcher interface {
Expand Down
32 changes: 27 additions & 5 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import (

var DefaultDir = filepath.Join(os.Getenv("HOME"), ".zoekt")

// Branch describes a single branch version.
type Branch struct {
Name string
Version string
}

// Options sets options for the index building.
type Options struct {
// IndexDir is a directory that holds *.zoekt index files.
Expand All @@ -50,20 +56,29 @@ type Options struct {
// RepoName is name of the repository.
RepoName string

// RepoDir is the path to the repository
// RepoDir is the path to the repository on the indexing machine.
RepoDir string

// RepoURL is the URL template for the repository.
// RepoURL is an URL to the repository.
RepoURL string

// RepoLineFragment is the URL fragment template for the line number.
RepoLineFragment string
// CommitURLTemplate is the URL template for the commit.
CommitURLTemplate string

// FileURLTemplate is the URL template for the repository.
FileURLTemplate string

// LineFragmentTemplate is the URL fragment template for the line number.
LineFragmentTemplate string

// Path to exuberant ctags binary to run
CTags string

// Path to namespace-sandbox from Bazel
NamespaceSandbox string

// Branches sets the version IDs for each branch
Branches []Branch
}

// Builder manages (parallel) creation of uniformly sized shards.
Expand Down Expand Up @@ -233,7 +248,14 @@ func (b *Builder) buildShard(todo []*zoekt.Document, nextShardNum int) error {

shardBuilder := zoekt.NewIndexBuilder()
shardBuilder.SetName(b.opts.RepoName)
shardBuilder.SetRepoURL(b.opts.RepoURL, b.opts.RepoLineFragment)
shardBuilder.SetURLTemplates(
b.opts.RepoURL,
b.opts.CommitURLTemplate, b.opts.FileURLTemplate, b.opts.LineFragmentTemplate)
for _, b := range b.opts.Branches {
if err := shardBuilder.AddBranch(b.Name, b.Version); err != nil {
return err
}
}
for _, t := range todo {
shardBuilder.Add(*t)
}
Expand Down
27 changes: 13 additions & 14 deletions build/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestBasic(t *testing.T) {
ShardMax: 1024,
RepoName: "repo",
RepoDir: "/repo",
RepoURL: "url",
Parallelism: 2,
SizeMax: 1 << 20,
}
Expand Down Expand Up @@ -93,12 +92,12 @@ func TestUpdate(t *testing.T) {
}

opts := Options{
IndexDir: dir,
ShardMax: 1024,
RepoName: "repo",
RepoURL: "url",
Parallelism: 2,
SizeMax: 1 << 20,
IndexDir: dir,
ShardMax: 1024,
RepoName: "repo",
FileURLTemplate: "url",
Parallelism: 2,
SizeMax: 1 << 20,

RepoDir: "/a",
}
Expand Down Expand Up @@ -130,7 +129,7 @@ func TestUpdate(t *testing.T) {
}

opts.RepoName = "repo2"
opts.RepoURL = "url2"
opts.FileURLTemplate = "url2"
opts.RepoDir = "/b"
if b, err := NewBuilder(opts); err != nil {
t.Fatalf("NewBuilder: %v", err)
Expand Down Expand Up @@ -176,12 +175,12 @@ func TestDeleteOldShards(t *testing.T) {
}

opts := Options{
IndexDir: dir,
ShardMax: 1024,
RepoName: "repo",
RepoURL: "url",
RepoDir: "/a",
SizeMax: 1 << 20,
IndexDir: dir,
ShardMax: 1024,
RepoName: "repo",
FileURLTemplate: "url",
RepoDir: "/a",
SizeMax: 1 << 20,
}
opts.SetDefaults()

Expand Down
18 changes: 15 additions & 3 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@ nextFileMatch:
}
sortFilesByScore(res.Files)
res.RepoURLs = map[string]string{
d.unaryData.RepoName: d.unaryData.RepoURL,
d.unaryData.RepoName: d.unaryData.FileURLTemplate,
}
res.LineFragments = map[string]string{
d.unaryData.RepoName: d.unaryData.RepoLineFragment,
d.unaryData.RepoName: d.unaryData.RepoLineFragmentTemplate,
}
return &res, nil
}
Expand Down Expand Up @@ -769,7 +769,19 @@ func (d *indexData) List(ctx context.Context, q query.Q) (*RepoList, error) {

l := &RepoList{}
if c.Value {
l.Repos = append(l.Repos, d.unaryData.RepoName)
repo := &Repository{
Name: d.unaryData.RepoName,
IndexTime: d.unaryData.IndexTime,
URL: d.unaryData.RepoURL,
CommitURLTemplate: d.unaryData.CommitURLTemplate,
}
for i, n := range d.unaryData.BranchNames {
repo.Branches = append(repo.Branches, RepositoryBranch{
Name: n,
Version: d.unaryData.BranchVersions[i]})
}

l.Repos = append(l.Repos, repo)
}
return l, nil
}
75 changes: 47 additions & 28 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/google/zoekt"
"github.com/google/zoekt/build"
"github.com/libgit2/git2go"
)

// RepoModTime returns the time of last fetch of a git repository.
Expand Down Expand Up @@ -95,59 +94,69 @@ func FindGitRepos(arg string) (map[string]string, error) {
return gitDirs, nil
}

// GuessRepoURL guesses the URL template for a repo mirrored from a
type templates struct {
repo, commit, file, line string
}

// guessRepoURL guesses the URL template for a repo mirrored from a
// well-known git hosting site.
func GuessRepoURL(repoDir string) (string, string, error) {
func guessRepoURL(repoDir string) (*templates, error) {
base, err := git.NewConfig()
if err != nil {
return "", "", err
return nil, err
}
cfg, err := git.OpenOndisk(base, filepath.Join(repoDir, "config"))
if err != nil {
return "", "", err
return nil, err
}

remoteURL, err := cfg.LookupString("remote.origin.url")
if err != nil {
return "", "", err
return nil, err
}

parsed, err := url.Parse(remoteURL)
if err != nil {
return "", "", err
return nil, err
}

if strings.HasSuffix(parsed.Host, "googlesource.com") {
/// eg. https://gerrit.googlesource.com/gitiles/+/master/tools/run_dev.sh#20
return remoteURL + "/+/{{.Branch}}/{{.Path}}", "{{.LineNumber}}", nil
return &templates{
repo: remoteURL,
commit: remoteURL + "/+/{{.Version}}",
file: remoteURL + "/+/{{.Branch}}/{{.Path}}",
line: "{{.LineNumber}}",
}, nil
} else if parsed.Host == "github.com" {
// CloneURL from the JSON API has .git
parsed.Path = strings.TrimSuffix(parsed.Path, ".git")

// eg. https://github.com/hanwen/go-fuse/blob/notify/genversion.sh#L10
return parsed.String() + "/blob/{{.Branch}}/{{.Path}}", "L{{.LineNumber}}", nil
return &templates{
repo: parsed.String(),
commit: parsed.String() + "/commit/{{.Version}}",
file: parsed.String() + "/blob/{{.Branch}}/{{.Path}}",
line: "L{{.LineNumber}}",
}, nil
}

return "", "", fmt.Errorf("scheme unknown for URL %s", remoteURL)
return nil, fmt.Errorf("scheme unknown for URL %s", remoteURL)
}

// GetTree returns a tree object for the given reference.
func getTree(repo *git.Repository, ref string) (*git.Tree, error) {
// getCommit returns a tree object for the given reference.
func getCommit(repo *git.Repository, ref string) (*git.Commit, error) {
obj, err := repo.RevparseSingle(ref)
if err != nil {
return nil, err
}
defer obj.Free()

treeObj, err := obj.Peel(git.ObjectTree)
commitObj, err := obj.Peel(git.ObjectCommit)
if err != nil {
return nil, err
}
tree, err := treeObj.AsTree()
if err != nil {
return nil, err
}
return tree, nil
return commitObj.AsCommit()
}

// IndexGitRepo indexes the git repository as specified by the options and arguments.
Expand All @@ -157,17 +166,13 @@ func IndexGitRepo(opts build.Options, branchPrefix string, branches []string, su
return err
}

url, fragment, err := GuessRepoURL(opts.RepoDir)
if err != nil {
if tpl, err := guessRepoURL(opts.RepoDir); err != nil {
log.Printf("guessRepoURL(%s): %s", opts.RepoDir, err)
} else {
opts.RepoURL = url
opts.RepoLineFragment = fragment
}

builder, err := build.NewBuilder(opts)
if err != nil {
return err
opts.RepoURL = tpl.repo
opts.FileURLTemplate = tpl.file
opts.CommitURLTemplate = tpl.commit
opts.LineFragmentTemplate = tpl.line
}

// name => branch
Expand All @@ -179,7 +184,16 @@ func IndexGitRepo(opts build.Options, branchPrefix string, branches []string, su
data := map[string]map[string]git.Oid{}
repos := map[git.Oid]*git.Repository{}
for _, b := range branches {
tree, err := getTree(repo, filepath.Join(branchPrefix, b))
commit, err := getCommit(repo, filepath.Join(branchPrefix, b))
if err != nil {
return err
}
opts.Branches = append(opts.Branches, build.Branch{
Name: b,
Version: commit.Id().String(),
})

tree, err := commit.Tree()
if err != nil {
return err
}
Expand All @@ -198,6 +212,11 @@ func IndexGitRepo(opts build.Options, branchPrefix string, branches []string, su
data[b] = fs
}

builder, err := build.NewBuilder(opts)
if err != nil {
return err
}

for n := range allfiles {
names = append(names, n)
}
Expand Down
24 changes: 18 additions & 6 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/google/zoekt/query"
)
Expand Down Expand Up @@ -46,17 +47,28 @@ type indexData struct {
fileNameNgrams map[ngram][]uint32

fileBranchMasks []uint32
branchNames map[uint]string
branchIDs map[string]uint

// mask (power of 2) => name
branchNames map[uint]string

// name => mask (power of 2)
branchIDs map[string]uint

unaryData indexUnaryData
}

type indexUnaryData struct {
RepoName string
RepoURL string
RepoLineFragment string
IndexFormatVersion int
RepoName string
RepoURL string

CommitURLTemplate string
FileURLTemplate string
RepoLineFragmentTemplate string
IndexFormatVersion int

IndexTime time.Time
BranchNames []string
BranchVersions []string
}

func (d *indexData) memoryUse() int {
Expand Down
Loading

0 comments on commit 4e11405

Please sign in to comment.