two types of byName

Clean up from 9769235d6b.
In some cases, looking up a remote by name even though it has no UUID is
desirable. This includes git annex sync, which can operate on remotes
without an annex, and XMPP pairing, which runs addRemote (with calls
byName) before the UUID of the XMPP remote has been configured in git.
This commit is contained in:
Joey Hess 2013-03-05 15:39:42 -04:00
parent 9e75dce764
commit 921f29c004
10 changed files with 27 additions and 16 deletions

View file

@ -24,6 +24,7 @@ module Remote (
remoteMap,
uuidDescriptions,
byName,
byNameWithUUID,
byCost,
prettyPrintUUIDs,
prettyListUUIDs,
@ -72,18 +73,27 @@ addName desc n
| otherwise = n ++ " (" ++ desc ++ ")"
{- When a name is specified, looks up the remote matching that name.
- (Or it can be a UUID.) Only finds currently configured git remotes. -}
- (Or it can be a UUID.) -}
byName :: Maybe String -> Annex (Maybe Remote)
byName Nothing = return Nothing
byName (Just n) = either error Just <$> byName' n
{- Like byName, but the remote must have a configured UUID. -}
byNameWithUUID :: Maybe String -> Annex (Maybe Remote)
byNameWithUUID n = do
v <- byName n
return $ checkuuid <$> v
where
checkuuid r
| uuid r == NoUUID = error $ "cannot determine uuid for " ++ name r
| otherwise = r
byName' :: String -> Annex (Either String Remote)
byName' "" = return $ Left "no remote specified"
byName' n = handle . filter matching <$> remoteList
where
handle [] = Left $ "there is no available git remote named \"" ++ n ++ "\""
handle (match:_)
| uuid match == NoUUID = Left $ "cannot determine uuid for " ++ name match
| otherwise = Right match
handle (match:_) = Right match
matching r = n == name r || toUUID n == uuid r
{- Looks up a remote by name (or by UUID, or even by description),