aboutsummaryrefslogtreecommitdiffstats
path: root/app/Git/CommitHash.hs
blob: db7a4780132b310491975e66404ac6e8752fd821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Git.CommitHash
  ( CommitHash (..),
    toShortText,
    toText,
  )
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

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 "<dirty>" (toText commitHash))

instance P.Render (P.Summarized CommitHash) where
  render (P.Summarized commitHash) =
    P.styled [P.color P.Yellow] $
      P.render (fromMaybe "<dirty>" (toShortText commitHash))