diff --git a/CHANGELOG b/CHANGELOG index d822869bcf..00ec380f96 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +git-annex (8.20210715) UNRELEASED; urgency=medium + + * whereused: New command, finds what files use a key, or where a key + was used historically. + + -- Joey Hess Wed, 14 Jul 2021 14:26:36 -0400 + git-annex (8.20210714) upstream; urgency=medium * assistant: Avoid unncessary git repository repair in a situation where diff --git a/CmdLine/GitAnnex.hs b/CmdLine/GitAnnex.hs index fe40081483..807aa14083 100644 --- a/CmdLine/GitAnnex.hs +++ b/CmdLine/GitAnnex.hs @@ -72,6 +72,7 @@ import qualified Command.FilterBranch import qualified Command.Find import qualified Command.FindRef import qualified Command.Whereis +import qualified Command.WhereUsed import qualified Command.List import qualified Command.Log import qualified Command.Merge @@ -207,6 +208,7 @@ cmds testoptparser testrunner mkbenchmarkgenerator = map addGitAnnexGlobalOption , Command.Find.cmd , Command.FindRef.cmd , Command.Whereis.cmd + , Command.WhereUsed.cmd , Command.List.cmd , Command.Log.cmd , Command.Merge.cmd diff --git a/Command/WhereUsed.hs b/Command/WhereUsed.hs new file mode 100644 index 0000000000..4534d90e1a --- /dev/null +++ b/Command/WhereUsed.hs @@ -0,0 +1,68 @@ +{- git-annex command + - + - Copyright 2021 Joey Hess + - + - Licensed under the GNU AGPL version 3 or higher. + -} + +module Command.WhereUsed where + +import Command +import Git.FilePath +import Annex.CatFile +import Database.Keys + +cmd :: Command +cmd = noCommit $ withGlobalOptions [annexedMatchingOptions] $ + command "whereused" SectionQuery + "lists repositories that have file content" + paramNothing (seek <$$> optParser) + +data WhereUsedOptions = WhereUsedOptions + { keyOptions :: KeyOptions + , historicalOption :: Bool + } + +optParser :: CmdParamsDesc -> Parser WhereUsedOptions +optParser _desc = WhereUsedOptions + <$> (parseUnusedKeysOption <|> parseSpecificKeyOption) + <*> switch + ( long "historical" + <> help "find historical uses" + ) + +seek :: WhereUsedOptions -> CommandSeek +seek o = withKeyOptions (Just (keyOptions o)) False dummyfileseeker + (commandAction . start o) dummyfilecommandseek (WorkTreeItems []) + where + dummyfileseeker = AnnexedFileSeeker + { startAction = \_ _ _ -> return Nothing + , checkContentPresent = Nothing + , usesLocationLog = False + } + dummyfilecommandseek = const noop + +start :: WhereUsedOptions -> (SeekInput, Key, ActionItem) -> CommandStart +start o (_, key, _) = startingCustomOutput key $ do + fs <- filterM stillassociated + =<< mapM (fromRepo . fromTopFilePath) + =<< getAssociatedFiles key + liftIO $ forM_ fs $ display key . fromRawFilePath + + when (historicalOption o && null fs) $ + findHistorical key + + next $ return True + where + -- Some associated files that are in the keys database may no + -- longer correspond to files in the repository. + stillassociated f = catKeyFile f >>= \case + Just k | k == key -> return True + _ -> return False + +display :: Key -> FilePath -> IO () +display key f = putStrLn (serializeKey key ++ " " ++ f) + +findHistorical :: Key -> Annex () +findHistorical key = do + error "TODO" diff --git a/doc/git-annex-whereused.mdwn b/doc/git-annex-whereused.mdwn index d142aae7fc..385720c5e1 100644 --- a/doc/git-annex-whereused.mdwn +++ b/doc/git-annex-whereused.mdwn @@ -4,7 +4,7 @@ git-annex whereused - find what files use, or used a key # SYNOPSIS -git annex whereused `--key=K|--unused` +git annex whereused `--key=keyname|--unused` # DESCRIPTION diff --git a/doc/git-annex.mdwn b/doc/git-annex.mdwn index 82b6925120..ca5d69fe34 100644 --- a/doc/git-annex.mdwn +++ b/doc/git-annex.mdwn @@ -373,10 +373,6 @@ content from the key-value store. See [[git-annex-addunused]](1) for details. -* `whereused` - - Finds what files use, or used a key. - * `fix [path ...]` Fixes up symlinks that have become broken to again point to annexed content. @@ -451,6 +447,10 @@ content from the key-value store. See [[git-annex-list]](1) for details. +* `whereused` + + Finds what files use, or used a key. + * `log [path ...]` Displays the location log for the specified file or files, diff --git a/git-annex.cabal b/git-annex.cabal index bf6df7557e..6b5684ea53 100644 --- a/git-annex.cabal +++ b/git-annex.cabal @@ -144,6 +144,7 @@ Extra-Source-Files: doc/git-annex-watch.mdwn doc/git-annex-webapp.mdwn doc/git-annex-whereis.mdwn + doc/git-annex-whereused.mdwn doc/git-remote-tor-annex.mdwn doc/logo.svg doc/logo_16x16.png @@ -830,6 +831,7 @@ Executable git-annex Command.View Command.Wanted Command.Whereis + Command.WhereUsed Common Config Config.Cost