2023-08-23 17:31:31 +00:00
|
|
|
{- git-annex command
|
2023-08-22 18:51:06 +00:00
|
|
|
-
|
|
|
|
- Copyright 2023 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.OldKeys where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Git.Types
|
|
|
|
import Git.Sha
|
|
|
|
import qualified Git.Command
|
|
|
|
import qualified Git.DiffTree as DiffTree
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.CatFile
|
|
|
|
import Utility.Terminal
|
|
|
|
import qualified Utility.Format
|
2023-08-23 17:31:31 +00:00
|
|
|
import qualified Database.Keys
|
2023-08-22 18:51:06 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Char8 as S8
|
|
|
|
|
|
|
|
cmd :: Command
|
2023-08-23 17:31:31 +00:00
|
|
|
cmd = noCommit $
|
2023-08-22 18:51:06 +00:00
|
|
|
command "oldkeys" SectionQuery
|
|
|
|
"list keys used for old versions of files"
|
|
|
|
paramPaths (seek <$$> optParser)
|
|
|
|
|
|
|
|
data OldKeysOptions = OldKeysOptions
|
|
|
|
{ fileOptions :: CmdParams
|
2023-08-22 19:00:29 +00:00
|
|
|
, revisionRange :: Maybe String
|
2023-08-23 17:31:31 +00:00
|
|
|
, uncheckedOption :: Bool
|
2023-08-22 18:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser OldKeysOptions
|
|
|
|
optParser desc = OldKeysOptions
|
|
|
|
<$> cmdParams desc
|
2023-08-22 19:00:29 +00:00
|
|
|
<*> optional (strOption
|
|
|
|
( long "revision-range" <> metavar "RANGE"
|
|
|
|
<> help "limit to a revision range"
|
|
|
|
))
|
2023-08-23 17:31:31 +00:00
|
|
|
<*> switch
|
|
|
|
( long "unchecked"
|
|
|
|
<> help "don't check if current files use keys"
|
|
|
|
)
|
2023-08-22 18:51:06 +00:00
|
|
|
|
|
|
|
seek :: OldKeysOptions -> CommandSeek
|
|
|
|
seek o = do
|
|
|
|
isterminal <- liftIO $ checkIsTerminal stdout
|
|
|
|
withdiff $ \l ->
|
|
|
|
forM_ l $ \i ->
|
|
|
|
when (DiffTree.srcsha i `notElem` nullShas) $ do
|
|
|
|
catKey (DiffTree.srcsha i) >>= \case
|
2023-08-23 17:31:31 +00:00
|
|
|
Just key -> commandAction $
|
|
|
|
start o isterminal key
|
|
|
|
Nothing -> return ()
|
2023-08-22 18:51:06 +00:00
|
|
|
where
|
|
|
|
withdiff a = do
|
|
|
|
(output, cleanup) <- Annex.inRepo $
|
|
|
|
Git.Command.pipeNullSplit ps
|
|
|
|
let l = filter (isfilemode . DiffTree.srcmode)
|
|
|
|
(DiffTree.parseDiffRaw output)
|
|
|
|
r <- a l
|
|
|
|
liftIO $ void cleanup
|
|
|
|
return r
|
|
|
|
|
|
|
|
ps =
|
|
|
|
[ Param "log"
|
|
|
|
, Param "-z"
|
|
|
|
-- Don't convert pointer files.
|
|
|
|
, Param "--no-textconv"
|
|
|
|
-- Don't abbreviate hashes.
|
|
|
|
, Param "--no-abbrev"
|
|
|
|
-- Don't show renames.
|
|
|
|
, Param "--no-renames"
|
|
|
|
-- Output the raw diff.
|
|
|
|
, Param "--raw"
|
|
|
|
-- Avoid outputting anything except for the raw diff.
|
|
|
|
, Param "--pretty="
|
2023-08-22 19:00:29 +00:00
|
|
|
]
|
|
|
|
++ case revisionRange o of
|
|
|
|
Nothing -> []
|
|
|
|
Just rr -> [Param rr]
|
|
|
|
++ map File (fileOptions o)
|
2023-08-22 18:51:06 +00:00
|
|
|
|
|
|
|
isfilemode m = case toTreeItemType m of
|
|
|
|
Just TreeFile -> True
|
|
|
|
Just TreeExecutable -> True
|
|
|
|
Just TreeSymlink -> True
|
|
|
|
_ -> False
|
|
|
|
|
2023-08-23 17:31:31 +00:00
|
|
|
start :: OldKeysOptions -> IsTerminal -> Key -> CommandStart
|
|
|
|
start o (IsTerminal isterminal) key
|
|
|
|
| uncheckedOption o = go
|
|
|
|
| otherwise = Database.Keys.getAssociatedFiles key >>= \case
|
|
|
|
[] -> go
|
|
|
|
_ -> stop
|
2023-08-22 18:51:06 +00:00
|
|
|
where
|
2023-08-23 17:31:31 +00:00
|
|
|
go = startingCustomOutput key $ do
|
|
|
|
liftIO $ S8.putStrLn $ if isterminal
|
|
|
|
then Utility.Format.encode_c (const False) sk
|
|
|
|
else sk
|
|
|
|
next $ return True
|
2023-08-22 18:51:06 +00:00
|
|
|
sk = serializeKey' key
|