Skip to content

Commit 69068bf

Browse files
zoekt-mirror-gerrit: add an option to fetch meta/config branch (#774)
1 parent 72f9500 commit 69068bf

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

cmd/zoekt-indexserver/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ConfigEntry struct {
5050
ExcludeTopics []string
5151
Active bool
5252
NoArchived bool
53+
GerritFetchMetaConfig bool
5354
}
5455

5556
func randomize(entries []ConfigEntry) []ConfigEntry {
@@ -259,6 +260,9 @@ func executeMirror(cfg []ConfigEntry, repoDir string, pendingRepos chan<- string
259260
if c.Active {
260261
cmd.Args = append(cmd.Args, "-active")
261262
}
263+
if c.GerritFetchMetaConfig {
264+
cmd.Args = append(cmd.Args, "-fetch-meta-config")
265+
}
262266
cmd.Args = append(cmd.Args, c.GerritApiURL)
263267
} else {
264268
log.Printf("executeMirror: ignoring config, because it does not contain any valid repository definition: %v", c)

cmd/zoekt-mirror-gerrit/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"strings"
3232

3333
gerrit "github.com/andygrunwald/go-gerrit"
34+
git "github.com/go-git/go-git/v5"
35+
"github.com/go-git/go-git/v5/config"
3436
"github.com/sourcegraph/zoekt/gitindex"
3537
)
3638

@@ -92,6 +94,7 @@ func main() {
9294
repoNameFormat := flag.String("repo-name-format", qualifiedRepoNameFormat, fmt.Sprintf("the format of the local repo name in zoekt (valid values: %s)", strings.Join(validRepoNameFormat, ", ")))
9395
excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
9496
deleteRepos := flag.Bool("delete", false, "delete missing repos")
97+
fetchMetaConfig := flag.Bool("fetch-meta-config", false, "fetch gerrit meta/config branch")
9598
httpCrendentialsPath := flag.String("http-credentials", "", "path to a file containing http credentials stored like 'user:password'.")
9699
active := flag.Bool("active", false, "mirror only active projects")
97100
flag.Parse()
@@ -216,6 +219,11 @@ func main() {
216219
} else {
217220
fmt.Println(dest)
218221
}
222+
if *fetchMetaConfig {
223+
if err := addMetaConfigFetch(filepath.Join(*dest, name+".git")); err != nil {
224+
log.Fatalf("addMetaConfigFetch: %v", err)
225+
}
226+
}
219227
}
220228
if *deleteRepos {
221229
if err := deleteStaleRepos(*dest, filter, projects, projectURL); err != nil {
@@ -263,3 +271,28 @@ func addPassword(u string, user *url.Userinfo) string {
263271
username := user.Username()
264272
return strings.Replace(u, fmt.Sprintf("://%s@", username), fmt.Sprintf("://%s:%s@", username, password), 1)
265273
}
274+
275+
func addMetaConfigFetch(repoDir string) error {
276+
repo, err := git.PlainOpen(repoDir)
277+
if err != nil {
278+
return err
279+
}
280+
281+
cfg, err := repo.Config()
282+
if err != nil {
283+
return err
284+
}
285+
286+
rm := cfg.Remotes["origin"]
287+
if rm != nil {
288+
configRefSpec := config.RefSpec("+refs/meta/config:refs/heads/meta/config")
289+
if !slices.Contains(rm.Fetch, configRefSpec) {
290+
rm.Fetch = append(rm.Fetch, configRefSpec)
291+
}
292+
}
293+
if err := repo.Storer.SetConfig(cfg); err != nil {
294+
return err
295+
}
296+
297+
return nil
298+
}

0 commit comments

Comments
 (0)