refactoring

This commit is contained in:
Joey Hess 2013-05-11 23:11:56 -04:00
parent 1e2ddcb68a
commit 5e1458152f
3 changed files with 23 additions and 31 deletions

View file

@ -5,8 +5,6 @@
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
{-# LANGUAGE CPP #-}
module Git.CatFile ( module Git.CatFile (
CatFileHandle, CatFileHandle,
catFileStart, catFileStart,
@ -30,7 +28,7 @@ import qualified Utility.CoProcess as CoProcess
type CatFileHandle = CoProcess.CoProcessHandle type CatFileHandle = CoProcess.CoProcessHandle
catFileStart :: Repo -> IO CatFileHandle catFileStart :: Repo -> IO CatFileHandle
catFileStart = gitCoProcessStart catFileStart = CoProcess.rawMode <=< gitCoProcessStart
[ Param "cat-file" [ Param "cat-file"
, Param "--batch" , Param "--batch"
] ]
@ -51,17 +49,8 @@ catObject h object = maybe L.empty fst <$> catObjectDetails h object
catObjectDetails :: CatFileHandle -> Ref -> IO (Maybe (L.ByteString, Sha)) catObjectDetails :: CatFileHandle -> Ref -> IO (Maybe (L.ByteString, Sha))
catObjectDetails h object = CoProcess.query h send receive catObjectDetails h object = CoProcess.query h send receive
where where
send to = do send to = hPutStrLn to $ show object
fileEncoding to
#ifdef __WINDOWS__
hSetNewlineMode to noNewlineTranslation
#endif
hPutStrLn to $ show object
receive from = do receive from = do
fileEncoding from
#ifdef __WINDOWS__
hSetNewlineMode from noNewlineTranslation
#endif
header <- hGetLine from header <- hGetLine from
case words header of case words header of
[sha, objtype, size] [sha, objtype, size]

View file

@ -5,8 +5,6 @@
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
{-# LANGUAGE CPP #-}
module Git.HashObject where module Git.HashObject where
import Common import Common
@ -19,7 +17,7 @@ import qualified Utility.CoProcess as CoProcess
type HashObjectHandle = CoProcess.CoProcessHandle type HashObjectHandle = CoProcess.CoProcessHandle
hashObjectStart :: Repo -> IO HashObjectHandle hashObjectStart :: Repo -> IO HashObjectHandle
hashObjectStart = gitCoProcessStart hashObjectStart = CoProcess.rawMode <=< gitCoProcessStart
[ Param "hash-object" [ Param "hash-object"
, Param "-w" , Param "-w"
, Param "--stdin-paths" , Param "--stdin-paths"
@ -32,23 +30,13 @@ hashObjectStop = CoProcess.stop
hashFile :: HashObjectHandle -> FilePath -> IO Sha hashFile :: HashObjectHandle -> FilePath -> IO Sha
hashFile h file = CoProcess.query h send receive hashFile h file = CoProcess.query h send receive
where where
send to = do send to = hPutStrLn to file
fileEncoding to receive from = getSha "hash-object" $ hGetLine from
#ifdef __WINDOWS__
hSetNewlineMode to noNewlineTranslation
#endif
hPutStrLn to file
receive from = getSha "hash-object" $ do
#ifdef __WINDOWS__
hSetNewlineMode from noNewlineTranslation
#endif
hGetLine from
{- Injects some content into git, returning its Sha. -} {- Injects some content into git, returning its Sha. -}
hashObject :: ObjectType -> String -> Repo -> IO Sha hashObject :: ObjectType -> String -> Repo -> IO Sha
hashObject objtype content repo = getSha subcmd $ do hashObject objtype content repo = getSha subcmd $
s <- pipeWriteRead (map Param params) content repo pipeWriteRead (map Param params) content repo
return s
where where
subcmd = "hash-object" subcmd = "hash-object"
params = [subcmd, "-t", show objtype, "-w", "--stdin"] params = [subcmd, "-t", show objtype, "-w", "--stdin"]

View file

@ -6,11 +6,14 @@
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
{-# LANGUAGE CPP #-}
module Utility.CoProcess ( module Utility.CoProcess (
CoProcessHandle, CoProcessHandle,
start, start,
stop, stop,
query query,
rawMode
) where ) where
import Common import Common
@ -33,3 +36,15 @@ query (_, from, to, _) send receive = do
_ <- send to _ <- send to
hFlush to hFlush to
receive from receive from
rawMode :: CoProcessHandle -> IO CoProcessHandle
rawMode ch@(_, from, to, _) = do
raw from
raw to
return ch
where
raw h = do
fileEncoding h
#ifdef __WINDOWS__
hSetNewlineMode h noNewlineTranslation
#endif