summaryrefslogtreecommitdiff
path: root/Annex/HashObject.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-03-14 15:54:46 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-03-14 15:54:46 -0400
commit9b29bd39c8dbf23bdf6930b51aba13992ccc49de (patch)
tree4cca35e79b28f6a27d7b688eb9d61d7e0c30aee1 /Annex/HashObject.hs
parent0e2dab692046d242ac68ebc8359493ca76ef51d1 (diff)
followup
Diffstat (limited to 'Annex/HashObject.hs')
-rw-r--r--Annex/HashObject.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/Annex/HashObject.hs b/Annex/HashObject.hs
new file mode 100644
index 000000000..aa8c2a174
--- /dev/null
+++ b/Annex/HashObject.hs
@@ -0,0 +1,54 @@
+{- git hash-object interface, with handle automatically stored in the Annex monad
+ -
+ - Copyright 2016 Joey Hess <id@joeyh.name>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Annex.HashObject (
+ hashFile,
+ hashBlob,
+ hashObjectHandle,
+ hashObjectStop,
+) where
+
+import qualified Data.ByteString.Lazy as L
+import qualified Data.Map as M
+import System.PosixCompat.Types
+
+import Annex.Common
+import qualified Git
+import qualified Git.HashObject
+import qualified Annex
+import Git.Types
+import Git.FilePath
+import qualified Git.Ref
+import Annex.Link
+
+hashObjectHandle :: Annex Git.HashObject.HashObjectHandle
+hashObjectHandle = maybe startup return =<< Annex.getState Annex.hashobjecthandle
+ where
+ startup = do
+ inRepo $ Git.hashObjectStart
+ Annex.changeState $ \s -> s { Annex.hashobjecthandle = Just h }
+ return h
+
+hashObjectStop :: Annex ()
+hashObjectStop = maybe noop stop =<< Annex.hashobjecthandle
+ where
+ stop h = do
+ liftIO $ Git.hashObjectStop h
+ Annex.changeState $ \s -> s { Annex.hashobjecthandle = Nothing }
+
+hashFile :: FilePath -> Annex Sha
+hashFile f = do
+ h <- hashObjectHandle
+ Git.HashObject.hashFile h f
+
+{- 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 content = do
+ h <- hashObjectHandle
+ Git.HashObject.hashFile h content