aboutsummaryrefslogtreecommitdiffstats
path: root/app/Git.hs
blob: 57fffdc611a09367dc19935421e652008f2e38f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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