module Git.CommitHash ( CommitHash (..), toShortText, toText, toTextUnsafe, ) where import Data.Binary (Binary) import Data.Maybe (fromMaybe) import Data.Text qualified as T import GHC.Generics (Generic) import Render qualified as P data CommitHash = WorkingTree | Commit T.Text deriving (Eq, Show, Binary, Generic) toShortText :: CommitHash -> Maybe T.Text toShortText = fmap (T.take 7) . toText toText :: CommitHash -> Maybe T.Text toText WorkingTree = Nothing toText (Commit hash) = Just hash toTextUnsafe :: CommitHash -> T.Text toTextUnsafe (Commit hash) = hash toTextUnsafe _ = error "toTextUnsafe: WorkingDir" instance P.Render CommitHash where render = P.render . P.Detailed instance P.Render (P.Detailed CommitHash) where render (P.Detailed commitHash) = P.styled [P.color P.Yellow] $ P.render (fromMaybe "" (toText commitHash)) instance P.Render (P.Summarized CommitHash) where render (P.Summarized commitHash) = P.styled [P.color P.Yellow] $ P.render (fromMaybe "" (toShortText commitHash))