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
This commit is contained in:
Joey Hess 2022-01-05 15:24:02 -04:00
parent 58afb00f6e
commit e416635021
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 16 additions and 15 deletions

View file

@ -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)

View file

@ -1,6 +1,6 @@
{- git-annex command
-
- Copyright 2019 Joey Hess <id@joeyh.name>
- Copyright 2019-2021 Joey Hess <id@joeyh.name>
-
- 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