2013-04-26 22:22:44 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-04-18 19:30:49 +00:00
|
|
|
- Copyright 2013-2023 Joey Hess <id@joeyh.name>
|
2013-04-26 22:22:44 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-04-26 22:22:44 +00:00
|
|
|
-}
|
|
|
|
|
2019-12-05 15:40:10 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2013-04-26 22:22:44 +00:00
|
|
|
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
|
2019-10-11 18:59:41 +00:00
|
|
|
import qualified Annex.SpecialRemote as 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
|
2020-01-10 18:10:20 +00:00
|
|
|
import Types.ProposedAccepted
|
2020-02-19 17:58:26 +00:00
|
|
|
import Git.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
|
2023-05-10 18:09:27 +00:00
|
|
|
cmd = withAnnexOptions [jsonOptions] $
|
|
|
|
command "enableremote" SectionSetup
|
|
|
|
"enables git-annex to use a remote"
|
|
|
|
(paramPair paramName $ paramOptional $ paramRepeating paramParamValue)
|
|
|
|
(withParams seek)
|
2013-04-26 22:22:44 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-10-01 18:12:06 +00:00
|
|
|
seek = withWords (commandAction . 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
|
2023-04-18 19:30:49 +00:00
|
|
|
go [] = deadLast name $
|
|
|
|
startSpecialRemote name (Logs.Remote.keyValToConfig Proposed rest)
|
2023-04-18 18:00:02 +00:00
|
|
|
go (r:_)
|
|
|
|
| not (null rest) = go []
|
|
|
|
| otherwise = 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 r
|
|
|
|
_ -> go []
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2023-04-18 18:00:02 +00:00
|
|
|
-- enableremote of a normal git remote with no added parameters is a special case
|
|
|
|
-- that retries probing the remote uuid.
|
|
|
|
startNormalRemote :: Git.RemoteName -> Git.Repo -> CommandStart
|
|
|
|
startNormalRemote name r = starting "enableremote (normal)" ai si $ do
|
|
|
|
setRemoteIgnore r False
|
|
|
|
r' <- Remote.Git.configRead False r
|
|
|
|
u <- getRepoUUID r'
|
|
|
|
next $ return $ u /= NoUUID
|
2020-09-14 20:49:33 +00:00
|
|
|
where
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (UnquotedString name))
|
2020-09-14 20:49:33 +00:00
|
|
|
si = SeekInput [name]
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2022-01-05 19:12:01 +00:00
|
|
|
startSpecialRemote :: Git.RemoteName -> Remote.RemoteConfig -> [(UUID, Remote.RemoteConfig, Maybe (SpecialRemote.ConfigFrom UUID))] -> CommandStart
|
2023-04-18 19:30:49 +00:00
|
|
|
startSpecialRemote = startSpecialRemote' "enableremote" performSpecialRemote
|
|
|
|
|
|
|
|
type PerformSpecialRemote = RemoteType -> UUID -> R.RemoteConfig -> R.RemoteConfig -> RemoteGitConfig -> Maybe (SpecialRemote.ConfigFrom UUID) -> CommandPerform
|
|
|
|
|
|
|
|
startSpecialRemote' :: String -> PerformSpecialRemote -> Git.RemoteName -> Remote.RemoteConfig -> [(UUID, Remote.RemoteConfig, Maybe (SpecialRemote.ConfigFrom UUID))] -> CommandStart
|
|
|
|
startSpecialRemote' cname perform name config [] = do
|
2019-10-11 18:59:41 +00:00
|
|
|
m <- SpecialRemote.specialRemoteMap
|
2020-09-22 17:52:26 +00:00
|
|
|
confm <- Logs.Remote.remoteConfigMap
|
2017-12-05 19:00:50 +00:00
|
|
|
Remote.nameToUUID' name >>= \case
|
2023-02-13 18:30:54 +00:00
|
|
|
([u], _) | u `M.member` m ->
|
2023-04-18 19:30:49 +00:00
|
|
|
startSpecialRemote' cname perform name config $
|
2022-01-05 19:12:01 +00:00
|
|
|
[(u, fromMaybe M.empty (M.lookup u confm), Nothing)]
|
2023-02-13 18:30:54 +00:00
|
|
|
(_, msg) -> unknownNameError msg
|
2023-04-18 19:30:49 +00:00
|
|
|
startSpecialRemote' cname perform name config ((u, c, mcu):[]) =
|
|
|
|
starting cname ai si $ do
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
let fullconfig = config `M.union` c
|
2019-10-11 18:59:41 +00:00
|
|
|
t <- either giveup return (SpecialRemote.findType fullconfig)
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
gc <- maybe (liftIO dummyRemoteGitConfig)
|
|
|
|
(return . Remote.gitconfig)
|
|
|
|
=<< Remote.byUUID u
|
2023-04-18 19:30:49 +00:00
|
|
|
perform t u c fullconfig gc mcu
|
2020-09-14 20:49:33 +00:00
|
|
|
where
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (UnquotedString name))
|
2020-09-14 20:49:33 +00:00
|
|
|
si = SeekInput [name]
|
2023-04-18 19:30:49 +00:00
|
|
|
startSpecialRemote' _ _ _ _ _ =
|
|
|
|
giveup "Multiple remotes have that name. Either use git-annex renameremote to rename them, or specify the uuid of the remote."
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2023-04-18 19:30:49 +00:00
|
|
|
performSpecialRemote :: PerformSpecialRemote
|
2019-10-11 18:59:41 +00:00
|
|
|
performSpecialRemote t u oldc c gc mcu = do
|
2017-09-04 16:40:33 +00:00
|
|
|
(c', u') <- R.setup t (R.Enable oldc) (Just u) Nothing c gc
|
2020-02-19 17:58:26 +00:00
|
|
|
next $ cleanupSpecialRemote t u' c' mcu
|
2016-05-24 19:24:38 +00:00
|
|
|
|
2020-02-19 17:58:26 +00:00
|
|
|
cleanupSpecialRemote :: RemoteType -> UUID -> R.RemoteConfig -> Maybe (SpecialRemote.ConfigFrom UUID) -> CommandCleanup
|
|
|
|
cleanupSpecialRemote t u c mcu = do
|
2019-10-11 18:59:41 +00:00
|
|
|
case mcu of
|
2023-04-18 18:00:02 +00:00
|
|
|
Nothing -> Logs.Remote.configSet u c
|
2019-10-11 18:59:41 +00:00
|
|
|
Just (SpecialRemote.ConfigFrom cu) -> do
|
2020-02-19 17:45:11 +00:00
|
|
|
setConfig (remoteAnnexConfig c "config-uuid") (fromUUID cu)
|
2019-10-11 18:59:41 +00:00
|
|
|
Logs.Remote.configSet cu 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
|
2020-02-19 17:58:26 +00:00
|
|
|
unless (Remote.gitSyncableRemoteType t) $
|
|
|
|
setConfig (remoteConfig c "skipFetchAll") (boolConfig True)
|
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
|
2019-10-11 18:59:41 +00:00
|
|
|
m <- SpecialRemote.specialRemoteMap
|
2019-01-01 19:39:45 +00:00
|
|
|
descm <- M.unionWith Remote.addName
|
|
|
|
<$> uuidDescMap
|
|
|
|
<*> pure (M.map toUUIDDesc 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
|
|
|
]
|
2023-04-18 19:30:49 +00:00
|
|
|
|
|
|
|
-- Use dead remote only when there is no other remote
|
|
|
|
-- with the same name
|
|
|
|
deadLast :: Git.RemoteName -> ([(UUID, Remote.RemoteConfig, Maybe (SpecialRemote.ConfigFrom UUID))] -> Annex a) -> Annex a
|
|
|
|
deadLast name use =
|
|
|
|
SpecialRemote.findExisting' name >>= \case
|
|
|
|
([], l) -> use l
|
|
|
|
(l, _) -> use l
|