2011-03-05 21:23:55 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2020-05-19 20:01:02 +00:00
|
|
|
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
|
2011-03-05 21:23:55 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-03-05 21:23:55 +00:00
|
|
|
-}
|
|
|
|
|
2020-05-19 20:01:02 +00:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
|
|
|
|
2011-03-05 21:23:55 +00:00
|
|
|
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
|
2020-05-19 20:01:02 +00:00
|
|
|
import qualified Utility.Format
|
|
|
|
import qualified Command.Find
|
|
|
|
import Types.ActionItem
|
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
|
2020-05-19 20:01:02 +00:00
|
|
|
, formatOption :: Maybe Utility.Format.Format
|
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
|
2021-08-25 18:20:33 +00:00
|
|
|
<*> parseBatchOption True
|
2020-05-19 20:01:02 +00:00
|
|
|
<*> optional parseFormatOption
|
|
|
|
|
|
|
|
parseFormatOption :: Parser Utility.Format.Format
|
|
|
|
parseFormatOption = option (Utility.Format.gen <$> str)
|
|
|
|
( long "format" <> metavar paramFormat
|
|
|
|
<> help "control format of output"
|
|
|
|
)
|
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
|
2020-07-22 18:23:28 +00:00
|
|
|
let seeker = AnnexedFileSeeker
|
|
|
|
{ startAction = start o m
|
|
|
|
, checkContentPresent = Nothing
|
|
|
|
, usesLocationLog = True
|
|
|
|
}
|
2016-01-20 16:46:00 +00:00
|
|
|
case batchOption o of
|
2020-07-13 21:04:02 +00:00
|
|
|
NoBatch -> do
|
2020-07-24 16:05:28 +00:00
|
|
|
withKeyOptions (keyOptions o) False seeker
|
2020-05-19 20:01:02 +00:00
|
|
|
(commandAction . startKeys o m)
|
2020-07-13 21:04:02 +00:00
|
|
|
(withFilesInGitAnnex ww seeker)
|
2020-05-28 19:55:17 +00:00
|
|
|
=<< workTreeItems ww (whereisFiles o)
|
2022-01-26 16:59:55 +00:00
|
|
|
Batch fmt -> batchOnly (keyOptions o) (whereisFiles o) $
|
|
|
|
batchAnnexed fmt seeker (startKeys o m)
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
start :: WhereisOptions -> M.Map UUID Remote -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start o remotemap si file key =
|
|
|
|
startKeys o remotemap (si, key, mkActionItem (key, afile))
|
2016-07-20 19:22:55 +00:00
|
|
|
where
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|
2014-01-26 18:26:32 +00:00
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
startKeys :: WhereisOptions -> M.Map UUID Remote -> (SeekInput, Key, ActionItem) -> CommandStart
|
|
|
|
startKeys o remotemap (si, key, ai)
|
2020-05-19 20:01:02 +00:00
|
|
|
| isJust (formatOption o) = startingCustomOutput ai go
|
2020-09-14 20:49:33 +00:00
|
|
|
| otherwise = starting "whereis" ai si go
|
2020-05-19 20:01:02 +00:00
|
|
|
where
|
|
|
|
go = perform o remotemap key ai
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2020-05-19 20:01:02 +00:00
|
|
|
perform :: WhereisOptions -> M.Map UUID Remote -> Key -> ActionItem -> CommandPerform
|
|
|
|
perform o remotemap key ai = do
|
2012-02-14 07:49:48 +00:00
|
|
|
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
|
2020-05-19 20:01:02 +00:00
|
|
|
case formatOption o of
|
|
|
|
Nothing -> do
|
|
|
|
let num = length safelocations
|
|
|
|
showNote $ show num ++ " " ++ copiesplural num
|
|
|
|
pp <- ppwhereis "whereis" safelocations urls
|
|
|
|
unless (null safelocations) $ showLongNote pp
|
|
|
|
pp' <- ppwhereis "untrusted" untrustedlocations urls
|
|
|
|
unless (null untrustedlocations) $ showLongNote $ untrustedheader ++ pp'
|
|
|
|
|
|
|
|
mapM_ (showRemoteUrls remotemap) urls
|
|
|
|
Just formatter -> liftIO $ do
|
2020-11-16 13:56:32 +00:00
|
|
|
let vs = Command.Find.formatVars key
|
2021-03-12 18:09:19 +00:00
|
|
|
(AssociatedFile (actionItemFile ai))
|
2020-05-19 20:01:02 +00:00
|
|
|
let showformatted muuid murl = putStr $
|
|
|
|
Utility.Format.format formatter $
|
|
|
|
M.fromList $ vs ++ catMaybes
|
|
|
|
[ fmap ("uuid",) muuid
|
|
|
|
, fmap ("url",) murl
|
|
|
|
]
|
|
|
|
let showformatted' muuid
|
|
|
|
| Utility.Format.formatContainsVar "url" formatter =
|
|
|
|
forM_ (concatMap snd urls) $
|
|
|
|
showformatted muuid . Just
|
|
|
|
| otherwise = showformatted muuid Nothing
|
|
|
|
if Utility.Format.formatContainsVar "uuid" formatter
|
|
|
|
then forM_ locations $
|
|
|
|
showformatted' . Just . fromUUID
|
|
|
|
else showformatted' Nothing
|
2016-01-15 18:16:48 +00:00
|
|
|
|
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
|
2021-03-22 23:56:02 +00:00
|
|
|
| uuid remote == webUUID =
|
|
|
|
map (fst . getDownloader) <$> getWebUrls key
|
2016-01-15 18:16:48 +00:00
|
|
|
| otherwise = (++)
|
2014-12-08 23:14:24 +00:00
|
|
|
<$> askremote
|
|
|
|
<*> claimedurls
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2020-02-27 18:09:18 +00:00
|
|
|
askremote = case whereisKey remote of
|
|
|
|
Nothing -> pure []
|
|
|
|
Just w -> tryNonAsync (w key) >>= \case
|
|
|
|
Right l -> pure l
|
|
|
|
Left e -> do
|
|
|
|
warning $ unwords
|
|
|
|
[ "unable to query remote"
|
|
|
|
, name remote
|
|
|
|
, "for urls:"
|
|
|
|
, show e
|
|
|
|
]
|
|
|
|
return []
|
2014-12-08 23:14:24 +00:00
|
|
|
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
|