From 016d1bee88dfac29325a88b75765cb269321905d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 5 Mar 2024 15:04:35 -0400 Subject: [PATCH] add reregisterurl command What this can currently be used for is only to change an url from being used by a special remote to being used by the web remote. This could have been a --move-from option to registerurl. But, that would have complicated its option and --batch processing, and also would have complicated unregisterurl, which is implemented on top of Command.Registerurl. So, a separate command was actually less complicated to implement. The generic description of the command is because I want to make this command a catch-all for other url updating kind of things, if there are ever any more. Also because it was hard to come up with a good name for the specific action. I considered `git-annex moveurl`, but that seems to indicate data is perhaps actually being moved, and seems to sit at the same level as addurl and rmurl, and this command is at the plumbing level of registerurl and unregisterurl. Sponsored-by: Dartmouth College's DANDI project --- CHANGELOG | 2 + CmdLine/GitAnnex.hs | 2 + Command/ReregisterUrl.hs | 79 +++++++++++++++++++ ..._260c238435f8f5e53511de7e574dd53f._comment | 29 +++++++ doc/git-annex-registerurl.mdwn | 2 + doc/git-annex-reregisterurl.mdwn | 64 +++++++++++++++ doc/git-annex-unregisterurl.mdwn | 2 + doc/git-annex.mdwn | 6 ++ git-annex.cabal | 1 + 9 files changed, 187 insertions(+) create mode 100644 Command/ReregisterUrl.hs create mode 100644 doc/forum/how_to___34__move__34___URL_between_remotes__63__/comment_6_260c238435f8f5e53511de7e574dd53f._comment create mode 100644 doc/git-annex-reregisterurl.mdwn diff --git a/CHANGELOG b/CHANGELOG index 2bc030c19d..e7a608e51c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,8 @@ git-annex (10.20240228) UNRELEASED; urgency=medium annexed files be verified with a checksum that is calculated on a later download from the web. This will become the default later. * Added dependency on unbounded-delays. + * reregisterurl: New command that can change an url from being + used by a special remote to being used by the web remote. -- Joey Hess Tue, 27 Feb 2024 13:07:10 -0400 diff --git a/CmdLine/GitAnnex.hs b/CmdLine/GitAnnex.hs index 47a37a3de7..debf30fffd 100644 --- a/CmdLine/GitAnnex.hs +++ b/CmdLine/GitAnnex.hs @@ -34,6 +34,7 @@ import qualified Command.MatchExpression import qualified Command.FromKey import qualified Command.RegisterUrl import qualified Command.UnregisterUrl +import qualified Command.ReregisterUrl import qualified Command.SetKey import qualified Command.DropKey import qualified Command.Transferrer @@ -196,6 +197,7 @@ cmds testoptparser testrunner mkbenchmarkgenerator = map addGitAnnexCommonOption , Command.FromKey.cmd , Command.RegisterUrl.cmd , Command.UnregisterUrl.cmd + , Command.ReregisterUrl.cmd , Command.SetKey.cmd , Command.DropKey.cmd , Command.Transferrer.cmd diff --git a/Command/ReregisterUrl.hs b/Command/ReregisterUrl.hs new file mode 100644 index 0000000000..5d95f0da43 --- /dev/null +++ b/Command/ReregisterUrl.hs @@ -0,0 +1,79 @@ +{- git-annex command + - + - Copyright 2015-2024 Joey Hess + - + - Licensed under the GNU AGPL version 3 or higher. + -} + +module Command.ReregisterUrl where + +import Command +import Logs.Web +import Command.FromKey (keyOpt, keyOpt') +import qualified Remote +import Git.Types + +cmd :: Command +cmd = withAnnexOptions [jsonOptions] $ command "reregisterurl" + SectionPlumbing "updates url registration information" + (paramKey) + (seek <$$> optParser) + +data ReregisterUrlOptions = ReregisterUrlOptions + { keyOpts :: CmdParams + , batchOption :: BatchMode + , moveFromOption :: Maybe (DeferredParse Remote) + } + +optParser :: CmdParamsDesc -> Parser ReregisterUrlOptions +optParser desc = ReregisterUrlOptions + <$> cmdParams desc + <*> parseBatchOption False + <*> optional (mkParseRemoteOption <$> parseMoveFromOption) + +parseMoveFromOption :: Parser RemoteName +parseMoveFromOption = strOption + ( long "move-from" <> metavar paramRemote + <> completeRemotes + ) + +seek :: ReregisterUrlOptions -> CommandSeek +seek o = case (batchOption o, keyOpts o) of + (Batch fmt, _) -> seekBatch o fmt + (NoBatch, ps) -> commandAction (start o ps) + +seekBatch :: ReregisterUrlOptions -> BatchFormat -> CommandSeek +seekBatch o fmt = batchOnly Nothing (keyOpts o) $ + batchInput fmt (pure . parsebatch) $ + batchCommandAction . start' o + where + parsebatch l = case keyOpt' l of + Left e -> Left e + Right k -> Right k + +start :: ReregisterUrlOptions -> [String] -> CommandStart +start o (keyname:[]) = start' o (si, keyOpt keyname) + where + si = SeekInput [keyname] +start _ _ = giveup "specify a key" + +start' :: ReregisterUrlOptions -> (SeekInput, Key) -> CommandStart +start' o (si, key) = + starting "reregisterurl" ai si $ + perform o key + where + ai = ActionItemKey key + +perform :: ReregisterUrlOptions -> Key -> CommandPerform +perform o key = maybe (pure Nothing) (Just <$$> getParsed) (moveFromOption o) >>= \case + Nothing -> next $ return True + Just r -> do + us <- map fst + . filter (\(_, d) -> d == OtherDownloader) + . map getDownloader + <$> getUrls key + us' <- filterM (\u -> (== r) <$> Remote.claimingUrl u) us + forM_ us' $ \u -> do + setUrlMissing key (setDownloader u OtherDownloader) + setUrlPresent key u + next $ return True diff --git a/doc/forum/how_to___34__move__34___URL_between_remotes__63__/comment_6_260c238435f8f5e53511de7e574dd53f._comment b/doc/forum/how_to___34__move__34___URL_between_remotes__63__/comment_6_260c238435f8f5e53511de7e574dd53f._comment new file mode 100644 index 0000000000..f1114fc3ba --- /dev/null +++ b/doc/forum/how_to___34__move__34___URL_between_remotes__63__/comment_6_260c238435f8f5e53511de7e574dd53f._comment @@ -0,0 +1,29 @@ +[[!comment format=mdwn + username="joey" + subject="""comment 6""" + date="2024-03-05T18:11:32Z" + content=""" +Went far enough down implementing `registerurl --move-from` to be sure that +it would complicate the code far more than just adding a new `moveurl` +command. So despite it being a fairly unusual situation, a new command +is better than that option. + +And implemented it: + + joey@darkstar:~/tmp/x>git-annex registerurl WORM--bar http://example.com/bar.fooregisterurl http://example.com/bar.foo ok + joey@darkstar:~/tmp/x> git-annex whereis --key WORM--bar + whereis WORM--bar (1 copy) + dca0b5f9-659a-4928-84db-ff9fd74d8fc8 -- [foo] + + foo: http://example.com/bar.foo + ok + joey@darkstar:~/tmp/x>git-annex reregisterurl WORM--bar --move-from=foo + reregisterurl WORM--bar ok + joey@darkstar:~/tmp/x>git-annex whereis --key WORM--bar + whereis WORM--bar (2 copies) + 00000000-0000-0000-0000-000000000001 -- web + dca0b5f9-659a-4928-84db-ff9fd74d8fc8 -- [foo] + + web: http://example.com/bar.foo + ok +"""]] diff --git a/doc/git-annex-registerurl.mdwn b/doc/git-annex-registerurl.mdwn index 753d06b704..bf5133b8db 100644 --- a/doc/git-annex-registerurl.mdwn +++ b/doc/git-annex-registerurl.mdwn @@ -68,6 +68,8 @@ special remote that claims it. (Usually the web special remote.) [[git-annex-unregisterurl]](1) +[[git-annex-reregisterurl]](1) + # AUTHOR Joey Hess diff --git a/doc/git-annex-reregisterurl.mdwn b/doc/git-annex-reregisterurl.mdwn new file mode 100644 index 0000000000..c22a0c6e98 --- /dev/null +++ b/doc/git-annex-reregisterurl.mdwn @@ -0,0 +1,64 @@ +# NAME + +git-annex reregisterurl - updates url registration information + +# SYNOPSIS + +git annex reregisterurl `[key]` + +# DESCRIPTION + +This plumbing-level command updates information about the urls that are +registered for a key. + +# OPTIONS + +* `--move-from=name|uuid` + + For each key, update any urls that are currently claimed by the + specified remote to be instead used by the web special remote. + + This could be used eg, when a special remote was needed to provide + authorization to get an url, but the url has now become publically + available and so the web special remote can be used. + + Note that, like `git-annex unregisterurl`, using this option unregisters + an url from a special remote, but it does not mark the content as not + present in that special remote. However, like `git-annex registerurl`, + this option does mark content as being present in the web special remote. + +* `--batch` + + In batch input mode, lines are read from stdin, and each line + should contain a key. + +* `-z` + + When in batch mode, the input is delimited by nulls instead of the usual + newlines. + +* `--json` + + Enable JSON output. This is intended to be parsed by programs that use + git-annex. Each line of output is a JSON object. + +* `--json-error-messages` + + Messages that would normally be output to standard error are included in + the JSON instead. + +* Also the [[git-annex-common-options]](1) can be used. + +# SEE ALSO + +[[git-annex]](1) + +[[git-annex-registerurl]](1) + +[[git-annex-unregisterurl]](1) + +# AUTHOR + +Joey Hess + +Warning: Automatically converted into a man page by mdwn2man. Edit with care. diff --git a/doc/git-annex-unregisterurl.mdwn b/doc/git-annex-unregisterurl.mdwn index ab535a73f6..e8192f4084 100644 --- a/doc/git-annex-unregisterurl.mdwn +++ b/doc/git-annex-unregisterurl.mdwn @@ -57,6 +57,8 @@ for it, because the content may still be present on the remote. [[git-annex-registerurl]](1) +[[git-annex-reregisterurl]](1) + [[git-annex-rmurl]](1) # AUTHOR diff --git a/doc/git-annex.mdwn b/doc/git-annex.mdwn index 9d61e675a1..30028a45e2 100644 --- a/doc/git-annex.mdwn +++ b/doc/git-annex.mdwn @@ -677,6 +677,12 @@ content from the key-value store. See [[git-annex-unregisterurl]](1) for details. +* `reregisterurl [key url]` + + Updates url registration information. + + See [[git-annex-reregisterurl]](1) for details. + * `setkey key file` Moves a file into the annex as the content of a key. diff --git a/git-annex.cabal b/git-annex.cabal index 842830180a..a207606d83 100644 --- a/git-annex.cabal +++ b/git-annex.cabal @@ -685,6 +685,7 @@ Executable git-annex Command.ReadPresentKey Command.RecvKey Command.RegisterUrl + Command.ReregisterUrl Command.Reinit Command.Reinject Command.RemoteDaemon -- 2.30.2