diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-02-29 04:11:10 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-14 07:07:39 +0100 |
commit | f92c593a3d2c4bdb023fdd834b6e8c874d063cc8 (patch) | |
tree | b506bb61f4951207aa1aff04080fd3d7874927c3 /app/Git.hs | |
parent | 941f0d4ccb688d42c0438e05051ed78a410431b6 (diff) |
feat: add `review` command
Prototype of the `review` command, cf. `anissue review -h`. Also adds
the `status` command.
Diffstat (limited to 'app/Git.hs')
-rw-r--r-- | app/Git.hs | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -8,6 +8,9 @@ module Git getCommitOf, readTextFileOfText, readTextFileOfBS, + resolveRef, + getCommitsBetween, + diffOf, ) where @@ -27,6 +30,7 @@ import Data.Time.Clock (UTCTime, getCurrentTime) import Exception qualified as E import GHC.Generics (Generic) import Git.CommitHash +import Patch qualified as A import Process (proc, sh) import Text.Printf (printf) @@ -91,7 +95,7 @@ readTextFileOfText :: CommitHash -> FilePath -> IO LT.Text readTextFileOfText = readTextFileOf LT.readFile LT.decodeUtf8 readTextFileOfBS :: CommitHash -> FilePath -> IO LB.ByteString -readTextFileOfBS = readTextFileOf LB.readFile (\x->x) +readTextFileOfBS = readTextFileOf LB.readFile (\x -> x) readTextFileOf :: (FilePath -> IO a) -> (LB.ByteString -> a) -> CommitHash -> FilePath -> IO a readTextFileOf readFile _ WorkingTree filePath = @@ -102,3 +106,20 @@ readTextFileOf _ decode (Commit hash) filePath = catch (decode <$> sh (proc "git show %:%" hash filePath)) (\(_ :: E.ProcessException) -> throwIO (E.CannotReadFile (printf "%s:%s" hash filePath))) + +resolveRef :: T.Text -> IO CommitHash +resolveRef = + fmap (Commit . T.strip . T.decodeUtf8 . LB.toStrict) + . sh + . proc "git rev-parse %" + +-- TODO Throw if `prevHash` is not an ancestor of `hash`. +getCommitsBetween :: CommitHash -> CommitHash -> IO [CommitHash] +getCommitsBetween (Commit prevHash) (Commit hash) = do + map (Commit . T.strip) . T.lines . T.decodeUtf8 . LB.toStrict + <$> sh (proc "git log --format=%%H %..%" prevHash hash) + +diffOf :: CommitHash -> CommitHash -> IO A.Patch +diffOf prevHash hash = + A.parse . T.decodeUtf8 . LB.toStrict + <$> sh (proc "git diff % %" (toTextUnsafe prevHash) (toTextUnsafe hash)) |