From 02f0996e2563cc6d79a127833b269fca84044cc8 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 17 Sep 2024 13:43:11 -0400 Subject: [PATCH] git-annex sim log --- Annex/Sim.hs | 4 ++-- Command/Sim.hs | 15 +++++++++++---- doc/git-annex-sim.mdwn | 13 ++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Annex/Sim.hs b/Annex/Sim.hs index b0536387ff..ecbdfbafa0 100644 --- a/Annex/Sim.hs +++ b/Annex/Sim.hs @@ -67,7 +67,7 @@ data SimState t = SimState , simRebalance :: Bool , simHistory :: [SimCommand] , simVectorClock :: VectorClock - , simFile :: Maybe FilePath + , simLogFile :: Maybe FilePath , simRootDirectory :: FilePath } deriving (Show, Read) @@ -90,7 +90,7 @@ emptySimState rngseed rootdir = SimState , simRebalance = False , simHistory = [] , simVectorClock = VectorClock 0 - , simFile = Nothing + , simLogFile = Nothing , simRootDirectory = rootdir } diff --git a/Command/Sim.hs b/Command/Sim.hs index a3e096ceff..a4eef730d9 100644 --- a/Command/Sim.hs +++ b/Command/Sim.hs @@ -30,6 +30,7 @@ seek ("end":[]) = do simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir whenM (liftIO $ doesDirectoryExist simdir) $ do liftIO $ removeDirectoryRecursive simdir + showLongNote $ UnquotedString "Sim ended." seek ("visit":reponame:[]) = do simdir <- fromRepo gitAnnexSimDir liftIO (restoreSim simdir) >>= \case @@ -51,6 +52,13 @@ seek ("visit":reponame:[]) = do , "Choose from:" , unwords $ map fromRepoName $ M.keys (simRepos st) ] +seek ("show":[]) = do + simdir <- fromRepo gitAnnexSimDir + liftIO (restoreSim simdir) >>= \case + Left err -> giveup err + Right st -> case simLogFile st of + Just f -> liftIO $ putStr =<< readFile f + Nothing -> return () seek ps = case parseSimCommand ps of Left err -> giveup err Right simcmd -> do @@ -74,15 +82,14 @@ start simfile = do rng <- fst . random <$> initStdGen let st = (emptySimState rng simdir) - { simFile = Just simlogfile } + { simLogFile = Just simlogfile } case simfile of Nothing -> startup simdir st [] Just f -> liftIO (readFile f) >>= \c -> case parseSimFile c of Left err -> giveup err Right cs -> startup simdir st cs - showLongNote $ UnquotedString "Sim started, logging to sim file " - <> QuotedPath (toRawFilePath simlogfile) + showLongNote $ UnquotedString "Sim started." where startup simdir st cs = do repobyname <- mkGetExistingRepoByName @@ -98,6 +105,6 @@ start simfile = do saveState :: SimState SimRepo -> IO () saveState st = do suspendSim st - case simFile st of + case simLogFile st of Just f -> writeFile f $ generateSimFile $ reverse $ simHistory st Nothing -> noop diff --git a/doc/git-annex-sim.mdwn b/doc/git-annex-sim.mdwn index 9b86a041fe..55d8e3ea6e 100644 --- a/doc/git-annex-sim.mdwn +++ b/doc/git-annex-sim.mdwn @@ -10,6 +10,8 @@ git annex sim command git annex sim visit repo +git annex sim show + git annex sim end # DESCRIPTION @@ -24,17 +26,18 @@ run after starting it. These are in the form "git annex sim command" with the command in the same format used in the sim file (see sim commands list below). For example, "git annex sim step 1" runs the simulation one step. -The simulation writes to an output sim file as it runs, which contains the +The simulation keeps a log as it runs, which contains the entire simulation input, as well as the actions performed in the -simulation, and the results of the simulation. -This allows re-running the same simulation later, as well as analyzing -the results of the simulation. +simulation, and the results of the simulation. Use "git-annex sim show" +to display the log. This allows re-running the same simulation later, +as well as analyzing the results of the simulation. -While a simulation is running, the command "git annex visit repo", where +While a simulation is running, the command "git annex sim visit repo", where "repo" is the name of one of the repositories in the simulation, will spawn a subshell in a git repository whose git-annex branch contains the state of that simulated repository. This allows running any git-annex command, such as `git-annex whereis` to examine the state of the simulation. +You should avoid making any changes to git-annex state. Exit the subshell to end the visit. Use "git annex sim end" to finish the simulation, and clean up.