2011-12-13 19:22:43 +00:00
|
|
|
{- git check-attr interface
|
|
|
|
-
|
2021-01-06 18:11:08 +00:00
|
|
|
- Copyright 2010-2021 Joey Hess <id@joeyh.name>
|
2011-12-13 19:22:43 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-12-13 19:22:43 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Git.CheckAttr where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2012-02-20 19:20:36 +00:00
|
|
|
import qualified Utility.CoProcess as CoProcess
|
2020-10-28 20:03:45 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2011-12-13 19:22:43 +00:00
|
|
|
|
2013-10-15 20:04:26 +00:00
|
|
|
import System.IO.Error
|
2020-10-28 19:40:50 +00:00
|
|
|
import qualified Data.ByteString as B
|
2013-10-15 20:04:26 +00:00
|
|
|
|
2020-10-28 19:40:50 +00:00
|
|
|
type CheckAttrHandle = (CoProcess.CoProcessHandle, [Attr], RawFilePath)
|
2012-02-14 03:42:44 +00:00
|
|
|
|
|
|
|
type Attr = String
|
|
|
|
|
2021-01-06 18:11:08 +00:00
|
|
|
{- Starts git check-attr running to look up the specified attributes
|
|
|
|
- and returns a handle. -}
|
2012-02-14 03:42:44 +00:00
|
|
|
checkAttrStart :: [Attr] -> Repo -> IO CheckAttrHandle
|
|
|
|
checkAttrStart attrs repo = do
|
2020-10-28 20:03:45 +00:00
|
|
|
currdir <- R.getCurrentDirectory
|
2016-11-01 18:03:55 +00:00
|
|
|
h <- gitCoProcessStart True params repo
|
2019-09-11 20:10:25 +00:00
|
|
|
return (h, attrs, currdir)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
params =
|
|
|
|
[ Param "check-attr"
|
2015-06-01 17:52:23 +00:00
|
|
|
, Param "-z"
|
|
|
|
, Param "--stdin"
|
2012-12-13 04:24:19 +00:00
|
|
|
] ++ map Param attrs ++
|
|
|
|
[ Param "--" ]
|
2011-12-22 18:59:25 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
checkAttrStop :: CheckAttrHandle -> IO ()
|
2019-09-11 20:10:25 +00:00
|
|
|
checkAttrStop (h, _, _) = CoProcess.stop h
|
2012-02-14 03:42:44 +00:00
|
|
|
|
2020-10-28 19:40:50 +00:00
|
|
|
checkAttr :: CheckAttrHandle -> Attr -> RawFilePath -> IO String
|
2021-01-06 18:11:08 +00:00
|
|
|
checkAttr h want file = checkAttrs h [want] file >>= return . \case
|
|
|
|
(v:_) -> v
|
|
|
|
[] -> ""
|
|
|
|
|
|
|
|
{- Gets attributes of a file. When an attribute is not specified,
|
|
|
|
- returns "" for it. -}
|
|
|
|
checkAttrs :: CheckAttrHandle -> [Attr] -> RawFilePath -> IO [String]
|
|
|
|
checkAttrs (h, attrs, currdir) want file = do
|
|
|
|
l <- CoProcess.query h send (receive "")
|
|
|
|
return (getvals l want)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2021-01-06 18:11:08 +00:00
|
|
|
getvals _ [] = []
|
|
|
|
getvals l (x:xs) = case map snd $ filter (\(attr, _) -> attr == x) l of
|
|
|
|
["unspecified"] -> "" : getvals l xs
|
|
|
|
[v] -> v : getvals l xs
|
2023-04-10 17:38:14 +00:00
|
|
|
_ -> giveup $ "unable to determine " ++ x ++ " attribute of " ++ fromRawFilePath file
|
2021-01-06 18:11:08 +00:00
|
|
|
|
2020-10-28 19:40:50 +00:00
|
|
|
send to = B.hPutStr to $ file' `B.snoc` 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
|
|
|
|
|
2019-09-11 20:10:25 +00:00
|
|
|
{- git check-attr chokes on some absolute filenames,
|
|
|
|
- so make sure the filename is relative. -}
|
|
|
|
file' = 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
|
2016-02-02 19:18:17 +00:00
|
|
|
|
2016-02-05 22:41:23 +00:00
|
|
|
{- User may enter this to override a previous attr setting, when they wish
|
|
|
|
- to not specify an attr for some files. -}
|
2016-02-02 19:18:17 +00:00
|
|
|
unspecifiedAttr :: String
|
|
|
|
unspecifiedAttr = "!"
|