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
|
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."
|
|
|
|
start (name:rest) = go =<< filter matchingname <$> Annex.fromRepo Git.remotes
|
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
|
|
|
|
go (r:_) = startNormalRemote name r
|
|
|
|
|
2016-11-30 18:35:24 +00:00
|
|
|
startNormalRemote :: Git.RemoteName -> Git.Repo -> CommandStart
|
2016-05-24 19:24:38 +00:00
|
|
|
startNormalRemote name r = do
|
|
|
|
showStart "enableremote" name
|
|
|
|
next $ next $ do
|
2016-05-24 19:48:22 +00:00
|
|
|
setRemoteIgnore r False
|
2016-05-24 19:24:38 +00:00
|
|
|
r' <- Remote.Git.configRead False r
|
|
|
|
u <- getRepoUUID r'
|
2016-05-24 19:48:22 +00:00
|
|
|
return $ u /= NoUUID
|
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
|
|
|
|
v <- Remote.nameToUUID' name
|
|
|
|
case v of
|
|
|
|
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)
|
2016-05-24 19:24:38 +00:00
|
|
|
showStart "enableremote" name
|
|
|
|
gc <- maybe def Remote.gitconfig <$> Remote.byUUID u
|
|
|
|
next $ performSpecialRemote t u fullconfig gc
|
|
|
|
|
|
|
|
performSpecialRemote :: RemoteType -> UUID -> R.RemoteConfig -> RemoteGitConfig -> CommandPerform
|
|
|
|
performSpecialRemote t u c gc = do
|
|
|
|
(c', u') <- R.setup t (Just u) Nothing c gc
|
|
|
|
next $ cleanupSpecialRemote u' c'
|
|
|
|
|
|
|
|
cleanupSpecialRemote :: UUID -> R.RemoteConfig -> CommandCleanup
|
|
|
|
cleanupSpecialRemote u c = do
|
|
|
|
Logs.Remote.configSet u c
|
2016-05-24 19:48:22 +00:00
|
|
|
mr <- Remote.byUUID u
|
|
|
|
case mr of
|
|
|
|
Nothing -> noop
|
|
|
|
Just r -> setRemoteIgnore (R.repo r) 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)
|
2016-05-24 19:48:22 +00:00
|
|
|
disabledremotes <- filterM isdisabled =<< Annex.fromRepo Git.remotes
|
|
|
|
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
|
|
|
|
, remoteAnnexIgnore <$> Annex.getRemoteGitConfig r
|
|
|
|
]
|