2014-02-23 04:08:29 +00:00
|
|
|
|
{- git-annex metadata
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Annex.MetaData where
|
|
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
|
import qualified Annex
|
|
|
|
|
import Types.MetaData
|
|
|
|
|
import Logs.MetaData
|
2014-02-24 18:41:33 +00:00
|
|
|
|
import Annex.CatFile
|
2014-02-23 04:08:29 +00:00
|
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
import Data.Time.Calendar
|
|
|
|
|
import Data.Time.Clock
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
|
|
tagMetaField :: MetaField
|
2014-02-25 22:45:09 +00:00
|
|
|
|
tagMetaField = mkMetaFieldUnchecked "tag"
|
2014-02-23 04:08:29 +00:00
|
|
|
|
|
|
|
|
|
yearMetaField :: MetaField
|
2014-02-25 22:45:09 +00:00
|
|
|
|
yearMetaField = mkMetaFieldUnchecked "year"
|
2014-02-23 04:08:29 +00:00
|
|
|
|
|
|
|
|
|
monthMetaField :: MetaField
|
2014-02-25 22:45:09 +00:00
|
|
|
|
monthMetaField = mkMetaFieldUnchecked "month"
|
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
|
|
|
|
|
- in th annex.
|
|
|
|
|
-
|
|
|
|
|
- Also, can generate new metadata, if configured to do so.
|
2014-02-23 04:08:29 +00:00
|
|
|
|
-}
|
2014-02-24 18:41:33 +00:00
|
|
|
|
genMetaData :: Key -> FilePath -> FileStatus -> Annex ()
|
|
|
|
|
genMetaData key file status = do
|
|
|
|
|
maybe noop (flip copyMetaData key) =<< catKeyFileHEAD file
|
|
|
|
|
whenM (annexGenMetaData <$> Annex.getGitConfig) $ do
|
|
|
|
|
metadata <- getCurrentMetaData key
|
|
|
|
|
let metadata' = genMetaData' status metadata
|
|
|
|
|
unless (metadata' == emptyMetaData) $
|
|
|
|
|
addMetaData key metadata'
|
|
|
|
|
|
|
|
|
|
{- Generates metadata from the FileStatus.
|
|
|
|
|
- Does not overwrite any existing metadata values. -}
|
2014-02-23 04:08:29 +00:00
|
|
|
|
genMetaData' :: FileStatus -> MetaData -> MetaData
|
|
|
|
|
genMetaData' status old = MetaData $ M.fromList $ filter isnew
|
|
|
|
|
[ (yearMetaField, S.singleton $ toMetaValue $ show y)
|
|
|
|
|
, (monthMetaField, S.singleton $ toMetaValue $ show m)
|
|
|
|
|
]
|
|
|
|
|
where
|
|
|
|
|
isnew (f, _) = S.null (currentMetaDataValues f old)
|
|
|
|
|
(y, m, _d) = toGregorian $ utctDay $
|
|
|
|
|
posixSecondsToUTCTime $ realToFrac $
|
|
|
|
|
modificationTime status
|