module Git ( withWorkingTree, getCommitHashes, ) where import Control.Exception (finally) import Data.ByteString.Lazy.Char8 qualified as LB8 import Data.Text qualified as T import Data.Text.Encoding qualified as T import Process (proc, sh, sh_) import System.Directory (createDirectoryIfMissing) import System.FilePath (dropTrailingPathSeparator, takeDirectory) -- | Runs an IO-action within a working tree. withWorkingTree :: FilePath -> T.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 getCommitHashes :: IO [T.Text] getCommitHashes = T.lines . T.decodeUtf8 . LB8.toStrict <$> sh "git log --format=%H"