I am trying to create a commit using gitlib, but it does not quite work as expected. What I'm stuck at is the application of function writeBlob. Here is my source code:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PartialTypeSignatures #-}
module GitOperation where
import Git
import Git.Libgit2 (lgFactory, LgRepo(..) )
import qualified Data.Text as T
import Control.Monad.Trans.Reader
import Control.Monad.Trans
import Data.ByteString.Char8 as BS
import Data.Foldable
import Data.Time.LocalTime
import Data.Time.Clock
import Data.Time.Calendar
import Control.Monad.State
import Conduit
repositoryOptions :: RepositoryOptions
repositoryOptions = RepositoryOptions { repoPath = "data" , repoWorkingDir = Just "data", repoIsBare = False , repoAutoCreate = True}
main :: IO ()
main = do
repo <- openRepository lgFactory repositoryOptions
runReaderT (addFileToRepository "content4.txt" "content4" "added content4 file") repo
addFileToRepository :: TreeFilePath -> String -> CommitMessage -> ReaderT LgRepo IO ()
addFileToRepository path content commitMessage = do
now <- lift getCurrentTime
timezone <- lift getCurrentTimeZone
treeBuilder <- newTreeBuilder Nothing
repo <- ask
blobOid <- createBlobUtf8 $ T.pack content
writeBlob (BS.unpack path) (BlobString $ BS.pack content)
transf <- lift $ runReaderT (runStateT (runTreeT currentTreeOid) treeBuilder) repo
let (year, month, day) = toGregorian $ utctDay now
timeOfDay = localTimeOfDay $ utcToLocalTime timezone now
s = Signature {
signatureName = "username",
signatureEmail = "me@website.com",
signatureWhen = ZonedTime {
zonedTimeToLocalTime = LocalTime {
localDay = fromGregorian year month day,
localTimeOfDay = timeOfDay
},
zonedTimeZone = TimeZone {
timeZoneMinutes = 180,
timeZoneSummerOnly = False,
timeZoneName = "EEST"
}
}
} in do
lift $ print s
ci <- case transf of
(oid, builder) -> do
newOid <- createTree $ putEntry path BlobEntry {blobEntryKind = PlainBlob, blobEntryOid = blobOid}
createCommit [] newOid s s commitMessage Nothing
lift $ print $ commitLog ci
The error message I am getting is:
Main.hs:125:3: error:
• No instance for (MonadResource IO)
arising from a use of ‘writeBlob’
• In a stmt of a 'do' block:
writeBlob (unpack path) (BlobString $ pack content)
In the expression:
do now <- lift getCurrentTime
timezone <- lift getCurrentTimeZone
treeBuilder <- newTreeBuilder Nothing
repo <- ask
....
In an equation for ‘addFileToRepository’:
addFileToRepository path content commitMessage
= do now <- lift getCurrentTime
timezone <- lift getCurrentTimeZone
treeBuilder <- newTreeBuilder Nothing
....
|
125 | writeBlob (BS.unpack path) (BlobString $ BS.pack content)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
I cannot quite understand how to make writeBlob the instance of MonadResource IO. Documentation for gitlib is quite obscure.
Without writeBlob file gets stuck with delete mark in staging area and nothing is really get committed even though git log says it is there:
Projects/test/data on master [?]
❯ git status
D content4.txt
Projects/test/data on master [?]
❯ git log
commit 90b3f8ac01ae590e5e7726046a59a5d9944ab88c (HEAD -> master)
Author: username <me@website.com>
Date: Mon May 17 15:26:38 2021 +0300
added content4 file
If somebody could help with resolving this issue and hopefully make commit created as expected, I would really appreciate it. Thanks in advance.