From e41663502111a344433661d32adebc2ff378a34c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 5 Jan 2022 15:24:02 -0400 Subject: [PATCH] renameremote: Better handling of case where there are multiple special remotes with a name Instead of renaming one at random, error out and ask that a uuid be specified. Sponsored-by: Brett Eisenberg on Patreon --- Annex/SpecialRemote.hs | 4 ++-- Assistant/MakeRemote.hs | 12 ++++++------ CHANGELOG | 2 +- Command/InitRemote.hs | 2 +- Command/RenameRemote.hs | 11 ++++++----- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Annex/SpecialRemote.hs b/Annex/SpecialRemote.hs index c6b736f8ad..5e2a1c2325 100644 --- a/Annex/SpecialRemote.hs +++ b/Annex/SpecialRemote.hs @@ -30,10 +30,10 @@ import qualified Data.Map as M - - Remotes that are not dead come first in the list - when a name appears multiple times. -} -findExisting :: RemoteName -> Annex (Maybe (UUID, RemoteConfig, Maybe (ConfigFrom UUID))) +findExisting :: RemoteName -> Annex [(UUID, RemoteConfig, Maybe (ConfigFrom UUID))] findExisting name = do (a, b) <- findExisting' name - return (headMaybe (a++b)) + return (a++b) {- Dead remotes with the name are in the second list, all others in the - first list. -} diff --git a/Assistant/MakeRemote.hs b/Assistant/MakeRemote.hs index 45ceb01337..46daa33a04 100644 --- a/Assistant/MakeRemote.hs +++ b/Assistant/MakeRemote.hs @@ -55,9 +55,9 @@ makeRsyncRemote :: RemoteName -> String -> Annex String makeRsyncRemote name location = makeRemote name location $ const $ void $ go =<< Annex.SpecialRemote.findExisting name where - go Nothing = setupSpecialRemote name Rsync.remote config Nothing + go [] = setupSpecialRemote name Rsync.remote config Nothing (Nothing, R.Init, Annex.SpecialRemote.newConfig name Nothing mempty mempty) Nothing - go (Just (u, c, mcu)) = setupSpecialRemote name Rsync.remote config Nothing + go ((u, c, mcu):_) = setupSpecialRemote name Rsync.remote config Nothing (Just u, R.Enable c, c) mcu config = M.fromList [ (encryptionField, Proposed "shared") @@ -86,16 +86,16 @@ initSpecialRemote name remotetype mcreds config = go 0 go n = do let fullname = if n == 0 then name else name ++ show n Annex.SpecialRemote.findExisting fullname >>= \case - Nothing -> setupSpecialRemote fullname remotetype config mcreds + [] -> setupSpecialRemote fullname remotetype config mcreds (Nothing, R.Init, Annex.SpecialRemote.newConfig fullname Nothing mempty mempty) Nothing - Just _ -> go (n + 1) + _ -> go (n + 1) {- Enables an existing special remote. -} enableSpecialRemote :: SpecialRemoteMaker enableSpecialRemote name remotetype mcreds config = Annex.SpecialRemote.findExisting name >>= \case - Nothing -> error $ "Cannot find a special remote named " ++ name - Just (u, c, mcu) -> setupSpecialRemote' False name remotetype config mcreds (Just u, R.Enable c, c) mcu + [] -> error $ "Cannot find a special remote named " ++ name + ((u, c, mcu):_) -> setupSpecialRemote' False name remotetype config mcreds (Just u, R.Enable c, c) mcu setupSpecialRemote :: RemoteName -> RemoteType -> R.RemoteConfig -> Maybe CredPair -> (Maybe UUID, R.SetupStage, R.RemoteConfig) -> Maybe (Annex.SpecialRemote.ConfigFrom UUID) -> Annex RemoteName setupSpecialRemote = setupSpecialRemote' True diff --git a/CHANGELOG b/CHANGELOG index 0c0833434b..b8c375d323 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,7 +3,7 @@ git-annex (8.20211232) UNRELEASED; urgency=medium * export: When a non-annexed symlink is in the tree to be exported, skip it. * import: When the previously exported tree contained a non-annexed symlink, preserve it in the imported tree so it does not get deleted. - * enableremote: Better handling of the unusual case where + * enableremote, renameremote: Better handling of the unusual case where multiple special remotes have been initialized with the same name. -- Joey Hess Mon, 03 Jan 2022 14:01:14 -0400 diff --git a/Command/InitRemote.hs b/Command/InitRemote.hs index ece7456089..b1bfc4e1e9 100644 --- a/Command/InitRemote.hs +++ b/Command/InitRemote.hs @@ -64,7 +64,7 @@ seek o = withWords (commandAction . (start o)) (cmdparams o) start :: InitRemoteOptions -> [String] -> CommandStart start _ [] = giveup "Specify a name for the remote." -start o (name:ws) = ifM (isJust <$> findExisting name) +start o (name:ws) = ifM (not . null <$> findExisting name) ( giveup $ "There is already a special remote named \"" ++ name ++ "\". (Use enableremote to enable an existing special remote.)" , ifM (isJust <$> Remote.byNameOnly name) diff --git a/Command/RenameRemote.hs b/Command/RenameRemote.hs index fd4683f1f1..3366182aa2 100644 --- a/Command/RenameRemote.hs +++ b/Command/RenameRemote.hs @@ -1,6 +1,6 @@ {- git-annex command - - - Copyright 2019 Joey Hess + - Copyright 2019-2021 Joey Hess - - Licensed under the GNU AGPL version 3 or higher. -} @@ -28,19 +28,20 @@ seek = withWords (commandAction . start) start :: [String] -> CommandStart start ps@(oldname:newname:[]) = Annex.SpecialRemote.findExisting oldname >>= \case - Just (u, cfg, mcu) -> Annex.SpecialRemote.findExisting newname >>= \case - Just _ -> giveup $ "The name " ++ newname ++ " is already used by a special remote." - Nothing -> go u cfg mcu + ((u, cfg, mcu):[]) -> Annex.SpecialRemote.findExisting newname >>= \case + [] -> go u cfg mcu + _ -> giveup $ "The name " ++ newname ++ " is already used by a special remote." -- Support lookup by uuid or description as well as remote name, -- as a fallback when there is nothing with the name in the -- special remote log. - Nothing -> Remote.nameToUUID' oldname >>= \case + [] -> Remote.nameToUUID' oldname >>= \case Left e -> giveup e Right u -> do m <- Logs.Remote.remoteConfigMap case M.lookup u m of Nothing -> giveup "That is not a special remote." Just cfg -> go u cfg Nothing + _ -> giveup $ "There are multiple special remotes named " ++ oldname ++ ". Provide instead the uuid or description of the remote to rename." where ai = ActionItemOther Nothing si = SeekInput ps -- 2.39.5