diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-10-16 14:20:23 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-10-16 14:21:23 +0200 |
commit | 82737c98e809bb3e970aa16750aa70f1adcfa03a (patch) | |
tree | f0b6d5f2352ff6b62f5bc5113b989c3c9b479055 /app/Git.hs | |
parent | b0112000b3f2966489bbf5ac1f2e15815f463d06 (diff) |
refactor `Git.withWorkingTree`
Diffstat (limited to 'app/Git.hs')
-rw-r--r-- | app/Git.hs | 15 |
1 files changed, 15 insertions, 0 deletions
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 |