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
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
2010-11-15 22:06:21 +00:00
|
|
|
|
2011-04-29 17:59:00 +00:00
|
|
|
type UnusedMap = M.Map String Key
|
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
|
|
|
def = [dontCheck fromOpt $ command "dropunused" (paramRepeating paramNumber)
|
2011-10-27 22:56:54 +00:00
|
|
|
seek "drop unused file content"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2011-04-29 17:59:00 +00:00
|
|
|
seek = [withUnusedMaps]
|
2010-11-15 22:06:21 +00:00
|
|
|
|
2011-04-29 17:59:00 +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
|
2011-03-24 03:47:02 +00:00
|
|
|
|
2011-09-15 20:50:49 +00:00
|
|
|
start :: (UnusedMap, UnusedMap, UnusedMap) -> FilePath -> CommandStart
|
2011-10-29 23:16:45 +00:00
|
|
|
start (unused, unusedbad, unusedtmp) s = search
|
2011-04-29 17:59:00 +00:00
|
|
|
[ (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) =
|
2011-04-29 17:59:00 +00:00
|
|
|
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
|
2011-04-29 17:59:00 +00:00
|
|
|
r <- Remote.byName name
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction $ "from " ++ Remote.name r
|
2011-11-09 20:54:18 +00:00
|
|
|
ok <- Remote.removeKey r key
|
|
|
|
next $ Command.Drop.cleanupRemote key r ok
|
2011-10-28 21:26:38 +00:00
|
|
|
droplocal = Command.Drop.performLocal key (Just 0) -- force drop
|
2011-04-03 00:59:41 +00:00
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
performOther :: (Key -> Git.Repo -> FilePath) -> Key -> CommandPerform
|
2011-04-29 17:59:00 +00:00
|
|
|
performOther filespec key = do
|
2011-11-08 19:34:10 +00:00
|
|
|
f <- fromRepo $ filespec key
|
2011-05-17 07:10:13 +00:00
|
|
|
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
|
|
|
|
2011-04-29 17:59:00 +00:00
|
|
|
readUnusedLog :: FilePath -> Annex UnusedMap
|
|
|
|
readUnusedLog prefix = do
|
2011-11-08 19:34:10 +00:00
|
|
|
f <- fromRepo $ gitAnnexUnusedLog prefix
|
2011-01-12 05:57:49 +00:00
|
|
|
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
|
2011-11-27 17:50:05 +00:00
|
|
|
parse line = (num, fromJust $ readKey $ tail rest)
|
2010-11-15 22:06:21 +00:00
|
|
|
where
|
2011-11-27 17:50:05 +00:00
|
|
|
(num, rest) = break (== ' ') line
|