build: move incremental decision into options

Currently build.Options returns a list of indexed versions. This is used
in each command to decide if we should incrementally index. However, it
has been updated over time to also do extra checks for incremental
indexing. If one of those checks fail, it returns nil to force the
indexing.

This commit updates the function to encapsulate what we want, the
decision of should we index given the build options.

Change-Id: I3a8b9073408cb785a299bc287af1e10570415c96
diff --git a/build/builder.go b/build/builder.go
index 1c41bc7..af695c9 100644
--- a/build/builder.go
+++ b/build/builder.go
@@ -27,6 +27,7 @@
 	"os"
 	"os/exec"
 	"path/filepath"
+	"reflect"
 	"regexp"
 	"runtime"
 	"runtime/pprof"
@@ -198,36 +199,36 @@
 		fmt.Sprintf("%s_v%d.%05d.zoekt", abs, zoekt.IndexFormatVersion, n))
 }
 
-// IndexVersions returns the versions as present in the index, for
-// implementing incremental indexing.
-func (o *Options) IndexVersions() []zoekt.RepositoryBranch {
+// IncrementalSkipIndexing returns true if the index present on disk matches
+// the build options.
+func (o *Options) IncrementalSkipIndexing() bool {
 	fn := o.shardName(0)
 
 	f, err := os.Open(fn)
 	if err != nil {
-		return nil
+		return false
 	}
 
 	iFile, err := zoekt.NewIndexFile(f)
 	if err != nil {
-		return nil
+		return false
 	}
 	defer iFile.Close()
 
 	repo, index, err := zoekt.ReadMetadata(iFile)
 	if err != nil {
-		return nil
+		return false
 	}
 
 	if index.IndexFeatureVersion != zoekt.FeatureVersion {
-		return nil
+		return false
 	}
 
 	if repo.IndexOptions != o.HashOptions() {
-		return nil
+		return false
 	}
 
-	return repo.Branches
+	return reflect.DeepEqual(repo.Branches, o.RepositoryDescription.Branches)
 }
 
 // IgnoreSizeMax determines whether the max size should be ignored.
diff --git a/cmd/zoekt-archive-index/main.go b/cmd/zoekt-archive-index/main.go
index da83e65..75f37d2 100644
--- a/cmd/zoekt-archive-index/main.go
+++ b/cmd/zoekt-archive-index/main.go
@@ -15,7 +15,6 @@
 	"io/ioutil"
 	"log"
 	"net/url"
-	"reflect"
 	"strings"
 
 	"github.com/google/zoekt"
@@ -141,11 +140,8 @@
 	bopts.RepositoryDescription.Branches = []zoekt.RepositoryBranch{{Name: opts.Branch, Version: opts.Commit}}
 	brs := []string{opts.Branch}
 
-	if opts.Incremental {
-		versions := bopts.IndexVersions()
-		if reflect.DeepEqual(versions, bopts.RepositoryDescription.Branches) {
-			return nil
-		}
+	if opts.Incremental && bopts.IncrementalSkipIndexing() {
+		return nil
 	}
 
 	a, err := openArchive(opts.Archive)
diff --git a/cmd/zoekt-repo-index/main.go b/cmd/zoekt-repo-index/main.go
index e928c6f..5d158ec 100644
--- a/cmd/zoekt-repo-index/main.go
+++ b/cmd/zoekt-repo-index/main.go
@@ -37,7 +37,6 @@
 	"net/url"
 	"path"
 	"path/filepath"
-	"reflect"
 	"sort"
 	"strings"
 
@@ -255,11 +254,8 @@
 		}
 	}
 
-	if *incremental {
-		versions := opts.IndexVersions()
-		if reflect.DeepEqual(versions, opts.RepositoryDescription.Branches) {
-			return
-		}
+	if *incremental && opts.IncrementalSkipIndexing() {
+		return
 	}
 
 	builder, err := build.NewBuilder(opts)
diff --git a/gitindex/index.go b/gitindex/index.go
index 3545b41..17100e9 100644
--- a/gitindex/index.go
+++ b/gitindex/index.go
@@ -24,7 +24,6 @@
 	"net/url"
 	"os"
 	"path/filepath"
-	"reflect"
 	"sort"
 	"strconv"
 	"strings"
@@ -422,11 +421,8 @@
 		branchVersions[b] = subVersions
 	}
 
-	if opts.Incremental {
-		versions := opts.BuildOptions.IndexVersions()
-		if reflect.DeepEqual(versions, opts.BuildOptions.RepositoryDescription.Branches) {
-			return nil
-		}
+	if opts.Incremental && opts.BuildOptions.IncrementalSkipIndexing() {
+		return nil
 	}
 
 	reposByPath := map[string]BlobLocation{}