imp: include: glob patterns always exclude the current file

Eg include **/*.journal is less likely no complain
This commit is contained in:
Simon Michael 2025-07-11 14:00:38 -07:00
parent 08017366b5
commit 0add2e90db
2 changed files with 16 additions and 9 deletions

View File

@ -333,7 +333,7 @@ includedirectivep = do
then pure filepaths then pure filepaths
else customFailure $ parseErrorAt parseroff $ "No files were matched by file pattern: " ++ fileglobpattern else customFailure $ parseErrorAt parseroff $ "No files were matched by file pattern: " ++ fileglobpattern
-- Find the files matched by a glob pattern, using filepattern. -- Find the files matched by a glob pattern, if any, using filepattern.
-- Uses the current parse context for detecting the current directory and for error messages. -- Uses the current parse context for detecting the current directory and for error messages.
-- This one also ignores all dotted directories (anything under .git/, foo/.secret/, etc.) -- This one also ignores all dotted directories (anything under .git/, foo/.secret/, etc.)
getFilePaths2 :: MonadIO m => Int -> SourcePos -> FilePath -> JournalParser m [FilePath] getFilePaths2 :: MonadIO m => Int -> SourcePos -> FilePath -> JournalParser m [FilePath]
@ -348,19 +348,23 @@ includedirectivep = do
realparentfilepath <- liftIO $ canonicalizePath parentfilepath -- Follow a symlink. If the path is already absolute, the operation never fails. realparentfilepath <- liftIO $ canonicalizePath parentfilepath -- Follow a symlink. If the path is already absolute, the operation never fails.
let cwd = takeDirectory realparentfilepath let cwd = takeDirectory realparentfilepath
-- find all matched files, in lexicographic order (the order ls would normally show them) -- Find all matched files, in lexicographic order (the order ls would normally show them).
-- (This might include the current file.)
filepaths <- liftIO $ filepaths <- liftIO $
dbg6 "include: matched files" map (cwd </>)
. map (cwd </>)
-- . sort -- XXX needed ? -- . sort -- XXX needed ?
<$> <$>
getDirectoryFilesIgnore cwd [expandedglob] ["**/.*/**"] getDirectoryFilesIgnore cwd [expandedglob] ["**/.*/**"]
-- throw an error if no files matched -- Throw an error if no files (not even the current file) were matched.
when (null filepaths) $ when (null filepaths) $
customFailure $ parseErrorAt off $ "No files were matched by file pattern: " ++ globpattern customFailure $ parseErrorAt off $ "No files were matched by file pattern: " ++ globpattern
pure filepaths -- If the current file was matched, exclude it now.
let filepaths' = filter (/= realparentfilepath) filepaths
dbg6IO "include: matched files (excluding current file)" filepaths'
pure filepaths'
-- Parse the given included file (and any deeper includes, recursively) -- Parse the given included file (and any deeper includes, recursively)
-- as if it was inlined in the current (parent) file. -- as if it was inlined in the current (parent) file.

View File

@ -2528,10 +2528,13 @@ Also, the path may have a file type prefix to force a specific file format
(as described in [Data formats](#data-formats)): `include timedot:~/notes/2023*.md`. (as described in [Data formats](#data-formats)): `include timedot:~/notes/2023*.md`.
The path may contain [glob patterns] to match multiple files, eg: `include *.journal`. The path may contain [glob patterns] to match multiple files, eg: `include *.journal`.
Note, the current file is always excluded from the matched paths.
(Though include cycles are still possible, and will be reported as an error.)
The special glob pattern `**` matches any number of path components.
It's useful for searching subdirectories.
Eg to include all .journal files below the current directory: `include **/*.journal`.
The special glob pattern `**/` matches any number of directory parts.
This is not robust; it can hang, and `**/*.journal` is rejected.
But this will work: `include */**/*.journal` (find all .journal files below the current directory).
[glob patterns]: https://hackage.haskell.org/package/Glob-0.9.2/docs/System-FilePath-Glob.html#v:compile [glob patterns]: https://hackage.haskell.org/package/Glob-0.9.2/docs/System-FilePath-Glob.html#v:compile