2014-02-13 01:12:22 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-07-26 18:53:00 +00:00
|
|
|
- Copyright 2014-2016 Joey Hess <id@joeyh.name>
|
2014-02-13 01:12:22 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.MetaData where
|
|
|
|
|
|
|
|
import Command
|
2014-02-23 04:08:29 +00:00
|
|
|
import Annex.MetaData
|
2017-08-14 17:55:38 +00:00
|
|
|
import Annex.VectorClock
|
2014-02-13 01:12:22 +00:00
|
|
|
import Logs.MetaData
|
2016-07-27 14:46:25 +00:00
|
|
|
import Annex.WorkTree
|
2016-07-26 23:50:02 +00:00
|
|
|
import Messages.JSON (JSONActionItem(..))
|
2016-07-27 14:46:25 +00:00
|
|
|
import Types.Messages
|
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 Utility.Aeson
|
make --batch honor matching options
When --batch is used with matching options like --in, --metadata, etc, only
operate on the provided files when they match those options. Otherwise, a
blank line is output in the batch protocol.
Affected commands: find, add, whereis, drop, copy, move, get
In the case of find, the documentation for --batch already said it honored
the matching options. The docs for the rest didn't, but it makes sense to
have them honor them. While this is a behavior change, why specify the
matching options with --batch if you didn't want them to apply?
Note that the batch output for all of the affected commands could
already output a blank line in other cases, so batch users should
already be prepared to deal with it.
git-annex metadata didn't seem worth making support the matching options,
since all it does is output metadata or set metadata, the use cases for
using it in combination with the martching options seem small. Made it
refuse to run when they're combined, leaving open the possibility for later
support if a use case develops.
This commit was sponsored by Brett Eisenberg on Patreon.
2018-08-08 16:03:30 +00:00
|
|
|
import Limit
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2016-07-27 14:46:25 +00:00
|
|
|
import qualified Data.Map as M
|
2016-07-26 18:53:00 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as BU
|
2016-12-13 15:07:49 +00:00
|
|
|
import Control.Concurrent
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = withGlobalOptions [jsonOptions, annexedMatchingOptions] $
|
2015-07-12 13:25:43 +00:00
|
|
|
command "metadata" SectionMetaData
|
|
|
|
"sets or gets metadata of a file"
|
|
|
|
paramPaths (seek <$$> optParser)
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2015-07-12 13:25:43 +00:00
|
|
|
data MetaDataOptions = MetaDataOptions
|
|
|
|
{ forFiles :: CmdParams
|
|
|
|
, getSet :: GetSet
|
|
|
|
, keyOptions :: Maybe KeyOptions
|
2016-07-27 14:46:25 +00:00
|
|
|
, batchOption :: BatchMode
|
2015-07-12 13:25:43 +00:00
|
|
|
}
|
2014-03-17 19:26:18 +00:00
|
|
|
|
2015-08-11 17:19:01 +00:00
|
|
|
data GetSet = Get MetaField | GetAll | Set [ModMeta]
|
2014-02-19 19:04:12 +00:00
|
|
|
|
2015-07-12 13:25:43 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser MetaDataOptions
|
|
|
|
optParser desc = MetaDataOptions
|
|
|
|
<$> cmdParams desc
|
2015-08-11 17:19:01 +00:00
|
|
|
<*> ((Get <$> getopt) <|> (Set <$> some modopts) <|> pure GetAll)
|
2016-08-03 16:37:12 +00:00
|
|
|
<*> optional parseKeyOptions
|
2016-07-27 14:46:25 +00:00
|
|
|
<*> parseBatchOption
|
2014-02-13 05:49:38 +00:00
|
|
|
where
|
2015-07-12 13:25:43 +00:00
|
|
|
getopt = option (eitherReader mkMetaField)
|
|
|
|
( long "get" <> short 'g' <> metavar paramField
|
|
|
|
<> help "get single metadata field"
|
|
|
|
)
|
|
|
|
modopts = option (eitherReader parseModMeta)
|
|
|
|
( long "set" <> short 's' <> metavar "FIELD[+-]=VALUE"
|
|
|
|
<> help "set or unset metadata value"
|
|
|
|
)
|
|
|
|
<|> (AddMeta tagMetaField . toMetaValue <$> strOption
|
|
|
|
( long "tag" <> short 't' <> metavar "TAG"
|
|
|
|
<> help "set a tag"
|
|
|
|
))
|
2016-02-29 17:00:46 +00:00
|
|
|
<|> (DelMeta tagMetaField . Just . toMetaValue <$> strOption
|
2015-07-12 13:25:43 +00:00
|
|
|
( long "untag" <> short 'u' <> metavar "TAG"
|
|
|
|
<> help "remove a tag"
|
|
|
|
))
|
2016-02-29 17:00:46 +00:00
|
|
|
<|> option (eitherReader (\f -> DelMeta <$> mkMetaField f <*> pure Nothing))
|
|
|
|
( long "remove" <> short 'r' <> metavar "FIELD"
|
|
|
|
<> help "remove all values of a field"
|
|
|
|
)
|
2017-09-28 16:36:10 +00:00
|
|
|
<|> flag' DelAllMeta
|
|
|
|
( long "remove-all"
|
|
|
|
<> help "remove all metadata"
|
|
|
|
)
|
2014-02-19 19:04:12 +00:00
|
|
|
|
2015-07-12 13:25:43 +00:00
|
|
|
seek :: MetaDataOptions -> CommandSeek
|
2016-12-13 15:07:49 +00:00
|
|
|
seek o = case batchOption o of
|
|
|
|
NoBatch -> do
|
2017-08-14 17:55:38 +00:00
|
|
|
c <- liftIO currentVectorClock
|
2016-12-13 15:07:49 +00:00
|
|
|
let seeker = case getSet o of
|
|
|
|
Get _ -> withFilesInGit
|
|
|
|
GetAll -> withFilesInGit
|
|
|
|
Set _ -> withFilesInGitNonRecursive
|
|
|
|
"Not recursively setting metadata. Use --force to do that."
|
|
|
|
withKeyOptions (keyOptions o) False
|
2018-10-01 18:12:06 +00:00
|
|
|
(commandAction . startKeys c o)
|
|
|
|
(seeker (commandAction . (whenAnnexed (start c o))))
|
2017-10-16 18:10:03 +00:00
|
|
|
=<< workTreeItems (forFiles o)
|
added -z
Added -z option to git-annex commands that use --batch, useful for
supporting filenames containing newlines.
It only controls input to --batch, the output will still be line delimited
unless --json or etc is used to get some other output. While git often
makes -z affect both input and output, I don't like trying them together,
and making it affect output would have been a significant complication,
and also git-annex output is generally not intended to be machine parsed,
unless using --json or a format option.
Commands that take pairs like "file key" still separate them with a space
in --batch mode. All such commands take care to support filenames with
spaces when parsing that, so there was no need to change it, and it would
have needed significant changes to the batch machinery to separate tose
with a null.
To make fromkey and registerurl support -z, I had to give them a --batch
option. The implicit batch mode they enter when not provided with input
parameters does not support -z as that would have complicated option
parsing. Seemed better to move these toward using the same --batch as
everything else, though the implicit batch mode can still be used.
This commit was sponsored by Ole-Morten Duesund on Patreon.
2018-09-20 20:09:21 +00:00
|
|
|
Batch fmt -> withMessageState $ \s -> case outputType s of
|
make --batch honor matching options
When --batch is used with matching options like --in, --metadata, etc, only
operate on the provided files when they match those options. Otherwise, a
blank line is output in the batch protocol.
Affected commands: find, add, whereis, drop, copy, move, get
In the case of find, the documentation for --batch already said it honored
the matching options. The docs for the rest didn't, but it makes sense to
have them honor them. While this is a behavior change, why specify the
matching options with --batch if you didn't want them to apply?
Note that the batch output for all of the affected commands could
already output a blank line in other cases, so batch users should
already be prepared to deal with it.
git-annex metadata didn't seem worth making support the matching options,
since all it does is output metadata or set metadata, the use cases for
using it in combination with the martching options seem small. Made it
refuse to run when they're combined, leaving open the possibility for later
support if a use case develops.
This commit was sponsored by Brett Eisenberg on Patreon.
2018-08-08 16:03:30 +00:00
|
|
|
JSONOutput _ -> ifM limited
|
|
|
|
( giveup "combining --batch with file matching options is not currently supported"
|
added -z
Added -z option to git-annex commands that use --batch, useful for
supporting filenames containing newlines.
It only controls input to --batch, the output will still be line delimited
unless --json or etc is used to get some other output. While git often
makes -z affect both input and output, I don't like trying them together,
and making it affect output would have been a significant complication,
and also git-annex output is generally not intended to be machine parsed,
unless using --json or a format option.
Commands that take pairs like "file key" still separate them with a space
in --batch mode. All such commands take care to support filenames with
spaces when parsing that, so there was no need to change it, and it would
have needed significant changes to the batch machinery to separate tose
with a null.
To make fromkey and registerurl support -z, I had to give them a --batch
option. The implicit batch mode they enter when not provided with input
parameters does not support -z as that would have complicated option
parsing. Seemed better to move these toward using the same --batch as
everything else, though the implicit batch mode can still be used.
This commit was sponsored by Ole-Morten Duesund on Patreon.
2018-09-20 20:09:21 +00:00
|
|
|
, batchInput fmt parseJSONInput $
|
make --batch honor matching options
When --batch is used with matching options like --in, --metadata, etc, only
operate on the provided files when they match those options. Otherwise, a
blank line is output in the batch protocol.
Affected commands: find, add, whereis, drop, copy, move, get
In the case of find, the documentation for --batch already said it honored
the matching options. The docs for the rest didn't, but it makes sense to
have them honor them. While this is a behavior change, why specify the
matching options with --batch if you didn't want them to apply?
Note that the batch output for all of the affected commands could
already output a blank line in other cases, so batch users should
already be prepared to deal with it.
git-annex metadata didn't seem worth making support the matching options,
since all it does is output metadata or set metadata, the use cases for
using it in combination with the martching options seem small. Made it
refuse to run when they're combined, leaving open the possibility for later
support if a use case develops.
This commit was sponsored by Brett Eisenberg on Patreon.
2018-08-08 16:03:30 +00:00
|
|
|
commandAction . startBatch
|
|
|
|
)
|
2016-12-13 15:07:49 +00:00
|
|
|
_ -> giveup "--batch is currently only supported in --json mode"
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
start :: VectorClock -> MetaDataOptions -> FilePath -> Key -> CommandStart
|
2018-10-01 18:12:06 +00:00
|
|
|
start c o file k = startKeys c o (k, mkActionItem afile)
|
2016-07-20 19:22:55 +00:00
|
|
|
where
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|
2014-03-17 19:26:18 +00:00
|
|
|
|
2018-10-01 18:12:06 +00:00
|
|
|
startKeys :: VectorClock -> MetaDataOptions -> (Key, ActionItem) -> CommandStart
|
|
|
|
startKeys c o (k, ai) = case getSet o of
|
2015-07-12 13:25:43 +00:00
|
|
|
Get f -> do
|
|
|
|
l <- S.toList . currentMetaDataValues f <$> getCurrentMetaData k
|
|
|
|
liftIO $ forM_ l $
|
|
|
|
putStrLn . fromMetaValue
|
|
|
|
stop
|
2015-08-11 17:19:01 +00:00
|
|
|
_ -> do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStartKey "metadata" k ai
|
2017-08-14 17:55:38 +00:00
|
|
|
next $ perform c o k
|
2015-08-11 17:19:01 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
perform :: VectorClock -> MetaDataOptions -> Key -> CommandPerform
|
|
|
|
perform c o k = case getSet o of
|
2015-08-11 17:19:01 +00:00
|
|
|
Set ms -> do
|
|
|
|
oldm <- getCurrentMetaData k
|
|
|
|
let m = combineMetaData $ map (modMeta oldm) ms
|
2018-08-31 16:23:22 +00:00
|
|
|
addMetaDataClocked k m c
|
2015-08-11 17:19:01 +00:00
|
|
|
next $ cleanup k
|
|
|
|
_ -> next $ cleanup k
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-13 01:48:25 +00:00
|
|
|
cleanup :: Key -> CommandCleanup
|
|
|
|
cleanup k = do
|
2016-07-26 18:53:00 +00:00
|
|
|
m <- getCurrentMetaData k
|
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 Object o = toJSON' (MetaDataFields m)
|
2016-07-26 18:53:00 +00:00
|
|
|
maybeShowJSON $ AesonObject o
|
|
|
|
showLongNote $ unlines $ concatMap showmeta $
|
|
|
|
map unwrapmeta (fromMetaData m)
|
2014-02-13 01:12:22 +00:00
|
|
|
return True
|
|
|
|
where
|
2014-02-23 17:58:16 +00:00
|
|
|
unwrapmeta (f, v) = (fromMetaField f, map fromMetaValue (S.toList v))
|
|
|
|
showmeta (f, vs) = map ((f ++ "=") ++) vs
|
2016-07-26 18:53:00 +00:00
|
|
|
|
|
|
|
-- Metadata serialized to JSON in the field named "fields" of
|
|
|
|
-- a larger object.
|
|
|
|
newtype MetaDataFields = MetaDataFields MetaData
|
|
|
|
deriving (Show)
|
|
|
|
|
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
|
|
|
instance ToJSON' MetaDataFields where
|
|
|
|
toJSON' (MetaDataFields m) = object [ (fieldsField, toJSON' m) ]
|
2016-07-26 18:53:00 +00:00
|
|
|
|
|
|
|
instance FromJSON MetaDataFields where
|
|
|
|
parseJSON (Object v) = do
|
|
|
|
f <- v .: fieldsField
|
|
|
|
case f of
|
|
|
|
Nothing -> return (MetaDataFields emptyMetaData)
|
|
|
|
Just v' -> MetaDataFields <$> parseJSON v'
|
|
|
|
parseJSON _ = fail "expected an object"
|
|
|
|
|
|
|
|
fieldsField :: T.Text
|
|
|
|
fieldsField = T.pack "fields"
|
|
|
|
|
2016-07-27 14:46:25 +00:00
|
|
|
parseJSONInput :: String -> Either String (Either FilePath Key, MetaData)
|
2016-07-26 18:53:00 +00:00
|
|
|
parseJSONInput i = do
|
2016-07-27 14:46:25 +00:00
|
|
|
v <- eitherDecode (BU.fromString i)
|
2016-07-26 23:50:02 +00:00
|
|
|
let m = case itemAdded v of
|
|
|
|
Nothing -> emptyMetaData
|
|
|
|
Just (MetaDataFields m') -> m'
|
2016-07-27 14:46:25 +00:00
|
|
|
case (itemKey v, itemFile v) of
|
|
|
|
(Just k, _) -> Right (Right k, m)
|
|
|
|
(Nothing, Just f) -> Right (Left f, m)
|
|
|
|
(Nothing, Nothing) -> Left "JSON input is missing either file or key"
|
|
|
|
|
2016-12-13 15:07:49 +00:00
|
|
|
startBatch :: (Either FilePath Key, MetaData) -> CommandStart
|
|
|
|
startBatch (i, (MetaData m)) = case i of
|
2016-07-27 14:46:25 +00:00
|
|
|
Left f -> do
|
|
|
|
mk <- lookupFile f
|
|
|
|
case mk of
|
2017-03-10 17:12:24 +00:00
|
|
|
Just k -> go k (mkActionItem (AssociatedFile (Just f)))
|
2016-11-16 01:29:54 +00:00
|
|
|
Nothing -> giveup $ "not an annexed file: " ++ f
|
2016-07-27 14:46:25 +00:00
|
|
|
Right k -> go k (mkActionItem k)
|
|
|
|
where
|
|
|
|
go k ai = do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStartKey "metadata" k ai
|
2016-07-27 14:46:25 +00:00
|
|
|
let o = MetaDataOptions
|
|
|
|
{ forFiles = []
|
|
|
|
, getSet = if MetaData m == emptyMetaData
|
|
|
|
then GetAll
|
|
|
|
else Set $ map mkModMeta (M.toList m)
|
|
|
|
, keyOptions = Nothing
|
|
|
|
, batchOption = NoBatch
|
|
|
|
}
|
2017-08-14 17:55:38 +00:00
|
|
|
t <- liftIO currentVectorClock
|
2016-12-13 15:07:49 +00:00
|
|
|
-- It would be bad if two batch mode changes used exactly
|
|
|
|
-- the same timestamp, since the order of adds and removals
|
|
|
|
-- of the same metadata value would then be indeterminate.
|
|
|
|
-- To guarantee that never happens, delay 1 microsecond,
|
|
|
|
-- so the timestamp will always be different. This is
|
|
|
|
-- probably less expensive than cleaner methods,
|
|
|
|
-- such as taking from a list of increasing timestamps.
|
|
|
|
liftIO $ threadDelay 1
|
2017-08-14 17:55:38 +00:00
|
|
|
next $ perform t o k
|
2016-07-27 14:46:25 +00:00
|
|
|
mkModMeta (f, s)
|
|
|
|
| S.null s = DelMeta f Nothing
|
|
|
|
| otherwise = SetMeta f s
|