From 82737c98e809bb3e970aa16750aa70f1adcfa03a Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 16 Oct 2023 14:20:23 +0200 Subject: refactor `Git.withWorkingTree` --- app/Git.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 app/Git.hs (limited to 'app/Git.hs') diff --git a/app/Git.hs b/app/Git.hs new file mode 100644 index 0000000..57fffdc --- /dev/null +++ b/app/Git.hs @@ -0,0 +1,15 @@ +module Git (withWorkingTree) where + +import Control.Exception (finally) +import Data.Text (Text) +import Process (proc, sh_) +import System.Directory (createDirectoryIfMissing) +import System.FilePath (dropTrailingPathSeparator, takeDirectory) + +-- | Runs an IO-action within a working tree. +withWorkingTree :: FilePath -> Text -> IO a -> IO a +withWorkingTree path hash action = do + createDirectoryIfMissing True (takeDirectory (dropTrailingPathSeparator path)) + sh_ $ proc "git worktree add --quiet --detach % %" path hash + action `finally` do + sh_ $ proc "git worktree remove --force %" path -- cgit v1.2.3