2012-10-03 21:04:52 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Vicfg where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import System.Environment (getEnv)
|
|
|
|
import Data.Tuple (swap)
|
2012-10-03 23:13:21 +00:00
|
|
|
import Data.Char (isSpace)
|
2012-10-03 21:04:52 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import Annex.Perms
|
|
|
|
import Types.TrustLevel
|
|
|
|
import Types.Group
|
|
|
|
import Logs.Trust
|
|
|
|
import Logs.Group
|
2012-10-04 19:48:59 +00:00
|
|
|
import Logs.PreferredContent
|
2013-10-07 21:11:13 +00:00
|
|
|
import Logs.Schedule
|
2013-05-23 18:54:59 +00:00
|
|
|
import Types.StandardGroups
|
2013-10-07 21:11:13 +00:00
|
|
|
import Types.ScheduledActivity
|
2012-10-03 21:04:52 +00:00
|
|
|
import Remote
|
|
|
|
|
|
|
|
def :: [Command]
|
|
|
|
def = [command "vicfg" paramNothing seek
|
2013-03-24 22:28:21 +00:00
|
|
|
SectionSetup "edit git-annex's configuration"]
|
2012-10-03 21:04:52 +00:00
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek = withNothing start
|
2012-10-03 21:04:52 +00:00
|
|
|
|
|
|
|
start :: CommandStart
|
|
|
|
start = do
|
|
|
|
f <- fromRepo gitAnnexTmpCfgFile
|
2012-10-03 23:13:21 +00:00
|
|
|
createAnnexDirectory $ parentDir f
|
|
|
|
cfg <- getCfg
|
2012-10-13 03:11:26 +00:00
|
|
|
descs <- uuidDescriptions
|
|
|
|
liftIO $ writeFile f $ genCfg cfg descs
|
2012-10-03 23:13:21 +00:00
|
|
|
vicfg cfg f
|
2012-10-03 21:04:52 +00:00
|
|
|
stop
|
|
|
|
|
2012-10-03 23:13:21 +00:00
|
|
|
vicfg :: Cfg -> FilePath -> Annex ()
|
|
|
|
vicfg curcfg f = do
|
2012-10-03 21:04:52 +00:00
|
|
|
vi <- liftIO $ catchDefaultIO "vi" $ getEnv "EDITOR"
|
|
|
|
-- Allow EDITOR to be processed by the shell, so it can contain options.
|
2012-12-18 16:19:24 +00:00
|
|
|
unlessM (liftIO $ boolSystem "sh" [Param "-c", Param $ unwords [vi, shellEscape f]]) $
|
2012-10-03 21:04:52 +00:00
|
|
|
error $ vi ++ " exited nonzero; aborting"
|
2012-10-03 23:13:21 +00:00
|
|
|
r <- parseCfg curcfg <$> liftIO (readFileStrict f)
|
2012-10-03 21:04:52 +00:00
|
|
|
liftIO $ nukeFile f
|
|
|
|
case r of
|
|
|
|
Left s -> do
|
|
|
|
liftIO $ writeFile f s
|
2012-10-03 23:13:21 +00:00
|
|
|
vicfg curcfg f
|
2012-10-03 23:37:39 +00:00
|
|
|
Right newcfg -> setCfg curcfg newcfg
|
2012-10-03 21:04:52 +00:00
|
|
|
|
|
|
|
data Cfg = Cfg
|
|
|
|
{ cfgTrustMap :: TrustMap
|
|
|
|
, cfgGroupMap :: M.Map UUID (S.Set Group)
|
2012-10-04 19:48:59 +00:00
|
|
|
, cfgPreferredContentMap :: M.Map UUID String
|
2013-10-07 21:11:13 +00:00
|
|
|
, cfgScheduleMap :: M.Map UUID [ScheduledActivity]
|
2012-10-03 21:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getCfg :: Annex Cfg
|
|
|
|
getCfg = Cfg
|
|
|
|
<$> trustMapRaw -- without local trust overrides
|
|
|
|
<*> (groupsByUUID <$> groupMap)
|
2012-10-04 19:48:59 +00:00
|
|
|
<*> preferredContentMapRaw
|
2013-10-07 21:11:13 +00:00
|
|
|
<*> scheduleMap
|
2012-10-03 21:04:52 +00:00
|
|
|
|
2012-10-03 23:37:39 +00:00
|
|
|
setCfg :: Cfg -> Cfg -> Annex ()
|
|
|
|
setCfg curcfg newcfg = do
|
2013-10-07 21:11:13 +00:00
|
|
|
let (trustchanges, groupchanges, preferredcontentchanges, schedulechanges) = diffCfg curcfg newcfg
|
2012-10-04 19:48:59 +00:00
|
|
|
mapM_ (uncurry trustSet) $ M.toList trustchanges
|
|
|
|
mapM_ (uncurry groupSet) $ M.toList groupchanges
|
|
|
|
mapM_ (uncurry preferredContentSet) $ M.toList preferredcontentchanges
|
2013-10-07 21:11:13 +00:00
|
|
|
mapM_ (uncurry scheduleSet) $ M.toList schedulechanges
|
2012-10-03 23:37:39 +00:00
|
|
|
|
2013-10-07 21:11:13 +00:00
|
|
|
diffCfg :: Cfg -> Cfg -> (TrustMap, M.Map UUID (S.Set Group), M.Map UUID String, M.Map UUID [ScheduledActivity])
|
|
|
|
diffCfg curcfg newcfg = (diff cfgTrustMap, diff cfgGroupMap, diff cfgPreferredContentMap, diff cfgScheduleMap)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
diff f = M.differenceWith (\x y -> if x == y then Nothing else Just x)
|
|
|
|
(f newcfg) (f curcfg)
|
2012-10-03 21:04:52 +00:00
|
|
|
|
2012-10-13 03:11:26 +00:00
|
|
|
genCfg :: Cfg -> M.Map UUID String -> String
|
2013-10-07 21:11:13 +00:00
|
|
|
genCfg cfg descs = unlines $ concat
|
|
|
|
[intro, trust, groups, preferredcontent, schedule]
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
intro =
|
|
|
|
[ com "git-annex configuration"
|
|
|
|
, com ""
|
|
|
|
, com "Changes saved to this file will be recorded in the git-annex branch."
|
|
|
|
, com ""
|
|
|
|
, com "Lines in this file have the format:"
|
|
|
|
, com " setting uuid = value"
|
|
|
|
]
|
|
|
|
|
|
|
|
trust = settings cfgTrustMap
|
|
|
|
[ ""
|
|
|
|
, com "Repository trust configuration"
|
2013-05-23 18:54:59 +00:00
|
|
|
, com "(Valid trust levels: " ++ trustlevels ++ ")"
|
2012-11-12 05:05:04 +00:00
|
|
|
]
|
|
|
|
(\(t, u) -> line "trust" u $ showTrustLevel t)
|
|
|
|
(\u -> lcom $ line "trust" u $ showTrustLevel SemiTrusted)
|
2013-05-23 18:54:59 +00:00
|
|
|
where
|
|
|
|
trustlevels = unwords $ map showTrustLevel [Trusted .. DeadTrusted]
|
2012-11-12 05:05:04 +00:00
|
|
|
|
|
|
|
groups = settings cfgGroupMap
|
|
|
|
[ ""
|
|
|
|
, com "Repository groups"
|
2013-05-23 18:54:59 +00:00
|
|
|
, com $ "(Standard groups: " ++ grouplist ++ ")"
|
2012-11-12 05:05:04 +00:00
|
|
|
, com "(Separate group names with spaces)"
|
|
|
|
]
|
|
|
|
(\(s, u) -> line "group" u $ unwords $ S.toList s)
|
|
|
|
(\u -> lcom $ line "group" u "")
|
2013-05-23 18:54:59 +00:00
|
|
|
where
|
|
|
|
grouplist = unwords $ map fromStandardGroup [minBound..]
|
2012-11-12 05:05:04 +00:00
|
|
|
|
|
|
|
preferredcontent = settings cfgPreferredContentMap
|
|
|
|
[ ""
|
|
|
|
, com "Repository preferred contents"
|
|
|
|
]
|
2013-05-25 16:44:58 +00:00
|
|
|
(\(s, u) -> line "content" u s)
|
|
|
|
(\u -> line "content" u "")
|
2012-11-12 05:05:04 +00:00
|
|
|
|
2013-10-07 21:11:13 +00:00
|
|
|
schedule = settings cfgScheduleMap
|
|
|
|
[ ""
|
|
|
|
, com "Scheduled activities"
|
|
|
|
, com "(Separate multiple activities with \"; \")"
|
|
|
|
]
|
2013-10-13 19:40:38 +00:00
|
|
|
(\(l, u) -> line "schedule" u $ fromScheduledActivities l)
|
2013-10-07 21:11:13 +00:00
|
|
|
(\u -> line "schedule" u "")
|
|
|
|
|
2012-11-12 05:05:04 +00:00
|
|
|
settings field desc showvals showdefaults = concat
|
|
|
|
[ desc
|
|
|
|
, concatMap showvals $ sort $ map swap $ M.toList $ field cfg
|
2013-09-25 07:09:06 +00:00
|
|
|
, concatMap (lcom . showdefaults) $ missing field
|
2012-11-12 05:05:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
line setting u value =
|
2013-09-25 07:09:06 +00:00
|
|
|
[ com $ "(for " ++ fromMaybe "" (M.lookup u descs) ++ ")"
|
2012-11-12 05:05:04 +00:00
|
|
|
, unwords [setting, fromUUID u, "=", value]
|
|
|
|
]
|
2013-09-25 07:09:06 +00:00
|
|
|
lcom = map (\l -> if "#" `isPrefixOf` l then l else '#' : l)
|
2012-11-12 05:05:04 +00:00
|
|
|
missing field = S.toList $ M.keysSet descs `S.difference` M.keysSet (field cfg)
|
2012-10-03 21:04:52 +00:00
|
|
|
|
|
|
|
{- If there's a parse error, returns a new version of the file,
|
|
|
|
- with the problem lines noted. -}
|
2012-10-03 23:13:21 +00:00
|
|
|
parseCfg :: Cfg -> String -> Either String Cfg
|
|
|
|
parseCfg curcfg = go [] curcfg . lines
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go c cfg []
|
2013-09-25 07:09:06 +00:00
|
|
|
| null (mapMaybe fst c) = Right cfg
|
2012-11-12 05:05:04 +00:00
|
|
|
| otherwise = Left $ unlines $
|
|
|
|
badheader ++ concatMap showerr (reverse c)
|
|
|
|
go c cfg (l:ls) = case parse (dropWhile isSpace l) cfg of
|
|
|
|
Left msg -> go ((Just msg, l):c) cfg ls
|
|
|
|
Right cfg' -> go ((Nothing, l):c) cfg' ls
|
|
|
|
|
|
|
|
parse l cfg
|
|
|
|
| null l = Right cfg
|
|
|
|
| "#" `isPrefixOf` l = Right cfg
|
|
|
|
| null setting || null u = Left "missing repository uuid"
|
|
|
|
| otherwise = handle cfg (toUUID u) setting value'
|
|
|
|
where
|
|
|
|
(setting, rest) = separate isSpace l
|
|
|
|
(r, value) = separate (== '=') rest
|
|
|
|
value' = trimspace value
|
|
|
|
u = reverse $ trimspace $ reverse $ trimspace r
|
|
|
|
trimspace = dropWhile isSpace
|
|
|
|
|
|
|
|
handle cfg u setting value
|
|
|
|
| setting == "trust" = case readTrustLevel value of
|
|
|
|
Nothing -> badval "trust value" value
|
|
|
|
Just t ->
|
|
|
|
let m = M.insert u t (cfgTrustMap cfg)
|
|
|
|
in Right $ cfg { cfgTrustMap = m }
|
|
|
|
| setting == "group" =
|
|
|
|
let m = M.insert u (S.fromList $ words value) (cfgGroupMap cfg)
|
|
|
|
in Right $ cfg { cfgGroupMap = m }
|
2013-05-25 16:44:58 +00:00
|
|
|
| setting == "content" =
|
2012-11-12 05:05:04 +00:00
|
|
|
case checkPreferredContentExpression value of
|
|
|
|
Just e -> Left e
|
|
|
|
Nothing ->
|
|
|
|
let m = M.insert u value (cfgPreferredContentMap cfg)
|
|
|
|
in Right $ cfg { cfgPreferredContentMap = m }
|
2013-10-13 19:40:38 +00:00
|
|
|
| setting == "schedule" = case parseScheduledActivities value of
|
|
|
|
Left e -> Left e
|
|
|
|
Right l ->
|
|
|
|
let m = M.insert u l (cfgScheduleMap cfg)
|
|
|
|
in Right $ cfg { cfgScheduleMap = m }
|
2012-11-12 05:05:04 +00:00
|
|
|
| otherwise = badval "setting" setting
|
|
|
|
|
|
|
|
showerr (Just msg, l) = [parseerr ++ msg, l]
|
|
|
|
showerr (Nothing, l)
|
|
|
|
-- filter out the header and parse error lines
|
|
|
|
-- from any previous parse failure
|
|
|
|
| any (`isPrefixOf` l) (parseerr:badheader) = []
|
|
|
|
| otherwise = [l]
|
|
|
|
|
|
|
|
badval desc val = Left $ "unknown " ++ desc ++ " \"" ++ val ++ "\""
|
|
|
|
badheader =
|
|
|
|
[ com "There was a problem parsing your input."
|
|
|
|
, com "Search for \"Parse error\" to find the bad lines."
|
|
|
|
, com "Either fix the bad lines, or delete them (to discard your changes)."
|
|
|
|
]
|
|
|
|
parseerr = com "Parse error in next line: "
|
2012-10-03 23:13:21 +00:00
|
|
|
|
|
|
|
com :: String -> String
|
|
|
|
com s = "# " ++ s
|