git-annex/Command/Unused.hs

342 lines
11 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2010-2016 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
{-# LANGUAGE BangPatterns, OverloadedStrings #-}
module Command.Unused where
import Command
import Logs.Unused
2011-10-04 04:40:47 +00:00
import Annex.Content
2011-10-15 20:21:08 +00:00
import Logs.Location
2010-11-15 22:04:19 +00:00
import qualified Annex
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
import qualified Git.Branch
import qualified Git.RefLog
import qualified Git.LsFiles as LsFiles
import qualified Git.DiffTree as DiffTree
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
import Annex.WorkTree
import Types.RefSpec
import Git.Types
import Git.Sha
import Git.FilePath
2016-06-02 20:59:15 +00:00
import Config
import Logs.View (is_branchView)
import Annex.BloomFilter
import qualified Database.Keys
import Annex.InodeSentinal
import qualified Data.Map as M
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import Data.Char
cmd :: Command
2016-01-07 00:41:25 +00:00
cmd = command "unused" SectionMaintenance "look for unused file content"
paramNothing (seek <$$> optParser)
data UnusedOptions = UnusedOptions
{ fromRemote :: Maybe RemoteName
, refSpecOption :: Maybe RefSpec
}
optParser :: CmdParamsDesc -> Parser UnusedOptions
optParser _ = UnusedOptions
<$> optional (strOption
( long "from" <> short 'f' <> metavar paramRemote
<> help "remote to check for unused content"
))
<*> optional (option (eitherReader parseRefSpec)
( long "used-refspec" <> metavar paramRefSpec
<> help "refs to consider used (default: all branches)"
))
seek :: UnusedOptions -> CommandSeek
seek = commandAction . start
start :: UnusedOptions -> CommandStart
start o = do
2015-05-14 19:44:08 +00:00
cfgrefspec <- fromMaybe allRefSpec . annexUsedRefSpec
<$> Annex.getGitConfig
let refspec = fromMaybe cfgrefspec (refSpecOption o)
let (name, perform) = case fromRemote o of
Nothing -> (".", checkUnused refspec)
Just "." -> (".", checkUnused refspec)
Just "here" -> (".", checkUnused refspec)
Just n -> (n, checkRemoteUnused n refspec)
starting "unused" (ActionItemOther (Just name)) (SeekInput []) perform
checkUnused :: RefSpec -> CommandPerform
checkUnused refspec = chain 0
2012-03-12 01:08:48 +00:00
[ check "" unusedMsg $ findunused =<< Annex.getState Annex.fast
, check "bad" staleBadMsg $ staleKeysPrune gitAnnexBadDir False
, check "tmp" staleTmpMsg $ staleKeysPrune gitAnnexTmpObjectDir True
2012-03-12 01:08:48 +00:00
]
2012-11-12 05:05:04 +00:00
where
findunused True = do
showNote "fast mode enabled; only finding stale files"
return []
findunused False = do
showAction "checking for unused data"
excludeReferenced refspec =<< listKeys InAnnex
2012-11-12 05:05:04 +00:00
chain _ [] = next $ return True
chain v (a:as) = do
v' <- a v
chain v' as
checkRemoteUnused :: RemoteName -> RefSpec -> CommandPerform
checkRemoteUnused remotename refspec = go =<< Remote.nameToUUID remotename
2012-11-12 05:05:04 +00:00
where
go u = do
2012-11-12 05:05:04 +00:00
showAction "checking for unused data"
r <- Remote.byUUID u
_ <- check "" (remoteUnusedMsg r remotename) (remoteunused u) 0
2012-11-12 05:05:04 +00:00
next $ return True
remoteunused u = excludeReferenced refspec =<< loggedKeysFor u
2012-03-12 01:08:48 +00:00
check :: FilePath -> ([(Int, Key)] -> String) -> Annex [Key] -> Int -> Annex Int
check file msg a c = do
l <- a
let unusedlist = number c l
unless (null l) $ showLongNote $ msg unusedlist
2020-10-30 19:55:59 +00:00
updateUnusedLog (toRawFilePath file) (M.fromList unusedlist)
2012-03-12 01:08:48 +00:00
return $ c + length l
2011-05-15 06:49:43 +00:00
2012-03-12 01:08:48 +00:00
number :: Int -> [a] -> [(Int, a)]
number _ [] = []
number n (x:xs) = (n+1, x) : number (n+1) xs
2011-04-03 00:59:41 +00:00
table :: [(Int, Key)] -> [String]
2011-07-15 16:47:14 +00:00
table l = " NUMBER KEY" : map cols l
2012-11-12 05:05:04 +00:00
where
cols (n,k) = " " ++ pad 6 (show n) ++ " " ++ serializeKey k
2012-11-12 05:05:04 +00:00
pad n s = s ++ replicate (n - length s) ' '
2010-11-15 22:04:19 +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]
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
["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 mheader mtrailer = unlines $
mheader ++
2011-04-03 00:59:41 +00:00
table u ++
["(To see where data was previously used, try: git log --stat --no-textconv -S'KEY')"] ++
mtrailer
2011-04-03 00:59:41 +00:00
remoteUnusedMsg :: Maybe Remote -> RemoteName -> [(Int, Key)] -> String
remoteUnusedMsg mr remotename u = unusedMsg' u
["Some annexed data on " ++ remotename ++ " is not used by any files:"]
(if isJust mr then [dropMsg (Just remotename)] else [])
dropMsg :: Maybe RemoteName -> String
2011-04-03 00:59:41 +00:00
dropMsg Nothing = dropMsg' ""
dropMsg (Just remotename) = dropMsg' $ " --from " ++ remotename
2011-04-03 00:59:41 +00:00
dropMsg' :: String -> String
dropMsg' s = "\nTo remove unwanted data: git-annex dropunused" ++ s ++ " NUMBER\n"
2011-04-03 00:59:41 +00:00
2012-03-12 19:21:20 +00:00
{- Finds keys in the list that are not referenced in the git repository.
-
- Strategy:
-
- Pass keys through these filters in order, only creating each bloom
- filter on demand if the previous one didn't filter out all keys.
-
- 1. Bloom filter containing all keys referenced by files in the work tree.
- This is the fastest one to build and will filter out most keys.
- 2. Bloom filter containing all keys in the diff from the work tree to
- the index.
2021-03-11 16:03:36 +00:00
- 3. Associated files filter. An unlocked file may have had its content
- added to the annex (by eg, git diff running the smudge filter),
- but the new key is not yet staged in the index. But if so, it will
- have an associated file.
- 4. Bloom filter containing all keys in the diffs between the index and
- branches matching the RefSpec. (This can take quite a while to build).
2012-03-12 19:21:20 +00:00
-}
excludeReferenced :: RefSpec -> [Key] -> Annex [Key]
excludeReferenced refspec ks = runbloomfilter withKeysReferencedM ks
>>= runbloomfilter withKeysReferencedDiffIndex
>>= runfilter associatedFilesFilter
>>= runbloomfilter (withKeysReferencedDiffGitRefs refspec)
2012-11-12 05:05:04 +00:00
where
runfilter _ [] = return [] -- optimisation
runfilter a l = a l
runbloomfilter a = runfilter $ \l -> bloomFilter l <$> genBloomFilter a
2012-03-12 19:21:20 +00:00
{- Given an initial value, folds it with each key referenced by
- files in the working tree. -}
withKeysReferenced :: v -> (Key -> v -> v) -> Annex v
withKeysReferenced initial a = withKeysReferenced' Nothing initial folda
2012-11-12 05:05:04 +00:00
where
folda k _ v = return $ a k v
2012-03-12 19:21:20 +00:00
{- Runs an action on each referenced key in the working tree. -}
2012-03-12 19:21:20 +00:00
withKeysReferencedM :: (Key -> Annex ()) -> Annex ()
withKeysReferencedM a = withKeysReferenced' Nothing () calla
2012-11-12 05:05:04 +00:00
where
calla k _ _ = a k
2012-03-12 19:21:20 +00:00
{- Folds an action over keys and files referenced in a particular directory. -}
withKeysFilesReferencedIn :: FilePath -> v -> (Key -> RawFilePath -> v -> Annex v) -> Annex v
withKeysFilesReferencedIn = withKeysReferenced' . Just
withKeysReferenced' :: Maybe FilePath -> v -> (Key -> RawFilePath -> v -> Annex v) -> Annex v
withKeysReferenced' mdir initial a = do
(files, clean) <- getfiles
r <- go initial files
liftIO $ void clean
return r
2012-11-12 05:05:04 +00:00
where
getfiles = case mdir of
Nothing -> ifM isBareRepo
( return ([], return True)
, do
top <- fromRepo Git.repoPath
inRepo $ LsFiles.allFiles [] [top]
)
Just dir -> inRepo $ LsFiles.inRepo [] [toRawFilePath dir]
2012-11-12 05:05:04 +00:00
go v [] = return v
go v (f:fs) = do
2020-07-10 18:17:35 +00:00
mk <- lookupKey f
case mk of
2012-11-12 05:05:04 +00:00
Nothing -> go v fs
Just k -> do
!v' <- a k f v
2012-11-12 05:05:04 +00:00
go v' fs
2012-03-12 19:21:20 +00:00
withKeysReferencedDiffGitRefs :: RefSpec -> (Key -> Annex ()) -> Annex ()
withKeysReferencedDiffGitRefs refspec a = do
rs <- relevantrefs <$> inRepo (Git.Command.pipeReadStrict [Param "show-ref"])
shaHead <- maybe (return Nothing) (inRepo . Git.Ref.sha)
=<< inRepo Git.Branch.currentUnsafe
let haveHead = any (\(shaRef, _) -> Just shaRef == shaHead) rs
let rs' = map snd (nubRefs rs)
usedrefs <- applyRefSpec refspec rs' (getreflog rs')
forM_ (if haveHead then usedrefs else Git.Ref.headRef : usedrefs) $
withKeysReferencedDiffGitRef a
2012-11-12 05:05:04 +00:00
where
relevantrefs = map (\(r, h) -> (Git.Ref r, Git.Ref h)) .
2012-11-12 05:05:04 +00:00
filter ourbranches .
map (separate' (== (fromIntegral (ord ' ')))) .
S8.lines
nubRefs = nubBy (\(x, _) (y, _) -> x == y)
ourbranchend = S.cons (fromIntegral (ord '/')) (Git.fromRef' Annex.Branch.name)
ourbranches (_, b) = not (ourbranchend `S.isSuffixOf` b)
&& not ("refs/synced/" `S.isPrefixOf` b)
&& not (is_branchView (Git.Ref b))
getreflog rs = inRepo $ Git.RefLog.getMulti rs
2012-03-12 19:21:20 +00:00
{- Runs an action on keys referenced in the given Git reference which
- differ from those referenced in the index. -}
withKeysReferencedDiffGitRef :: (Key -> Annex ()) -> Git.Ref -> Annex ()
withKeysReferencedDiffGitRef a ref = do
2011-12-12 22:23:24 +00:00
showAction $ "checking " ++ Git.Ref.describe ref
withKeysReferencedDiff a
(inRepo $ DiffTree.diffIndex ref)
DiffTree.srcsha
{- Runs an action on keys referenced in the index which differ from the
- work tree. -}
withKeysReferencedDiffIndex :: (Key -> Annex ()) -> Annex ()
withKeysReferencedDiffIndex a = unlessM (isBareRepo) $
withKeysReferencedDiff a
(inRepo $ DiffTree.diffFiles [])
DiffTree.srcsha
withKeysReferencedDiff :: (Key -> Annex ()) -> (Annex ([DiffTree.DiffTreeItem], IO Bool)) -> (DiffTree.DiffTreeItem -> Sha) -> Annex ()
withKeysReferencedDiff a getdiff extractsha = do
(ds, clean) <- getdiff
forM_ ds go
liftIO $ void clean
where
go d = do
let sha = extractsha d
unless (sha `elem` nullShas) $
catKey sha >>= maybe noop a
{- Filters out keys that have an associated file that's not modified. -}
associatedFilesFilter :: [Key] -> Annex [Key]
associatedFilesFilter = filterM go
where
go k = do
cs <- Database.Keys.getInodeCaches k
if null cs
then return True
else checkunmodified cs
=<< Database.Keys.getAssociatedFiles k
checkunmodified _ [] = return True
checkunmodified cs (f:fs) = do
relf <- fromRepo $ fromTopFilePath f
ifM (sameInodeCache relf cs)
( return False
, checkunmodified cs fs
)
data UnusedMaps = UnusedMaps
{ unusedMap :: UnusedMap
, unusedBadMap :: UnusedMap
, unusedTmpMap :: UnusedMap
}
withUnusedMaps :: (UnusedMaps -> Int -> CommandStart) -> CmdParams -> CommandSeek
withUnusedMaps a params = do
unused <- readUnusedMap ""
unusedbad <- readUnusedMap "bad"
unusedtmp <- readUnusedMap "tmp"
let m = unused `M.union` unusedbad `M.union` unusedtmp
let unusedmaps = UnusedMaps unused unusedbad unusedtmp
commandActions $ map (a unusedmaps) $ concatMap (unusedSpec m) params
unusedSpec :: UnusedMap -> String -> [Int]
unusedSpec m spec
| spec == "all" = if M.null m
then []
else [fst (M.findMin m)..fst (M.findMax m)]
| "-" `isInfixOf` spec = range $ separate (== '-') spec
| otherwise = maybe badspec (: []) (readish spec)
where
range (a, b) = case (readish a, readish b) of
(Just x, Just y) -> [x..y]
_ -> badspec
badspec = giveup $ "Expected number or range, not \"" ++ spec ++ "\""
{- Seek action for unused content. Finds the number in the maps, and
- calls one of 3 actions, depending on the type of unused file. -}
startUnused :: String
-> (Key -> CommandPerform)
-> (Key -> CommandPerform)
-> (Key -> CommandPerform)
-> UnusedMaps -> Int -> CommandStart
startUnused message unused badunused tmpunused maps n = search
[ (unusedMap maps, unused)
, (unusedBadMap maps, badunused)
, (unusedTmpMap maps, tmpunused)
]
where
search [] = giveup $ show n ++ " not valid (run git annex unused for list)"
search ((m, a):rest) =
case M.lookup n m of
Nothing -> search rest
make CommandStart return a StartMessage The goal is to be able to run CommandStart in the main thread when -J is used, rather than unncessarily passing it off to a worker thread, which incurs overhead that is signficant when the CommandStart is going to quickly decide to stop. To do that, the message it displays needs to be displayed in the worker thread, after the CommandStart has run. Also, the change will mean that CommandStart will no longer necessarily run with the same Annex state as CommandPerform. While its docs already said it should avoid modifying Annex state, I audited all the CommandStart code as part of the conversion. (Note that CommandSeek already sometimes runs with a different Annex state, and that has not been a source of any problems, so I am not too worried that this change will lead to breakage going forward.) The only modification of Annex state I found was it calling allowMessages in some Commands that default to noMessages. Dealt with that by adding a startCustomOutput and a startingUsualMessages. This lets a command start with noMessages and then select the output it wants for each CommandStart. One bit of breakage: onlyActionOn has been removed from commands that used it. The plan is that, since a StartMessage contains an ActionItem, when a Key can be extracted from that, the parallel job runner can run onlyActionOn' automatically. Then commands won't need to worry about this detail. Future work. Otherwise, this was a fairly straightforward process of making each CommandStart compile again. Hopefully other behavior changes were mostly avoided. In a few cases, a command had a CommandStart that called a CommandPerform that then called showStart multiple times. I have collapsed those down to a single start action. The main command to perhaps suffer from it is Command.Direct, which used to show a start for each file, and no longer does. Another minor behavior change is that some commands used showStart before, but had an associated file and a Key available, so were changed to ShowStart with an ActionItemAssociatedFile. That will not change the normal output or behavior, but --json output will now include the key. This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
Just key -> starting message
(ActionItemOther $ Just $ show n)
(SeekInput [])
make CommandStart return a StartMessage The goal is to be able to run CommandStart in the main thread when -J is used, rather than unncessarily passing it off to a worker thread, which incurs overhead that is signficant when the CommandStart is going to quickly decide to stop. To do that, the message it displays needs to be displayed in the worker thread, after the CommandStart has run. Also, the change will mean that CommandStart will no longer necessarily run with the same Annex state as CommandPerform. While its docs already said it should avoid modifying Annex state, I audited all the CommandStart code as part of the conversion. (Note that CommandSeek already sometimes runs with a different Annex state, and that has not been a source of any problems, so I am not too worried that this change will lead to breakage going forward.) The only modification of Annex state I found was it calling allowMessages in some Commands that default to noMessages. Dealt with that by adding a startCustomOutput and a startingUsualMessages. This lets a command start with noMessages and then select the output it wants for each CommandStart. One bit of breakage: onlyActionOn has been removed from commands that used it. The plan is that, since a StartMessage contains an ActionItem, when a Key can be extracted from that, the parallel job runner can run onlyActionOn' automatically. Then commands won't need to worry about this detail. Future work. Otherwise, this was a fairly straightforward process of making each CommandStart compile again. Hopefully other behavior changes were mostly avoided. In a few cases, a command had a CommandStart that called a CommandPerform that then called showStart multiple times. I have collapsed those down to a single start action. The main command to perhaps suffer from it is Command.Direct, which used to show a start for each file, and no longer does. Another minor behavior change is that some commands used showStart before, but had an associated file and a Key available, so were changed to ShowStart with an ActionItemAssociatedFile. That will not change the normal output or behavior, but --json output will now include the key. This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
(a key)