2011-09-28 18:03:59 +00:00
|
|
|
{- git ls-tree interface
|
|
|
|
-
|
2021-03-23 16:44:29 +00:00
|
|
|
- Copyright 2011-2021 Joey Hess <id@joeyh.name>
|
2011-09-28 18:03:59 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-09-28 18:03:59 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Git.LsTree (
|
2011-09-28 20:43:10 +00:00
|
|
|
TreeItem(..),
|
2021-03-23 16:44:29 +00:00
|
|
|
LsTreeRecursive(..),
|
|
|
|
LsTreeLong(..),
|
2011-09-29 23:04:24 +00:00
|
|
|
lsTree,
|
2016-02-23 20:36:08 +00:00
|
|
|
lsTree',
|
2020-12-22 18:06:40 +00:00
|
|
|
lsTreeStrict,
|
|
|
|
lsTreeStrict',
|
2013-10-21 19:28:06 +00:00
|
|
|
lsTreeParams,
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
lsTreeFiles,
|
2016-02-23 20:36:08 +00:00
|
|
|
parseLsTree,
|
2019-05-20 20:37:04 +00:00
|
|
|
formatLsTree,
|
2011-09-28 18:03:59 +00:00
|
|
|
) where
|
|
|
|
|
2011-12-20 18:37:53 +00:00
|
|
|
import Common
|
2011-09-30 03:43:42 +00:00
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2013-10-17 18:51:19 +00:00
|
|
|
import Git.FilePath
|
2023-04-12 21:18:29 +00:00
|
|
|
import qualified Git.Quote
|
2019-12-06 17:58:28 +00:00
|
|
|
import Utility.Attoparsec
|
2011-09-28 18:03:59 +00:00
|
|
|
|
2015-05-10 20:50:46 +00:00
|
|
|
import Numeric
|
2019-11-25 20:18:19 +00:00
|
|
|
import Data.Either
|
2021-05-17 14:46:24 +00:00
|
|
|
import Data.Char
|
2015-05-10 20:50:46 +00:00
|
|
|
import System.Posix.Types
|
2019-11-25 20:18:19 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2020-12-22 18:06:40 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString as AS
|
2019-11-25 20:18:19 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2015-05-10 20:50:46 +00:00
|
|
|
|
2011-09-28 18:03:59 +00:00
|
|
|
data TreeItem = TreeItem
|
2011-09-28 18:14:52 +00:00
|
|
|
{ mode :: FileMode
|
2019-11-25 20:18:19 +00:00
|
|
|
, typeobj :: S.ByteString
|
2016-01-01 19:56:24 +00:00
|
|
|
, sha :: Ref
|
2021-03-23 16:44:29 +00:00
|
|
|
, size :: Maybe FileSize
|
2013-10-17 18:51:19 +00:00
|
|
|
, file :: TopFilePath
|
2021-03-23 16:44:29 +00:00
|
|
|
-- ^ only available when long is used
|
2020-12-22 18:06:40 +00:00
|
|
|
} deriving (Show)
|
2011-09-28 18:03:59 +00:00
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
data LsTreeRecursive = LsTreeRecursive | LsTreeNonRecursive
|
|
|
|
|
|
|
|
{- Enabling --long also gets the size of tree items.
|
|
|
|
- This slows down ls-tree some, since it has to look up the size of each
|
|
|
|
- blob.
|
|
|
|
-}
|
|
|
|
data LsTreeLong = LsTreeLong Bool
|
2019-02-21 21:32:59 +00:00
|
|
|
|
|
|
|
{- Lists the contents of a tree, with lazy output. -}
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTree :: LsTreeRecursive -> LsTreeLong -> Ref -> Repo -> IO ([TreeItem], IO Bool)
|
2016-02-23 20:36:08 +00:00
|
|
|
lsTree = lsTree' []
|
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTree' :: [CommandParam] -> LsTreeRecursive -> LsTreeLong -> Ref -> Repo -> IO ([TreeItem], IO Bool)
|
|
|
|
lsTree' ps recursive long t repo = do
|
|
|
|
(l, cleanup) <- pipeNullSplit (lsTreeParams recursive long t ps) repo
|
|
|
|
return (rights (map (parseLsTree long) l), cleanup)
|
2013-10-21 19:28:06 +00:00
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTreeStrict :: LsTreeRecursive -> LsTreeLong -> Ref -> Repo -> IO [TreeItem]
|
2020-12-22 18:06:40 +00:00
|
|
|
lsTreeStrict = lsTreeStrict' []
|
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTreeStrict' :: [CommandParam] -> LsTreeRecursive -> LsTreeLong -> Ref -> Repo -> IO [TreeItem]
|
|
|
|
lsTreeStrict' ps recursive long t repo = rights . map (parseLsTreeStrict long)
|
|
|
|
<$> pipeNullSplitStrict (lsTreeParams recursive long t ps) repo
|
2020-12-22 18:06:40 +00:00
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTreeParams :: LsTreeRecursive -> LsTreeLong -> Ref -> [CommandParam] -> [CommandParam]
|
|
|
|
lsTreeParams recursive long r ps =
|
2015-06-01 17:52:23 +00:00
|
|
|
[ Param "ls-tree"
|
|
|
|
, Param "--full-tree"
|
|
|
|
, Param "-z"
|
2021-03-23 16:44:29 +00:00
|
|
|
] ++ recursiveparams ++ longparams ++ ps ++
|
2015-07-06 18:21:43 +00:00
|
|
|
[ Param "--"
|
|
|
|
, File $ fromRef r
|
2015-06-01 17:52:23 +00:00
|
|
|
]
|
2019-02-21 21:32:59 +00:00
|
|
|
where
|
2021-03-23 16:44:29 +00:00
|
|
|
recursiveparams = case recursive of
|
2019-02-21 21:32:59 +00:00
|
|
|
LsTreeRecursive -> [ Param "-r" ]
|
|
|
|
LsTreeNonRecursive -> []
|
2021-03-23 16:44:29 +00:00
|
|
|
longparams = case long of
|
|
|
|
LsTreeLong True -> [ Param "--long" ]
|
|
|
|
LsTreeLong False -> []
|
2011-09-28 18:03:59 +00:00
|
|
|
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
{- Lists specified files in a tree. -}
|
2021-03-23 16:44:29 +00:00
|
|
|
lsTreeFiles :: LsTreeLong -> Ref -> [FilePath] -> Repo -> IO [TreeItem]
|
|
|
|
lsTreeFiles long t fs repo = rights . map (parseLsTree long . L.fromStrict)
|
2019-11-25 20:18:19 +00:00
|
|
|
<$> pipeNullSplitStrict ps repo
|
2013-03-19 02:09:51 +00:00
|
|
|
where
|
2015-06-01 17:52:23 +00:00
|
|
|
ps =
|
|
|
|
[ Param "ls-tree"
|
|
|
|
, Param "--full-tree"
|
|
|
|
, Param "-z"
|
|
|
|
, Param "--"
|
|
|
|
, File $ fromRef t
|
|
|
|
] ++ map File fs
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
parseLsTree :: LsTreeLong -> L.ByteString -> Either String TreeItem
|
|
|
|
parseLsTree long b = case A.parse (parserLsTree long) b of
|
2019-11-25 20:18:19 +00:00
|
|
|
A.Done _ r -> Right r
|
|
|
|
A.Fail _ _ err -> Left err
|
|
|
|
|
2021-03-23 16:44:29 +00:00
|
|
|
parseLsTreeStrict :: LsTreeLong -> S.ByteString -> Either String TreeItem
|
|
|
|
parseLsTreeStrict long b = go (AS.parse (parserLsTree long) b)
|
2020-12-22 18:06:40 +00:00
|
|
|
where
|
|
|
|
go (AS.Done _ r) = Right r
|
|
|
|
go (AS.Fail _ _ err) = Left err
|
|
|
|
go (AS.Partial c) = go (c mempty)
|
|
|
|
|
2017-02-20 17:44:55 +00:00
|
|
|
{- Parses a line of ls-tree output, in format:
|
2021-03-23 16:44:29 +00:00
|
|
|
- mode SP type SP sha TAB file
|
|
|
|
- Or long format:
|
|
|
|
- mode SP type SP sha SPACES size TAB file
|
2017-02-20 17:44:55 +00:00
|
|
|
-
|
2021-01-28 16:36:37 +00:00
|
|
|
- The TAB can also be a space. Git does not use that, but an earlier
|
|
|
|
- version of formatLsTree did, and this keeps parsing what it output
|
|
|
|
- working.
|
2021-03-23 16:44:29 +00:00
|
|
|
-}
|
|
|
|
parserLsTree :: LsTreeLong -> A.Parser TreeItem
|
|
|
|
parserLsTree long = case long of
|
|
|
|
LsTreeLong False ->
|
|
|
|
startparser <*> pure Nothing <* filesep <*> fileparser
|
|
|
|
LsTreeLong True ->
|
|
|
|
startparser <* sizesep <*> sizeparser <* filesep <*> fileparser
|
|
|
|
where
|
|
|
|
startparser = TreeItem
|
|
|
|
-- mode
|
|
|
|
<$> octal
|
|
|
|
<* A8.char ' '
|
|
|
|
-- type
|
|
|
|
<*> A8.takeTill (== ' ')
|
|
|
|
<* A8.char ' '
|
|
|
|
-- sha
|
|
|
|
<*> (Ref <$> A8.takeTill A8.isSpace)
|
|
|
|
|
2023-04-12 21:18:29 +00:00
|
|
|
fileparser = asTopFilePath . Git.Quote.unquote <$> A.takeByteString
|
2021-03-23 16:44:29 +00:00
|
|
|
|
|
|
|
sizeparser = fmap Just A8.decimal
|
|
|
|
|
|
|
|
filesep = A8.space
|
|
|
|
|
|
|
|
sizesep = A.many1 A8.space
|
|
|
|
|
|
|
|
{- Inverse of parseLsTree. Note that the long output format is not
|
|
|
|
- generated, so any size information is not included. -}
|
2021-05-17 14:46:24 +00:00
|
|
|
formatLsTree :: TreeItem -> S.ByteString
|
|
|
|
formatLsTree ti = S.intercalate (S.singleton (fromIntegral (ord ' ')))
|
2021-08-11 00:45:02 +00:00
|
|
|
[ encodeBS (showOct (mode ti) "")
|
2021-05-17 14:46:24 +00:00
|
|
|
, typeobj ti
|
|
|
|
, fromRef' (sha ti)
|
|
|
|
] <> (S.cons (fromIntegral (ord '\t')) (getTopFilePath (file ti)))
|