git-annex/Command/DropUnused.hs

80 lines
2.1 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 qualified Data.Map as M
2011-10-05 20:02:51 +00:00
import Common.Annex
2010-11-15 22:06:21 +00:00
import Command
import qualified Annex
import qualified Command.Drop
2011-04-03 00:59:41 +00:00
import qualified Remote
import qualified Git
import Types.Key
2010-11-15 22:06:21 +00:00
type UnusedMap = M.Map String Key
def :: [Command]
def = [dontCheck fromOpt $ command "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) -> FilePath -> CommandStart
start (unused, unusedbad, unusedtmp) s = search
[ (unused, perform)
, (unusedbad, performOther gitAnnexBadLocation)
, (unusedtmp, performOther gitAnnexTmpLocation)
]
where
2011-05-15 06:02:46 +00:00
search [] = stop
2011-07-15 16:47:14 +00:00
search ((m, a):rest) =
case M.lookup s m of
Nothing -> search rest
Just key -> do
showStart "dropunused" s
2011-05-15 06:02:46 +00:00
next $ a key
2011-04-03 00:59:41 +00:00
perform :: Key -> CommandPerform
2011-05-15 06:49:43 +00:00
perform key = maybe droplocal dropremote =<< Annex.getState Annex.fromremote
where
dropremote name = do
r <- Remote.byName name
showAction $ "from " ++ Remote.name r
next $ Command.Drop.cleanupRemote key r
droplocal = Command.Drop.performLocal key (Just 0) -- force drop
2011-04-03 00:59:41 +00:00
performOther :: (Git.Repo -> Key -> FilePath) -> Key -> CommandPerform
performOther filespec key = do
g <- gitRepo
let f = filespec g key
liftIO $ whenM (doesFileExist f) $ removeFile f
2011-05-15 06:02:46 +00:00
next $ return True
2010-11-15 22:06:21 +00:00
readUnusedLog :: FilePath -> Annex UnusedMap
readUnusedLog prefix = do
g <- gitRepo
let f = gitAnnexUnusedLog prefix g
e <- liftIO $ doesFileExist f
if e
2011-10-31 20:46:51 +00:00
then M.fromList . map parse . lines <$> liftIO (readFile f)
2011-07-15 16:47:14 +00:00
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