add back subrepo branch validation

This condition is checked, but no action is ever taken if branchEqual is
false. The error seems to have been lost during the refactor in
Id5671685ec65c3b52ba35c9b2f4c66fb0558a82b. This commit adds it back.

Change-Id: Iac19da9ebdb79e62127b7f1950a429f2a466636b
diff --git a/index_test.go b/index_test.go
index 0f84197..3ff2111 100644
--- a/index_test.go
+++ b/index_test.go
@@ -20,6 +20,7 @@
 	"fmt"
 	"reflect"
 	"regexp/syntax"
+	"strconv"
 	"strings"
 	"testing"
 
@@ -1260,6 +1261,72 @@
 	}
 }
 
+func TestNewIndexBuilder(t *testing.T) {
+	var branchX100 []RepositoryBranch
+	for i := 0; i < 100; i++ {
+		branchX100 = append(branchX100, RepositoryBranch{
+			Name: strconv.Itoa(i),
+		})
+	}
+
+	cases := []struct {
+		name string
+		repo Repository
+		errS string
+	}{{
+		name: "empty",
+		repo: Repository{},
+	}, {
+		name: "too-many-branches",
+		repo: Repository{
+			Branches: branchX100,
+		},
+		errS: "too many branches",
+	}, {
+		name: "subrepo-branches",
+		repo: Repository{
+			Branches: []RepositoryBranch{{
+				Name: "dev",
+			}},
+			SubRepoMap: map[string]*Repository{
+				"sub": {
+					Branches: []RepositoryBranch{{
+						Name: "dev",
+					}},
+				},
+			},
+		},
+	}, {
+		name: "subrepo-mismatch-branches",
+		repo: Repository{
+			Branches: []RepositoryBranch{{
+				Name: "dev",
+			}},
+			SubRepoMap: map[string]*Repository{
+				"sub": {
+					Branches: []RepositoryBranch{{
+						Name: "main",
+					}},
+				},
+			},
+		},
+		errS: "got subrepository branches [{main }], want main repository branches [{dev }]",
+	}}
+
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			_, err := NewIndexBuilder(&tc.repo)
+			var got string
+			if err != nil {
+				got = err.Error()
+			}
+			if got != tc.errS {
+				t.Fatalf("unexpected error:\nwanted: %s\ngot:    %s", tc.errS, got)
+			}
+		})
+	}
+}
+
 func TestSearchEither(t *testing.T) {
 	b := testIndexBuilder(t, nil,
 		Document{Name: "f1", Content: []byte("bla needle bla")},
diff --git a/indexbuilder.go b/indexbuilder.go
index 3ce7816..e316269 100644
--- a/indexbuilder.go
+++ b/indexbuilder.go
@@ -222,6 +222,9 @@
 				branchEqual = branchEqual && (b.Name == desc.Branches[i].Name)
 			}
 		}
+		if !branchEqual {
+			return fmt.Errorf("got subrepository branches %v, want main repository branches %v", subrepo.Branches, desc.Branches)
+		}
 	}
 
 	if len(desc.Branches) > 64 {