{-# LANGUAGE QuasiQuotes #-}

-- | "Development.GitRev.Internal.Git" for 'OsString'.
--
-- @since 0.1
module Development.GitRev.Internal.Git.OsString
  ( -- * Built-in
    GitError (..),
    gitBranchQ,
    gitCommitCountQ,
    gitCommitDateQ,
    gitDescribeQ,
    gitDiffQ,
    gitDirtyQ,
    gitDirtyTrackedQ,
    gitHashQ,
    gitShortHashQ,
    gitTreeQ,

    -- * Git primitives
    runGitQ,
    runGitPostProcessQ,
    IndexUsed (..),
  )
where

import Control.Exception (Exception (displayException))
import Data.Bifunctor (Bifunctor (first))
import Development.GitRev.Internal.Git.Common
  ( GitProcessArgs
      ( MkGitProcessArgs,
        fromStringTotal,
        gitRootArgs,
        runProcessGit,
        toOsPath,
        toStringTotal
      ),
    IndexUsed (IdxNotUsed, IdxUsed),
  )
import Development.GitRev.Internal.Git.Common qualified as Common
import Development.GitRev.Internal.OsString qualified as OsStringI
import Language.Haskell.TH (Q)
import Language.Haskell.TH.Syntax (Lift)
import System.OsString (OsString, osstr)
import System.Process qualified as Process

-- $setup
-- >>> :set -XQuasiQuotes
-- >>> import Development.GitRev.Typed.OsString (qToCode)

-- | Returns the current git branch.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitBranchQ)
-- Right ...
--
-- @since 0.1
gitBranchQ :: Q (Either GitError OsString)
gitBranchQ :: Q (Either GitError OsString)
gitBranchQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|rev-parse|], [osstr|--abbrev-ref|], [osstr|HEAD|]] IndexUsed
IdxNotUsed

-- | Returns the git commit count.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitCommitCountQ)
-- Right ...
--
-- @since 0.1
gitCommitCountQ :: Q (Either GitError OsString)
gitCommitCountQ :: Q (Either GitError OsString)
gitCommitCountQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|rev-list|], [osstr|HEAD|], [osstr|--count|]] IndexUsed
IdxNotUsed

-- | Returns the latest git commit date.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitCommitDateQ)
-- Right ...
--
-- @since 0.1
gitCommitDateQ :: Q (Either GitError OsString)
gitCommitDateQ :: Q (Either GitError OsString)
gitCommitDateQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ
    [ [osstr|log|],
      [osstr|HEAD|],
      [osstr|-1|],
      [osstr|--format=%cd|]
    ]
    IndexUsed
IdxNotUsed

-- | Returns the git description.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitDescribeQ)
-- Right ...
--
-- @since 0.1
gitDescribeQ :: Q (Either GitError OsString)
gitDescribeQ :: Q (Either GitError OsString)
gitDescribeQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|describe|], [osstr|--long|], [osstr|--always|]] IndexUsed
IdxNotUsed

-- | Return the diff of the working copy with HEAD.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitDiffQ)
-- Right ...
--
-- @since 0.1
gitDiffQ :: Q (Either GitError OsString)
gitDiffQ :: Q (Either GitError OsString)
gitDiffQ = (OsString -> OsString)
-> [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitPostProcessQ OsString -> OsString
forall a. a -> a
id [[osstr|diff|], [osstr|HEAD|]] IndexUsed
IdxNotUsed

-- | Returns the git dirty status.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitDirtyQ)
-- Right ...
--
-- @since 0.1
gitDirtyQ :: Q (Either GitError Bool)
gitDirtyQ :: Q (Either GitError Bool)
gitDirtyQ =
  (OsString -> Bool)
-> Either GitError OsString -> Either GitError Bool
forall a b. (a -> b) -> Either GitError a -> Either GitError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap OsString -> Bool
forall a. (Eq a, Monoid a) => a -> Bool
Common.nonEmpty
    (Either GitError OsString -> Either GitError Bool)
-> Q (Either GitError OsString) -> Q (Either GitError Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|status|], [osstr|--porcelain|]] IndexUsed
IdxUsed

