From af6e902f1a3e93640df397ca39bdc25938b983ab Mon Sep 17 00:00:00 2001 From: Alex Brandt Date: Mon, 2 Sep 2024 07:58:02 +0000 Subject: [PATCH] Remove Git.root and use gitlib. --- .devcontainer/devcontainer.json | 2 +- lib/initialise/Cabal.hs | 2 +- lib/initialise/Git.hs | 8 -------- templatise.cabal | 4 +++- test/initialise/Hooks.hs | 11 +++++++++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 6ce4410..218758e 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -14,7 +14,7 @@ // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], // Use 'postCreateCommand' to run commands after the container is created. - // "postCreateCommand": "", + "postCreateCommand": "sudo apt-get update && sudo apt-get install -y libicu-dev", // Configure tool-specific properties. "customizations": { "vscode": { diff --git a/lib/initialise/Cabal.hs b/lib/initialise/Cabal.hs index ee527b5..5c6404e 100644 --- a/lib/initialise/Cabal.hs +++ b/lib/initialise/Cabal.hs @@ -36,7 +36,7 @@ import System.Directory.Extra (createDirectoryIfMissing, removeDirectoryRecursiv import System.FilePath (replaceBaseName, ()) import Prelude hiding (concat, head, readFile, unlines, writeFile) -#if __GLASGOW_HASKELL__ < 908 +#if __GLASGOW_HASKELL__ < 904 import Text.Parsec.Error (ParseError) instance Exception ParseError #endif diff --git a/lib/initialise/Git.hs b/lib/initialise/Git.hs index 0f51249..3bf49d8 100644 --- a/lib/initialise/Git.hs +++ b/lib/initialise/Git.hs @@ -1,6 +1,5 @@ module Git ( config, - root, ) where @@ -14,10 +13,3 @@ config key = do case output of Just (x :| _) -> pure $ pack x Nothing -> fail $ "`git config " <> unpack key <> "` returned no output" - -root :: IO Text -root = do - output <- nonEmpty . lines <$> readProcess "git" ["rev-parse", "--show-toplevel"] "" - case output of - Just (x :| _) -> pure $ pack x - Nothing -> fail "`git rev-parse --show-toplevel` returned no output" diff --git a/templatise.cabal b/templatise.cabal index 63c1a7b..2242e84 100644 --- a/templatise.cabal +++ b/templatise.cabal @@ -78,7 +78,7 @@ library initialise-library , http-conduit ^>=2.3.8.3 , regex-tdfa ^>=1.3.2.2 - if impl(ghc <9.8) + if impl(ghc <9.4) build-depends: parsec ^>=3.1.15.0 hs-source-dirs: lib/initialise @@ -96,6 +96,7 @@ test-suite initialise-test main-is: Main.hs build-depends: , directory ^>=1.3.6.2 + , gitlib-libgit2 ^>=3.1.2.1 , hspec ^>=2.11.4 , initialise-library , tasty ^>=1.4.3 || ^>=1.5 @@ -103,6 +104,7 @@ test-suite initialise-test , tasty-hspec ^>=1.2.0.4 , temporary ^>=1.3 + --, gitlib ^>=3.1.3 other-modules: CabalGolden DefaultsSpec diff --git a/test/initialise/Hooks.hs b/test/initialise/Hooks.hs index e449b3d..ecc34af 100644 --- a/test/initialise/Hooks.hs +++ b/test/initialise/Hooks.hs @@ -3,7 +3,8 @@ module Hooks (withGitRepo, withProjectCopy) where import Control.Applicative ((<|>)) import Control.Monad (void) import Data.Text (unpack) -import Git (root) +import Git (repositoryPath) +import Git.Libgit2 (defaultRepositoryOptions, openRepository) import System.Directory (getCurrentDirectory, withCurrentDirectory) import System.FilePath (takeFileName, ()) import System.IO.Temp (withSystemTempDirectory) @@ -22,7 +23,13 @@ withGitRepo action = withProjectCopy :: (FilePath -> IO ()) -> IO () withProjectCopy action = do withSystemTempDirectory "initialise" $ \p -> do - r <- unpack <$> root <|> getCurrentDirectory + r <- root void $ readProcess "cp" ["-a", r, p] "" let p' = p takeFileName r withCurrentDirectory p' $ action p' + +root :: IO String +root = do + cwd <- getCurrentDirectory + repo <- openRepository defaultRepositoryOptions cwd + either cwd (liftIO . repositoryPath) repo