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