2014-02-23 04:08:29 +00:00
|
|
|
{- git-annex metadata
|
|
|
|
-
|
2016-02-27 14:55:02 +00:00
|
|
|
- Copyright 2014-2016 Joey Hess <id@joeyh.name>
|
2014-02-23 04:08:29 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-02-23 04:08:29 +00:00
|
|
|
-}
|
|
|
|
|
2014-03-18 22:55:43 +00:00
|
|
|
module Annex.MetaData (
|
|
|
|
genMetaData,
|
2014-07-03 18:35:20 +00:00
|
|
|
dateMetaData,
|
2016-02-27 14:55:02 +00:00
|
|
|
parseModMeta,
|
|
|
|
parseMetaDataMatcher,
|
2014-03-18 22:55:43 +00:00
|
|
|
module X
|
|
|
|
) where
|
2014-02-23 04:08:29 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-02-23 04:08:29 +00:00
|
|
|
import qualified Annex
|
2014-03-18 22:55:43 +00:00
|
|
|
import Types.MetaData as X
|
|
|
|
import Annex.MetaData.StandardFields as X
|
2014-02-23 04:08:29 +00:00
|
|
|
import Logs.MetaData
|
2014-02-24 18:41:33 +00:00
|
|
|
import Annex.CatFile
|
2016-02-27 14:55:02 +00:00
|
|
|
import Utility.Glob
|
2014-02-23 04:08:29 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2019-01-07 19:51:05 +00:00
|
|
|
import qualified Data.Text as T
|
2014-02-23 04:08:29 +00:00
|
|
|
import Data.Time.Calendar
|
|
|
|
import Data.Time.Clock
|
|
|
|
import Data.Time.Clock.POSIX
|
2022-12-22 18:26:35 +00:00
|
|
|
import Text.Read
|
2014-02-23 04:08:29 +00:00
|
|
|
|
2014-02-24 18:41:33 +00:00
|
|
|
{- Adds metadata for a file that has just been ingested into the
|
|
|
|
- annex, but has not yet been committed to git.
|
2014-02-23 04:08:29 +00:00
|
|
|
-
|
2014-02-24 18:41:33 +00:00
|
|
|
- When the file has been modified, the metadata is copied over
|
|
|
|
- from the old key to the new key. Note that it looks at the old key as
|
|
|
|
- committed to HEAD -- the new key may or may not have already been staged
|
2016-01-07 18:21:12 +00:00
|
|
|
- in the index.
|
2014-02-24 18:41:33 +00:00
|
|
|
-
|
|
|
|
- Also, can generate new metadata, if configured to do so.
|
2014-02-23 04:08:29 +00:00
|
|
|
-}
|
2021-12-09 19:25:59 +00:00
|
|
|
genMetaData :: Key -> RawFilePath -> Maybe POSIXTime -> Annex ()
|
|
|
|
genMetaData key file mmtime = do
|
2017-11-15 20:55:38 +00:00
|
|
|
catKeyFileHEAD file >>= \case
|
2017-09-28 16:56:35 +00:00
|
|
|
Nothing -> noop
|
2018-04-04 17:42:15 +00:00
|
|
|
Just oldkey ->
|
|
|
|
-- Have to copy first, before adding any
|
|
|
|
-- more metadata, because copyMetaData does not
|
|
|
|
-- preserve any metadata already on key.
|
|
|
|
whenM (copyMetaData oldkey key <&&> (not <$> onlydatemeta oldkey)) $
|
2017-09-28 16:56:35 +00:00
|
|
|
warncopied
|
2021-12-09 19:25:59 +00:00
|
|
|
whenM (annexGenMetaData <$> Annex.getGitConfig) $
|
|
|
|
case mmtime of
|
|
|
|
Just mtime -> do
|
|
|
|
old <- getCurrentMetaData key
|
|
|
|
addMetaData key $
|
|
|
|
dateMetaData (posixSecondsToUTCTime mtime) old
|
|
|
|
Nothing -> noop
|
import metadata from feeds
When annex.genmetadata is set, metadata from the feed is added to files
that are imported from it.
Reused the same feedtitle and itemtitle, feedauthor, itemauthor, etc names
that are used in --template.
Also added title and author, which are the item title/author if available,
falling back to the feed title/author. These are more likely to be common
metadata fields.
(There is a small bit of dupication here, but once git gets
around to packing the object, it will compress it away.)
The itempubdate field is not included in the metadata as a string; instead
it is used to generate year and month fields, same as is done when adding
files with annex.genmetadata set.
This commit was sponsored by Amitai Schlair, who cooincidentially
is responsible for ikiwiki generating nice feed metadata!
2014-07-03 17:46:09 +00:00
|
|
|
where
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
warncopied = warning $ UnquotedString $
|
2019-11-26 19:27:22 +00:00
|
|
|
"Copied metadata from old version of " ++ fromRawFilePath file ++ " to new version. " ++
|
|
|
|
"If you don't want this copied metadata, run: git annex metadata --remove-all " ++ fromRawFilePath file
|
2018-04-04 17:42:15 +00:00
|
|
|
-- If the only fields copied were date metadata, and they'll
|
|
|
|
-- be overwritten with the current mtime, no need to warn about
|
|
|
|
-- copying.
|
|
|
|
onlydatemeta oldkey = ifM (annexGenMetaData <$> Annex.getGitConfig)
|
|
|
|
( null . filter (not . isDateMetaField . fst) . fromMetaData
|
|
|
|
<$> getCurrentMetaData oldkey
|
|
|
|
, return False
|
|
|
|
)
|
2014-02-24 18:41:33 +00:00
|
|
|
|
import metadata from feeds
When annex.genmetadata is set, metadata from the feed is added to files
that are imported from it.
Reused the same feedtitle and itemtitle, feedauthor, itemauthor, etc names
that are used in --template.
Also added title and author, which are the item title/author if available,
falling back to the feed title/author. These are more likely to be common
metadata fields.
(There is a small bit of dupication here, but once git gets
around to packing the object, it will compress it away.)
The itempubdate field is not included in the metadata as a string; instead
it is used to generate year and month fields, same as is done when adding
files with annex.genmetadata set.
This commit was sponsored by Amitai Schlair, who cooincidentially
is responsible for ikiwiki generating nice feed metadata!
2014-07-03 17:46:09 +00:00
|
|
|
{- Generates metadata for a file's date stamp.
|
2018-04-04 17:42:15 +00:00
|
|
|
-
|
|
|
|
- Any date fields in the old metadata will be overwritten.
|
|
|
|
-
|
|
|
|
- Note that the returned MetaData does not contain all the input MetaData,
|
|
|
|
- only changes to add the date fields. -}
|
2014-07-03 18:35:20 +00:00
|
|
|
dateMetaData :: UTCTime -> MetaData -> MetaData
|
2018-04-04 17:42:15 +00:00
|
|
|
dateMetaData mtime old = modMeta old $
|
2021-08-11 00:45:02 +00:00
|
|
|
(SetMeta yearMetaField $ S.singleton $ toMetaValue $ encodeBS $ show y)
|
2018-04-04 17:42:15 +00:00
|
|
|
`ComposeModMeta`
|
2021-08-11 00:45:02 +00:00
|
|
|
(SetMeta monthMetaField $ S.singleton $ toMetaValue $ encodeBS $ show m)
|
2018-04-04 17:42:15 +00:00
|
|
|
`ComposeModMeta`
|
2021-08-11 00:45:02 +00:00
|
|
|
(SetMeta dayMetaField $ S.singleton $ toMetaValue $ encodeBS $ show d)
|
2014-02-23 04:08:29 +00:00
|
|
|
where
|
2017-10-25 19:11:12 +00:00
|
|
|
(y, m, d) = toGregorian $ utctDay mtime
|
2016-02-27 14:55:02 +00:00
|
|
|
|
|
|
|
{- Parses field=value, field+=value, field-=value, field?=value -}
|
|
|
|
parseModMeta :: String -> Either String ModMeta
|
|
|
|
parseModMeta p = case lastMaybe f of
|
2019-01-07 19:51:05 +00:00
|
|
|
Just '+' -> AddMeta <$> mkMetaField (T.pack f') <*> v
|
|
|
|
Just '-' -> DelMeta <$> mkMetaField (T.pack f') <*> (Just <$> v)
|
|
|
|
Just '?' -> MaybeSetMeta <$> mkMetaField (T.pack f') <*> v
|
|
|
|
_ -> SetMeta <$> mkMetaField (T.pack f) <*> (S.singleton <$> v)
|
2016-02-27 14:55:02 +00:00
|
|
|
where
|
|
|
|
(f, sv) = separate (== '=') p
|
|
|
|
f' = beginning f
|
2019-01-07 19:51:05 +00:00
|
|
|
v = pure (toMetaValue (encodeBS sv))
|
2016-02-27 14:55:02 +00:00
|
|
|
|
|
|
|
{- Parses field=value, field<value, field<=value, field>value, field>=value -}
|
|
|
|
parseMetaDataMatcher :: String -> Either String (MetaField, MetaValue -> Bool)
|
|
|
|
parseMetaDataMatcher p = (,)
|
2019-01-07 19:51:05 +00:00
|
|
|
<$> mkMetaField (T.pack f)
|
2016-02-27 14:55:02 +00:00
|
|
|
<*> pure matcher
|
|
|
|
where
|
|
|
|
(f, op_v) = break (`elem` "=<>") p
|
|
|
|
matcher = case op_v of
|
|
|
|
('=':v) -> checkglob v
|
2022-12-12 17:33:24 +00:00
|
|
|
('<':'=':v) -> checkcmp (<=) (<=) v
|
|
|
|
('<':v) -> checkcmp (<) (<) v
|
|
|
|
('>':'=':v) -> checkcmp (>=) (>=) v
|
|
|
|
('>':v) -> checkcmp (>) (>) v
|
2016-02-27 14:55:02 +00:00
|
|
|
_ -> checkglob ""
|
|
|
|
checkglob v =
|
2023-03-13 23:06:23 +00:00
|
|
|
let cglob = compileGlob v CaseInsensitive (GlobFilePath False)
|
2019-01-07 19:51:05 +00:00
|
|
|
in matchGlob cglob . decodeBS . fromMetaValue
|
2022-12-12 17:33:24 +00:00
|
|
|
checkcmp cmp cmp' v mv' =
|
|
|
|
let v' = decodeBS (fromMetaValue mv')
|
|
|
|
in case (doubleval v, doubleval v') of
|
|
|
|
(Just d, Just d') -> d' `cmp` d
|
|
|
|
_ -> v' `cmp'` v
|
2022-12-22 18:26:35 +00:00
|
|
|
doubleval v = readMaybe v :: Maybe Double
|