diff options
Diffstat (limited to 'app/Git.hs')
-rw-r--r-- | app/Git.hs | 41 |
1 files changed, 33 insertions, 8 deletions
@@ -2,6 +2,7 @@ module Git ( module Git.CommitHash, getCommitHashes, getRootDir, + getFilesOf, getChangedFilesOf, Commit (..), Author (..), @@ -14,14 +15,13 @@ module Git ) where -import Control.Exception (IOException, catch, throw, throwIO) +import Control.Exception (IOException, catch, throwIO) import Data.Binary (Binary) import Data.Binary.Instances () import Data.ByteString.Lazy qualified as LB -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 Data.Text.Lazy qualified as LT import Data.Text.Lazy.Encoding qualified as LT import Data.Text.Lazy.IO qualified as LT @@ -33,8 +33,25 @@ import Patch qualified as A import Process (proc, sh, sh_) import Text.Printf (printf) -getCommitHashes :: IO (NonEmpty T.Text) -getCommitHashes = fromMaybe (throw E.NoCommits) . N.nonEmpty . reverse . T.lines <$> sh "git log --format=%H" +getCommitHashes :: Maybe CommitHash -> Maybe CommitHash -> IO [CommitHash] +getCommitHashes maybeBottomCommit Nothing = + getCommitHashes maybeBottomCommit (Just WorkingTree) +getCommitHashes (Just WorkingTree) (Just WorkingTree) = + pure [WorkingTree] +getCommitHashes (Just WorkingTree) (Just (Commit _)) = + pure [] +getCommitHashes Nothing (Just WorkingTree) = + (WorkingTree :) . map Commit . T.lines + <$> sh (proc "git log --format=%%H HEAD") +getCommitHashes (Just (Commit bottomHash)) (Just WorkingTree) = + (WorkingTree :) . map Commit . T.lines + <$> sh (proc "git log --format=%%H %..HEAD" bottomHash) +getCommitHashes Nothing (Just (Commit topHash)) = + map Commit . T.lines + <$> sh (proc "git log --format=%%H %" topHash) +getCommitHashes (Just (Commit bottomHash)) (Just (Commit topHash)) = + map Commit . T.lines + <$> sh (proc "git log --format=%%H %..%" bottomHash topHash) getRootDir :: IO FilePath getRootDir = @@ -43,13 +60,21 @@ getRootDir = where stripTrailingNL s = fromMaybe s $ T.stripSuffix "\n" s +getFilesOf :: CommitHash -> IO [FilePath] +getFilesOf WorkingTree = + map T.unpack . T.lines + <$> sh "git ls-files --cached --modified --others --exclude-standard --full-name" +getFilesOf (Commit hash) = + map T.unpack . T.lines + <$> sh (proc "git ls-tree -r --name-only --full-name --full-tree %" hash) + getChangedFilesOf :: CommitHash -> IO [FilePath] getChangedFilesOf WorkingTree = map T.unpack . T.lines - <$> sh "git ls-files --modified" -getChangedFilesOf (Commit hash) = + <$> sh "git ls-files --modified --others --exclude-standard --full-name" +getChangedFilesOf (Commit hash) = do map T.unpack . T.lines - <$> sh (proc "git show -p --name-only --format= %" hash) + <$> sh (proc "git diff-tree -r --name-only %" hash) data Commit = Commit' { commitHash :: CommitHash, |