From 1e75f8998e8ba9c88ce6dbf7e809e30c233eb611 Mon Sep 17 00:00:00 2001
From: Alexander Foremny <aforemny@posteo.de>
Date: Thu, 20 Feb 2025 15:02:04 +0100
Subject: `withStore` creates repository/ initial commit

---
 src/Store/Store.hs | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

(limited to 'src/Store/Store.hs')

diff --git a/src/Store/Store.hs b/src/Store/Store.hs
index 61be89b..94382af 100644
--- a/src/Store/Store.hs
+++ b/src/Store/Store.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE LambdaCase #-}
+
 module Store.Store
   ( StoreM,
     withStore,
@@ -71,11 +73,25 @@ type StoreM = StoreT IO
 
 withStore :: FilePath -> G.RefName -> StoreM a -> IO a
 withStore repoPath ref action = do
-  repo <- G.openRepository GB.lgFactory G.defaultRepositoryOptions {G.repoPath}
+  repo <-
+    G.openRepository
+      GB.lgFactory
+      G.defaultRepositoryOptions
+        { G.repoPath,
+          G.repoAutoCreate = True
+        }
+
   (cid, tid) <- G.runRepository GB.lgFactory repo do
-    Just cid <- fmap Tagged <$> G.resolveReference ref
-    tid <- (.commitTree) <$> G.lookupCommit cid
-    pure (cid, tid)
+    fmap Tagged <$> G.resolveReference ref >>= \case
+      Nothing -> do
+        now <- lift (utcToZonedTime <$> getCurrentTimeZone <*> getCurrentTime)
+        let sig = G.Signature "author" "email" now
+        tid <- G.createTree (pure ())
+        cid <- G.commitOid <$> G.createCommit [] tid sig sig "auto-init" (Just ref)
+        pure (cid, tid)
+      Just cid -> do
+        tid <- (.commitTree) <$> G.lookupCommit cid
+        pure (cid, tid)
 
   runReaderT
     (evalStateT (runStoreT action) (State {cid, tid}))
-- 
cgit v1.2.3