2022-07-25 21:32:39 +00:00
|
|
|
{- git hash-object interface
|
2016-03-14 19:54:46 +00:00
|
|
|
-
|
2022-07-25 21:32:39 +00:00
|
|
|
- Copyright 2016-2022 Joey Hess <id@joeyh.name>
|
2016-03-14 19:54:46 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2016-03-14 19:54:46 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.HashObject (
|
|
|
|
hashFile,
|
|
|
|
hashBlob,
|
|
|
|
hashObjectStop,
|
2022-07-25 21:32:39 +00:00
|
|
|
mkConcurrentHashObjectHandle,
|
|
|
|
withHashObjectHandle,
|
2016-03-14 19:54:46 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Git.HashObject
|
|
|
|
import qualified Annex
|
|
|
|
import Git.Types
|
2022-07-25 21:32:39 +00:00
|
|
|
import Utility.ResourcePool
|
|
|
|
import Types.Concurrency
|
|
|
|
import Annex.Concurrent.Utility
|
2016-03-14 19:54:46 +00:00
|
|
|
|
|
|
|
hashObjectStop :: Annex ()
|
2016-03-14 19:58:46 +00:00
|
|
|
hashObjectStop = maybe noop stop =<< Annex.getState Annex.hashobjecthandle
|
2016-03-14 19:54:46 +00:00
|
|
|
where
|
2022-07-25 21:32:39 +00:00
|
|
|
stop p = do
|
|
|
|
liftIO $ freeResourcePool p Git.HashObject.hashObjectStop
|
2016-03-14 19:54:46 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.hashobjecthandle = Nothing }
|
|
|
|
|
2020-10-28 21:25:59 +00:00
|
|
|
hashFile :: RawFilePath -> Annex Sha
|
2022-07-25 21:32:39 +00:00
|
|
|
hashFile f = withHashObjectHandle $ \h ->
|
2016-03-14 19:58:46 +00:00
|
|
|
liftIO $ Git.HashObject.hashFile h f
|
2016-03-14 19:54:46 +00:00
|
|
|
|
|
|
|
{- 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. -}
|
2019-01-03 17:19:59 +00:00
|
|
|
hashBlob :: Git.HashObject.HashableBlob b => b -> Annex Sha
|
2022-07-25 21:32:39 +00:00
|
|
|
hashBlob content = withHashObjectHandle $ \h ->
|
2016-03-14 19:58:46 +00:00
|
|
|
liftIO $ Git.HashObject.hashBlob h content
|
2022-07-25 21:32:39 +00:00
|
|
|
|
|
|
|
withHashObjectHandle :: (Git.HashObject.HashObjectHandle -> Annex a) -> Annex a
|
|
|
|
withHashObjectHandle a =
|
|
|
|
maybe mkpool go =<< Annex.getState Annex.hashobjecthandle
|
|
|
|
where
|
|
|
|
go p = withResourcePool p start a
|
|
|
|
start = inRepo $ Git.HashObject.hashObjectStart True
|
|
|
|
mkpool = do
|
|
|
|
-- This only runs in non-concurrent code paths;
|
|
|
|
-- a concurrent pool is set up earlier when needed.
|
|
|
|
p <- mkResourcePoolNonConcurrent start
|
|
|
|
Annex.changeState $ \s -> s { Annex.hashobjecthandle = Just p }
|
|
|
|
go p
|
|
|
|
|
|
|
|
mkConcurrentHashObjectHandle :: Concurrency -> Annex (ResourcePool Git.HashObject.HashObjectHandle)
|
|
|
|
mkConcurrentHashObjectHandle c =
|
|
|
|
Annex.getState Annex.hashobjecthandle >>= \case
|
|
|
|
Just p@(ResourcePool {}) -> return p
|
|
|
|
_ -> mkResourcePool =<< liftIO (maxHashObjects c)
|
|
|
|
|
|
|
|
{- git hash-object is typically CPU bound, and is not likely to be the main
|
|
|
|
- bottleneck for any command. So limit to the number of CPU cores, maximum,
|
|
|
|
- while respecting the -Jn value.
|
|
|
|
-}
|
|
|
|
maxHashObjects :: Concurrency -> IO Int
|
|
|
|
maxHashObjects = concurrencyUpToCpus
|