add reregisterurl command
authorJoey Hess <joeyh@joeyh.name>
Tue, 5 Mar 2024 19:04:35 +0000 (15:04 -0400)
committerJoey Hess <joeyh@joeyh.name>
Tue, 5 Mar 2024 19:06:14 +0000 (15:06 -0400)
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
CmdLine/GitAnnex.hs
Command/ReregisterUrl.hs [new file with mode: 0644]
doc/forum/how_to___34__move__34___URL_between_remotes__63__/comment_6_260c238435f8f5e53511de7e574dd53f._comment [new file with mode: 0644]
doc/git-annex-registerurl.mdwn
doc/git-annex-reregisterurl.mdwn [new file with mode: 0644]
doc/git-annex-unregisterurl.mdwn
doc/git-annex.mdwn
git-annex.cabal

index 2bc030c19d1a0202656c6d24b16d033da3d93b1d..e7a608e51c66ec6792bf38a37bcb14473a37fe90 100644 (file)
--- 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 <id@joeyh.name>  Tue, 27 Feb 2024 13:07:10 -0400
 
index 47a37a3de75e7c5ae02cb068e272d0965c888496..debf30fffdf49ec31e12c7f5d0a8394c4d68b087 100644 (file)
@@ -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 (file)
index 0000000..5d95f0d
--- /dev/null
@@ -0,0 +1,79 @@
+{- git-annex command
+ -
+ - Copyright 2015-2024 Joey Hess <id@joeyh.name>
+ -
+ - 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 (file)
index 0000000..f1114fc
--- /dev/null
@@ -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
+"""]]
index 753d06b70460aa7aaa67ad905a59b7b2f0ceb5c5..bf5133b8db9bd1914d7605f83a39716b4f96a14d 100644 (file)
@@ -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 <id@joeyh.name>
diff --git a/doc/git-annex-reregisterurl.mdwn b/doc/git-annex-reregisterurl.mdwn
new file mode 100644 (file)
index 0000000..c22a0c6
--- /dev/null
@@ -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 <id@joeyh.name>
+
+Warning: Automatically converted into a man page by mdwn2man. Edit with care.
index ab535a73f62aa4206fe371d15c39866eeea6c53c..e8192f40843cee8ddd78bdcd363e809c76f4f7be 100644 (file)
@@ -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
index 9d61e675a19ab33e687c2e45aa7bca55b4afdeaf..30028a45e2d81cbdaca49a4a8ad13d34329029c8 100644 (file)
@@ -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.
index 842830180a55daf794b43b45519922d47c90fda2..a207606d831125b1ff96e09801cfe6ceccd89892 100644 (file)
@@ -685,6 +685,7 @@ Executable git-annex
     Command.ReadPresentKey
     Command.RecvKey
     Command.RegisterUrl
+    Command.ReregisterUrl
     Command.Reinit
     Command.Reinject
     Command.RemoteDaemon