info: Added --autoenable option

Use cases include using git-annex init --no-autoenable and then going back
and enabling the special remotes that have autoenable configured. As well
as just querying to remember which ones have it enabled.

It lists all special remotes that have autoenable=yes whether currently
enabled or not. And it can be used with --json.

I pondered making this "git-annex info autoenable", but that seemed wrong
because then if the use has a directory named "autoenable", it's unclear
what they are asking for. (Although "git-annex info remote" may be
similarly unclear.) Making it an option does mean that it can't be provided
via --batch though.

Sponsored-by: Dartmouth College's Datalad project
This commit is contained in:
Joey Hess 2022-06-01 14:20:38 -04:00
parent 0d50c90794
commit c59ea5b1ca
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 57 additions and 11 deletions

View file

@ -63,7 +63,10 @@ newConfig name sameas fromuser m = case sameas of
specialRemoteMap :: Annex (M.Map UUID RemoteName) specialRemoteMap :: Annex (M.Map UUID RemoteName)
specialRemoteMap = do specialRemoteMap = do
m <- Logs.Remote.remoteConfigMap m <- Logs.Remote.remoteConfigMap
return $ M.fromList $ mapMaybe go (M.toList m) return $ specialRemoteNameMap m
specialRemoteNameMap :: M.Map UUID RemoteConfig -> M.Map UUID RemoteName
specialRemoteNameMap = M.fromList . mapMaybe go . M.toList
where where
go (u, c) = case lookupName c of go (u, c) = case lookupName c of
Nothing -> Nothing Nothing -> Nothing
@ -85,14 +88,14 @@ findType config = maybe unspecified (specified . fromProposedAccepted) $
autoEnable :: Annex () autoEnable :: Annex ()
autoEnable = do autoEnable = do
remotemap <- M.filter configured <$> remoteConfigMap m <- autoEnableable
enabled <- getenabledremotes enabled <- getenabledremotes
forM_ (M.toList remotemap) $ \(cu, c) -> unless (cu `M.member` enabled) $ do forM_ (M.toList m) $ \(cu, c) -> unless (cu `M.member` enabled) $ do
let u = case findSameasUUID c of let u = case findSameasUUID c of
Just (Sameas u') -> u' Just (Sameas u') -> u'
Nothing -> cu Nothing -> cu
case (lookupName c, findType c) of case (lookupName c, findType c) of
(Just name, Right t) -> whenM (canenable u) $ do (Just name, Right t) -> do
showSideAction $ "Auto enabling special remote " ++ name showSideAction $ "Auto enabling special remote " ++ name
dummycfg <- liftIO dummyRemoteGitConfig dummycfg <- liftIO dummyRemoteGitConfig
tryNonAsync (setup t (AutoEnable c) (Just u) Nothing c dummycfg) >>= \case tryNonAsync (setup t (AutoEnable c) (Just u) Nothing c dummycfg) >>= \case
@ -102,13 +105,25 @@ autoEnable = do
setConfig (remoteAnnexConfig c "config-uuid") (fromUUID cu) setConfig (remoteAnnexConfig c "config-uuid") (fromUUID cu)
_ -> return () _ -> return ()
where where
configured rc = fromMaybe False $
trueFalseParser' . fromProposedAccepted
=<< M.lookup autoEnableField rc
canenable u = (/= DeadTrusted) <$> lookupTrust u
getenabledremotes = M.fromList getenabledremotes = M.fromList
. map (\r -> (getcu r, r)) . map (\r -> (getcu r, r))
<$> remoteList <$> remoteList
getcu r = fromMaybe getcu r = fromMaybe
(Remote.uuid r) (Remote.uuid r)
(remoteAnnexConfigUUID (Remote.gitconfig r)) (remoteAnnexConfigUUID (Remote.gitconfig r))
autoEnableable :: Annex (M.Map UUID RemoteConfig)
autoEnableable = do
tm <- trustMap
(M.filterWithKey (notdead tm) . M.filter configured)
<$> remoteConfigMap
where
configured c = fromMaybe False $
trueFalseParser' . fromProposedAccepted
=<< M.lookup autoEnableField c
notdead tm cu c =
let u = case findSameasUUID c of
Just (Sameas u') -> u'
Nothing -> cu
in lookupTrust' u tm /= DeadTrusted

View file

@ -1,6 +1,7 @@
git-annex (10.20220526) UNRELEASED; urgency=medium git-annex (10.20220526) UNRELEASED; urgency=medium
* init: Added --no-autoenable option. * init: Added --no-autoenable option.
* info: Added --autoenable option.
-- Joey Hess <id@joeyh.name> Wed, 01 Jun 2022 13:23:05 -0400 -- Joey Hess <id@joeyh.name> Wed, 01 Jun 2022 13:23:05 -0400

View file

@ -22,6 +22,7 @@ import qualified Git
import qualified Annex import qualified Annex
import qualified Remote import qualified Remote
import qualified Types.Remote as Remote import qualified Types.Remote as Remote
import qualified Annex.SpecialRemote as SpecialRemote
import Utility.DataUnits import Utility.DataUnits
import Utility.DiskFree import Utility.DiskFree
import Annex.Content import Annex.Content
@ -105,6 +106,7 @@ data InfoOptions = InfoOptions
{ infoFor :: CmdParams { infoFor :: CmdParams
, bytesOption :: Bool , bytesOption :: Bool
, batchOption :: BatchMode , batchOption :: BatchMode
, autoenableOption :: Bool
} }
optParser :: CmdParamsDesc -> Parser InfoOptions optParser :: CmdParamsDesc -> Parser InfoOptions
@ -115,6 +117,10 @@ optParser desc = InfoOptions
<> help "display file sizes in bytes" <> help "display file sizes in bytes"
) )
<*> parseBatchOption False <*> parseBatchOption False
<*> switch
( long "autoenable"
<> help "list special remotes that are configured to autoenable"
)
seek :: InfoOptions -> CommandSeek seek :: InfoOptions -> CommandSeek
seek o = case batchOption o of seek o = case batchOption o of
@ -124,7 +130,9 @@ seek o = case batchOption o of
start :: InfoOptions -> [String] -> CommandStart start :: InfoOptions -> [String] -> CommandStart
start o [] = do start o [] = do
globalInfo o if autoenableOption o
then autoenableInfo
else globalInfo o
stop stop
start o ps = do start o ps = do
mapM_ (\p -> itemInfo o (SeekInput [p], p)) ps mapM_ (\p -> itemInfo o (SeekInput [p], p)) ps
@ -140,6 +148,19 @@ globalInfo o = do
evalStateT (mapM_ showStat stats) (emptyStatInfo o) evalStateT (mapM_ showStat stats) (emptyStatInfo o)
return True return True
autoenableInfo :: Annex ()
autoenableInfo = showCustom "info" (SeekInput []) $ do
m <- SpecialRemote.specialRemoteNameMap
<$> SpecialRemote.autoEnableable
descm <- M.unionWith Remote.addName
<$> uuidDescMap
<*> pure (M.map toUUIDDesc m)
s <- Remote.prettyPrintUUIDsDescs
"autoenable special remotes"
descm (M.keys m)
showRaw (encodeBS s)
return True
itemInfo :: InfoOptions -> (SeekInput, String) -> Annex () itemInfo :: InfoOptions -> (SeekInput, String) -> Annex ()
itemInfo o (si, p) = ifM (isdir p) itemInfo o (si, p) = ifM (isdir p)
( dirInfo o p si ( dirInfo o p si

View file

@ -1,6 +1,6 @@
{- git-annex trust log {- git-annex trust log
- -
- Copyright 2010-2020 Joey Hess <id@joeyh.name> - Copyright 2010-2022 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU AGPL version 3 or higher. - Licensed under the GNU AGPL version 3 or higher.
-} -}
@ -14,6 +14,7 @@ module Logs.Trust (
trustPartition, trustPartition,
trustExclude, trustExclude,
lookupTrust, lookupTrust,
lookupTrust',
trustMapLoad, trustMapLoad,
) where ) where
@ -37,7 +38,10 @@ trustGet level = M.keys . M.filter (== level) <$> trustMap
{- Returns the TrustLevel of a given repo UUID. -} {- Returns the TrustLevel of a given repo UUID. -}
lookupTrust :: UUID -> Annex TrustLevel lookupTrust :: UUID -> Annex TrustLevel
lookupTrust u = (fromMaybe def . M.lookup u) <$> trustMap lookupTrust u = lookupTrust' u <$> trustMap
lookupTrust' :: UUID -> TrustMap -> TrustLevel
lookupTrust' u m = fromMaybe def $ M.lookup u m
{- Partitions a list of UUIDs to those matching a TrustLevel and not. -} {- Partitions a list of UUIDs to those matching a TrustLevel and not. -}
trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID]) trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID])

View file

@ -45,6 +45,11 @@ for the local repository and all annexed content.
Makes the `--batch` input be delimited by nulls instead of the usual Makes the `--batch` input be delimited by nulls instead of the usual
newlines. newlines.
* `--autoenable`
Display a list of special remotes that have been configured to
autoenable.
* matching options * matching options
The [[git-annex-matching-options]](1) can be used to select what The [[git-annex-matching-options]](1) can be used to select what