git-annex/Logs/PreferredContent.hs

102 lines
3.5 KiB
Haskell
Raw Normal View History

{- 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 (
preferredContentLog,
preferredContentSet,
isPreferredContent,
preferredContentMap,
preferredContentMapLoad,
preferredContentMapRaw,
checkPreferredContentExpression,
setStandardGroup,
) where
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Either
import Common.Annex
import Logs.PreferredContent.Raw
import qualified Annex.Branch
import qualified Annex
import Logs
import Logs.UUIDBased
import qualified Utility.Matcher
import Annex.FileMatcher
import Annex.UUID
2013-10-28 18:05:55 +00:00
import Types.Limit
2012-10-08 19:18:58 +00:00
import Types.Group
2013-04-26 03:44:55 +00:00
import Types.Remote (RemoteConfig)
2012-10-08 19:18:58 +00:00
import Logs.Group
2013-04-26 03:44:55 +00:00
import Logs.Remote
import Types.StandardGroups
{- Checks if a file is preferred content for the specified repository
- (or the current repository if none is specified). -}
isPreferredContent :: Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
isPreferredContent mu notpresent mkey afile def = do
u <- maybe getUUID return mu
m <- preferredContentMap
case M.lookup u m of
Nothing -> return def
Just matcher -> checkMatcher matcher mkey afile notpresent def
{- The map is cached for speed. -}
preferredContentMap :: Annex Annex.PreferredContentMap
preferredContentMap = maybe preferredContentMapLoad return
=<< Annex.getState Annex.preferredcontentmap
{- Loads the map, updating the cache. -}
preferredContentMapLoad :: Annex Annex.PreferredContentMap
preferredContentMapLoad = do
2012-10-08 19:18:58 +00:00
groupmap <- groupMap
2013-04-26 03:44:55 +00:00
configmap <- readRemoteLog
m <- simpleMap
2013-04-26 03:44:55 +00:00
. parseLogWithUUID ((Just .) . makeMatcher groupmap configmap)
<$> Annex.Branch.get preferredContentLog
Annex.changeState $ \s -> s { Annex.preferredcontentmap = Just m }
return m
{- This intentionally never fails, even on unparsable expressions,
2013-12-19 09:57:50 +00:00
- because the configuration is shared among repositories and newer
- versions of git-annex may add new features. Instead, parse errors
- result in a Matcher that will always succeed. -}
2014-01-01 23:58:02 +00:00
makeMatcher :: GroupMap -> M.Map UUID RemoteConfig -> UUID -> PreferredContentExpression -> FileMatcher
2013-04-26 03:44:55 +00:00
makeMatcher groupmap configmap u expr
| expr == "standard" = standardMatcher groupmap configmap u
| null (lefts tokens) = Utility.Matcher.generate $ rights tokens
2012-12-13 04:45:27 +00:00
| otherwise = matchAll
2012-11-11 04:51:07 +00:00
where
2013-04-26 03:44:55 +00:00
tokens = exprParser groupmap configmap (Just u) expr
{- Standard matchers are pre-defined for some groups. If none is defined,
- or a repository is in multiple groups with standard matchers, match all. -}
2013-04-26 03:44:55 +00:00
standardMatcher :: GroupMap -> M.Map UUID RemoteConfig -> UUID -> FileMatcher
standardMatcher groupmap configmap u =
maybe matchAll (makeMatcher groupmap configmap u . preferredContent) $
getStandardGroup =<< u `M.lookup` groupsByUUID groupmap
2012-10-10 19:15:56 +00:00
{- Checks if an expression can be parsed, if not returns Just error -}
2014-01-01 23:58:02 +00:00
checkPreferredContentExpression :: PreferredContentExpression -> Maybe String
2013-04-26 03:44:55 +00:00
checkPreferredContentExpression expr
| expr == "standard" = Nothing
| otherwise = case parsedToMatcher tokens of
Left e -> Just e
Right _ -> Nothing
2012-11-11 04:51:07 +00:00
where
2013-04-26 03:44:55 +00:00
tokens = exprParser emptyGroupMap M.empty Nothing expr
{- Puts a UUID in a standard group, and sets its preferred content to use
- the standard expression for that group, unless something is already set. -}
setStandardGroup :: UUID -> StandardGroup -> Annex ()
setStandardGroup u g = do
groupSet u $ S.singleton $ fromStandardGroup g
m <- preferredContentMap
unless (isJust $ M.lookup u m) $
preferredContentSet u "standard"