Skip to content

Commit

Permalink
Merge pull request #1 from leboncoin/gerrit-mirror-delete-repos
Browse files Browse the repository at this point in the history
zoekt-mirror-gerrit: delete stale repos
  • Loading branch information
df11 authored Dec 3, 2021
2 parents 1e59c33 + 16ce947 commit 4841e59
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/zoekt-indexserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func executeMirror(cfg []ConfigEntry, repoDir string, pendingRepos chan<- string
}
} else if c.GerritApiURL != "" {
cmd = exec.Command("zoekt-mirror-gerrit",
"-dest", repoDir)
"-dest", repoDir, "-delete")
if c.CredentialPath != "" {
cmd.Args = append(cmd.Args, "-http-credentials", c.CredentialPath)
}
Expand Down
29 changes: 28 additions & 1 deletion cmd/zoekt-mirror-gerrit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func main() {
dest := flag.String("dest", "", "destination directory")
namePattern := flag.String("name", "", "only clone repos whose name matches the regexp.")
excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
deleteRepos := flag.Bool("delete", false, "delete missing repos")
httpCrendentialsPath := flag.String("http-credentials", "", "path to a file containing http credentials stored like 'user:password'.")
flag.Parse()

Expand Down Expand Up @@ -149,7 +150,7 @@ func main() {
continue
}

cloneURL, err := url.Parse(strings.Replace(projectURL, "${project}", k, -1))
cloneURL, err := url.Parse(strings.Replace(projectURL, "${project}", k, 1))
if err != nil {
log.Fatalf("url.Parse: %v", err)
}
Expand Down Expand Up @@ -181,4 +182,30 @@ func main() {
fmt.Println(dest)
}
}
if *deleteRepos {
if err := deleteStaleRepos(*dest, filter, projects, projectURL); err != nil {
log.Fatalf("deleteStaleRepos: %v", err)
}
}
}

func deleteStaleRepos(destDir string, filter *gitindex.Filter, repos map[string]gerrit.ProjectInfo, projectURL string) error {
u, err := url.Parse(strings.Replace(projectURL, "${project}", "", 1))
if err != nil {
return err
}

names := map[string]struct{}{}
for name, _ := range repos {
u, err := url.Parse(strings.Replace(projectURL, "${project}", name, 1))
if err != nil {
return err
}
names[filepath.Join(u.Host, u.Path) + ".git"] = struct{}{}
}

if err := gitindex.DeleteRepos(destDir, u, names, filter); err != nil {
log.Fatalf("deleteRepos: %v", err)
}
return nil
}
5 changes: 4 additions & 1 deletion gitindex/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
)

// DeleteRepos deletes stale repos under a specific path in disk. The `names`
Expand All @@ -19,7 +20,9 @@ func DeleteRepos(baseDir string, urlPrefix *url.URL, names map[string]struct{},
var toDelete []string
for _, p := range paths {
_, exists := names[p]
if filter.Include(filepath.Base(p)) && !exists {
repoName := strings.Replace(p, filepath.Join(urlPrefix.Host, urlPrefix.Path), "", 1)
repoName = strings.TrimPrefix(repoName, "/")
if filter.Include(repoName) && !exists {
toDelete = append(toDelete, p)
}
}
Expand Down

0 comments on commit 4841e59

Please sign in to comment.