Skip to content

Commit

Permalink
build: remove Options.RepoDir
Browse files Browse the repository at this point in the history
The build/ package has no use for RepoDir. Instead, use
RepositoryDescription.Name for the default shard name, and add a
RepoDir to gitindex.Options.

Make call to Options.SetDefaults implicit.

Change-Id: I022b2db79a4c5bb8df88bfacb8387ab86ff15811
  • Loading branch information
hanwen committed Oct 28, 2017
1 parent f3e8b54 commit 3b3a31d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 38 deletions.
27 changes: 11 additions & 16 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ type Options struct {
// SubRepositories is a path => sub repository map.
SubRepositories map[string]*zoekt.Repository

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

// Path to exuberant ctags binary to run
CTags string

Expand Down Expand Up @@ -124,21 +121,18 @@ func (o *Options) SetDefaults() {
if o.ShardMax == 0 {
o.ShardMax = 128 << 20
}
}

// ShardName returns the name the given index shard.
func (o *Options) shardName(n int) (string, error) {
abs, err := filepath.Abs(o.RepoDir)
if err != nil {
return "", err
}

if u := o.RepositoryDescription.URL; u != "" {
parsed, _ := url.Parse(u)
if o.RepositoryDescription.Name == "" && o.RepositoryDescription.URL != "" {
parsed, _ := url.Parse(o.RepositoryDescription.URL)
if parsed != nil {
abs = url.QueryEscape(filepath.Join(parsed.Host, parsed.Path))
o.RepositoryDescription.Name = filepath.Join(parsed.Host, parsed.Path)
}
}
}

// ShardName returns the name the given index shard.
func (o *Options) shardName(n int) (string, error) {
abs := o.RepositoryDescription.Name
return filepath.Join(o.IndexDir,
fmt.Sprintf("%s_v%d.%05d.zoekt", strings.Replace(abs, "/", "_", -1), zoekt.IndexFormatVersion, n)), nil
}
Expand Down Expand Up @@ -175,8 +169,9 @@ func (o *Options) IndexVersions() []zoekt.RepositoryBranch {

// NewBuilder creates a new Builder instance.
func NewBuilder(opts Options) (*Builder, error) {
if opts.RepoDir == "" {
return nil, fmt.Errorf("must set options.RepoDir")
opts.SetDefaults()
if opts.RepositoryDescription.Name == "" {
return nil, fmt.Errorf("builder: must set Name")
}

b := &Builder{
Expand Down
7 changes: 1 addition & 6 deletions build/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestBasic(t *testing.T) {
RepositoryDescription: zoekt.Repository{
Name: "repo",
},
RepoDir: "/repo",
Parallelism: 2,
SizeMax: 1 << 20,
}
Expand Down Expand Up @@ -103,8 +102,6 @@ func TestUpdate(t *testing.T) {
},
Parallelism: 2,
SizeMax: 1 << 20,

RepoDir: "/a",
}

if b, err := NewBuilder(opts); err != nil {
Expand Down Expand Up @@ -138,7 +135,6 @@ func TestUpdate(t *testing.T) {
FileURLTemplate: "url2",
}

opts.RepoDir = "/b"
if b, err := NewBuilder(opts); err != nil {
t.Fatalf("NewBuilder: %v", err)
} else {
Expand Down Expand Up @@ -193,7 +189,6 @@ func TestDeleteOldShards(t *testing.T) {
Name: "repo",
FileURLTemplate: "url",
},
RepoDir: "/a",
SizeMax: 1 << 20,
}
opts.SetDefaults()
Expand Down Expand Up @@ -266,9 +261,9 @@ func TestPartialSuccess(t *testing.T) {
opts := Options{
IndexDir: dir,
ShardMax: 1024,
RepoDir: "/a",
SizeMax: 1 << 20,
}
opts.RepositoryDescription.Name = "repo"
opts.SetDefaults()

b, err := NewBuilder(opts)
Expand Down
3 changes: 1 addition & 2 deletions cmd/zoekt-git-index/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func main() {
exitStatus := 0
for dir, name := range gitRepos {
opts.RepositoryDescription.Name = name
opts.RepoDir = filepath.Clean(dir)

gitOpts := gitindex.Options{
BranchPrefix: *branchPrefix,
Incremental: *incremental,
Expand All @@ -96,6 +94,7 @@ func main() {
AllowMissingBranch: *allowMissing,
BuildOptions: opts,
Branches: branches,
RepoDir: dir,
}

if err := gitindex.IndexGitRepo(gitOpts); err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/zoekt-index/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func indexArg(arg string, opts build.Options, ignore map[string]struct{}) error
return err
}

opts.RepoDir = dir
opts.RepositoryDescription.Name = filepath.Base(dir)
builder, err := build.NewBuilder(opts)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/zoekt-repo-index/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func main() {
}
}

opts.RepoDir = branches[0].manifestPath
perBranch := map[string]map[gitindex.FileKey]gitindex.BlobLocation{}
opts.SubRepositories = map[string]*zoekt.Repository{}

Expand Down
13 changes: 10 additions & 3 deletions gitindex/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ type Options struct {
AllowMissingBranch bool
RepoCacheDir string
BuildOptions build.Options
RepoDir string

BranchPrefix string
Branches []string
Expand Down Expand Up @@ -313,13 +314,19 @@ func expandBranches(repo *git.Repository, bs []string, prefix string) ([]string,

// IndexGitRepo indexes the git repository as specified by the options.
func IndexGitRepo(opts Options) error {
repo, err := git.PlainOpen(opts.BuildOptions.RepoDir)
// Set max thresholds, since we use them in this function.
opts.BuildOptions.SetDefaults()
if opts.RepoDir == "" {
return fmt.Errorf("gitindex: must set RepoDir")
}
repo, err := git.PlainOpen(opts.RepoDir)
if err != nil {
log.Println("B")
return err
}

if err := setTemplatesFromConfig(&opts.BuildOptions.RepositoryDescription, opts.BuildOptions.RepoDir); err != nil {
log.Printf("setTemplatesFromConfig(%s): %s", opts.BuildOptions.RepoDir, err)
if err := setTemplatesFromConfig(&opts.BuildOptions.RepositoryDescription, opts.RepoDir); err != nil {
log.Printf("setTemplatesFromConfig(%s): %s", opts.RepoDir, err)
}

repoCache := NewRepoCache(opts.RepoCacheDir)
Expand Down
19 changes: 10 additions & 9 deletions gitindex/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ func TestSubmoduleIndex(t *testing.T) {

buildOpts := build.Options{
IndexDir: indexDir,
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
}
buildOpts.SetDefaults()

opts := Options{
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
BuildOptions: buildOpts,
BranchPrefix: "refs/heads/",
Branches: []string{"master"},
Expand Down Expand Up @@ -233,11 +231,10 @@ func TestAllowMissingBranch(t *testing.T) {

buildOpts := build.Options{
IndexDir: indexDir,
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
}
buildOpts.SetDefaults()

opts := Options{
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
BuildOptions: buildOpts,
BranchPrefix: "refs/heads/",
Branches: []string{"master", "nonexist"},
Expand Down Expand Up @@ -304,11 +301,14 @@ func TestBranchWildcard(t *testing.T) {

buildOpts := build.Options{
IndexDir: indexDir,
RepoDir: filepath.Join(dir + "/repo"),
RepositoryDescription: zoekt.Repository{
Name: "repo",
},
}
buildOpts.SetDefaults()

opts := Options{
RepoDir: filepath.Join(dir + "/repo"),
BuildOptions: buildOpts,
BranchPrefix: "refs/heads",
Branches: []string{"branchdir/*"},
Expand Down Expand Up @@ -353,16 +353,17 @@ func TestSkipSubmodules(t *testing.T) {

buildOpts := build.Options{
IndexDir: indexDir,
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
RepositoryDescription: zoekt.Repository{
Name: "gerrit.googlesource.com/adir",
},
}
buildOpts.SetDefaults()

if err := os.Rename(dir+"/gerrit.googlesource.com/bdir.git",
dir+"/gerrit.googlesource.com/notexist.git"); err != nil {
t.Fatalf("Rename: %v", err)
}

opts := Options{
RepoDir: filepath.Join(dir, "gerrit.googlesource.com", "adir.git"),
BuildOptions: buildOpts,
BranchPrefix: "refs/heads",
Branches: []string{"master"},
Expand Down

0 comments on commit 3b3a31d

Please sign in to comment.