sim: Add metadata command
authorJoey Hess <joeyh@joeyh.name>
Thu, 26 Sep 2024 16:20:37 +0000 (12:20 -0400)
committerJoey Hess <joeyh@joeyh.name>
Thu, 26 Sep 2024 16:20:37 +0000 (12:20 -0400)
Only really needed for completeness, preferred content expressions can
match against metadata.

Annex/Sim.hs
Annex/Sim/File.hs
Types/MetaData.hs
doc/git-annex-sim.mdwn
doc/todo/git-annex_proxies.mdwn

index f35f1b85c799efb50bf82f3591982659ad489fb3..08293152fb865d05703e78cbcca34016ed63e2fd 100644 (file)
@@ -27,6 +27,7 @@ import Annex.Link
 import Annex.Wanted
 import Annex.CatFile
 import Annex.Action (quiesce)
+import Annex.MetaData
 import Logs.Group
 import Logs.Trust
 import Logs.PreferredContent
@@ -36,6 +37,7 @@ import Logs.MaxSize
 import Logs.Difference
 import Logs.UUID
 import Logs.Location
+import Logs.MetaData
 import Utility.Env
 import qualified Annex
 import qualified Remote
@@ -67,6 +69,7 @@ data SimState t = SimState
        , simNumCopies :: NumCopies
        , simMinCopies :: MinCopies
        , simGroups :: M.Map UUID (S.Set Group)
+       , simMetaData :: M.Map Key MetaData
        , simWanted :: M.Map UUID PreferredContentExpression
        , simRequired :: M.Map UUID PreferredContentExpression
        , simGroupWanted :: M.Map Group PreferredContentExpression
@@ -91,6 +94,7 @@ emptySimState rngseed rootdir = SimState
        , simNumCopies = configuredNumCopies 1
        , simMinCopies = configuredMinCopies 1
        , simGroups = mempty
+       , simMetaData = mempty
        , simWanted = mempty
        , simRequired = mempty
        , simGroupWanted = mempty
@@ -245,6 +249,7 @@ data SimCommand
        | CommandTrustLevel RepoName TrustLevel
        | CommandGroup RepoName Group
        | CommandUngroup RepoName Group
+       | CommandMetaData RawFilePath String
        | CommandWanted RepoName PreferredContentExpression
        | CommandRequired RepoName PreferredContentExpression
        | CommandGroupWanted Group PreferredContentExpression
@@ -508,6 +513,19 @@ applySimCommand' (CommandUngroup repo groupname) st _ =
                Right $ Right $ st
                        { simGroups = M.adjust (S.delete groupname) u (simGroups st)
                        }
+applySimCommand' (CommandMetaData file modmetaexpr) st _ =
+       case parseModMeta modmetaexpr of
+               Left err -> Left err
+               Right modmeta -> case M.lookup file (simFiles st) of
+                       Nothing -> Left $ "Cannot set metadata of unknown file " ++ fromRawFilePath file
+                       Just k -> Right $ Right $ st
+                               { simMetaData = M.alter (addmeta modmeta) k
+                                       (simMetaData st)
+                               }
+  where
+       addmeta modmeta (Just metadata) = Just $ unionMetaData metadata $
+               modMeta metadata modmeta
+       addmeta modmeta Nothing = Just $ modMeta emptyMetaData modmeta
 applySimCommand' (CommandWanted repo expr) st _ = 
        checkKnownRepo repo st $ \u ->
                checkValidPreferredContentExpression [expr] $ Right $ st
@@ -1229,6 +1247,12 @@ updateSimRepoState newst sr = do
                        , addDiff = \u -> groupChange u . const
                        , removeDiff = const . flip groupChange (const mempty)
                        }
