convert Git.HashObject to use ByteStrings

Both lazy and strict, because sometimes it's more efficient to build a
small strict bytestring, and other times better to lazily stream.
This commit is contained in:
Joey Hess 2019-01-03 13:19:59 -04:00
parent 7d51b0c109
commit 53905490df
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 20 additions and 11 deletions

View file

@ -41,7 +41,7 @@ hashFile f = do
{- Note that the content will be written to a temp file.
- So it may be faster to use Git.HashObject.hashObject for large
- blob contents. -}
hashBlob :: String -> Annex Sha
hashBlob :: Git.HashObject.HashableBlob b => b -> Annex Sha
hashBlob content = do
h <- hashObjectHandle
liftIO $ Git.HashObject.hashBlob h content

View file

@ -112,7 +112,7 @@ addAnnexLink linktarget file = do
{- Injects a symlink target into git, returning its Sha. -}
hashSymlink :: LinkTarget -> Annex Sha
hashSymlink linktarget = hashBlob (toInternalGitPath linktarget)
hashSymlink linktarget = hashBlob $ toRawFilePath $ toInternalGitPath linktarget
{- Stages a symlink to an annexed object, using a Sha of its target. -}
stageSymlink :: FilePath -> Sha -> Annex ()
@ -122,7 +122,7 @@ stageSymlink file sha =
{- Injects a pointer file content into git, returning its Sha. -}
hashPointerFile :: Key -> Annex Sha
hashPointerFile key = hashBlob (formatPointer key)
hashPointerFile key = hashBlob $ encodeBS $ formatPointer key
{- Stages a pointer file, using a Sha of its content -}
stagePointerFile :: FilePath -> Maybe FileMode -> Sha -> Annex ()

View file

@ -1,6 +1,6 @@
{- git hash-object interface
-
- Copyright 2011-2014 Joey Hess <id@joeyh.name>
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
@ -17,6 +17,9 @@ import Git.Types
import qualified Utility.CoProcess as CoProcess
import Utility.Tmp
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
type HashObjectHandle = CoProcess.CoProcessHandle
hashObjectStart :: Repo -> IO HashObjectHandle
@ -37,14 +40,20 @@ hashFile h file = CoProcess.query h send receive
send to = hPutStrLn to =<< absPath file
receive from = getSha "hash-object" $ hGetLine from
class HashableBlob t where
hashableBlobToHandle :: Handle -> t -> IO ()
instance HashableBlob L.ByteString where
hashableBlobToHandle = L.hPut
instance HashableBlob S.ByteString where
hashableBlobToHandle = S.hPut
{- Injects a blob into git. Unfortunately, the current git-hash-object
- interface does not allow batch hashing without using temp files. -}
hashBlob :: HashObjectHandle -> String -> IO Sha
hashBlob h s = withTmpFile "hash" $ \tmp tmph -> do
#ifdef mingw32_HOST_OS
hSetNewlineMode tmph noNewlineTranslation
#endif
hPutStr tmph s
hashBlob :: HashableBlob b => HashObjectHandle -> b -> IO Sha
hashBlob h b = withTmpFile "hash" $ \tmp tmph -> do
hashableBlobToHandle tmph b
hClose tmph
hashFile h tmp

View file

@ -85,7 +85,7 @@ mergeFile info file hashhandle h = case filter (/= nullSha) [Ref asha, Ref bsha]
[] -> return Nothing
(sha:[]) -> use sha
shas -> use
=<< either return (\s -> hashBlob hashhandle (unlines s))
=<< either return (hashBlob hashhandle . encodeBS . unlines)
=<< calcMerge . zip shas <$> mapM getcontents shas
where
[_colonmode, _bmode, asha, bsha, _status] = words info