oldkeys: check associated files by default and add --unchecked

Removed the prior code that checked for keys used by current versions of
the files being acted on. It is redundant with the associated files
check (so long as the associated files database is always up-to-date,
which reconcileStaged should accomplish).

Sponsored-by: Luke T. Shumaker on Patreon
This commit is contained in:
Joey Hess 2023-08-23 13:31:31 -04:00
parent 1e580a30be
commit 49b97b0675
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 34 additions and 47 deletions

View file

@ -1,4 +1,4 @@
{- GIT-annex command {- git-annex command
- -
- Copyright 2023 Joey Hess <id@joeyh.name> - Copyright 2023 Joey Hess <id@joeyh.name>
- -
@ -16,13 +16,12 @@ import qualified Annex
import Annex.CatFile import Annex.CatFile
import Utility.Terminal import Utility.Terminal
import qualified Utility.Format import qualified Utility.Format
import qualified Database.Keys
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Char8 as S8
cmd :: Command cmd :: Command
cmd = noCommit $ withAnnexOptions [annexedMatchingOptions] $ cmd = noCommit $
command "oldkeys" SectionQuery command "oldkeys" SectionQuery
"list keys used for old versions of files" "list keys used for old versions of files"
paramPaths (seek <$$> optParser) paramPaths (seek <$$> optParser)
@ -30,6 +29,7 @@ cmd = noCommit $ withAnnexOptions [annexedMatchingOptions] $
data OldKeysOptions = OldKeysOptions data OldKeysOptions = OldKeysOptions
{ fileOptions :: CmdParams { fileOptions :: CmdParams
, revisionRange :: Maybe String , revisionRange :: Maybe String
, uncheckedOption :: Bool
} }
optParser :: CmdParamsDesc -> Parser OldKeysOptions optParser :: CmdParamsDesc -> Parser OldKeysOptions
@ -39,20 +39,21 @@ optParser desc = OldKeysOptions
( long "revision-range" <> metavar "RANGE" ( long "revision-range" <> metavar "RANGE"
<> help "limit to a revision range" <> help "limit to a revision range"
)) ))
<*> switch
( long "unchecked"
<> help "don't check if current files use keys"
)
seek :: OldKeysOptions -> CommandSeek seek :: OldKeysOptions -> CommandSeek
seek o = do seek o = do
isterminal <- liftIO $ checkIsTerminal stdout isterminal <- liftIO $ checkIsTerminal stdout
-- Get the diff twice and make separate passes over it
-- to avoid needing to cache it all in memory.
currentkeys <- withdiff getcurrentkeys
withdiff $ \l -> withdiff $ \l ->
forM_ l $ \i -> forM_ l $ \i ->
when (DiffTree.srcsha i `notElem` nullShas) $ do when (DiffTree.srcsha i `notElem` nullShas) $ do
catKey (DiffTree.srcsha i) >>= \case catKey (DiffTree.srcsha i) >>= \case
Just key | S.notMember key currentkeys -> Just key -> commandAction $
commandAction $ start isterminal key start o isterminal key
_ -> return () Nothing -> return ()
where where
withdiff a = do withdiff a = do
(output, cleanup) <- Annex.inRepo $ (output, cleanup) <- Annex.inRepo $
@ -88,37 +89,16 @@ seek o = do
Just TreeSymlink -> True Just TreeSymlink -> True
_ -> False _ -> False
-- Accumulate the most recent key used for each file start :: OldKeysOptions -> IsTerminal -> Key -> CommandStart
-- (that is not deleted). start o (IsTerminal isterminal) key
-- Those keys should never be listed as old keys, even if | uncheckedOption o = go
-- some other file did have them as an old key. This avoids | otherwise = Database.Keys.getAssociatedFiles key >>= \case
-- surprising behavior for renames and reverts. [] -> go
getcurrentkeys l = getcurrentkeys' l M.empty _ -> stop
getcurrentkeys' [] m = pure $ S.fromList $ catMaybes $ M.elems m where
getcurrentkeys' (i:l) m go = startingCustomOutput key $ do
| not (isfilemode (DiffTree.dstmode i)) =
getcurrentkeys' l m
| DiffTree.dstsha i `elem` nullShas =
getcurrentkeys' l $
M.insertWith (\_ prev -> prev)
(DiffTree.file i)
Nothing
m
| otherwise = case M.lookup (DiffTree.file i) m of
Just _ -> getcurrentkeys' l m
Nothing -> catKey (DiffTree.dstsha i) >>= \case
Just key -> getcurrentkeys' l $
M.insert
(DiffTree.file i)
(Just key)
m
_ -> getcurrentkeys' l m
start :: IsTerminal -> Key -> CommandStart
start (IsTerminal isterminal) key = startingCustomOutput key $ do
liftIO $ S8.putStrLn $ if isterminal liftIO $ S8.putStrLn $ if isterminal
then Utility.Format.encode_c (const False) sk then Utility.Format.encode_c (const False) sk
else sk else sk
next $ return True next $ return True
where
sk = serializeKey' key sk = serializeKey' key

View file

@ -15,14 +15,21 @@ The output from this command can be piped into a command like
The keys are listed in order from newest to oldest. The keys are listed in order from newest to oldest.
When listing old keys for a directory, it will include the most recent When listing old keys for a directory, it will include keys used by deleted
key used by deleted files (but not by renamed files). files that were in that directory in past commits.
Note that the listed keys may still be used by other files in the
repository.
# OPTIONS # OPTIONS
* --unchecked
By default this command does not list a key that is also used by any
file in the currently checked out branch. This option makes it also
list such keys.
The default behavior avoids surprises when dropping listed keys.
This option can be useful when eg copying all old versions of a file to a
remote.
* --revision-range=value * --revision-range=value
Only list old keys used in the specified range of revisions. Only list old keys used in the specified range of revisions.