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-09-12 16:48:49 +00:00
|
|
|
import Git.Types
|
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)
|
2023-09-12 16:48:49 +00:00
|
|
|
(batchable run optParser)
|
|
|
|
|
|
|
|
data LookupKeyOptions = LookupKeyOptions
|
|
|
|
{ refOption :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: Parser LookupKeyOptions
|
|
|
|
optParser = LookupKeyOptions
|
|
|
|
<$> switch
|
|
|
|
( long "ref"
|
|
|
|
<> help "look up key used by git ref to file"
|
|
|
|
)
|
|
|
|
|
|
|
|
run :: LookupKeyOptions -> SeekInput -> String -> Annex Bool
|
|
|
|
run o _ file
|
|
|
|
| refOption o = catKey (Ref (toRawFilePath file)) >>= display
|
|
|
|
| otherwise = seekSingleGitFile file >>= \case
|
2015-07-12 00:43:45 +00:00
|
|
|
Nothing -> return False
|
2023-09-12 16:48:49 +00:00
|
|
|
Just file' -> catKeyFile file' >>= display
|
|
|
|
|
|
|
|
display :: Maybe Key -> Annex Bool
|
|
|
|
display (Just k) = do
|
|
|
|
IsTerminal isterminal <- liftIO $ checkIsTerminal stdout
|
|
|
|
let sk = serializeKey k
|
|
|
|
liftIO $ putStrLn $ if isterminal then safeOutput sk else sk
|
|
|
|
return True
|
|
|
|
display 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)
|
2023-10-30 18:59:09 +00:00
|
|
|
seekSingleGitFile file
|
|
|
|
| isRelative file = return (Just (toRawFilePath file))
|
|
|
|
| otherwise = do
|
|
|
|
(l, cleanup) <- inRepo (Git.LsFiles.inRepo [] [toRawFilePath file])
|
|
|
|
r <- case l of
|
|
|
|
(f:[]) | takeFileName (fromRawFilePath f) == takeFileName file ->
|
|
|
|
return (Just f)
|
|
|
|
_ -> return Nothing
|
|
|
|
void $ liftIO cleanup
|
|
|
|
return r
|