2014-02-13 01:12:22 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.MetaData where
|
|
|
|
|
|
|
|
import Common.Annex
|
2014-02-13 05:49:38 +00:00
|
|
|
import qualified Annex
|
2014-02-13 01:12:22 +00:00
|
|
|
import Command
|
2014-02-23 04:08:29 +00:00
|
|
|
import Annex.MetaData
|
2014-02-13 01:12:22 +00:00
|
|
|
import Logs.MetaData
|
|
|
|
import Types.MetaData
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2014-02-13 05:57:43 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
def :: [Command]
|
2014-02-23 17:58:16 +00:00
|
|
|
def = [withOptions [setOption, tagOption, untagOption, jsonOption] $
|
2014-02-19 19:04:12 +00:00
|
|
|
command "metadata" paramPaths seek
|
2014-02-19 18:55:34 +00:00
|
|
|
SectionMetaData "sets metadata of a file"]
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-19 19:04:12 +00:00
|
|
|
storeModMeta :: ModMeta -> Annex ()
|
|
|
|
storeModMeta modmeta = Annex.changeState $
|
|
|
|
\s -> s { Annex.modmeta = modmeta:Annex.modmeta s }
|
|
|
|
|
2014-02-13 05:49:38 +00:00
|
|
|
setOption :: Option
|
2014-02-13 06:24:30 +00:00
|
|
|
setOption = Option ['s'] ["set"] (ReqArg mkmod "FIELD[+-]=VALUE") "set metadata"
|
2014-02-13 05:49:38 +00:00
|
|
|
where
|
2014-02-19 19:04:12 +00:00
|
|
|
mkmod = either error storeModMeta . parseModMeta
|
|
|
|
|
|
|
|
tagOption :: Option
|
|
|
|
tagOption = Option ['t'] ["tag"] (ReqArg mkmod "TAG") "set a tag"
|
|
|
|
where
|
|
|
|
mkmod = storeModMeta . AddMeta tagMetaField . toMetaValue
|
|
|
|
|
|
|
|
untagOption :: Option
|
|
|
|
untagOption = Option ['u'] ["untag"] (ReqArg mkmod "TAG") "remove a tag"
|
|
|
|
where
|
|
|
|
mkmod = storeModMeta . AddMeta tagMetaField . mkMetaValue (CurrentlySet False)
|
2014-02-13 05:49:38 +00:00
|
|
|
|
2014-02-13 01:12:22 +00:00
|
|
|
seek :: CommandSeek
|
2014-02-13 05:49:38 +00:00
|
|
|
seek ps = do
|
|
|
|
modmeta <- Annex.getState Annex.modmeta
|
2014-02-13 05:57:43 +00:00
|
|
|
now <- liftIO getPOSIXTime
|
|
|
|
withFilesInGit (whenAnnexed $ start now modmeta) ps
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-13 05:57:43 +00:00
|
|
|
start :: POSIXTime -> [ModMeta] -> FilePath -> (Key, Backend) -> CommandStart
|
|
|
|
start now ms file (k, _) = do
|
2014-02-13 05:49:38 +00:00
|
|
|
showStart "metadata" file
|
2014-02-13 05:57:43 +00:00
|
|
|
next $ perform now ms k
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-13 05:57:43 +00:00
|
|
|
perform :: POSIXTime -> [ModMeta] -> Key -> CommandPerform
|
|
|
|
perform _ [] k = next $ cleanup k
|
|
|
|
perform now ms k = do
|
2014-02-13 01:48:25 +00:00
|
|
|
oldm <- getCurrentMetaData k
|
2014-02-23 04:08:29 +00:00
|
|
|
let m = foldl' unionMetaData emptyMetaData $ map (modMeta oldm) ms
|
2014-02-13 05:57:43 +00:00
|
|
|
addMetaData' k m now
|
2014-02-13 01:48:25 +00:00
|
|
|
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
|
2014-02-23 17:58:16 +00:00
|
|
|
l <- map unwrapmeta . fromMetaData <$> getCurrentMetaData k
|
|
|
|
maybeShowJSON l
|
|
|
|
showLongNote $ unlines $ concatMap showmeta l
|
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
|