git-annex/Command/DropUnused.hs

94 lines
2.4 KiB
Haskell
Raw Normal View History

2010-11-15 22:06:21 +00:00
{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.DropUnused where
import Control.Monad (when)
2010-11-15 22:06:21 +00:00
import Control.Monad.State (liftIO)
import qualified Data.Map as M
import System.Directory
import Data.Maybe
2010-11-15 22:06:21 +00:00
import Command
import Types
import Messages
import Locations
import qualified Annex
import qualified Command.Drop
2011-04-03 00:59:41 +00:00
import qualified Command.Move
import qualified Remote
import qualified GitRepo as Git
2010-11-15 22:06:21 +00:00
import Backend
import Key
2010-11-15 22:06:21 +00:00
type UnusedMap = M.Map String Key
command :: [Command]
command = [repoCommand "dropunused" (paramRepeating paramNumber) seek
"drop unused file content"]
seek :: [CommandSeek]
seek = [withUnusedMaps]
2010-11-15 22:06:21 +00:00
{- Read unused logs once, and pass the maps to each start action. -}
withUnusedMaps :: CommandSeek
withUnusedMaps params = do
unused <- readUnusedLog ""
unusedbad <- readUnusedLog "bad"
unusedtmp <- readUnusedLog "tmp"
return $ map (start (unused, unusedbad, unusedtmp)) params
start :: (UnusedMap, UnusedMap, UnusedMap) -> CommandStartString
start (unused, unusedbad, unusedtmp) s = notBareRepo $ search
[ (unused, perform)
, (unusedbad, performOther gitAnnexBadLocation)
, (unusedtmp, performOther gitAnnexTmpLocation)
]
where
search [] = return Nothing
search ((m, a):rest) = do
case M.lookup s m of
Nothing -> search rest
Just key -> do
showStart "dropunused" s
return $ Just $ a key
2011-04-03 00:59:41 +00:00
perform :: Key -> CommandPerform
perform key = do
from <- Annex.getState Annex.fromremote
case from of
Just name -> do
r <- Remote.byName name
showNote $ "from " ++ Remote.name r ++ "..."
return $ Just $ Command.Move.fromCleanup r True key
_ -> do
backend <- keyBackend key
Command.Drop.perform key backend (Just 0) -- force drop
2011-04-03 00:59:41 +00:00
performOther :: (Git.Repo -> Key -> FilePath) -> Key -> CommandPerform
performOther filespec key = do
g <- Annex.gitRepo
let f = filespec g key
e <- liftIO $ doesFileExist f
when e $ liftIO $ removeFile f
return $ Just $ return True
2010-11-15 22:06:21 +00:00
readUnusedLog :: FilePath -> Annex UnusedMap
readUnusedLog prefix = do
2010-11-15 22:06:21 +00:00
g <- Annex.gitRepo
let f = gitAnnexUnusedLog prefix g
e <- liftIO $ doesFileExist f
if e
then do
l <- liftIO $ readFile f
return $ M.fromList $ map parse $ lines l
else return $ M.empty
2010-11-15 22:06:21 +00:00
where
parse line = (head ws, fromJust $ readKey $ unwords $ tail ws)
2010-11-15 22:06:21 +00:00
where
ws = words line