From eafbd88429a80f058efaa4efd28fbfb8271065c3 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 20 Oct 2023 09:55:13 +0200 Subject: record both creation and update in provenance --- app/Issue/Provenance.hs | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'app/Issue/Provenance.hs') diff --git a/app/Issue/Provenance.hs b/app/Issue/Provenance.hs index f3d382c..4d69472 100644 --- a/app/Issue/Provenance.hs +++ b/app/Issue/Provenance.hs @@ -2,28 +2,45 @@ module Issue.Provenance ( Provenance (..), - provenanceFromHEAD, + Commit (..), + Author (..), + commitFromHEAD, ) where +import Control.Exception (throwIO) import Data.Binary (Binary, Put, get, put) import Data.ByteString.Lazy.Char8 (toStrict) import Data.Fixed (Pico) import Data.Function ((&)) -import Data.Text (Text, lines, splitOn, unpack) +import Data.Text (lines, splitOn, unpack) +import Data.Text qualified as T import Data.Text.Encoding (decodeUtf8) import Data.Time.Calendar (Day (..), toModifiedJulianDay) import Data.Time.Clock (DiffTime, UTCTime (..), picosecondsToDiffTime) +import Exception qualified as E import GHC.Generics (Generic) import Process (sh) import System.Process.Typed (setWorkingDir) import Prelude hiding (lines) data Provenance = Provenance - { firstCommit :: Text, + { first :: Commit, + last :: Commit + } + deriving (Show, Generic, Binary, Eq) + +data Commit = Commit + { -- TODO `T.Text` -> `CommitHash` + hash :: T.Text, date :: UTCTime, - authorEmail :: Text, - authorName :: Text + author :: Author + } + deriving (Show, Generic, Binary, Eq) + +data Author = Author + { name :: T.Text, + email :: T.Text } deriving (Show, Generic, Binary, Eq) @@ -43,22 +60,20 @@ instance Binary DiffTime where get = fmap picosecondsToDiffTime get put = (put :: Pico -> Put) . realToFrac -provenanceFromHEAD :: FilePath -> IO (Maybe Provenance) -provenanceFromHEAD cwd = do +commitFromHEAD :: FilePath -> IO Commit +commitFromHEAD cwd = do rawProvenance <- fmap (splitOn "\NUL" . head . lines . decodeUtf8 . toStrict) $ sh $ "git show --quiet --format='%H%x00%ai%x00%ae%x00%an'" & setWorkingDir cwd - pure $ case rawProvenance of - firstCommit' : rawDate : authorEmail : authorName : _ -> + case rawProvenance of + hash : rawDate : authorEmail : authorName : _ -> let date = read (unpack rawDate) - in Just - Provenance - { firstCommit = firstCommit', + in pure + Commit + { hash = hash, date = date, - authorEmail = authorEmail, - authorName = authorName + author = Author authorName authorEmail } - _ -> - Nothing + _ -> throwIO E.NoCommits -- cgit v1.2.3