git-annex/Command/Whereis.hs

113 lines
3.3 KiB
Haskell
Raw Normal View History

{- git-annex command
-
2016-01-20 16:46:00 +00:00
- Copyright 2010-2016 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Whereis where
import Command
import Remote
2011-10-15 20:21:08 +00:00
import Logs.Trust
import Logs.Web
import Remote.Web (getWebUrls)
import Annex.UUID
import qualified Data.Map as M
cmd :: Command
cmd = noCommit $ withGlobalOptions (jsonOption : annexedMatchingOptions) $
command "whereis" SectionQuery
"lists repositories that have file content"
2015-07-10 20:32:33 +00:00
paramPaths (seek <$$> optParser)
data WhereisOptions = WhereisOptions
{ whereisFiles :: CmdParams
, keyOptions :: Maybe KeyOptions
2016-01-20 16:46:00 +00:00
, batchOption :: BatchMode
}
2015-07-10 20:32:33 +00:00
optParser :: CmdParamsDesc -> Parser WhereisOptions
optParser desc = WhereisOptions
<$> cmdParams desc
<*> optional (parseKeyOptions False)
2016-01-20 16:46:00 +00:00
<*> parseBatchOption
2015-07-10 20:32:33 +00:00
seek :: WhereisOptions -> CommandSeek
seek o = do
m <- remoteMap id
2016-01-20 16:46:00 +00:00
let go = whenAnnexed $ start m
case batchOption o of
Batch -> batchFiles go
NoBatch ->
withKeyOptions (keyOptions o) False
(startKeys m)
(withFilesInGit go)
(whereisFiles o)
start :: M.Map UUID Remote -> FilePath -> Key -> CommandStart
start remotemap file key = start' remotemap key (Just file)
2014-01-26 18:26:32 +00:00
startKeys :: M.Map UUID Remote -> Key -> CommandStart
startKeys remotemap key = start' remotemap key Nothing
start' :: M.Map UUID Remote -> Key -> AssociatedFile -> CommandStart
start' remotemap key afile = do
2014-01-26 19:53:01 +00:00
showStart' "whereis" key afile
next $ perform remotemap key
2012-02-16 04:41:30 +00:00
perform :: M.Map UUID Remote -> Key -> CommandPerform
perform remotemap key = do
locations <- keyLocations key
urls <- getUUIDUrls key locations remotemap
(untrustedlocations, safelocations) <- trustPartition UnTrusted locations
let num = length safelocations
2011-03-05 21:41:36 +00:00
showNote $ show num ++ " " ++ copiesplural num
pp <- ppwhereis "whereis" safelocations urls
2011-10-31 20:46:51 +00:00
unless (null safelocations) $ showLongNote pp
pp' <- ppwhereis "untrusted" untrustedlocations urls
2011-10-31 20:46:51 +00:00
unless (null untrustedlocations) $ showLongNote $ untrustedheader ++ pp'
mapM_ (showRemoteUrls remotemap) urls
if null safelocations then stop else next $ return True
2012-11-12 05:05:04 +00:00
where
copiesplural 1 = "copy"
copiesplural _ = "copies"
untrustedheader = "The following untrusted locations may also have copies:\n"
ppwhereis h ls urls = do
descm <- uuidDescriptions
let urlvals = map (\(u, us) -> (u, Just us)) $
filter (\(u,_) -> u `elem` ls) urls
prettyPrintUUIDsWith (Just "urls") h descm (const Nothing) urlvals
getUUIDUrls :: Key -> [UUID] -> M.Map UUID Remote -> Annex [(UUID, [URLString])]
getUUIDUrls key uuids remotemap = forM uuids $ \uu -> (,)
<$> pure uu
<*> maybe (pure []) (getRemoteUrls key) (M.lookup uu remotemap)
getRemoteUrls :: Key -> Remote -> Annex [URLString]
getRemoteUrls key remote
| uuid remote == webUUID = getWebUrls key
| otherwise = (++)
<$> askremote
<*> claimedurls
2012-11-12 05:05:04 +00:00
where
askremote = maybe (pure []) (flip id key) (whereisKey remote)
claimedurls = do
us <- map fst
. filter (\(_, d) -> d == OtherDownloader)
. map getDownloader
<$> getUrls key
filterM (\u -> (==) <$> pure remote <*> claimingUrl u) us
showRemoteUrls :: M.Map UUID Remote -> (UUID, [URLString]) -> Annex ()
showRemoteUrls remotemap (uu, us)
| null us = noop
| otherwise = case M.lookup uu remotemap of
Just r -> do
let ls = unlines $ map (\u -> name r ++ ": " ++ u) us
outputMessage noop ('\n' : indent ls ++ "\n")
Nothing -> noop