2021-07-14 18:25:52 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2021 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2023-04-11 18:27:22 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2021-07-14 18:25:52 +00:00
|
|
|
module Command.WhereUsed where
|
|
|
|
|
|
|
|
import Command
|
whereused --historical
Does not check the reflog, but otherwise works.
It's possible for it to display something that is not an annexed file,
if a non-annexed file somehow ends up containing something that looks
like the key's name. This seems very unlikely to happen, and it would
add a lot of complexity to detect it and somehow skip over that file,
since the git log would need to either be run again, or not limited to 1
result and canceled once enough results have been read.
Also, it kind of seems ok, if a file refers to a key, to consider that
as a place the key was used, for some definition of used. So, I punted
on dealing with that. May revisit later.
Sponsored-by: Brock Spratlen on Patreon
2021-07-14 19:38:28 +00:00
|
|
|
import Git
|
|
|
|
import Git.Sha
|
2021-07-14 18:25:52 +00:00
|
|
|
import Git.FilePath
|
whereused --historical
Does not check the reflog, but otherwise works.
It's possible for it to display something that is not an annexed file,
if a non-annexed file somehow ends up containing something that looks
like the key's name. This seems very unlikely to happen, and it would
add a lot of complexity to detect it and somehow skip over that file,
since the git log would need to either be run again, or not limited to 1
result and canceled once enough results have been read.
Also, it kind of seems ok, if a file refers to a key, to consider that
as a place the key was used, for some definition of used. So, I punted
on dealing with that. May revisit later.
Sponsored-by: Brock Spratlen on Patreon
2021-07-14 19:38:28 +00:00
|
|
|
import qualified Git.Command
|
|
|
|
import qualified Git.DiffTree as DiffTree
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Annex.Branch
|
2021-07-14 18:25:52 +00:00
|
|
|
import Annex.CatFile
|
|
|
|
import Database.Keys
|
|
|
|
|
whereused --historical
Does not check the reflog, but otherwise works.
It's possible for it to display something that is not an annexed file,
if a non-annexed file somehow ends up containing something that looks
like the key's name. This seems very unlikely to happen, and it would
add a lot of complexity to detect it and somehow skip over that file,
since the git log would need to either be run again, or not limited to 1
result and canceled once enough results have been read.
Also, it kind of seems ok, if a file refers to a key, to consider that
as a place the key was used, for some definition of used. So, I punted
on dealing with that. May revisit later.
Sponsored-by: Brock Spratlen on Patreon
2021-07-14 19:38:28 +00:00
|
|
|
import Data.Char
|
|
|
|
import qualified Data.ByteString as S
|
2023-04-11 18:27:22 +00:00
|
|
|
import qualified Data.ByteString.Char8 as S8
|
whereused --historical
Does not check the reflog, but otherwise works.
It's possible for it to display something that is not an annexed file,
if a non-annexed file somehow ends up containing something that looks
like the key's name. This seems very unlikely to happen, and it would
add a lot of complexity to detect it and somehow skip over that file,
since the git log would need to either be run again, or not limited to 1
result and canceled once enough results have been read.
Also, it kind of seems ok, if a file refers to a key, to consider that
as a place the key was used, for some definition of used. So, I punted
on dealing with that. May revisit later.
Sponsored-by: Brock Spratlen on Patreon
2021-07-14 19:38:28 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
|
2021-07-14 18:25:52 +00:00
|
|
|
cmd :: Command
|
2022-06-29 17:28:08 +00:00
|
|
|
cmd = noCommit $ withAnnexOptions [annexedMatchingOptions] $
|
2021-07-14 18:25:52 +00:00
|
|
|
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
|
2023-04-12 19:11:44 +00:00
|
|
|
fs <- filterM stillassociated
|
|
|
|
=<< mapM (liftIO . relPathCwdToFile)
|
2021-07-14 18:25:52 +00:00
|
|
|
=<< mapM (fromRepo . fromTopFilePath)
|
|
|
|
=<< getAssociatedFiles key
|
2023-04-11 18:27:22 +00:00
|
|
|
forM_ fs $ display key . QuotedPath
|
2021-07-14 18:25:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-04-11 18:27:22 +00:00
|
|
|
display :: Key -> StringContainingQuotedPath -> Annex ()
|
|
|
|
display key loc = do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
liftIO $ S8.putStrLn $ quote qp $
|
|
|
|
UnquotedByteString (serializeKey' key) <> " " <> loc
|
2021-07-14 18:25:52 +00:00
|
|
|
|
|
|
|
findHistorical :: Key -> Annex ()
|
|
|
|
findHistorical key = do
|
2021-07-14 20:05:20 +00:00
|
|
|
-- Find most recent change to the key, in all branches and
|
|
|
|
-- tags, except the git-annex branch.
|
|
|
|
found <- searchLog key
|
|
|
|
-- Search all local branches, except git-annex branch.
|
|
|
|
[ Param ("--exclude=*/" ++ fromRef (Annex.Branch.name))
|
|
|
|
, Param "--glob=*"
|
|
|
|
-- Also search remote branches
|
|
|
|
, Param ("--exclude=" ++ fromRef (Annex.Branch.name))
|
|
|
|
, Param "--remotes=*"
|
|
|
|
-- And search tags.
|
|
|
|
, Param "--tags=*"
|
|
|
|
-- Output the commit hash
|
|
|
|
, Param "--pretty=%H"
|
2023-04-11 18:27:22 +00:00
|
|
|
] $ \h fs -> do
|
|
|
|
commitsha <- liftIO $ getSha "log" (pure h)
|
2021-07-14 20:05:20 +00:00
|
|
|
commitdesc <- S.takeWhile (/= fromIntegral (ord '\n'))
|
2023-04-11 18:27:22 +00:00
|
|
|
<$> inRepo (Git.Command.pipeReadStrict
|
2021-07-14 20:05:20 +00:00
|
|
|
[ Param "describe"
|
|
|
|
, Param "--contains"
|
|
|
|
, Param "--all"
|
|
|
|
, Param (fromRef commitsha)
|
2023-04-11 18:27:22 +00:00
|
|
|
])
|
2021-07-14 20:05:20 +00:00
|
|
|
if S.null commitdesc
|
|
|
|
then return False
|
|
|
|
else process fs $
|
|
|
|
displayreffile (Ref commitdesc)
|
whereused --historical
Does not check the reflog, but otherwise works.
It's possible for it to display something that is not an annexed file,
if a non-annexed file somehow ends up containing something that looks
like the key's name. This seems very unlikely to happen, and it would
add a lot of complexity to detect it and somehow skip over that file,
since the git log would need to either be run again, or not limited to 1
result and canceled once enough results have been read.
Also, it kind of seems ok, if a file refers to a key, to consider that
as a place the key was used, for some definition of used. So, I punted
on dealing with that. May revisit later.
Sponsored-by: Brock Spratlen on Patreon
2021-07-14 19:38:28 +00:00
|
|
|
|
2021-07-14 20:05:20 +00:00
|
|
|
unless found $
|
|
|
|
void $ searchLog key
|
|
|
|
[ Param "--walk-reflogs"
|
|
|
|
-- Output the reflog selector
|
|
|
|
, Param "--pretty=%gd"
|
2023-04-11 18:27:22 +00:00
|
|
|
] $ \h fs -> process fs $
|
2021-07-14 20:05:20 +00:00
|
|
|
displayreffile (Ref h)
|
|
|
|
where
|
|
|
|
process fs a = or <$> forM fs a
|
|
|
|
|
|
|
|
displayreffile r f = do
|
2023-04-12 19:18:04 +00:00
|
|
|
tf <- inRepo $ toTopFilePath f
|
2023-04-11 18:27:22 +00:00
|
|
|
display key (descBranchFilePath (BranchFilePath r tf))
|
2021-07-14 20:05:20 +00:00
|
|
|
return True
|
|
|
|
|
2023-04-11 18:27:22 +00:00
|
|
|
searchLog :: Key -> [CommandParam] -> (S.ByteString -> [RawFilePath] -> Annex Bool) -> Annex Bool
|
|
|
|
searchLog key ps a = do
|
|
|
|
(output, cleanup) <- Annex.inRepo $ Git.Command.pipeNullSplit ps'
|
2021-07-14 20:05:20 +00:00
|
|
|
found <- case output of
|
|
|
|
(h:rest) -> do
|
|
|
|
let diff = DiffTree.parseDiffRaw rest
|
2023-04-11 18:27:22 +00:00
|
|
|
repo <- Annex.gitRepo
|
2021-07-14 20:05:20 +00:00
|
|
|
let fs = map (flip fromTopFilePath repo . DiffTree.file) diff
|
2023-04-11 18:27:22 +00:00
|
|
|
rfs <- liftIO $ mapM relPathCwdToFile fs
|
|
|
|
a (L.toStrict h) rfs
|
2021-07-14 20:05:20 +00:00
|
|
|
_ -> return False
|
2023-04-11 18:27:22 +00:00
|
|
|
liftIO $ void cleanup
|
2021-07-14 20:05:20 +00:00
|
|
|
return found
|
|
|
|
where
|
|
|
|
ps' =
|
|
|
|
[ Param "log"
|
|
|
|
, Param "-z"
|
|
|
|
-- Don't convert pointer files.
|
|
|
|
, Param "--no-textconv"
|
|
|
|
-- Don't abbreviate hashes.
|
|
|
|
, Param "--no-abbrev"
|
|
|
|
-- Only find the most recent commit, for speed.
|
|
|
|
, Param "-n1"
|
2021-07-14 20:28:07 +00:00
|
|
|
-- Be sure to treat -G as a regexp.
|
|
|
|
, Param "--basic-regexp"
|
|
|
|
-- Find commits that contain the key. The object has to
|
|
|
|
-- end with the key to avoid confusion with longer keys,
|
|
|
|
-- so a regexp is used. Since annex pointer files
|
|
|
|
-- may contain a newline followed by perhaps something
|
|
|
|
-- else, that is also matched.
|
|
|
|
, Param ("-G" ++ escapeRegexp (fromRawFilePath (keyFile key)) ++ "($|\n)")
|
2021-07-14 20:05:20 +00:00
|
|
|
-- Skip commits where the file was deleted,
|
|
|
|
-- only find those where it was added or modified.
|
|
|
|
, Param "--diff-filter=ACMRTUX"
|
|
|
|
-- Output the raw diff.
|
|
|
|
, Param "--raw"
|
|
|
|
] ++ ps
|
2021-07-14 20:28:07 +00:00
|
|
|
|
|
|
|
escapeRegexp :: String -> String
|
|
|
|
escapeRegexp = concatMap esc
|
|
|
|
where
|
|
|
|
esc c
|
|
|
|
| isAscii c && isAlphaNum c = [c]
|
|
|
|
| otherwise = ['[', c, ']']
|