git-annex/Git/CheckAttr.hs

65 lines
1.9 KiB
Haskell
Raw Normal View History

2011-12-13 19:22:43 +00:00
{- git check-attr interface
-
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
2011-12-13 19:22:43 +00:00
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Git.CheckAttr where
import Common
import Git
2011-12-14 19:56:11 +00:00
import Git.Command
import qualified Git.Version
2012-02-20 19:20:36 +00:00
import qualified Utility.CoProcess as CoProcess
2011-12-13 19:22:43 +00:00
2012-02-20 19:20:36 +00:00
type CheckAttrHandle = (CoProcess.CoProcessHandle, [Attr], String)
type Attr = String
{- Starts git check-attr running to look up the specified gitattributes
2012-02-21 04:16:24 +00:00
- values and returns a handle. -}
checkAttrStart :: [Attr] -> Repo -> IO CheckAttrHandle
checkAttrStart attrs repo = do
2011-12-13 19:22:43 +00:00
cwd <- getCurrentDirectory
2013-05-12 21:37:06 +00:00
h <- CoProcess.rawMode =<< gitCoProcessStart params repo
2012-02-20 19:20:36 +00:00
return (h, attrs, cwd)
2012-12-13 04:24:19 +00:00
where
params =
[ Param "check-attr"
, Params "-z --stdin"
] ++ map Param attrs ++
[ Param "--" ]
checkAttrStop :: CheckAttrHandle -> IO ()
2012-02-20 19:20:36 +00:00
checkAttrStop (h, _, _) = CoProcess.stop h
{- Gets an attribute of a file. -}
checkAttr :: CheckAttrHandle -> Attr -> FilePath -> IO String
2012-02-20 19:20:36 +00:00
checkAttr (h, attrs, cwd) want file = do
pairs <- CoProcess.query h send receive
let vals = map snd $ filter (\(attr, _) -> attr == want) pairs
case vals of
[v] -> return v
_ -> error $ "unable to determine " ++ want ++ " attribute of " ++ file
2012-12-13 04:24:19 +00:00
where
2013-05-12 21:37:06 +00:00
send to = hPutStr to $ file' ++ "\0"
2012-12-13 04:24:19 +00:00
receive from = forM attrs $ \attr -> do
l <- hGetLine from
return (attr, attrvalue attr l)
{- Before git 1.7.7, git check-attr worked best with
- absolute filenames; using them worked around some bugs
- with relative filenames.
-
- With newer git, git check-attr chokes on some absolute
- filenames, and the bugs that necessitated them were fixed,
- so use relative filenames. -}
oldgit = Git.Version.older "1.7.7"
file'
| oldgit = absPathFrom cwd file
| otherwise = relPathDirToFile cwd $ absPathFrom cwd file
attrvalue attr l = end bits !! 0
where
bits = split sep l
sep = ": " ++ attr ++ ": "