2011-03-05 21:23:55 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-01-20 16:46:00 +00:00
|
|
|
- Copyright 2010-2016 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
|
|
|
|
|
|
|
|
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)
|
2016-01-15 18:16:48 +00:00
|
|
|
import Annex.UUID
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2016-01-06 16:33:32 +00:00
|
|
|
import qualified Data.Map as M
|
Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale
As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.
It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.
This commit was supported by the NSF-funded DataLad project.
2018-04-16 19:42:45 +00:00
|
|
|
import qualified Data.Vector as V
|
2016-01-06 16:33:32 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = noCommit $ withGlobalOptions [jsonOptions, 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
|
2016-01-20 16:46:00 +00:00
|
|
|
, batchOption :: BatchMode
|
2015-07-09 23:03:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 20:32:33 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser WhereisOptions
|
|
|
|
optParser desc = WhereisOptions
|
|
|
|
<$> cmdParams desc
|
2016-08-03 16:37:12 +00:00
|
|
|
<*> optional parseKeyOptions
|
2016-01-20 16:46:00 +00:00
|
|
|
<*> parseBatchOption
|
2015-07-10 20:32:33 +00:00
|
|
|
|
|
|
|
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
|
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)
|
2017-10-16 18:10:03 +00:00
|
|
|
=<< workTreeItems (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
|
2016-07-20 19:22:55 +00:00
|
|
|
start remotemap file key = startKeys remotemap key (mkActionItem afile)
|
|
|
|
where
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|
2014-01-26 18:26:32 +00:00
|
|
|
|
2016-07-20 19:22:55 +00:00
|
|
|
startKeys :: M.Map UUID Remote -> Key -> ActionItem -> CommandStart
|
|
|
|
startKeys remotemap key ai = do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStartKey "whereis" key ai
|
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
|
2016-01-15 18:16:48 +00:00
|
|
|
urls <- getUUIDUrls key locations remotemap
|
2012-02-14 07:49:48 +00:00
|
|
|
(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
|
2016-01-15 18:16:48 +00:00
|
|
|
pp <- ppwhereis "whereis" safelocations urls
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null safelocations) $ showLongNote pp
|
2016-01-15 18:16:48 +00:00
|
|
|
pp' <- ppwhereis "untrusted" untrustedlocations urls
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null untrustedlocations) $ showLongNote $ untrustedheader ++ pp'
|
2015-08-17 15:35:34 +00:00
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
mapM_ (showRemoteUrls remotemap) urls
|
|
|
|
|
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"
|
2016-01-15 18:16:48 +00:00
|
|
|
ppwhereis h ls urls = do
|
|
|
|
descm <- uuidDescriptions
|
Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale
As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.
It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.
This commit was supported by the NSF-funded DataLad project.
2018-04-16 19:42:45 +00:00
|
|
|
let urlvals = map (\(u, us) -> (u, Just (V.fromList us))) $
|
2016-01-15 18:16:48 +00:00
|
|
|
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)
|
2012-02-14 07:49:48 +00:00
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
getRemoteUrls :: Key -> Remote -> Annex [URLString]
|
|
|
|
getRemoteUrls key remote
|
|
|
|
| uuid remote == webUUID = getWebUrls key
|
|
|
|
| otherwise = (++)
|
2014-12-08 23:14:24 +00:00
|
|
|
<$> askremote
|
|
|
|
<*> claimedurls
|
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
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
showRemoteUrls :: M.Map UUID Remote -> (UUID, [URLString]) -> Annex ()
|
|
|
|
showRemoteUrls remotemap (uu, us)
|
|
|
|
| null us = noop
|
|
|
|
| otherwise = case M.lookup uu remotemap of
|
2016-09-09 19:49:44 +00:00
|
|
|
Just r -> showLongNote $
|
|
|
|
unlines $ map (\u -> name r ++ ": " ++ u) us
|
2016-01-15 18:16:48 +00:00
|
|
|
Nothing -> noop
|