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
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = noCommit $ withOptions (jsonOption : annexedMatchingOptions ++ keyOptions) $
|
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-08 19:08:02 +00:00
|
|
|
paramPaths (withParams seek)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2015-07-09 23:03:21 +00:00
|
|
|
data WhereisOptions = WhereisOptions
|
|
|
|
{ whereisFiles :: CmdParams
|
|
|
|
, jsonOption :: GlobalSetter
|
|
|
|
, keyOptions :: Maybe KeyOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
-- TODO: annexedMatchingOptions
|
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
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 ps = do
|
|
|
|
m <- remoteMap id
|
2015-03-25 21:06:14 +00:00
|
|
|
withKeyOptions False
|
2014-01-26 18:26:32 +00:00
|
|
|
(startKeys m)
|
|
|
|
(withFilesInGit $ whenAnnexed $ start m)
|
|
|
|
ps
|
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'
|
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
|
|
|
|
unless (null ls) $ showLongNote $ unlines $
|
|
|
|
map (\l -> name remote ++ ": " ++ l) 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
|