golfing
This commit is contained in:
parent
7724f895a8
commit
1c9c9a0cee
2 changed files with 14 additions and 19 deletions
|
@ -23,7 +23,6 @@ module LocationLog (
|
|||
) where
|
||||
|
||||
import System.FilePath
|
||||
import Control.Monad (when)
|
||||
import Control.Applicative
|
||||
import Data.Maybe
|
||||
|
||||
|
@ -36,16 +35,16 @@ import PresenceLog
|
|||
|
||||
{- Log a change in the presence of a key's value in a repository. -}
|
||||
logChange :: Git.Repo -> Key -> UUID -> LogStatus -> Annex ()
|
||||
logChange repo key u s = do
|
||||
when (null u) $
|
||||
error $ "unknown UUID for " ++ Git.repoDescribe repo ++
|
||||
" (have you run git annex init there?)"
|
||||
addLog (logFile key) =<< logNow s u
|
||||
logChange repo key u s
|
||||
| null u = error $
|
||||
"unknown UUID for " ++ Git.repoDescribe repo ++
|
||||
" (have you run git annex init there?)"
|
||||
| otherwise = addLog (logFile key) =<< logNow s u
|
||||
|
||||
{- Returns a list of repository UUIDs that, according to the log, have
|
||||
- the value of a key. -}
|
||||
keyLocations :: Key -> Annex [UUID]
|
||||
keyLocations key = currentLog $ logFile key
|
||||
keyLocations = currentLog . logFile
|
||||
|
||||
{- Finds all keys that have location log information.
|
||||
- (There may be duplicate keys in the list.) -}
|
||||
|
|
|
@ -85,7 +85,7 @@ readLog :: FilePath -> Annex [LogLine]
|
|||
readLog file = parseLog <$> Branch.get file
|
||||
|
||||
parseLog :: String -> [LogLine]
|
||||
parseLog s = filter parsable $ map read $ lines s
|
||||
parseLog = filter parsable . map read . lines
|
||||
where
|
||||
-- some lines may be unparseable, avoid them
|
||||
parsable l = status l /= Undefined
|
||||
|
@ -102,23 +102,18 @@ logNow s i = do
|
|||
|
||||
{- Reads a log and returns only the info that is still in effect. -}
|
||||
currentLog :: FilePath -> Annex [String]
|
||||
currentLog file = do
|
||||
ls <- readLog file
|
||||
return $ map info $ filterPresent ls
|
||||
currentLog file = map info . filterPresent <$> readLog file
|
||||
|
||||
{- Returns the info from LogLines that are in effect. -}
|
||||
filterPresent :: [LogLine] -> [LogLine]
|
||||
filterPresent ls = filter (\l -> InfoPresent == status l) $ compactLog ls
|
||||
|
||||
type LogMap = Map.Map String LogLine
|
||||
filterPresent = filter (\l -> InfoPresent == status l) . compactLog
|
||||
|
||||
{- Compacts a set of logs, returning a subset that contains the current
|
||||
- status. -}
|
||||
compactLog :: [LogLine] -> [LogLine]
|
||||
compactLog = compactLog' Map.empty
|
||||
compactLog' :: LogMap -> [LogLine] -> [LogLine]
|
||||
compactLog' m [] = Map.elems m
|
||||
compactLog' m (l:ls) = compactLog' (mapLog m l) ls
|
||||
compactLog = Map.elems . foldl mapLog Map.empty
|
||||
|
||||
type LogMap = Map.Map String LogLine
|
||||
|
||||
{- Inserts a log into a map of logs, if the log has better (ie, newer)
|
||||
- information than the other logs in the map -}
|
||||
|
@ -128,5 +123,6 @@ mapLog m l =
|
|||
then Map.insert i l m
|
||||
else m
|
||||
where
|
||||
better = maybe True (\l' -> date l' <= date l) $ Map.lookup i m
|
||||
better = maybe True newer $ Map.lookup i m
|
||||
newer l' = date l' <= date l
|
||||
i = info l
|
||||
|
|
Loading…
Add table
Reference in a new issue