2011-03-05 21:23:55 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2014 Joey Hess <id@joeyh.name>
|
2011-03-05 21:23:55 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Whereis where
|
|
|
|
|
2012-02-14 07:49:48 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-03-05 21:23:55 +00:00
|
|
|
import Command
|
2011-07-01 19:24:07 +00:00
|
|
|
import Remote
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Trust
|
2014-12-08 23:14:24 +00:00
|
|
|
import Logs.Web
|
2015-08-17 15:35:34 +00:00
|
|
|
import Remote.Web (getWebUrls)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 17:18:46 +00:00
|
|
|
cmd = noCommit $ withGlobalOptions (jsonOption : annexedMatchingOptions) $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "whereis" SectionQuery
|
2015-07-08 16:33:27 +00:00
|
|
|
"lists repositories that have file content"
|
2015-07-10 20:32:33 +00:00
|
|
|
paramPaths (seek <$$> optParser)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2015-07-09 23:03:21 +00:00
|
|
|
data WhereisOptions = WhereisOptions
|
|
|
|
{ whereisFiles :: CmdParams
|
|
|
|
, keyOptions :: Maybe KeyOptions
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:32:33 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser WhereisOptions
|
|
|
|
optParser desc = WhereisOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> optional (parseKeyOptions False)
|
|
|
|
|
|
|
|
seek :: WhereisOptions -> CommandSeek
|
|
|
|
seek o = do
|
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
|
|
|
m <- remoteMap id
|
2015-07-10 20:32:33 +00:00
|
|
|
withKeyOptions (keyOptions o) False
|
2014-01-26 18:26:32 +00:00
|
|
|
(startKeys m)
|
|
|
|
(withFilesInGit $ whenAnnexed $ start m)
|
2015-07-10 20:32:33 +00:00
|
|
|
(whereisFiles o)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
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
|
2012-02-14 07:49:48 +00:00
|
|
|
next $ perform remotemap key
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2012-02-16 04:41:30 +00:00
|
|
|
perform :: M.Map UUID Remote -> Key -> CommandPerform
|
2012-02-14 07:49:48 +00:00
|
|
|
perform remotemap key = do
|
|
|
|
locations <- keyLocations key
|
|
|
|
(untrustedlocations, safelocations) <- trustPartition UnTrusted locations
|
2011-09-06 20:59:53 +00:00
|
|
|
let num = length safelocations
|
2011-03-05 21:41:36 +00:00
|
|
|
showNote $ show num ++ " " ++ copiesplural num
|
2011-09-06 20:59:53 +00:00
|
|
|
pp <- prettyPrintUUIDs "whereis" safelocations
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null safelocations) $ showLongNote pp
|
2011-09-06 20:59:53 +00:00
|
|
|
pp' <- prettyPrintUUIDs "untrusted" untrustedlocations
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null untrustedlocations) $ showLongNote $ untrustedheader ++ pp'
|
2015-08-17 15:35:34 +00:00
|
|
|
|
|
|
|
-- Since other remotes than the web remote can set urls
|
|
|
|
-- where a key can be downloaded, get and show all such urls
|
|
|
|
-- as a special case.
|
|
|
|
showRemote "web" =<< getWebUrls key
|
2012-06-12 15:32:06 +00:00
|
|
|
forM_ (mapMaybe (`M.lookup` remotemap) locations) $
|
2012-02-14 07:49:48 +00:00
|
|
|
performRemote key
|
2011-09-06 20:59:53 +00:00
|
|
|
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"
|
2012-02-14 07:49:48 +00:00
|
|
|
|
|
|
|
performRemote :: Key -> Remote -> Annex ()
|
2014-12-08 23:14:24 +00:00
|
|
|
performRemote key remote = do
|
|
|
|
ls <- (++)
|
|
|
|
<$> askremote
|
|
|
|
<*> claimedurls
|
2015-08-17 15:35:34 +00:00
|
|
|
showRemote (name remote) ls
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2014-12-08 23:14:24 +00:00
|
|
|
askremote = maybe (pure []) (flip id key) (whereisKey remote)
|
|
|
|
claimedurls = do
|
|
|
|
us <- map fst
|
|
|
|
. filter (\(_, d) -> d == OtherDownloader)
|
|
|
|
. map getDownloader
|
|
|
|
<$> getUrls key
|
2014-12-11 18:09:57 +00:00
|
|
|
filterM (\u -> (==) <$> pure remote <*> claimingUrl u) us
|
2015-08-17 15:35:34 +00:00
|
|
|
|
|
|
|
showRemote :: String -> [String] -> Annex ()
|
|
|
|
showRemote n ls
|
|
|
|
| null ls = return ()
|
|
|
|
| otherwise = showLongNote $ unlines $
|
|
|
|
map (\l -> n ++ ": " ++ l) ls
|