-- | Returns the git dirty status, ignoring untracked files.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitDirtyTrackedQ)
-- Right ...
--
-- @since 0.1
gitDirtyTrackedQ :: Q (Either GitError Bool)
gitDirtyTrackedQ :: Q (Either GitError Bool)
gitDirtyTrackedQ =
  (OsString -> Bool)
-> Either GitError OsString -> Either GitError Bool
forall a b. (a -> b) -> Either GitError a -> Either GitError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap OsString -> Bool
forall a. (Eq a, Monoid a) => a -> Bool
Common.nonEmpty
    (Either GitError OsString -> Either GitError Bool)
-> Q (Either GitError OsString) -> Q (Either GitError Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ
      [[osstr|status|], [osstr|--porcelain|], [osstr|--untracked-files=no|]]
      IndexUsed
IdxUsed

-- | Returns the latest git hash.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitHashQ)
-- Right ...
--
-- @since 0.1
gitHashQ :: Q (Either GitError OsString)
gitHashQ :: Q (Either GitError OsString)
gitHashQ = [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|rev-parse|], [osstr|HEAD|]] IndexUsed
IdxNotUsed

-- | Returns the latest git short hash.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitShortHashQ)
-- Right ...
--
-- @since 0.1
gitShortHashQ :: Q (Either GitError OsString)
gitShortHashQ :: Q (Either GitError OsString)
gitShortHashQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ [[osstr|rev-parse|], [osstr|--short|], [osstr|HEAD|]] IndexUsed
IdxNotUsed

