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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
{- git check-ignore interface
-
- Copyright 2013 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
module Git.CheckIgnore (
CheckIgnoreHandle,
checkIgnoreStart,
checkIgnoreStop,
checkIgnored
) where
import Common
import Git
import Git.Command
import qualified Utility.CoProcess as CoProcess
import System.IO.Error
import qualified Data.ByteString as B
type CheckIgnoreHandle = CoProcess.CoProcessHandle
{- Starts git check-ignore running, and returns a handle.
-
- This relies on git check-ignore --non-matching -v outputting
- lines for both matching an non-matching files. Also relies on
- GIT_FLUSH behavior flushing the output buffer when git check-ignore
- is piping to us.
-
- check-ignore does not support --literal-pathspecs, so remove that
- from the gitGlobalOpts if set.
-}
checkIgnoreStart :: Repo -> IO CheckIgnoreHandle
checkIgnoreStart repo = gitCoProcessStart True params repo'
where
params =
[ Param "check-ignore"
, Param "-z"
, Param "--stdin"
, Param "--verbose"
, Param "--non-matching"
]
repo' = repo { gitGlobalOpts = filter (not . pathspecs) (gitGlobalOpts repo) }
pathspecs (Param "--literal-pathspecs") = True
pathspecs _ = False
{- For some reason, check-ignore --batch always exits nonzero,
- so ignore any error. -}
checkIgnoreStop :: CheckIgnoreHandle -> IO ()
checkIgnoreStop = void . tryIO . CoProcess.stop
{- Returns True if a file is ignored. -}
checkIgnored :: CheckIgnoreHandle -> OsPath -> IO Bool
checkIgnored h file = CoProcess.query h send (receive "")
where
send to = do
B.hPutStr to $ fromOsPath file `B.snoc` 0
hFlush to
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)
parse s = case segment (== '\0') s of
(_source:_line:pattern:_pathname:_eol:[]) -> Just $ not $ null pattern
_ -> Nothing
eofError = ioError $ mkIOError userErrorType "git check-ignore EOF" Nothing Nothing
|