This commit is contained in:
Joey Hess 2021-11-04 15:03:12 -04:00
parent 68257e9076
commit 218e1983ad
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 37 additions and 37 deletions

View file

@ -14,9 +14,6 @@ module Git.FilterProcess (
WelcomeMessage(..),
Version(..),
Capability(..),
readUntilFlushPkt,
readUntilFlushPktOrSize,
discardUntilFlushPkt,
longRunningProcessHandshake,
longRunningFilterProcessHandshake,
FilterRequest(..),
@ -77,40 +74,6 @@ decodeCapability pktline = decodeConfigValue pktline >>= \case
ConfigValue "capability" c -> Just $ Capability c
_ -> Nothing
{- Reads PktLines until a flushPkt (or EOF),
- and returns all except the flushPkt -}
readUntilFlushPkt :: IO [PktLine]
readUntilFlushPkt = go []
where
go l = readPktLine stdin >>= \case
Just pktline | not (isFlushPkt pktline) -> go (pktline:l)
_ -> return (reverse l)
{- Reads PktLines until at least the specified number of bytes have been
- read, or until a flushPkt (or EOF). Returns Right if it did read a
- flushPkt/EOF, and Left if there is still content leftover that needs to
- be read. -}
readUntilFlushPktOrSize :: Int -> IO (Either [PktLine] [PktLine])
readUntilFlushPktOrSize = go []
where
go l n = readPktLine stdin >>= \case
Just pktline
| isFlushPkt pktline -> return (Right (reverse l))
| otherwise ->
let len = B.length (pktLineToByteString pktline)
n' = n - len
in if n' <= 0
then return (Left (reverse (pktline:l)))
else go (pktline:l) n'
Nothing -> return (Right (reverse l))
{- Reads PktLines until a flushPkt (or EOF), and throws them away. -}
discardUntilFlushPkt :: IO ()
discardUntilFlushPkt = readPktLine stdin >>= \case
Just pktline | isFlushPkt pktline -> return ()
Nothing -> return ()
_ -> discardUntilFlushPkt
longRunningProcessHandshake
:: (WelcomeMessage -> Maybe WelcomeMessage)
-> ([Version] -> [Version])

View file

@ -19,6 +19,9 @@ module Git.PktLine (
writePktLine,
flushPkt,
isFlushPkt,
readUntilFlushPkt,
readUntilFlushPktOrSize,
discardUntilFlushPkt,
) where
import System.IO
@ -115,3 +118,37 @@ flushPkt = PktLine mempty
isFlushPkt :: PktLine -> Bool
isFlushPkt (PktLine b) = b == mempty
{- Reads PktLines until a flushPkt (or EOF),
- and returns all except the flushPkt -}
readUntilFlushPkt :: IO [PktLine]
readUntilFlushPkt = go []
where
go l = readPktLine stdin >>= \case
Just pktline | not (isFlushPkt pktline) -> go (pktline:l)
_ -> return (reverse l)
{- Reads PktLines until at least the specified number of bytes have been
- read, or until a flushPkt (or EOF). Returns Right if it did read a
- flushPkt/EOF, and Left if there is still content leftover that needs to
- be read. -}
readUntilFlushPktOrSize :: Int -> IO (Either [PktLine] [PktLine])
readUntilFlushPktOrSize = go []
where
go l n = readPktLine stdin >>= \case
Just pktline
| isFlushPkt pktline -> return (Right (reverse l))
| otherwise ->
let len = B.length (pktLineToByteString pktline)
n' = n - len
in if n' <= 0
then return (Left (reverse (pktline:l)))
else go (pktline:l) n'
Nothing -> return (Right (reverse l))
{- Reads PktLines until a flushPkt (or EOF), and throws them away. -}
discardUntilFlushPkt :: IO ()
discardUntilFlushPkt = readPktLine stdin >>= \case
Just pktline | isFlushPkt pktline -> return ()
Nothing -> return ()
_ -> discardUntilFlushPkt