git-annex/Command/LookupKey.hs
Joey Hess d3ab5e626b
rename key2file and file2key
What these generate is not really suitable to be used as a filename,
which is why keyFile and fileKey further escape it. These are just
serializing Keys.

Also removed a quickcheck test that was very unlikely to test anything
useful, since it relied on random chance creating something that looks
like a serialized key. The other test is sufficient for testing what
that was intended to test anyway.
2019-01-14 13:03:35 -04:00

39 lines
1 KiB
Haskell

{- git-annex command
-
- Copyright 2013-2017 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.LookupKey where
import Command
import Annex.CatFile
import qualified Git.LsFiles
cmd :: Command
cmd = notBareRepo $ noCommit $ noMessages $
command "lookupkey" SectionPlumbing
"looks up key used for file"
(paramRepeating paramFile)
(batchable run (pure ()))
run :: () -> String -> Annex Bool
run _ file = seekSingleGitFile file >>= \case
Nothing -> return False
Just file' -> catKeyFile file' >>= \case
Just k -> do
liftIO $ putStrLn $ serializeKey k
return True
Nothing -> return False
-- To support absolute filenames, pass through git ls-files.
-- But, this plumbing command does not recurse through directories.
seekSingleGitFile :: FilePath -> Annex (Maybe FilePath)
seekSingleGitFile file = do
(l, cleanup) <- inRepo (Git.LsFiles.inRepo [file])
r <- case l of
(f:[]) | takeFileName f == takeFileName file -> return (Just f)
_ -> return Nothing
void $ liftIO cleanup
return r