From 9c5e4ca8bf6f74637d449e1ba03cfdeaccb3bc17 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 7 Aug 2023 14:24:05 -0500 Subject: [PATCH] use .bindown if it is gitignored --- internal/bindown/config.go | 26 +++++++++++++++----------- internal/bindown/util.go | 11 +++++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/internal/bindown/config.go b/internal/bindown/config.go index 97d4a57..bf66355 100644 --- a/internal/bindown/config.go +++ b/internal/bindown/config.go @@ -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 diff --git a/internal/bindown/util.go b/internal/bindown/util.go index 8e399e8..e8734e9 100644 --- a/internal/bindown/util.go +++ b/internal/bindown/util.go @@ -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) {