+               updateField oldst newst simMetaData $ DiffUpdate
+                       { replaceDiff = replaceNew addMetaData
+                       , addDiff = addMetaData
+                       , removeDiff = \k old -> addMetaData k $
+                               modMeta old DelAllMeta
+                       }
                updateField oldst newst simWanted $ DiffUpdate
                        { replaceDiff = replaceNew preferredContentSet
                        , addDiff = preferredContentSet
index 554a3a621b9e89faa22525ee6f3cd193941c9469..ad0b219bd63ab4e2b4fdcd505361e9201fc26cbb 100644 (file)
@@ -73,6 +73,8 @@ generateSimFile = unlines . map unwords . go
                ["group", repo, fromGroup group] : go rest
        go (CommandUngroup (RepoName repo) group : rest) =
                ["ungroup", repo, fromGroup group] : go rest
+       go (CommandMetaData f modmeta : rest) =
+               ["metadata", fromRawFilePath f, modmeta] : go rest
        go (CommandWanted (RepoName repo) expr : rest) =
                ["wanted", repo, expr] : go rest
        go (CommandRequired (RepoName repo) expr : rest) =
@@ -188,6 +190,8 @@ parseSimCommand ("group":repo:group:[]) =
        Right $ CommandGroup (RepoName repo) (toGroup group)
 parseSimCommand ("ungroup":repo:group:[]) =
        Right $ CommandUngroup (RepoName repo) (toGroup group)
+parseSimCommand ("metadata":file:modmeta:[]) =
+       Right $ CommandMetaData (toRawFilePath file) modmeta
 parseSimCommand ("wanted":repo:expr) =
        Right $ CommandWanted (RepoName repo) (unwords expr)
 parseSimCommand ("required":repo:expr) =
index 316c06a2f7ac4717d83b0535cf2e53c859ffd97a..ef6331879949b2125691a05d344410b0b2de8482 100644 (file)
@@ -62,7 +62,7 @@ import qualified Data.ByteString as B
 import qualified Data.ByteString.Char8 as B8
 
 newtype MetaData = MetaData (M.Map MetaField (S.Set MetaValue))
-       deriving (Show, Eq, Ord)
+       deriving (Read, Show, Eq, Ord)
 
 instance ToJSON' MetaData where
        toJSON' (MetaData m) = object $ map go (M.toList m)
index e7ecec5b69b2c40ba9df0c7d690eb6d9e2f5b545..25214acd10e139b638ea8a11e32921090873445e 100644 (file)
@@ -309,16 +309,6 @@ as passed to "git annex sim" while a simulation is running.
   Sets the trust level of the repository. This is equivilant to
   [[git-annex-trust]](1), [[git-annex-untrust]](1), etc.
 
-* `group repo group`
-
-  Add a repository to a group. This is equivilant to
-  [[git-annex-group]](1).
-
-* `ungroup repo group`
-
-  Remove a repository from a group. This is equivilant to
-  [[git-annex-ungroup]](1).
-
 * `wanted repo expression`
 
   Configure the preferred content of a repository. This is equivilant
@@ -352,6 +342,22 @@ as passed to "git annex sim" while a simulation is running.
   
   Configure the groupwanted to a random expression.
 
+* `group repo group`
+
+  Add a repository to a group. This is equivilant to
+  [[git-annex-group]](1).
+
+* `ungroup repo group`
+
+  Remove a repository from a group. This is equivilant to
+  [[git-annex-ungroup]](1).
+
+* `metadata filename expression`
+
+  Change the metadata of the simulated file. The expression is
+  in the same format as the --set option of the [[git-annex-metadata]]
+  command. For example: `metadata foo year=2025`
+
 * `maxsize repo size`
 
   Configure the maximum size of a repository. This is equivilant to
index 812bee88db1f9d4ee5a52a81fbfd5b6047644637..f6edadade85035c6703c64f89a29de7dd2d6a7b0 100644 (file)
@@ -28,11 +28,7 @@ Planned schedule of work:
 
 ## work notes
 
-* Currently working in [[todo/proving_preferred_content_behavior]]
-  in the `sim` branch.
-
-* sim: Add support for metadata, so preferred content that matches on it
-  will work
+* Currently working in [[todo/proving_preferred_content_behavior]].
 
 * The sim cannot be safely interrupted, or two processes be run
   concurrently. Both unlike other git-annex commands. Either document these