Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster globbing by respecting root .gitignore. #1209

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Spago/Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading