2010-11-15 20:35:06 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2011-09-29 00:12:11 +00:00
|
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
2010-11-15 20:35:06 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-08-31 23:13:02 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2010-11-15 20:35:06 +00:00
|
|
|
module Command.Unused where
|
|
|
|
|
2011-01-30 05:41:15 +00:00
|
|
|
import qualified Data.Set as S
|
2011-09-30 03:43:42 +00:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-15 20:35:06 +00:00
|
|
|
import Command
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-09-28 20:43:10 +00:00
|
|
|
import Utility.FileMode
|
2011-10-16 04:31:25 +00:00
|
|
|
import Utility.TempFile
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Location
|
2010-11-15 22:04:19 +00:00
|
|
|
import qualified Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import qualified Git.Command
|
2011-12-12 22:23:24 +00:00
|
|
|
import qualified Git.Ref
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2011-09-28 20:43:10 +00:00
|
|
|
import qualified Git.LsTree as LsTree
|
2011-01-16 20:05:05 +00:00
|
|
|
import qualified Backend
|
2011-04-03 00:59:41 +00:00
|
|
|
import qualified Remote
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Branch
|
|
|
|
import Annex.CatFile
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
def = [withOptions [fromOption] $ command "unused" paramNothing seek
|
2011-03-19 22:58:49 +00:00
|
|
|
"look for unused file content"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
fromOption :: Option
|
|
|
|
fromOption = fieldOption ['f'] "from" paramRemote "remote to check for unused content"
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
seek = [withNothing $ start]
|
2010-11-15 20:35:06 +00:00
|
|
|
|
|
|
|
{- Finds unused content in the annex. -}
|
2011-09-15 20:50:49 +00:00
|
|
|
start :: CommandStart
|
2011-10-29 23:16:45 +00:00
|
|
|
start = do
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
from <- Annex.getField "from"
|
2011-05-29 02:37:17 +00:00
|
|
|
let (name, action) = case from of
|
|
|
|
Nothing -> (".", checkUnused)
|
|
|
|
Just "." -> (".", checkUnused)
|
|
|
|
Just n -> (n, checkRemoteUnused n)
|
|
|
|
showStart "unused" name
|
|
|
|
next action
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2011-05-29 02:20:22 +00:00
|
|
|
checkUnused :: CommandPerform
|
2010-11-15 20:35:06 +00:00
|
|
|
checkUnused = do
|
2011-04-29 17:59:00 +00:00
|
|
|
(unused, stalebad, staletmp) <- unusedKeys
|
2011-05-29 02:24:48 +00:00
|
|
|
_ <- list "" unusedMsg unused 0 >>=
|
|
|
|
list "bad" staleBadMsg stalebad >>=
|
|
|
|
list "tmp" staleTmpMsg staletmp
|
2011-05-29 02:20:22 +00:00
|
|
|
next $ return True
|
2011-04-29 17:59:00 +00:00
|
|
|
where
|
|
|
|
list file msg l c = do
|
|
|
|
let unusedlist = number c l
|
2011-09-09 05:45:41 +00:00
|
|
|
unless (null l) $ showLongNote $ msg unusedlist
|
2011-04-29 17:59:00 +00:00
|
|
|
writeUnusedFile file unusedlist
|
2011-05-29 02:28:14 +00:00
|
|
|
return $ c + length l
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2011-05-29 02:20:22 +00:00
|
|
|
checkRemoteUnused :: String -> CommandPerform
|
|
|
|
checkRemoteUnused name = do
|
|
|
|
checkRemoteUnused' =<< Remote.byName name
|
|
|
|
next $ return True
|
2011-05-15 06:49:43 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
checkRemoteUnused' :: Remote -> Annex ()
|
2011-05-15 06:49:43 +00:00
|
|
|
checkRemoteUnused' r = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "checking for unused data"
|
2011-10-29 21:49:37 +00:00
|
|
|
remotehas <- loggedKeysFor (Remote.uuid r)
|
2011-09-28 21:35:47 +00:00
|
|
|
remoteunused <- excludeReferenced remotehas
|
2011-04-03 00:59:41 +00:00
|
|
|
let list = number 0 remoteunused
|
2011-04-29 17:59:00 +00:00
|
|
|
writeUnusedFile "" list
|
2011-09-09 05:45:41 +00:00
|
|
|
unless (null remoteunused) $ showLongNote $ remoteUnusedMsg r list
|
2011-04-03 00:59:41 +00:00
|
|
|
|
2011-04-29 17:59:00 +00:00
|
|
|
writeUnusedFile :: FilePath -> [(Int, Key)] -> Annex ()
|
|
|
|
writeUnusedFile prefix l = do
|
2011-11-08 19:34:10 +00:00
|
|
|
logfile <- fromRepo $ gitAnnexUnusedLog prefix
|
|
|
|
liftIO $ viaTmp writeFile logfile $
|
2011-04-03 00:59:41 +00:00
|
|
|
unlines $ map (\(n, k) -> show n ++ " " ++ show k) l
|
|
|
|
|
|
|
|
table :: [(Int, Key)] -> [String]
|
2011-07-15 16:47:14 +00:00
|
|
|
table l = " NUMBER KEY" : map cols l
|
2010-11-15 20:35:06 +00:00
|
|
|
where
|
2011-03-12 19:30:17 +00:00
|
|
|
cols (n,k) = " " ++ pad 6 (show n) ++ " " ++ show k
|
2010-11-15 22:04:19 +00:00
|
|
|
pad n s = s ++ replicate (n - length s) ' '
|
|
|
|
|
2011-01-28 18:10:50 +00:00
|
|
|
number :: Int -> [a] -> [(Int, a)]
|
2010-11-15 22:04:19 +00:00
|
|
|
number _ [] = []
|
2011-07-15 16:47:14 +00:00
|
|
|
number n (x:xs) = (n+1, x) : number (n+1) xs
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2011-04-03 00:59:41 +00:00
|
|
|
staleTmpMsg :: [(Int, Key)] -> String
|
|
|
|
staleTmpMsg t = unlines $
|
|
|
|
["Some partially transferred data exists in temporary files:"]
|
|
|
|
++ table t ++ [dropMsg Nothing]
|
2011-04-29 17:59:00 +00:00
|
|
|
|
|
|
|
staleBadMsg :: [(Int, Key)] -> String
|
|
|
|
staleBadMsg t = unlines $
|
|
|
|
["Some corrupted files have been preserved by fsck, just in case:"]
|
|
|
|
++ table t ++ [dropMsg Nothing]
|
|
|
|
|
2011-04-03 00:59:41 +00:00
|
|
|
unusedMsg :: [(Int, Key)] -> String
|
|
|
|
unusedMsg u = unusedMsg' u
|
2011-09-23 22:04:38 +00:00
|
|
|
["Some annexed data is no longer used by any files:"]
|
|
|
|
[dropMsg Nothing]
|
2011-04-03 00:59:41 +00:00
|
|
|
unusedMsg' :: [(Int, Key)] -> [String] -> [String] -> String
|
|
|
|
unusedMsg' u header trailer = unlines $
|
|
|
|
header ++
|
|
|
|
table u ++
|
|
|
|
["(To see where data was previously used, try: git log --stat -S'KEY')"] ++
|
|
|
|
trailer
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
remoteUnusedMsg :: Remote -> [(Int, Key)] -> String
|
2011-09-23 22:04:38 +00:00
|
|
|
remoteUnusedMsg r u = unusedMsg' u
|
|
|
|
["Some annexed data on " ++ name ++ " is not used by any files:"]
|
|
|
|
[dropMsg $ Just r]
|
|
|
|
where
|
|
|
|
name = Remote.name r
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
dropMsg :: Maybe Remote -> String
|
2011-04-03 00:59:41 +00:00
|
|
|
dropMsg Nothing = dropMsg' ""
|
|
|
|
dropMsg (Just r) = dropMsg' $ " --from " ++ Remote.name r
|
|
|
|
dropMsg' :: String -> String
|
2011-06-23 16:23:25 +00:00
|
|
|
dropMsg' s = "\nTo remove unwanted data: git-annex dropunused" ++ s ++ " NUMBER\n"
|
2011-04-03 00:59:41 +00:00
|
|
|
|
2010-11-15 20:35:06 +00:00
|
|
|
{- Finds keys whose content is present, but that do not seem to be used
|
2011-04-29 17:59:00 +00:00
|
|
|
- by any files in the git repo, or that are only present as bad or tmp
|
|
|
|
- files. -}
|
|
|
|
unusedKeys :: Annex ([Key], [Key], [Key])
|
2010-11-15 20:35:06 +00:00
|
|
|
unusedKeys = do
|
2011-03-22 21:41:06 +00:00
|
|
|
fast <- Annex.getState Annex.fast
|
|
|
|
if fast
|
|
|
|
then do
|
2011-04-29 17:59:00 +00:00
|
|
|
showNote "fast mode enabled; only finding stale files"
|
2011-05-17 01:18:34 +00:00
|
|
|
tmp <- staleKeys gitAnnexTmpDir
|
|
|
|
bad <- staleKeys gitAnnexBadDir
|
2011-04-29 17:59:00 +00:00
|
|
|
return ([], bad, tmp)
|
2011-03-22 21:41:06 +00:00
|
|
|
else do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "checking for unused data"
|
2011-03-22 21:41:06 +00:00
|
|
|
present <- getKeysPresent
|
2011-09-28 21:35:47 +00:00
|
|
|
unused <- excludeReferenced present
|
2011-05-17 01:18:34 +00:00
|
|
|
staletmp <- staleKeysPrune gitAnnexTmpDir present
|
|
|
|
stalebad <- staleKeysPrune gitAnnexBadDir present
|
2011-04-29 17:59:00 +00:00
|
|
|
return (unused, stalebad, staletmp)
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2011-09-28 21:35:47 +00:00
|
|
|
{- Finds keys in the list that are not referenced in the git repository. -}
|
|
|
|
excludeReferenced :: [Key] -> Annex [Key]
|
2011-09-28 21:38:41 +00:00
|
|
|
excludeReferenced [] = return [] -- optimisation
|
2011-09-28 21:35:47 +00:00
|
|
|
excludeReferenced l = do
|
2011-12-14 19:56:11 +00:00
|
|
|
c <- inRepo $ Git.Command.pipeRead [Param "show-ref"]
|
2011-09-29 00:12:11 +00:00
|
|
|
removewith (getKeysReferenced : map getKeysReferencedInGit (refs c))
|
2011-09-28 21:35:47 +00:00
|
|
|
(S.fromList l)
|
|
|
|
where
|
|
|
|
-- Skip the git-annex branches, and get all other unique refs.
|
2011-12-15 22:11:42 +00:00
|
|
|
refs = map (Git.Ref . snd) .
|
|
|
|
nubBy uniqref .
|
2011-09-28 21:35:47 +00:00
|
|
|
filter ourbranches .
|
2011-12-15 22:11:42 +00:00
|
|
|
map (separate (== ' ')) . lines . L.unpack
|
|
|
|
uniqref (a, _) (b, _) = a == b
|
2011-12-09 05:57:13 +00:00
|
|
|
ourbranchend = '/' : show Annex.Branch.name
|
2011-12-15 22:11:42 +00:00
|
|
|
ourbranches (_, b) = not $ ourbranchend `isSuffixOf` b
|
2011-09-29 00:12:11 +00:00
|
|
|
removewith [] s = return $ S.toList s
|
|
|
|
removewith (a:as) s
|
|
|
|
| s == S.empty = return [] -- optimisation
|
|
|
|
| otherwise = do
|
|
|
|
referenced <- a
|
|
|
|
let !s' = s `S.difference` S.fromList referenced
|
|
|
|
removewith as s'
|
2011-09-28 21:35:47 +00:00
|
|
|
|
2011-04-03 00:59:41 +00:00
|
|
|
{- Finds items in the first, smaller list, that are not
|
|
|
|
- present in the second, larger list.
|
|
|
|
-
|
|
|
|
- Constructing a single set, of the list that tends to be
|
|
|
|
- smaller, appears more efficient in both memory and CPU
|
|
|
|
- than constructing and taking the S.difference of two sets. -}
|
|
|
|
exclude :: Ord a => [a] -> [a] -> [a]
|
|
|
|
exclude [] _ = [] -- optimisation
|
|
|
|
exclude smaller larger = S.toList $ remove larger $ S.fromList smaller
|
2010-11-15 20:35:06 +00:00
|
|
|
where
|
2011-01-30 05:41:15 +00:00
|
|
|
remove a b = foldl (flip S.delete) b a
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
{- List of keys referenced by symlinks in the git repo. -}
|
|
|
|
getKeysReferenced :: Annex [Key]
|
|
|
|
getKeysReferenced = do
|
2011-11-08 19:34:10 +00:00
|
|
|
top <- fromRepo Git.workTree
|
|
|
|
files <- inRepo $ LsFiles.inRepo [top]
|
2011-01-16 20:05:05 +00:00
|
|
|
keypairs <- mapM Backend.lookupFile files
|
2011-09-28 21:35:47 +00:00
|
|
|
return $ map fst $ catMaybes keypairs
|
2011-09-28 20:43:10 +00:00
|
|
|
|
2011-09-28 21:35:47 +00:00
|
|
|
{- List of keys referenced by symlinks in a git ref. -}
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
getKeysReferencedInGit :: Git.Ref -> Annex [Key]
|
2011-09-28 21:35:47 +00:00
|
|
|
getKeysReferencedInGit ref = do
|
2011-12-12 22:23:24 +00:00
|
|
|
showAction $ "checking " ++ Git.Ref.describe ref
|
2011-11-08 19:34:10 +00:00
|
|
|
findkeys [] =<< inRepo (LsTree.lsTree ref)
|
2011-09-28 20:43:10 +00:00
|
|
|
where
|
2011-09-28 21:35:47 +00:00
|
|
|
findkeys c [] = return c
|
2011-09-29 00:12:11 +00:00
|
|
|
findkeys c (l:ls)
|
|
|
|
| isSymLink (LsTree.mode l) = do
|
|
|
|
content <- catFile ref $ LsTree.file l
|
2011-11-12 21:45:12 +00:00
|
|
|
case fileKey (takeFileName $ L.unpack content) of
|
2011-09-29 00:12:11 +00:00
|
|
|
Nothing -> findkeys c ls
|
|
|
|
Just k -> findkeys (k:c) ls
|
|
|
|
| otherwise = findkeys c ls
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2011-04-29 17:59:00 +00:00
|
|
|
{- Looks in the specified directory for bad/tmp keys, and returns a list
|
|
|
|
- of those that might still have value, or might be stale and removable.
|
|
|
|
-
|
|
|
|
- When a list of presently available keys is provided, stale keys
|
|
|
|
- that no longer have value are deleted.
|
|
|
|
-}
|
2011-05-17 01:18:34 +00:00
|
|
|
staleKeysPrune :: (Git.Repo -> FilePath) -> [Key] -> Annex [Key]
|
|
|
|
staleKeysPrune dirspec present = do
|
|
|
|
contents <- staleKeys dirspec
|
2011-09-23 22:13:24 +00:00
|
|
|
|
2011-04-29 17:59:00 +00:00
|
|
|
let stale = contents `exclude` present
|
2011-10-04 02:24:57 +00:00
|
|
|
let dups = contents `exclude` stale
|
2011-04-29 17:59:00 +00:00
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
dir <- fromRepo dirspec
|
2011-10-04 02:24:57 +00:00
|
|
|
liftIO $ forM_ dups $ \t -> removeFile $ dir </> keyFile t
|
2011-04-29 17:59:00 +00:00
|
|
|
|
|
|
|
return stale
|
|
|
|
|
2011-05-17 01:18:34 +00:00
|
|
|
staleKeys :: (Git.Repo -> FilePath) -> Annex [Key]
|
|
|
|
staleKeys dirspec = do
|
2011-11-08 19:34:10 +00:00
|
|
|
dir <- fromRepo dirspec
|
2011-04-29 17:59:00 +00:00
|
|
|
exists <- liftIO $ doesDirectoryExist dir
|
|
|
|
if not exists
|
2011-01-28 18:10:50 +00:00
|
|
|
then return []
|
|
|
|
else do
|
2011-04-29 17:59:00 +00:00
|
|
|
contents <- liftIO $ getDirectoryContents dir
|
2011-01-28 18:10:50 +00:00
|
|
|
files <- liftIO $ filterM doesFileExist $
|
2011-04-29 17:59:00 +00:00
|
|
|
map (dir </>) contents
|
2011-07-15 16:47:14 +00:00
|
|
|
return $ mapMaybe (fileKey . takeFileName) files
|