Skip to content

Commit

Permalink
Respect root .gitignore while globbing. (#1209)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Blugatroff committed Apr 2, 2024
1 parent 9e83d87 commit 7308e90
Showing 1 changed file with 26 additions and 1 deletion.
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

0 comments on commit 7308e90

Please sign in to comment.