info: When run on a single annexed file, displays some info about the file, including its key and size.

This commit is contained in:
Joey Hess 2014-10-21 13:24:15 -04:00
parent 91b2b3a817
commit 4a9e70c705
3 changed files with 58 additions and 26 deletions

View file

@ -1,6 +1,6 @@
{- git-annex command
-
- Copyright 2011 Joey Hess <joey@kitenet.net>
- Copyright 2011-2014 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
@ -24,6 +24,7 @@ import Command
import Utility.DataUnits
import Utility.DiskFree
import Annex.Content
import Annex.Link
import Types.Key
import Logs.UUID
import Logs.Trust
@ -65,6 +66,9 @@ data StatInfo = StatInfo
, referencedData :: Maybe KeyData
, numCopiesStats :: Maybe NumCopiesStats
}
emptyStatInfo :: StatInfo
emptyStatInfo = StatInfo Nothing Nothing Nothing
-- a state monad for running Stats in
type StatState = StateT StatInfo Annex
@ -72,36 +76,48 @@ type StatState = StateT StatInfo Annex
cmd :: [Command]
cmd = [noCommit $ dontCheck repoExists $ withOptions [jsonOption] $
command "info" paramPaths seek SectionQuery
"shows general information about the annex"]
"shows information about the specified item or the repository as a whole"]
seek :: CommandSeek
seek = withWords start
start :: [FilePath] -> CommandStart
start :: [String] -> CommandStart
start [] = do
globalInfo
stop
start ps = do
mapM_ localInfo =<< filterM isdir ps
mapM_ itemInfo ps
stop
where
isdir = liftIO . catchBoolIO . (isDirectory <$$> getFileStatus)
globalInfo :: Annex ()
globalInfo = do
stats <- selStats global_fast_stats global_slow_stats
showCustom "info" $ do
evalStateT (mapM_ showStat stats) (StatInfo Nothing Nothing Nothing)
evalStateT (mapM_ showStat stats) emptyStatInfo
return True
localInfo :: FilePath -> Annex ()
localInfo dir = showCustom (unwords ["info", dir]) $ do
stats <- selStats (tostats local_fast_stats) (tostats local_slow_stats)
evalStateT (mapM_ showStat stats) =<< getLocalStatInfo dir
itemInfo :: String -> Annex ()
itemInfo p = ifM (isdir p)
( dirInfo p
, maybe noinfo (fileInfo p) =<< isAnnexLink p
)
where
isdir = liftIO . catchBoolIO . (isDirectory <$$> getFileStatus)
noinfo = error $ p ++ " is not a directory or an annexed file"
dirInfo :: FilePath -> Annex ()
dirInfo dir = showCustom (unwords ["info", dir]) $ do
stats <- selStats (tostats dir_fast_stats) (tostats dir_slow_stats)
evalStateT (mapM_ showStat stats) =<< getDirStatInfo dir
return True
where
tostats = map (\s -> s dir)
fileInfo :: FilePath -> Key -> Annex ()
fileInfo file k = showCustom (unwords ["info", file]) $ do
evalStateT (mapM_ showStat (file_stats file k)) emptyStatInfo
return True
selStats :: [Stat] -> [Stat] -> Annex [Stat]
selStats fast_stats slow_stats = do
fast <- Annex.getState Annex.fast
@ -132,19 +148,26 @@ global_slow_stats =
, bloom_info
, backend_usage
]
local_fast_stats :: [FilePath -> Stat]
local_fast_stats =
dir_fast_stats :: [FilePath -> Stat]
dir_fast_stats =
[ local_dir
, const local_annex_keys
, const local_annex_size
, const known_annex_files
, const known_annex_size
]
local_slow_stats :: [FilePath -> Stat]
local_slow_stats =
dir_slow_stats :: [FilePath -> Stat]
dir_slow_stats =
[ const numcopies_stats
]
file_stats :: FilePath -> Key -> [Stat]
file_stats f k =
[ local_file f
, key_size k
, key_name k
]
stat :: String -> (String -> StatState String) -> Stat
stat desc a = return $ Just (desc, a desc)
@ -184,6 +207,9 @@ remote_list level = stat n $ nojson $ lift $ do
local_dir :: FilePath -> Stat
local_dir dir = stat "directory" $ json id $ return dir
local_file :: FilePath -> Stat
local_file file = stat "file" $ json id $ return file
local_annex_keys :: Stat
local_annex_keys = stat "local annex keys" $ json show $
countKeys <$> cachedPresentData
@ -206,6 +232,12 @@ tmp_size = staleSize "temporary object directory size" gitAnnexTmpObjectDir
bad_data_size :: Stat
bad_data_size = staleSize "bad keys size" gitAnnexBadDir
key_size :: Key -> Stat
key_size k = stat "size" $ json id $ pure $ showSizeKeys $ foldKeys [k]
key_name :: Key -> Stat
key_name k = stat "key" $ json id $ pure $ key2file k
bloom_info :: Stat
bloom_info = stat "bloom filter size" $ json id $ do
localkeys <- countKeys <$> cachedPresentData
@ -296,12 +328,12 @@ cachedReferencedData = do
put s { referencedData = Just v }
return v
-- currently only available for local info
-- currently only available for directory info
cachedNumCopiesStats :: StatState (Maybe NumCopiesStats)
cachedNumCopiesStats = numCopiesStats <$> get
getLocalStatInfo :: FilePath -> Annex StatInfo
getLocalStatInfo dir = do
getDirStatInfo :: FilePath -> Annex StatInfo
getDirStatInfo dir = do
fast <- Annex.getState Annex.fast
matcher <- Limit.getMatcher
(presentdata, referenceddata, numcopiesstats) <-