2013-04-26 22:22:44 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-05-24 19:24:38 +00:00
|
|
|
- Copyright 2013-2016 Joey Hess <id@joeyh.name>
|
2013-04-26 22:22:44 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.EnableRemote where
|
|
|
|
|
|
|
|
import Command
|
2016-05-24 19:24:38 +00:00
|
|
|
import qualified Annex
|
2013-04-26 22:22:44 +00:00
|
|
|
import qualified Logs.Remote
|
|
|
|
import qualified Types.Remote as R
|
2016-05-24 19:48:22 +00:00
|
|
|
import qualified Git
|
2016-11-30 18:35:24 +00:00
|
|
|
import qualified Git.Types as Git
|
2015-09-14 18:49:48 +00:00
|
|
|
import qualified Annex.SpecialRemote
|
2015-10-26 18:55:40 +00:00
|
|
|
import qualified Remote
|
2016-05-23 21:03:20 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2016-05-24 19:24:38 +00:00
|
|
|
import qualified Remote.Git
|
2015-10-26 18:55:40 +00:00
|
|
|
import Logs.UUID
|
2016-05-24 19:24:38 +00:00
|
|
|
import Annex.UUID
|
2016-05-24 19:48:22 +00:00
|
|
|
import Config
|
2017-08-17 16:26:14 +00:00
|
|
|
import Config.DynamicConfig
|
|
|
|
import Types.GitConfig
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = command "enableremote" SectionSetup
|
2016-05-24 19:24:38 +00:00
|
|
|
"enables git-annex to use a remote"
|
2013-04-26 22:22:44 +00:00
|
|
|
(paramPair paramName $ paramOptional $ paramRepeating paramKeyValue)
|
2015-07-08 19:08:02 +00:00
|
|
|
(withParams seek)
|
2013-04-26 22:22:44 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withWords start
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
start :: [String] -> CommandStart
|
2016-05-24 19:24:38 +00:00
|
|
|
start [] = unknownNameError "Specify the remote to enable."
|
2018-01-09 19:36:56 +00:00
|
|
|
start (name:rest) = go =<< filter matchingname <$> Annex.getGitRemotes
|
2013-04-26 22:22:44 +00:00
|
|
|
where
|
2016-05-24 19:24:38 +00:00
|
|
|
matchingname r = Git.remoteName r == Just name
|
|
|
|
go [] = startSpecialRemote name (Logs.Remote.keyValToConfig rest)
|
|
|
|
=<< Annex.SpecialRemote.findExisting name
|
2017-04-07 17:51:06 +00:00
|
|
|
go (r:_) = do
|
|
|
|
-- This could be either a normal git remote or a special
|
|
|
|
-- remote that has an url (eg gcrypt).
|
|
|
|
rs <- Remote.remoteList
|
|
|
|
case filter (\rmt -> Remote.name rmt == name) rs of
|
|
|
|
(rmt:_) | Remote.remotetype rmt == Remote.Git.remote ->
|
|
|
|
startNormalRemote name rest r
|
|
|
|
_ -> go []
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2017-04-07 17:51:06 +00:00
|
|
|
-- Normal git remotes are special-cased; enableremote retries probing
|
|
|
|
-- the remote uuid.
|
|
|
|
startNormalRemote :: Git.RemoteName -> [String] -> Git.Repo -> CommandStart
|
|
|
|
startNormalRemote name restparams r
|
|
|
|
| null restparams = do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStart' "enableremote" (Just name)
|
2017-04-07 17:51:06 +00:00
|
|
|
next $ next $ do
|
|
|
|
setRemoteIgnore r False
|
|
|
|
r' <- Remote.Git.configRead False r
|
|
|
|
u <- getRepoUUID r'
|
|
|
|
return $ u /= NoUUID
|
|
|
|
| otherwise = giveup $
|
|
|
|
"That is a normal git remote; passing these parameters does not make sense: " ++ unwords restparams
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2016-11-30 18:35:24 +00:00
|
|
|
startSpecialRemote :: Git.RemoteName -> Remote.RemoteConfig -> Maybe (UUID, Remote.RemoteConfig) -> CommandStart
|
2016-05-24 19:24:38 +00:00
|
|
|
startSpecialRemote name config Nothing = do
|
|
|
|
m <- Annex.SpecialRemote.specialRemoteMap
|
|
|
|
confm <- Logs.Remote.readRemoteLog
|
2017-12-05 19:00:50 +00:00
|
|
|
Remote.nameToUUID' name >>= \case
|
2016-05-24 19:24:38 +00:00
|
|
|
Right u | u `M.member` m ->
|
|
|
|
startSpecialRemote name config $
|
|
|
|
Just (u, fromMaybe M.empty (M.lookup u confm))
|
|
|
|
_ -> unknownNameError "Unknown remote name."
|
|
|
|
startSpecialRemote name config (Just (u, c)) = do
|
|
|
|
let fullconfig = config `M.union` c
|
2016-11-16 01:29:54 +00:00
|
|
|
t <- either giveup return (Annex.SpecialRemote.findType fullconfig)
|
2017-11-28 18:40:26 +00:00
|
|
|
showStart' "enableremote" (Just name)
|
2017-08-17 16:26:14 +00:00
|
|
|
gc <- maybe (liftIO dummyRemoteGitConfig)
|
|
|
|
(return . Remote.gitconfig)
|
|
|
|
=<< Remote.byUUID u
|
2017-09-04 16:40:33 +00:00
|
|
|
next $ performSpecialRemote t u c fullconfig gc
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2017-09-04 16:40:33 +00:00
|
|
|
performSpecialRemote :: RemoteType -> UUID -> R.RemoteConfig -> R.RemoteConfig -> RemoteGitConfig -> CommandPerform
|
|
|
|
performSpecialRemote t u oldc c gc = do
|
|
|
|
(c', u') <- R.setup t (R.Enable oldc) (Just u) Nothing c gc
|
2016-05-24 19:24:38 +00:00
|
|
|
next $ cleanupSpecialRemote u' c'
|
|
|
|
|
|
|
|
cleanupSpecialRemote :: UUID -> R.RemoteConfig -> CommandCleanup
|
|
|
|
cleanupSpecialRemote u c = do
|
|
|
|
Logs.Remote.configSet u c
|
2017-12-05 19:00:50 +00:00
|
|
|
Remote.byUUID u >>= \case
|
2016-05-24 19:48:22 +00:00
|
|
|
Nothing -> noop
|
2018-06-04 18:31:55 +00:00
|
|
|
Just r -> do
|
|
|
|
repo <- R.getRepo r
|
|
|
|
setRemoteIgnore repo False
|
2016-05-24 19:24:38 +00:00
|
|
|
return True
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
unknownNameError :: String -> Annex a
|
|
|
|
unknownNameError prefix = do
|
2015-10-26 18:55:40 +00:00
|
|
|
m <- Annex.SpecialRemote.specialRemoteMap
|
|
|
|
descm <- M.unionWith Remote.addName <$> uuidMap <*> pure m
|
2016-05-24 19:24:38 +00:00
|
|
|
specialmsg <- if M.null m
|
2015-10-26 18:55:40 +00:00
|
|
|
then pure "(No special remotes are currently known; perhaps use initremote instead?)"
|
|
|
|
else Remote.prettyPrintUUIDsDescs
|
|
|
|
"known special remotes"
|
|
|
|
descm (M.keys m)
|
2018-01-09 19:36:56 +00:00
|
|
|
disabledremotes <- filterM isdisabled =<< Annex.getGitRemotes
|
2016-05-24 19:48:22 +00:00
|
|
|
let remotesmsg = unlines $ map ("\t" ++) $
|
|
|
|
mapMaybe Git.remoteName disabledremotes
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ concat $ filter (not . null) [prefix ++ "\n", remotesmsg, specialmsg]
|
2016-05-24 19:48:22 +00:00
|
|
|
where
|
|
|
|
isdisabled r = anyM id
|
|
|
|
[ (==) NoUUID <$> getRepoUUID r
|
2017-08-17 16:26:14 +00:00
|
|
|
, liftIO . getDynamicConfig . remoteAnnexIgnore
|
|
|
|
=<< Annex.getRemoteGitConfig r
|
2016-05-24 19:48:22 +00:00
|
|
|
]
|