aboutsummaryrefslogtreecommitdiff
path: root/Command/LookupKey.hs
blob: 1a2a57f220ccd2f4075cbd3f90c83596dbb27153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{- 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 $ key2file 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