From c57695007bdf637a9462bf004ae2b3c3f218b3ab Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 16 Apr 2019 12:23:46 -0400 Subject: [PATCH] prevent renaming to name already in use Also, look up the name in the special remote log first, only fall back to remote name/uuid/description lookup if it fails. This should avoid violating least surprise in cases where the special remote they wish t rename is not enabled, or has a git remote with a different name. --- Command/RenameRemote.hs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Command/RenameRemote.hs b/Command/RenameRemote.hs index 608471094f..ca351afe85 100644 --- a/Command/RenameRemote.hs +++ b/Command/RenameRemote.hs @@ -8,6 +8,7 @@ module Command.RenameRemote where import Command +import qualified Annex.SpecialRemote import qualified Logs.Remote import qualified Types.Remote as R import qualified Remote @@ -24,15 +25,24 @@ seek :: CmdParams -> CommandSeek seek = withWords (commandAction . start) start :: [String] -> CommandStart -start (oldname:newname:[]) = do - m <- Logs.Remote.readRemoteLog - Remote.nameToUUID' oldname >>= \case +start (oldname:newname:[]) = Annex.SpecialRemote.findExisting oldname >>= \case + Just (u, cfg) -> Annex.SpecialRemote.findExisting newname >>= \case + Just _ -> giveup $ "The name " ++ newname ++ " is already used by a special remote." + Nothing -> go u cfg + -- 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 Left e -> giveup e - Right u -> case M.lookup u m of - Nothing -> giveup "That is not a special remote." - Just cfg -> do - showStart' "rename" Nothing - next $ perform u cfg newname + Right u -> do + m <- Logs.Remote.readRemoteLog + case M.lookup u m of + Nothing -> giveup "That is not a special remote." + Just cfg -> go u cfg + where + go u cfg = do + showStart' "rename" Nothing + next $ perform u cfg newname start _ = giveup "Specify an old name (or uuid or description) and a new name." perform :: UUID -> R.RemoteConfig -> String -> CommandPerform