Skip to content

Commit

Permalink
use .bindown if it is gitignored
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Aug 7, 2023
1 parent 208a6d1 commit 9c5e4ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
26 changes: 15 additions & 11 deletions internal/bindown/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,18 +681,22 @@ func findCacheDir(cfgDir string) (string, error) {
if err == nil && info.IsDir() {
return bindownDir, nil
}
// if .cache is ignored by .gitignore, use it
// if .bindown is in .gitignore, use it
ig, err := dirIsGitIgnored(bindownDir)
if err != nil {
return "", err
}
if ig {
return bindownDir, nil
}
// if .cache is in .gitignore, use it
cacheDir := filepath.Join(cfgDir, ".cache")
// When a dir is ignored with a trailing slash, we need to check if a file in that dir is ignored.
for _, dir := range []string{cacheDir, filepath.Join(cacheDir, "downloads")} {
var ignored bool
ignored, err = fileIsGitignored(dir)
if err != nil {
return "", err
}
if ignored {
return cacheDir, nil
}
ig, err = dirIsGitIgnored(cacheDir)
if err != nil {
return "", err
}
if ig {
return cacheDir, nil
}
// default to .bindown
return bindownDir, nil
Expand Down
11 changes: 11 additions & 0 deletions internal/bindown/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ func Unique[V comparable](vals, buf []V) []V {
return buf
}

func dirIsGitIgnored(dir string) (bool, error) {
ig, err := fileIsGitignored(dir)
if err != nil {
return false, err
}
if ig {
return true, nil
}
return fileIsGitignored(filepath.Join(dir, "x"))
}

// fileIsGitignored returns true if the file is ignored by a .gitignore file in the same directory or any parent
// directory from the same git repo.
func fileIsGitignored(filename string) (bool, error) {
Expand Down

0 comments on commit 9c5e4ca

Please sign in to comment.