git-annex/Command/Init.hs
Joey Hess 780367200b
remove dead nodes when loading the cluster log
This is to avoid inserting a cluster uuid into the location log when
only dead nodes in the cluster contain the content of a key.

One reason why this is necessary is Remote.keyLocations, which excludes
dead repositories from the list. But there are probably many more.

Implementing this was challenging, because Logs.Location importing
Logs.Cluster which imports Logs.Trust which imports Remote.List resulted
in an import cycle through several other modules.

Resorted to making Logs.Location not import Logs.Cluster, and instead
it assumes that Annex.clusters gets populated when necessary before it's
called.

That's done in Annex.Startup, which is run by the git-annex command
(but not other commands) at early startup in initialized repos. Or,
is run after initialization.

Note that is Remote.Git, it is unable to import Annex.Startup, because
Remote.Git importing Logs.Cluster leads the the same import cycle.
So ensureInitialized is not passed annexStartup in there.

Other commands, like git-annex-shell currently don't run annexStartup
either.

So there are cases where Logs.Location will not see clusters. So it won't add
any cluster UUIDs when loading the log. That's ok, the only reason to do
that is to make display of where objects are located include clusters,
and to make commands like git-annex get --from treat keys as being located
in a cluster. git-annex-shell certainly does not do anything like that,
and I'm pretty sure Remote.Git (and callers to Remote.Git.onLocalRepo)
don't either.
2024-06-16 14:39:44 -04:00

91 lines
2.5 KiB
Haskell

{- git-annex command
-
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
{-# LANGUAGE OverloadedStrings #-}
module Command.Init where
import Command
import Annex.Init
import Annex.Version
import Annex.Startup
import Types.RepoVersion
import qualified Annex.SpecialRemote
import Control.Monad.Fail as Fail (MonadFail(..))
import qualified Data.Map as M
cmd :: Command
cmd = dontCheck repoExists $ withAnnexOptions [jsonOptions] $
command "init" SectionSetup "initialize git-annex"
paramDesc (seek <$$> optParser)
data InitOptions = InitOptions
{ initDesc :: String
, initVersion :: Maybe RepoVersion
, autoEnableOnly :: Bool
, noAutoEnable :: Bool
}
optParser :: CmdParamsDesc -> Parser InitOptions
optParser desc = InitOptions
<$> (unwords <$> cmdParams desc)
<*> optional (option (str >>= parseRepoVersion)
( long "version" <> metavar paramValue
<> help "Override default annex.version"
))
<*> switch
( long "autoenable"
<> help "only enable special remotes configured with autoenable=true"
)
<*> switch
( long "no-autoenable"
<> help "do not enable special remotes configured with autoenable=true"
)
parseRepoVersion :: MonadFail m => String -> m RepoVersion
parseRepoVersion s = case RepoVersion <$> readish s of
Nothing -> Fail.fail $ "version parse error"
Just v
| v `elem` supportedVersions -> return v
| otherwise -> case M.lookup v autoUpgradeableVersions of
Just v' -> return v'
Nothing -> Fail.fail $ s ++ " is not a currently supported repository version"
seek :: InitOptions -> CommandSeek
seek = commandAction . start
start :: InitOptions -> CommandStart
start os
| autoEnableOnly os =
starting "init" (ActionItemOther (Just "autoenable")) si $
performAutoEnableOnly
| otherwise =
starting "init" (ActionItemOther (Just $ UnquotedString $ initDesc os)) si $
perform os
where
si = SeekInput []
perform :: InitOptions -> CommandPerform
perform os = do
case initVersion os of
Nothing -> noop
Just wantversion -> getVersion >>= \case
Just v | v /= wantversion ->
giveup $ "This repository is already a initialized with version " ++ show (fromRepoVersion v) ++ ", not changing to requested version."
_ -> noop
initialize startupAnnex
(if null (initDesc os) then Nothing else Just (initDesc os))
(initVersion os)
unless (noAutoEnable os)
Annex.SpecialRemote.autoEnable
next $ return True
performAutoEnableOnly :: CommandPerform
performAutoEnableOnly = do
Annex.SpecialRemote.autoEnable
next $ return True