2012-10-04 19:48:59 +00:00
|
|
|
{- git-annex preferred content matcher configuration
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.PreferredContent (
|
|
|
|
preferredContentSet,
|
2012-10-08 17:16:53 +00:00
|
|
|
isPreferredContent,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap,
|
|
|
|
preferredContentMapRaw,
|
|
|
|
checkPreferredContentExpression,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Either
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex
|
|
|
|
import Logs.UUIDBased
|
2012-10-08 17:39:18 +00:00
|
|
|
import Limit
|
2012-10-04 19:48:59 +00:00
|
|
|
import qualified Utility.Matcher
|
2012-10-08 17:16:53 +00:00
|
|
|
import Annex.UUID
|
|
|
|
import Git.FilePath
|
2012-10-08 19:18:58 +00:00
|
|
|
import Types.Group
|
|
|
|
import Logs.Group
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
{- Filename of preferred-content.log. -}
|
|
|
|
preferredContentLog :: FilePath
|
|
|
|
preferredContentLog = "preferred-content.log"
|
|
|
|
|
|
|
|
{- Changes the preferred content configuration of a remote. -}
|
|
|
|
preferredContentSet :: UUID -> String -> Annex ()
|
|
|
|
preferredContentSet uuid@(UUID _) val = do
|
|
|
|
ts <- liftIO getPOSIXTime
|
|
|
|
Annex.Branch.change preferredContentLog $
|
|
|
|
showLog id . changeLog ts uuid val . parseLog Just
|
|
|
|
Annex.changeState $ \s -> s { Annex.groupmap = Nothing }
|
|
|
|
preferredContentSet NoUUID _ = error "unknown UUID; cannot modify"
|
|
|
|
|
2012-10-08 17:16:53 +00:00
|
|
|
{- Checks if a file is preferred content for the specified repository
|
|
|
|
- (or the current repository if none is specified). -}
|
|
|
|
isPreferredContent :: Maybe UUID -> AssumeNotPresent -> TopFilePath -> Annex Bool
|
|
|
|
isPreferredContent mu notpresent file = do
|
|
|
|
u <- maybe getUUID return mu
|
|
|
|
m <- preferredContentMap
|
|
|
|
case M.lookup u m of
|
|
|
|
Nothing -> return True
|
|
|
|
Just matcher ->
|
|
|
|
Utility.Matcher.matchM2 matcher notpresent $
|
|
|
|
getTopFilePath file
|
|
|
|
|
2012-10-04 19:48:59 +00:00
|
|
|
{- Read the preferredContentLog into a map. The map is cached for speed. -}
|
|
|
|
preferredContentMap :: Annex Annex.PreferredContentMap
|
|
|
|
preferredContentMap = do
|
2012-10-08 19:18:58 +00:00
|
|
|
groupmap <- groupMap
|
2012-10-04 19:48:59 +00:00
|
|
|
cached <- Annex.getState Annex.preferredcontentmap
|
|
|
|
case cached of
|
|
|
|
Just m -> return m
|
|
|
|
Nothing -> do
|
2012-10-08 19:18:58 +00:00
|
|
|
m <- simpleMap . parseLog (Just . makeMatcher groupmap)
|
2012-10-04 19:48:59 +00:00
|
|
|
<$> Annex.Branch.get preferredContentLog
|
|
|
|
Annex.changeState $ \s -> s { Annex.preferredcontentmap = Just m }
|
|
|
|
return m
|
|
|
|
|
|
|
|
preferredContentMapRaw :: Annex (M.Map UUID String)
|
|
|
|
preferredContentMapRaw = simpleMap . parseLog Just
|
|
|
|
<$> Annex.Branch.get preferredContentLog
|
|
|
|
|
|
|
|
{- This intentionally never fails, even on unparsable expressions,
|
|
|
|
- because the configuration is shared amoung repositories and newer
|
|
|
|
- versions of git-annex may add new features. Instead, parse errors
|
|
|
|
- result in a Matcher that will always succeed. -}
|
2012-10-08 19:18:58 +00:00
|
|
|
makeMatcher :: GroupMap -> String -> Utility.Matcher.Matcher MatchFiles
|
|
|
|
makeMatcher groupmap s
|
2012-10-04 19:48:59 +00:00
|
|
|
| null (lefts tokens) = Utility.Matcher.generate $ rights tokens
|
|
|
|
| otherwise = Utility.Matcher.generate []
|
|
|
|
where
|
2012-10-08 19:18:58 +00:00
|
|
|
tokens = map (parseToken groupmap) (tokenizeMatcher s)
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
{- Checks if an expression can be parsed, if not returns Just error -}
|
|
|
|
checkPreferredContentExpression :: String -> Maybe String
|
2012-10-08 19:18:58 +00:00
|
|
|
checkPreferredContentExpression s =
|
|
|
|
case lefts $ map (parseToken emptyGroupMap) (tokenizeMatcher s) of
|
|
|
|
[] -> Nothing
|
|
|
|
l -> Just $ unwords $ map ("Parse failure: " ++) l
|
2012-10-04 19:48:59 +00:00
|
|
|
|
2012-10-08 19:18:58 +00:00
|
|
|
parseToken :: GroupMap -> String -> Either String (Utility.Matcher.Token MatchFiles)
|
|
|
|
parseToken groupmap t
|
2012-10-04 19:48:59 +00:00
|
|
|
| any (== t) Utility.Matcher.tokens = Right $ Utility.Matcher.token t
|
|
|
|
| otherwise = maybe (Left $ "near " ++ show t) use $ M.lookup k m
|
|
|
|
where
|
|
|
|
(k, v) = separate (== '=') t
|
|
|
|
m = M.fromList
|
|
|
|
[ ("include", limitInclude)
|
|
|
|
, ("exclude", limitExclude)
|
|
|
|
, ("in", limitIn)
|
|
|
|
, ("copies", limitCopies)
|
2012-10-08 19:18:58 +00:00
|
|
|
, ("inbackend", limitInBackend)
|
2012-10-08 17:39:18 +00:00
|
|
|
, ("largerthan", limitSize (>))
|
|
|
|
, ("smallerthan", limitSize (<))
|
2012-10-10 16:59:45 +00:00
|
|
|
, ("inallgroup", limitInAllGroup groupmap)
|
2012-10-04 19:48:59 +00:00
|
|
|
]
|
|
|
|
use a = Utility.Matcher.Operation <$> a v
|
|
|
|
|
|
|
|
{- This is really dumb tokenization; there's no support for quoted values.
|
|
|
|
- Open and close parens are always treated as standalone tokens;
|
|
|
|
- otherwise tokens must be separated by whitespace. -}
|
|
|
|
tokenizeMatcher :: String -> [String]
|
|
|
|
tokenizeMatcher = filter (not . null ) . concatMap splitparens . words
|
|
|
|
where
|
|
|
|
splitparens = segmentDelim (`elem` "()")
|