From 1873a84fd21c028bab0a8fb98436230402a57ade Mon Sep 17 00:00:00 2001 From: Jasper Reef Date: Tue, 2 Apr 2024 02:03:11 +0200 Subject: [PATCH] Respect root .gitignore while globbing. Instead of globbing everything for **/spago.yaml and then filtering the matches against .gitignore files, use every entry in the root .gitignore as a negative match (ignore) while globbing. The resulting matches still need to be checked seperately using `Git.isIgnored`, because the new function only considers the root .gitignore file, .gitignore files in subdirectories are ignored. --- src/Spago/Config.purs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Spago/Config.purs b/src/Spago/Config.purs index ff368d03f..48118d6c1 100644 --- a/src/Spago/Config.purs +++ b/src/Spago/Config.purs @@ -163,6 +163,31 @@ type ReadWorkspaceOptions = , migrateConfig :: Boolean } +-- | Same as `Glob.match'` but if there is a .gitignore file in the same directory, +-- | then the `ignore` option will be filled accordingly. +-- | This function does not respect any .gitignore files in subdirectories. +-- | Translation of: https://github.com/sindresorhus/globby/issues/50#issuecomment-467897064 +gitIgnoringGlob :: String -> Array String -> Spago (LogEnv _) { failed :: Array String, succeeded :: Array String } +gitIgnoringGlob dir patterns = do + gitignore <- try (liftAff $ FS.readTextFile $ Path.concat [ dir, ".gitignore" ]) >>= case _ of + Left err -> do + logDebug $ "Could not read .gitignore to exclude directories from globbing, error: " <> Aff.message err + pure "" + Right contents -> pure contents + let + isComment = isJust <<< String.stripPrefix (String.Pattern "#") + dropPrefixSlashes line = maybe line dropPrefixSlashes $ String.stripPrefix (String.Pattern "/") line + dropSuffixSlashes line = maybe line dropSuffixSlashes $ String.stripSuffix (String.Pattern "/") line + + ignore :: Array String + ignore = + map (dropSuffixSlashes <<< dropPrefixSlashes) + $ Array.filter (not <<< or [ String.null, isComment ]) + $ map String.trim + $ String.split (String.Pattern "\n") + $ gitignore + liftAff $ Glob.match' dir patterns { ignore: [ ".spago" ] <> ignore } + -- | Reads all the configurations in the tree and builds up the Map of local -- | packages to be integrated in the package set readWorkspace :: ReadWorkspaceOptions -> Spago (Registry.RegistryEnv _) Workspace @@ -198,7 +223,7 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do logDebug "Gathering all the spago configs in the tree..." { succeeded: otherConfigPaths, failed, ignored } <- do - result <- liftAff $ Glob.match' Paths.cwd [ "**/spago.yaml" ] { ignore: [ ".spago", "spago.yaml" ] } + result <- gitIgnoringGlob Paths.cwd [ "**/spago.yaml" ] -- If a file is gitignored then we don't include it as a package let filterGitignored path = do