2013-12-15 18:02:23 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2017-12-08 19:35:02 +00:00
|
|
|
- Copyright 2013-2017 Joey Hess <id@joeyh.name>
|
2013-12-15 18:02:23 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-12-15 18:02:23 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.LookupKey where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Annex.CatFile
|
2017-12-08 19:35:02 +00:00
|
|
|
import qualified Git.LsFiles
|
2023-04-12 18:03:44 +00:00
|
|
|
import Utility.Terminal
|
|
|
|
import Utility.SafeOutput
|
2013-12-15 18:02:23 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2016-01-29 17:20:24 +00:00
|
|
|
cmd = notBareRepo $ noCommit $ noMessages $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "lookupkey" SectionPlumbing
|
|
|
|
"looks up key used for file"
|
2015-07-12 00:43:45 +00:00
|
|
|
(paramRepeating paramFile)
|
|
|
|
(batchable run (pure ()))
|
2013-12-15 18:02:23 +00:00
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
run :: () -> SeekInput -> String -> Annex Bool
|
|
|
|
run _ _ file = seekSingleGitFile file >>= \case
|
2017-12-08 19:35:02 +00:00
|
|
|
Nothing -> return False
|
|
|
|
Just file' -> catKeyFile file' >>= \case
|
2015-07-12 00:43:45 +00:00
|
|
|
Just k -> do
|
2023-04-12 18:03:44 +00:00
|
|
|
IsTerminal isterminal <- liftIO $ checkIsTerminal stdout
|
|
|
|
let sk = serializeKey k
|
|
|
|
liftIO $ putStrLn $ if isterminal then safeOutput sk else sk
|
2015-07-12 00:43:45 +00:00
|
|
|
return True
|
|
|
|
Nothing -> return False
|
2017-12-08 19:35:02 +00:00
|
|
|
|
|
|
|
-- To support absolute filenames, pass through git ls-files.
|
|
|
|
-- But, this plumbing command does not recurse through directories.
|
2019-12-05 15:40:10 +00:00
|
|
|
seekSingleGitFile :: FilePath -> Annex (Maybe RawFilePath)
|
2017-12-08 19:35:02 +00:00
|
|
|
seekSingleGitFile file = do
|
2020-05-28 19:55:17 +00:00
|
|
|
(l, cleanup) <- inRepo (Git.LsFiles.inRepo [] [toRawFilePath file])
|
2017-12-08 19:35:02 +00:00
|
|
|
r <- case l of
|
2019-12-05 15:40:10 +00:00
|
|
|
(f:[]) | takeFileName (fromRawFilePath f) == takeFileName file ->
|
|
|
|
return (Just f)
|
2017-12-08 19:35:02 +00:00
|
|
|
_ -> return Nothing
|
|
|
|
void $ liftIO cleanup
|
|
|
|
return r
|