feat: look for repos in the usual folders, not just ~/git
Without roots in the config, repos are found under whichever of ~/git, ~/repo, ~/repos, ~/projects, ~/code, ~/source and ~/src exist. Missing ones cost a stat each. A root that's a symlink to another is scanned once, and a repo under two roots keeps the first one's name. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
5 files changed
+48
-11
No files matched your search
@@ -115,8 +115,8 @@ type Repos struct {
|
||||
// Enabled defaults to true.
|
||||
Enabled *bool `yaml:"enabled"`
|
||||
Paths []string `yaml:"paths"`
|
||||
// Roots are folders of repos. Defaults to ~/git; an empty list
|
||||
// turns it off.
|
||||
// Roots are folders of repos. Defaults to repo.DefaultRoots (~/git,
|
||||
// ~/src, ~/code and the like); an empty list turns it off.
|
||||
Roots *[]string `yaml:"roots"`
|
||||
// Include and Exclude are globs relative to a repo's root (** for any
|
||||
// depth), for repos without their own .lazysecrets.yaml. Without them,
|
||||
|
||||
@@ -80,6 +80,10 @@ type repoInfo struct {
|
||||
here bool // the repo lazysecrets started in
|
||||
}
|
||||
|
||||
// DefaultRoots are the usual folders of repos, used when the config
|
||||
// doesn't set roots. Ones that don't exist cost a stat each.
|
||||
var DefaultRoots = []string{"~/git", "~/repo", "~/repos", "~/projects", "~/code", "~/source", "~/src"}
|
||||
|
||||
func New(s Settings) *Provider {
|
||||
if s.Sops == "" {
|
||||
s.Sops = "sops"
|
||||
@@ -116,10 +120,18 @@ func (p *Provider) load() {
|
||||
}
|
||||
add(root)
|
||||
}
|
||||
// Missing roots are skipped (most of the defaults are), and one
|
||||
// that's a symlink to another is scanned once.
|
||||
scanned := map[string]bool{}
|
||||
for _, dir := range p.s.Roots {
|
||||
base, _ := filepath.EvalSymlinks(expandHome(dir))
|
||||
for _, root := range reposIn(expandHome(dir), 2) {
|
||||
if name, err := filepath.Rel(base, root); err == nil && !strings.HasPrefix(name, "..") {
|
||||
base, err := filepath.EvalSymlinks(expandHome(dir))
|
||||
if err != nil || scanned[base] {
|
||||
continue
|
||||
}
|
||||
scanned[base] = true
|
||||
for _, root := range reposIn(base, 2) {
|
||||
// A repo under two roots keeps the first one's name.
|
||||
if name, err := filepath.Rel(base, root); err == nil && !strings.HasPrefix(name, "..") && names[root] == "" {
|
||||
names[root] = filepath.ToSlash(name)
|
||||
}
|
||||
add(root)
|
||||
|
||||
@@ -336,6 +336,30 @@ func TestRoots(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootsDeduped(t *testing.T) {
|
||||
root, _ := filepath.EvalSymlinks(t.TempDir())
|
||||
os.Rename(gitRepo(t, map[string]string{"secrets/a.yaml": sopsYAML}), filepath.Join(root, "infra"))
|
||||
os.MkdirAll(filepath.Join(root, "acme"), 0o755)
|
||||
os.Rename(gitRepo(t, map[string]string{"prod.env": sopsEnv}), filepath.Join(root, "acme", "billing"))
|
||||
|
||||
// ~/src → ~/git, and a root reaching one repo at another depth.
|
||||
links := t.TempDir()
|
||||
alias := filepath.Join(links, "src")
|
||||
os.Symlink(root, alias)
|
||||
missing := filepath.Join(links, "nope")
|
||||
acme := filepath.Join(root, "acme")
|
||||
|
||||
srcs, err := New(Settings{Dir: links, Roots: []string{missing, root, alias, acme}}).Sources(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := names(srcs, func(s provider.Source) string { return s.Name })
|
||||
slices.Sort(got)
|
||||
if !slices.Equal(got, []string{"acme/billing", "infra"}) {
|
||||
t.Errorf("repos: %v, want each once, named from the first root", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStarredReposAlongside(t *testing.T) {
|
||||
here := gitRepo(t, map[string]string{"secrets/a.yaml": sopsYAML})
|
||||
starred := gitRepo(t, map[string]string{"prod.env": sopsEnv})
|
||||
|
||||
Reference in new issue
Block a user