summaryrefslogtreecommitdiff
path: root/Remote.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-03-29 13:49:54 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-03-29 14:10:12 -0400
commit05751d55cd8002e6a2a2afc520622fb6697472e3 (patch)
tree6a86d2ebfd6888f611d86ec33984ab512d28e087 /Remote.hs
parenta3b6586902d6689b07c050b1fc50e19f4115c42e (diff)
clean up remote.log handling
Diffstat (limited to 'Remote.hs')
-rw-r--r--Remote.hs67
1 files changed, 32 insertions, 35 deletions
diff --git a/Remote.hs b/Remote.hs
index 71bc08c8a..f79b51262 100644
--- a/Remote.hs
+++ b/Remote.hs
@@ -21,7 +21,7 @@ module Remote (
remotesWithUUID,
remotesWithoutUUID,
- configGet,
+ readRemoteLog,
configSet,
keyValToMap
) where
@@ -71,7 +71,8 @@ genList = do
if null rs
then do
rs' <- runGenerators
- Annex.changeState $ \s -> s { Annex.remotes = rs' }
+ rs'' <- getConfigs rs'
+ Annex.changeState $ \s -> s { Annex.remotes = rs'' }
return rs'
else return rs
@@ -132,51 +133,47 @@ remoteLog = do
g <- Annex.gitRepo
return $ gitStateDir g ++ "remote.log"
-{- Reads the uuid and config of the specified remote from the remoteLog. -}
-configGet :: String -> Annex (Maybe (UUID, M.Map String String))
-configGet n = do
- rs <- readRemoteLog
- let matches = filter (matchName n) rs
- case matches of
- [] -> return Nothing
- ((u, _, c):_) -> return $ Just (u, c)
-
-{- Changes or adds a remote's config in the remoteLog. -}
-configSet :: String -> UUID -> M.Map String String -> Annex ()
-configSet n u c = do
- rs <- readRemoteLog
- let others = filter (not . matchName n) rs
- writeRemoteLog $ (u, n, c):others
-
-matchName :: String -> (UUID, String, M.Map String String) -> Bool
-matchName n (_, n', _) = n == n'
-
-readRemoteLog :: Annex [(UUID, String, M.Map String String)]
-readRemoteLog = do
+{- Load stored config into remotes.
+ -
+ - This way, the log is read once, lazily, so if no remotes access
+ - their config, no work is done.
+ -}
+getConfigs :: [Remote Annex] -> Annex [Remote Annex]
+getConfigs rs = do
+ m <- readRemoteLog
+ return $ map (get m) rs
+ where
+ get m r = r { config = M.lookup (uuid r) m }
+
+{- Adds or updates a remote's config in the log. -}
+configSet :: UUID -> M.Map String String -> Annex ()
+configSet u c = do
+ m <- readRemoteLog
l <- remoteLog
- s <- liftIO $ catch (readFile l) ignoreerror
- return $ remoteLogParse s
+ liftIO $ writeFile l $ unlines $ map toline $ M.toList $ M.insert u c m
where
- ignoreerror _ = return []
+ toline (u', c') = u' ++ " " ++ (unwords $ mapToKeyVal c')
-writeRemoteLog :: [(UUID, String, M.Map String String)] -> Annex ()
-writeRemoteLog rs = do
+{- Map of remotes by uuid containing key/value config maps. -}
+readRemoteLog :: Annex (M.Map UUID (M.Map String String))
+readRemoteLog = do
l <- remoteLog
- liftIO $ writeFile l $ unlines $ map toline rs
+ s <- liftIO $ catch (readFile l) ignoreerror
+ return $ remoteLogParse s
where
- toline (u, n, c) = u ++ " " ++ n ++ (unwords $ mapToKeyVal c)
+ ignoreerror _ = return ""
-remoteLogParse :: String -> [(UUID, String, M.Map String String)]
-remoteLogParse s = catMaybes $ map parseline $ filter (not . null) $ lines s
+remoteLogParse :: String -> M.Map UUID (M.Map String String)
+remoteLogParse s =
+ M.fromList $ catMaybes $ map parseline $ filter (not . null) $ lines s
where
parseline l
- | length w > 2 = Just (u, n, c)
+ | length w > 2 = Just (u, c)
| otherwise = Nothing
where
w = words l
u = w !! 0
- n = w !! 1
- c = keyValToMap $ drop 2 w
+ c = keyValToMap $ tail w
{- Given Strings like "key=value", generates a Map. -}
keyValToMap :: [String] -> M.Map String String