2011-12-13 19:22:43 +00:00
|
|
|
{- git check-attr interface
|
|
|
|
-
|
2012-02-14 03:42:44 +00:00
|
|
|
- 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
|
2015-01-05 19:54:52 +00:00
|
|
|
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
|
|
|
|
2013-10-15 20:04:26 +00:00
|
|
|
import System.IO.Error
|
|
|
|
|
2015-01-05 19:54:52 +00:00
|
|
|
type CheckAttrHandle = (CoProcess.CoProcessHandle, [Attr], Bool, String)
|
2012-02-14 03:42:44 +00:00
|
|
|
|
|
|
|
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. -}
|
2012-02-14 03:42:44 +00:00
|
|
|
checkAttrStart :: [Attr] -> Repo -> IO CheckAttrHandle
|
|
|
|
checkAttrStart attrs repo = do
|
2014-06-10 23:20:14 +00:00
|
|
|
currdir <- getCurrentDirectory
|
2013-05-31 16:20:17 +00:00
|
|
|
h <- CoProcess.rawMode =<< gitCoProcessStart True params repo
|
2015-01-05 19:54:52 +00:00
|
|
|
oldgit <- Git.Version.older "1.7.7"
|
|
|
|
return (h, attrs, oldgit, currdir)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
params =
|
|
|
|
[ Param "check-attr"
|
|
|
|
, Params "-z --stdin"
|
|
|
|
] ++ map Param attrs ++
|
|
|
|
[ Param "--" ]
|
2011-12-22 18:59:25 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
checkAttrStop :: CheckAttrHandle -> IO ()
|
2015-01-05 19:54:52 +00:00
|
|
|
checkAttrStop (h, _, _, _) = CoProcess.stop h
|
2012-02-14 03:42:44 +00:00
|
|
|
|
|
|
|
{- Gets an attribute of a file. -}
|
|
|
|
checkAttr :: CheckAttrHandle -> Attr -> FilePath -> IO String
|
2015-01-05 19:54:52 +00:00
|
|
|
checkAttr (h, attrs, oldgit, currdir) want file = do
|
2013-10-15 20:04:26 +00:00
|
|
|
pairs <- CoProcess.query h send (receive "")
|
2012-02-14 03:42:44 +00:00
|
|
|
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"
|
2013-10-15 20:04:26 +00:00
|
|
|
receive c from = do
|
|
|
|
s <- hGetSomeString from 1024
|
|
|
|
if null s
|
|
|
|
then eofError
|
|
|
|
else do
|
|
|
|
let v = c ++ s
|
|
|
|
maybe (receive v from) return (parse v)
|
|
|
|
eofError = ioError $ mkIOError userErrorType "git check-attr EOF" Nothing Nothing
|
|
|
|
parse s
|
|
|
|
-- new null separated output
|
|
|
|
| '\0' `elem` s = if "\0" `isSuffixOf` s
|
|
|
|
then
|
|
|
|
let bits = segment (== '\0') s
|
2013-10-16 16:14:14 +00:00
|
|
|
in if length bits == (numattrs * 3) + 1
|
2013-10-15 20:04:26 +00:00
|
|
|
then Just $ getattrvalues bits []
|
|
|
|
else Nothing -- more attributes to come
|
|
|
|
else Nothing -- output incomplete
|
|
|
|
-- old one line per value output
|
|
|
|
| otherwise = if "\n" `isSuffixOf` s
|
|
|
|
then
|
|
|
|
let ls = lines s
|
|
|
|
in if length ls == numattrs
|
|
|
|
then Just $ map (\(attr, val) -> (attr, oldattrvalue attr val))
|
|
|
|
(zip attrs ls)
|
|
|
|
else Nothing -- more attributes to come
|
|
|
|
else Nothing -- line incomplete
|
|
|
|
numattrs = length attrs
|
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
{- 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. -}
|
|
|
|
file'
|
2014-06-10 23:20:14 +00:00
|
|
|
| oldgit = absPathFrom currdir file
|
2015-01-06 19:31:24 +00:00
|
|
|
| otherwise = relPathDirToFileAbs currdir $ absPathFrom currdir file
|
2013-10-15 20:04:26 +00:00
|
|
|
oldattrvalue attr l = end bits !! 0
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
bits = split sep l
|
|
|
|
sep = ": " ++ attr ++ ": "
|
2013-10-15 20:04:26 +00:00
|
|
|
getattrvalues (_filename:attr:val:rest) c = getattrvalues rest ((attr,val):c)
|
|
|
|
getattrvalues _ c = c
|