2012-10-01 19:12:04 +00:00
|
|
|
{- git-annex group log
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Group (
|
2012-10-01 19:17:21 +00:00
|
|
|
groupChange,
|
2012-10-04 19:48:59 +00:00
|
|
|
groupSet,
|
2012-10-01 19:12:04 +00:00
|
|
|
lookupGroups,
|
|
|
|
groupMap,
|
2012-10-10 23:13:49 +00:00
|
|
|
getStandardGroup,
|
2012-10-01 19:12:04 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex
|
|
|
|
import Logs.UUIDBased
|
|
|
|
import Types.Group
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-01 19:12:04 +00:00
|
|
|
|
|
|
|
{- Filename of group.log. -}
|
|
|
|
groupLog :: FilePath
|
|
|
|
groupLog = "group.log"
|
|
|
|
|
|
|
|
{- Returns the groups of a given repo UUID. -}
|
|
|
|
lookupGroups :: UUID -> Annex (S.Set Group)
|
2012-10-02 17:45:30 +00:00
|
|
|
lookupGroups u = (fromMaybe S.empty . M.lookup u) . groupsByUUID <$> groupMap
|
2012-10-01 19:12:04 +00:00
|
|
|
|
2012-10-01 19:17:21 +00:00
|
|
|
{- Applies a set modifier to change the groups for a uuid in the groupLog. -}
|
|
|
|
groupChange :: UUID -> (S.Set Group -> S.Set Group) -> Annex ()
|
|
|
|
groupChange uuid@(UUID _) modifier = do
|
|
|
|
curr <- lookupGroups uuid
|
2012-10-01 19:12:04 +00:00
|
|
|
ts <- liftIO getPOSIXTime
|
|
|
|
Annex.Branch.change groupLog $
|
2012-10-01 19:17:21 +00:00
|
|
|
showLog (unwords . S.toList) .
|
|
|
|
changeLog ts uuid (modifier curr) .
|
|
|
|
parseLog (Just . S.fromList . words)
|
2012-10-01 19:12:04 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.groupmap = Nothing }
|
2012-10-04 19:48:59 +00:00
|
|
|
groupChange NoUUID _ = error "unknown UUID; cannot modify"
|
|
|
|
|
|
|
|
groupSet :: UUID -> S.Set Group -> Annex ()
|
|
|
|
groupSet u g = groupChange u (const g)
|
2012-10-01 19:12:04 +00:00
|
|
|
|
|
|
|
{- Read the groupLog into a map. The map is cached for speed. -}
|
|
|
|
groupMap :: Annex GroupMap
|
|
|
|
groupMap = do
|
|
|
|
cached <- Annex.getState Annex.groupmap
|
|
|
|
case cached of
|
|
|
|
Just m -> return m
|
|
|
|
Nothing -> do
|
2012-10-02 17:45:30 +00:00
|
|
|
m <- makeGroupMap . simpleMap .
|
|
|
|
parseLog (Just . S.fromList . words) <$>
|
|
|
|
Annex.Branch.get groupLog
|
2012-10-01 19:12:04 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.groupmap = Just m }
|
|
|
|
return m
|
2012-10-02 17:45:30 +00:00
|
|
|
|
|
|
|
makeGroupMap :: M.Map UUID (S.Set Group) -> GroupMap
|
|
|
|
makeGroupMap byuuid = GroupMap byuuid bygroup
|
|
|
|
where
|
|
|
|
bygroup = M.fromListWith S.union $
|
|
|
|
concat $ map explode $ M.toList byuuid
|
|
|
|
explode (u, s) = map (\g -> (g, S.singleton u)) (S.toList s)
|
2012-10-10 20:04:28 +00:00
|
|
|
|
|
|
|
{- If a repository is in exactly one standard group, returns it. -}
|
2012-10-10 20:23:41 +00:00
|
|
|
getStandardGroup :: S.Set Group -> Maybe StandardGroup
|
|
|
|
getStandardGroup s = case catMaybes $ map toStandardGroup $ S.toList s of
|
|
|
|
[g] -> Just g
|
|
|
|
_ -> Nothing
|