summaryrefslogtreecommitdiff
path: root/Git/ByteString.hs
blob: 4eb6a4876b8d2717b8232cd5af99e74392d7608f (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{- module using Data.ByteString.Lazy.Char8 for git IO
 -
 - This can be imported instead of Git when more efficient ByteString IO
 - is needed.
 -
 - Copyright 2011 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Git.ByteString (
	module Git,
	pipeRead,
	pipeWrite,
	pipeWriteRead,
	pipeNullSplit
) where

import Control.Applicative
import System.Cmd.Utils
import System.IO
import qualified Data.ByteString.Lazy.Char8 as L

import Git hiding (pipeRead, pipeWrite, pipeWriteRead, pipeNullSplit)
import Utility.SafeCommand

{- Runs a git subcommand and returns its output, lazily. 
 -
 - Note that this leaves the git process running, and so zombies will
 - result unless reap is called.
 -}
pipeRead :: Repo -> [CommandParam] -> IO L.ByteString
pipeRead repo params = assertLocal repo $ do
	(_, h) <- hPipeFrom "git" $ toCommand $ gitCommandLine repo params
	hSetBinaryMode h True
	L.hGetContents h

{- Runs a git subcommand, feeding it input.
 - You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
pipeWrite :: Repo -> [CommandParam] -> L.ByteString -> IO PipeHandle
pipeWrite repo params s = assertLocal repo $ do
	(p, h) <- hPipeTo "git" (toCommand $ gitCommandLine repo params)
	L.hPut h s
	hClose h
	return p

{- Runs a git subcommand, feeding it input, and returning its output.
 - You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
pipeWriteRead :: Repo -> [CommandParam] -> L.ByteString -> IO (PipeHandle, L.ByteString)
pipeWriteRead repo params s = assertLocal repo $ do
	(p, from, to) <- hPipeBoth "git" (toCommand $ gitCommandLine repo params)
	hSetBinaryMode from True
	L.hPut to s
	hClose to
	c <- L.hGetContents from
	return (p, c)

{- Reads null terminated output of a git command (as enabled by the -z 
 - parameter), and splits it. -}
pipeNullSplit :: Repo -> [CommandParam] -> IO [L.ByteString]
pipeNullSplit repo params = filter (not . L.null) . L.split '\0' <$>
	pipeRead repo params