-- | Returns the hash of the current tree.
--
-- ==== __Examples__
--
-- >>> $$(qToCode gitTreeQ)
-- Right ...
--
-- @since 0.1
gitTreeQ :: Q (Either GitError OsString)
gitTreeQ :: Q (Either GitError OsString)
gitTreeQ =
  [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ
    [ [osstr|show|],
      [osstr|HEAD|],
      [osstr|--format=%T|],
      [osstr|--no-patch|]
    ]
    IndexUsed
IdxNotUsed

-- | Errors that can be encountered with git.
--
-- @since 0.1
newtype GitError = MkGitError
  { -- | @since 0.1
    GitError -> OsString
reason :: OsString
  }
  deriving stock
    ( -- | @since 0.1
      GitError -> GitError -> Bool
(GitError -> GitError -> Bool)
-> (GitError -> GitError -> Bool) -> Eq GitError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GitError -> GitError -> Bool
== :: GitError -> GitError -> Bool
$c/= :: GitError -> GitError -> Bool
/= :: GitError -> GitError -> Bool
Eq,
      -- | @since 0.1
      (forall (m :: * -> *). Quote m => GitError -> m Exp)
-> (forall (m :: * -> *). Quote m => GitError -> Code m GitError)
-> Lift GitError
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => GitError -> m Exp
forall (m :: * -> *). Quote m => GitError -> Code m GitError
$clift :: forall (m :: * -> *). Quote m => GitError -> m Exp
lift :: forall (m :: * -> *). Quote m => GitError -> m Exp
$cliftTyped :: forall (m :: * -> *). Quote m => GitError -> Code m GitError
liftTyped :: forall (m :: * -> *). Quote m => GitError -> Code m GitError
Lift,
      -- | @since 0.1
      Int -> GitError -> ShowS
[GitError] -> ShowS
GitError -> String
(Int -> GitError -> ShowS)
-> (GitError -> String) -> ([GitError] -> ShowS) -> Show GitError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GitError -> ShowS
showsPrec :: Int -> GitError -> ShowS
$cshow :: GitError -> String
show :: GitError -> String
$cshowList :: [GitError] -> ShowS
showList :: [GitError] -> ShowS
Show
    )

-- | @since 0.1
instance Exception GitError where
  displayException :: GitError -> String
displayException (MkGitError OsString
s) = String
"Git error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ OsString -> String
OsStringI.decodeLenient OsString
s

-- | Runs git with the arguments. If 'IdxUsed' is passed, it is tracked for
-- recompliation purposes.
--
-- ==== __Examples__
--
-- >>> :{
--   -- Returns 'YYYY-MM-DD' rather than e.g. gitCommitDateQ's
--   -- 'Fri May 2 13:29:59 2025 +1200'.
--   gitCommitDateShortQ :: Q (Either GitError OsString)
--   gitCommitDateShortQ =
--     runGitQ
--       [[osstr|log|], [osstr|HEAD|], [osstr|-1|], [osstr|--format=%cs|]]
--       IdxNotUsed
-- :}
--
-- @since 0.1
runGitQ ::
  -- | Arguments to git.
  [OsString] ->
  -- | Whether the index is used.
  IndexUsed ->
  Q (Either GitError OsString)
runGitQ :: [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitQ = (OsString -> OsString)
-> [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitPostProcessQ OsString -> OsString
Common.tillNewLineOsPath

-- | Like 'runGitQ', except it applies the given function to the result.
-- Normal 'runGitQ' takes everything up until the first new line or carriage
-- return.
--
-- ==== __Examples__
--
-- >>> :{
--   runGitNoProcessQ :: [OsString] -> IndexUsed -> Q (Either GitError OsString)
--   runGitNoProcessQ = runGitPostProcessQ id
-- :}
--
-- @since 0.1
runGitPostProcessQ ::
  -- | Function to run on the result.
  (OsString -> OsString) ->
  -- | Arguments to git.
  [OsString] ->
  -- | Whether the index is used.
  IndexUsed ->
  Q (Either GitError OsString)
runGitPostProcessQ :: (OsString -> OsString)
-> [OsString] -> IndexUsed -> Q (Either GitError OsString)
runGitPostProcessQ OsString -> OsString
postProcess [OsString]
args IndexUsed
idxUsed =
  (GitError OsString -> GitError)
-> Either (GitError OsString) OsString -> Either GitError OsString
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first GitError OsString -> GitError
mapGitError
    (Either (GitError OsString) OsString -> Either GitError OsString)
-> Q (Either (GitError OsString) OsString)
-> Q (Either GitError OsString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GitProcessArgs OsString
-> (OsString -> OsString)
-> [OsString]
-> IndexUsed
-> Q (Either (GitError OsString) OsString)
forall str.
GitProcessArgs str
-> (str -> str)
-> [str]
-> IndexUsed
-> Q (Either (GitError str) str)
Common.runGitPostprocess
      GitProcessArgs OsString
gitProcessArgs
      OsString -> OsString
postProcess
      [OsString]
args
      IndexUsed
idxUsed

mapGitError :: Common.GitError OsString -> GitError
mapGitError :: GitError OsString -> GitError
mapGitError (Common.MkGitError OsString
s) = OsString -> GitError
MkGitError OsString
s

gitProcessArgs :: GitProcessArgs OsString
gitProcessArgs :: GitProcessArgs OsString
gitProcessArgs =
  MkGitProcessArgs
    { fromStringTotal :: String -> OsString
fromStringTotal = String -> OsString
OsStringI.encodeLenient,
      gitRootArgs :: [OsString]
gitRootArgs = [[osstr|rev-parse|], [osstr|--show-toplevel|]],
      -- TODO: Once process gets OsString support, replace
      -- readProcessWithExitCode below. Should make all of this encoding
      -- unnecessary.
      runProcessGit :: [OsString] -> IO (ExitCode, OsString, OsString)
runProcessGit = \[OsString]
args -> do
        args' <- (OsString -> IO String) -> [OsString] -> IO [String]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse OsString -> IO String
forall (m :: * -> *). MonadThrow m => OsString -> m String
OsStringI.decodeThrowM [OsString]
args
        (ec, out, err) <- Process.readProcessWithExitCode "git" args' ""
        (ec,,OsStringI.encodeLenient err) <$> OsStringI.encodeThrowM out,
      toOsPath :: OsString -> IO OsString
toOsPath = OsString -> IO OsString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure,
      toStringTotal :: OsString -> String
toStringTotal = OsString -> String
OsStringI.decodeLenient
    }