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
|
|
|
|
|
2011-01-28 18:10:50 +00:00
|
|
|
import Control.Monad (when)
|
2010-11-15 22:06:21 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
|
|
|
import qualified Data.Map as M
|
2011-01-12 05:57:49 +00:00
|
|
|
import System.Directory
|
2011-03-16 01:34:13 +00:00
|
|
|
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
|
2010-11-15 22:06:21 +00:00
|
|
|
import Backend
|
2011-03-16 01:34:13 +00:00
|
|
|
import Key
|
2010-11-15 22:06:21 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "dropunused" (paramRepeating paramNumber) seek
|
2010-12-30 19:06:26 +00:00
|
|
|
"drop unused file content"]
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2011-03-24 03:47:02 +00:00
|
|
|
seek = [withUnusedMap]
|
2010-11-15 22:06:21 +00:00
|
|
|
|
2011-03-24 03:47:02 +00:00
|
|
|
{- Read unusedlog once, and pass the map to each start action. -}
|
|
|
|
withUnusedMap :: CommandSeek
|
|
|
|
withUnusedMap params = do
|
2010-11-15 22:06:21 +00:00
|
|
|
m <- readUnusedLog
|
2011-03-24 03:47:02 +00:00
|
|
|
return $ map (start m) params
|
|
|
|
|
|
|
|
start :: M.Map String Key -> CommandStartString
|
|
|
|
start m s = notBareRepo $ do
|
2010-11-15 22:06:21 +00:00
|
|
|
case M.lookup s m of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just key -> do
|
|
|
|
showStart "dropunused" s
|
2011-04-03 00:59:41 +00:00
|
|
|
from <- Annex.getState Annex.fromremote
|
|
|
|
case from of
|
|
|
|
Just name -> do
|
|
|
|
r <- Remote.byName name
|
|
|
|
return $ Just $ performRemote r key
|
|
|
|
_ -> return $ Just $ perform key
|
|
|
|
|
|
|
|
{- drop both content in the backend and any tmp file for the key -}
|
|
|
|
perform :: Key -> CommandPerform
|
|
|
|
perform key = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let tmp = gitAnnexTmpLocation g key
|
|
|
|
tmp_exists <- liftIO $ doesFileExist tmp
|
|
|
|
when tmp_exists $ liftIO $ removeFile tmp
|
|
|
|
backend <- keyBackend key
|
|
|
|
Command.Drop.perform key backend (Just 0) -- force drop
|
|
|
|
|
|
|
|
performRemote :: Remote.Remote Annex -> Key -> CommandPerform
|
|
|
|
performRemote r key = do
|
|
|
|
showNote $ "from " ++ Remote.name r ++ "..."
|
|
|
|
return $ Just $ Command.Move.fromCleanup r True key
|
2010-11-15 22:06:21 +00:00
|
|
|
|
|
|
|
readUnusedLog :: Annex (M.Map String Key)
|
|
|
|
readUnusedLog = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-27 21:00:32 +00:00
|
|
|
let f = gitAnnexUnusedLog g
|
2011-01-12 05:57:49 +00:00
|
|
|
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
|
2011-03-16 01:34:13 +00:00
|
|
|
parse line = (head ws, fromJust $ readKey $ unwords $ tail ws)
|
2010-11-15 22:06:21 +00:00
|
|
|
where
|
|
|
|
ws = words line
|