2010-11-15 20:35:06 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-07-10 20:05:56 +00:00
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
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
|
|
|
|
|
2013-07-03 19:26:59 +00:00
|
|
|
import qualified Data.Map as M
|
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
|
2012-05-02 18:59:05 +00:00
|
|
|
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
|
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
|
2013-08-26 17:01:48 +00:00
|
|
|
import qualified Git.Branch
|
2015-07-07 21:13:50 +00:00
|
|
|
import qualified Git.RefLog
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2013-08-26 00:47:49 +00:00
|
|
|
import qualified Git.DiffTree as DiffTree
|
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
|
2012-08-08 20:06:01 +00:00
|
|
|
import Types.Key
|
2015-05-14 19:31:38 +00:00
|
|
|
import Types.RefSpec
|
2013-10-17 19:01:23 +00:00
|
|
|
import Git.FilePath
|
2015-07-10 20:05:56 +00:00
|
|
|
import Git.Types
|
2014-06-04 18:03:41 +00:00
|
|
|
import Logs.View (is_branchView)
|
2015-06-16 21:58:15 +00:00
|
|
|
import Annex.BloomFilter
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 17:49:46 +00:00
|
|
|
cmd = -- withGlobalOptions [unusedFromOption, refSpecOption] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "unused" SectionMaintenance
|
|
|
|
"look for unused file content"
|
2015-07-10 20:05:56 +00:00
|
|
|
paramNothing (seek <$$> optParser)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-10 20:05:56 +00:00
|
|
|
data UnusedOptions = UnusedOptions
|
|
|
|
{ fromRemote :: Maybe RemoteName
|
|
|
|
, refSpecOption :: Maybe RefSpec
|
|
|
|
}
|
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
|
|
|
|
2015-07-10 20:05:56 +00:00
|
|
|
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 "unused-refspec" <> metavar paramRefSpec
|
|
|
|
<> help "refs to consider used (default: all branches)"
|
|
|
|
))
|
2015-05-14 19:31:38 +00:00
|
|
|
|
2015-07-10 20:05:56 +00:00
|
|
|
seek :: UnusedOptions -> CommandSeek
|
|
|
|
seek = commandAction . start
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2015-07-10 20:05:56 +00:00
|
|
|
start :: UnusedOptions -> CommandStart
|
|
|
|
start o = do
|
2015-05-14 19:44:08 +00:00
|
|
|
cfgrefspec <- fromMaybe allRefSpec . annexUsedRefSpec
|
|
|
|
<$> Annex.getGitConfig
|
2015-07-10 20:05:56 +00:00
|
|
|
let refspec = fromMaybe cfgrefspec (refSpecOption o)
|
|
|
|
let (name, perform) = case fromRemote o of
|
2015-05-14 19:31:38 +00:00
|
|
|
Nothing -> (".", checkUnused refspec)
|
|
|
|
Just "." -> (".", checkUnused refspec)
|
|
|
|
Just "here" -> (".", checkUnused refspec)
|
|
|
|
Just n -> (n, checkRemoteUnused n refspec)
|
2011-05-29 02:37:17 +00:00
|
|
|
showStart "unused" name
|
2015-07-09 23:03:21 +00:00
|
|
|
next perform
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2015-05-14 19:31:38 +00:00
|
|
|
checkUnused :: RefSpec -> CommandPerform
|
|
|
|
checkUnused refspec = chain 0
|
2012-03-12 01:08:48 +00:00
|
|
|
[ check "" unusedMsg $ findunused =<< Annex.getState Annex.fast
|
2013-07-25 23:51:08 +00:00
|
|
|
, check "bad" staleBadMsg $ staleKeysPrune gitAnnexBadDir False
|
2014-02-26 20:52:56 +00:00
|
|
|
, 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"
|
2014-03-07 16:43:56 +00:00
|
|
|
-- InAnnex, not InRepository because if a direct mode
|
|
|
|
-- file exists, it is obviously not unused.
|
2015-05-14 19:31:38 +00:00
|
|
|
excludeReferenced refspec =<< getKeysPresent InAnnex
|
2012-11-12 05:05:04 +00:00
|
|
|
chain _ [] = next $ return True
|
|
|
|
chain v (a:as) = do
|
|
|
|
v' <- a v
|
|
|
|
chain v' as
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2015-05-14 19:31:38 +00:00
|
|
|
checkRemoteUnused :: String -> RefSpec -> CommandPerform
|
|
|
|
checkRemoteUnused name refspec = go =<< fromJust <$> Remote.byNameWithUUID (Just name)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go r = do
|
|
|
|
showAction "checking for unused data"
|
|
|
|
_ <- check "" (remoteUnusedMsg r) (remoteunused r) 0
|
|
|
|
next $ return True
|
2015-05-14 19:31:38 +00:00
|
|
|
remoteunused r = excludeReferenced refspec <=< loggedKeysFor $ Remote.uuid r
|
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
|
2014-01-22 19:33:02 +00:00
|
|
|
updateUnusedLog 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) ++ " " ++ key2file k
|
|
|
|
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]
|
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
|
2015-07-09 23:03:21 +00:00
|
|
|
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 -S'KEY')"] ++
|
2015-07-09 23:03:21 +00:00
|
|
|
mtrailer
|
2011-04-03 00:59:41 +00:00
|
|
|
|
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]
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
name = Remote.name r
|
2011-09-23 22:04:38 +00:00
|
|
|
|
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
|
|
|
|
2012-03-12 19:21:20 +00:00
|
|
|
{- Finds keys in the list that are not referenced in the git repository.
|
|
|
|
-
|
|
|
|
- Strategy:
|
|
|
|
-
|
|
|
|
- * Build a bloom filter of all keys referenced by symlinks. This
|
|
|
|
- is the fastest one to build and will filter out most keys.
|
|
|
|
- * If keys remain, build a second bloom filter of keys referenced by
|
2015-05-14 19:31:38 +00:00
|
|
|
- branches maching the RefSpec.
|
2012-03-12 19:21:20 +00:00
|
|
|
- * The list is streamed through these bloom filters lazily, so both will
|
|
|
|
- exist at the same time. This means that twice the memory is used,
|
|
|
|
- but they're relatively small, so the added complexity of using a
|
|
|
|
- mutable bloom filter does not seem worthwhile.
|
|
|
|
- * Generating the second bloom filter can take quite a while, since
|
|
|
|
- it needs enumerating all keys in all git branches. But, the common
|
|
|
|
- case, if the second filter is needed, is for some keys to be globally
|
|
|
|
- unused, and in that case, no short-circuit is possible.
|
|
|
|
- Short-circuiting if the first filter filters all the keys handles the
|
|
|
|
- other common case.
|
|
|
|
-}
|
2015-05-14 19:31:38 +00:00
|
|
|
excludeReferenced :: RefSpec -> [Key] -> Annex [Key]
|
|
|
|
excludeReferenced refspec ks = runfilter firstlevel ks >>= runfilter secondlevel
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
runfilter _ [] = return [] -- optimisation
|
2015-06-16 22:37:41 +00:00
|
|
|
runfilter a l = bloomFilter l <$> genBloomFilter a
|
2012-11-12 05:05:04 +00:00
|
|
|
firstlevel = withKeysReferencedM
|
2015-05-14 19:31:38 +00:00
|
|
|
secondlevel = withKeysReferencedInGit refspec
|
2011-09-28 21:35:47 +00:00
|
|
|
|
2012-03-12 19:21:20 +00:00
|
|
|
{- Given an initial value, folds it with each key referenced by
|
|
|
|
- symlinks in the git repo. -}
|
2012-03-11 19:19:07 +00:00
|
|
|
withKeysReferenced :: v -> (Key -> v -> v) -> Annex v
|
2013-03-11 05:22:56 +00:00
|
|
|
withKeysReferenced initial a = withKeysReferenced' Nothing initial folda
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-03-11 05:22:56 +00:00
|
|
|
folda k _ v = return $ a k v
|
2012-03-12 19:21:20 +00:00
|
|
|
|
|
|
|
{- Runs an action on each referenced key in the git repo. -}
|
|
|
|
withKeysReferencedM :: (Key -> Annex ()) -> Annex ()
|
2013-03-11 05:22:56 +00:00
|
|
|
withKeysReferencedM a = withKeysReferenced' Nothing () calla
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-03-11 05:22:56 +00:00
|
|
|
calla k _ _ = a k
|
2012-03-12 19:21:20 +00:00
|
|
|
|
2013-03-11 05:22:56 +00:00
|
|
|
{- Folds an action over keys and files referenced in a particular directory. -}
|
|
|
|
withKeysFilesReferencedIn :: FilePath -> v -> (Key -> FilePath -> v -> Annex v) -> Annex v
|
|
|
|
withKeysFilesReferencedIn = withKeysReferenced' . Just
|
|
|
|
|
|
|
|
withKeysReferenced' :: Maybe FilePath -> v -> (Key -> FilePath -> v -> Annex v) -> Annex v
|
|
|
|
withKeysReferenced' mdir initial a = do
|
2012-10-04 23:56:32 +00:00
|
|
|
(files, clean) <- getfiles
|
|
|
|
r <- go initial files
|
|
|
|
liftIO $ void clean
|
|
|
|
return r
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-03-11 05:22:56 +00:00
|
|
|
getfiles = case mdir of
|
|
|
|
Nothing -> ifM isBareRepo
|
|
|
|
( return ([], return True)
|
|
|
|
, do
|
|
|
|
top <- fromRepo Git.repoPath
|
2013-08-22 14:20:03 +00:00
|
|
|
inRepo $ LsFiles.allFiles [top]
|
2013-03-11 05:22:56 +00:00
|
|
|
)
|
|
|
|
Just dir -> inRepo $ LsFiles.inRepo [dir]
|
2012-11-12 05:05:04 +00:00
|
|
|
go v [] = return v
|
|
|
|
go v (f:fs) = do
|
|
|
|
x <- Backend.lookupFile f
|
|
|
|
case x of
|
|
|
|
Nothing -> go v fs
|
2014-04-17 22:03:39 +00:00
|
|
|
Just k -> do
|
2013-03-11 05:22:56 +00:00
|
|
|
!v' <- a k f v
|
2012-11-12 05:05:04 +00:00
|
|
|
go v' fs
|
2012-03-12 19:21:20 +00:00
|
|
|
|
2015-05-14 19:31:38 +00:00
|
|
|
withKeysReferencedInGit :: RefSpec -> (Key -> Annex ()) -> Annex ()
|
|
|
|
withKeysReferencedInGit refspec a = do
|
2013-08-26 17:01:48 +00:00
|
|
|
current <- inRepo Git.Branch.currentUnsafe
|
|
|
|
shaHead <- maybe (return Nothing) (inRepo . Git.Ref.sha) current
|
2015-07-07 21:13:50 +00:00
|
|
|
rs <- relevantrefs (shaHead, current)
|
2015-05-14 19:31:38 +00:00
|
|
|
<$> inRepo (Git.Command.pipeReadStrict [Param "show-ref"])
|
2015-07-07 21:31:30 +00:00
|
|
|
usedrefs <- applyRefSpec refspec rs (getreflog rs)
|
2015-05-14 19:31:38 +00:00
|
|
|
forM_ usedrefs $
|
|
|
|
withKeysReferencedInGitRef a
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-08-26 17:01:48 +00:00
|
|
|
relevantrefs headRef = addHead headRef .
|
2012-11-12 05:05:04 +00:00
|
|
|
filter ourbranches .
|
2013-08-26 00:47:49 +00:00
|
|
|
map (separate (== ' ')) .
|
|
|
|
lines
|
2013-08-26 17:01:48 +00:00
|
|
|
nubRefs = map (Git.Ref . snd) . nubBy (\(x, _) (y, _) -> x == y)
|
2014-02-19 05:09:17 +00:00
|
|
|
ourbranchend = '/' : Git.fromRef Annex.Branch.name
|
2012-11-12 05:05:04 +00:00
|
|
|
ourbranches (_, b) = not (ourbranchend `isSuffixOf` b)
|
|
|
|
&& not ("refs/synced/" `isPrefixOf` b)
|
2014-06-04 18:03:41 +00:00
|
|
|
&& not (is_branchView (Git.Ref b))
|
2013-08-26 17:01:48 +00:00
|
|
|
addHead headRef refs = case headRef of
|
|
|
|
-- if HEAD diverges from all branches (except the branch it
|
|
|
|
-- points to), run the actions on staged keys (and keys
|
|
|
|
-- that are only present in the work tree if the repo is
|
|
|
|
-- non bare)
|
|
|
|
(Just (Git.Ref x), Just (Git.Ref b))
|
|
|
|
| all (\(x',b') -> x /= x' || b == b') refs ->
|
|
|
|
Git.Ref.headRef
|
|
|
|
: nubRefs (filter ((/= x) . fst) refs)
|
|
|
|
_ -> nubRefs refs
|
2015-07-07 21:31:30 +00:00
|
|
|
getreflog rs = inRepo $ Git.RefLog.getMulti rs
|
2012-03-12 19:21:20 +00:00
|
|
|
|
2013-08-26 00:47:49 +00:00
|
|
|
{- Runs an action on keys referenced in the given Git reference which
|
2015-07-07 21:23:02 +00:00
|
|
|
- differ from those referenced in the work tree. -}
|
2012-03-12 19:21:20 +00:00
|
|
|
withKeysReferencedInGitRef :: (Key -> Annex ()) -> Git.Ref -> Annex ()
|
|
|
|
withKeysReferencedInGitRef a ref = do
|
2011-12-12 22:23:24 +00:00
|
|
|
showAction $ "checking " ++ Git.Ref.describe ref
|
2013-08-26 17:01:48 +00:00
|
|
|
bare <- isBareRepo
|
|
|
|
(ts,clean) <- inRepo $ if bare
|
2015-05-14 19:31:38 +00:00
|
|
|
then DiffTree.diffIndex ref
|
|
|
|
else DiffTree.diffWorkTree ref
|
2013-08-26 17:01:48 +00:00
|
|
|
let lookAtWorkingTree = not bare && ref == Git.Ref.headRef
|
|
|
|
forM_ ts $ tKey lookAtWorkingTree >=> maybe noop a
|
2013-08-26 00:47:49 +00:00
|
|
|
liftIO $ void clean
|
2013-08-26 04:14:19 +00:00
|
|
|
where
|
2014-04-17 22:03:39 +00:00
|
|
|
tKey True = Backend.lookupFile . getTopFilePath . DiffTree.file
|
2014-03-19 18:49:01 +00:00
|
|
|
tKey False = fileKey . takeFileName . decodeBS <$$>
|
2013-10-17 19:01:23 +00:00
|
|
|
catFile ref . getTopFilePath . DiffTree.file
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2013-07-03 19:26:59 +00:00
|
|
|
data UnusedMaps = UnusedMaps
|
|
|
|
{ unusedMap :: UnusedMap
|
|
|
|
, unusedBadMap :: UnusedMap
|
|
|
|
, unusedTmpMap :: UnusedMap
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
withUnusedMaps :: (UnusedMaps -> Int -> CommandStart) -> CmdParams -> CommandSeek
|
2013-07-03 19:26:59 +00:00
|
|
|
withUnusedMaps a params = do
|
2014-01-22 19:33:02 +00:00
|
|
|
unused <- readUnusedMap ""
|
|
|
|
unusedbad <- readUnusedMap "bad"
|
|
|
|
unusedtmp <- readUnusedMap "tmp"
|
2013-11-18 21:24:18 +00:00
|
|
|
let m = unused `M.union` unusedbad `M.union` unusedtmp
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
let unusedmaps = UnusedMaps unused unusedbad unusedtmp
|
|
|
|
seekActions $ return $ map (a unusedmaps) $
|
2013-11-18 21:24:18 +00:00
|
|
|
concatMap (unusedSpec m) params
|
2013-07-03 19:26:59 +00:00
|
|
|
|
2013-11-18 21:24:18 +00:00
|
|
|
unusedSpec :: UnusedMap -> String -> [Int]
|
|
|
|
unusedSpec m spec
|
2014-01-18 15:38:01 +00:00
|
|
|
| spec == "all" = if M.null m
|
|
|
|
then []
|
|
|
|
else [fst (M.findMin m)..fst (M.findMax m)]
|
2013-07-03 19:26:59 +00:00
|
|
|
| "-" `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 = error $ "Expected number or range, not \"" ++ spec ++ "\""
|
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
{- Seek action for unused content. Finds the number in the maps, and
|
|
|
|
- calls one of 3 actions, depending on the type of unused file. -}
|
2013-07-03 19:26:59 +00:00
|
|
|
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
|
2013-07-08 20:47:34 +00:00
|
|
|
search [] = error $ show n ++ " not valid (run git annex unused for list)"
|
2013-07-03 19:26:59 +00:00
|
|
|
search ((m, a):rest) =
|
|
|
|
case M.lookup n m of
|
|
|
|
Nothing -> search rest
|
|
|
|
Just key -> do
|
|
|
|
showStart message (show n)
|
|
|
|
next $ a key
|