From 561ef3d43d94bdf098fee37295d7588905ec0c9e Mon Sep 17 00:00:00 2001 From: Alexander Foremny <aforemny@posteo.de> Date: Tue, 7 Nov 2023 22:38:03 +0100 Subject: improve getCommitHashes `getCommitHashes` now returns at least one commit, and reverses commits by default (ie. oldest to newset). --- app/Git.hs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'app/Git.hs') diff --git a/app/Git.hs b/app/Git.hs index af02b70..653ddbd 100644 --- a/app/Git.hs +++ b/app/Git.hs @@ -5,11 +5,14 @@ module Git ) where -import Control.Exception (finally) +import Control.Exception (finally, throw) import Data.ByteString.Lazy.Char8 qualified as LB8 +import Data.List.NonEmpty (NonEmpty) +import Data.List.NonEmpty qualified as N import Data.Maybe (fromMaybe) import Data.Text qualified as T import Data.Text.Encoding qualified as T +import Exception qualified as E import Process (proc, sh, sh_) import System.Directory (createDirectoryIfMissing) import System.FilePath (dropTrailingPathSeparator, takeDirectory) @@ -22,8 +25,8 @@ withWorkingTree path hash action = do 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" +getCommitHashes :: IO (NonEmpty T.Text) +getCommitHashes = fromMaybe (throw E.NoCommits) . N.nonEmpty . reverse . T.lines . T.decodeUtf8 . LB8.toStrict <$> sh "git log --format=%H" getRootDir :: IO FilePath getRootDir = T.unpack . stripTrailingNL . T.decodeUtf8 . LB8.toStrict <$> sh (proc "git rev-parse --show-toplevel") -- cgit v1.2.3