packages feed

project-forge-0.1.0.0: src/ProjectForge/Get/Git.hs

{-|
Functions needed for getting templates from remote git repositories
-}

module ProjectForge.Get.Git (
  GitCloneArgs(..)
  , GitURL
  , Branch
  , gitClone
) where

import           System.Process.Typed (ProcessConfig, proc)


-- | A git branch as a @String@.
type Branch = String

-- | A git url as a @String@.
type GitURL = String

{-|
A limited set of arguments for @git clone@ that correspond
to the following command:

@
git clone \
   --branch <OptionalBranch>\
   --depth <OptionalDepth> \
   <repository> \
   <directory>
@

-}
data GitCloneArgs = MkGitCloneArgs {
  -- | [git clone repository](https://www.git-scm.com/docs/git-clone#Documentation/git-clone.txt-ltrepositorygt)
    repository :: !GitURL
  -- | [git clone directory](https://www.git-scm.com/docs/git-clone#Documentation/git-clone.txt-ltdirectorygt)
  , directory  :: !FilePath
  -- | [git clone branch](https://www.git-scm.com/docs/git-clone#Documentation/git-clone.txt---branchltnamegt)
  , branch     :: !(Maybe Branch)
  -- | [git clone depth](https://www.git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt)
  , depth      :: !(Maybe Integer)
  } deriving (Eq, Show)

-- Convert @GitCloneArgs@ to a list for @'System.Process.Typed.proc'@.
cloneArgsToList :: GitCloneArgs -> [String]
cloneArgsToList args =
     maybe [] (\x -> ["--branch", x]) (branch args)
  <> maybe [] (\x-> ["--depth", show x]) (depth args)
  <> [ repository args, directory args ]

{-|
A @'System.Process.Typed.ProcessConfig'@ for:

@
git clone <<args>>
@

NOTE:
According to the
[`typed-process` documentaiton](https://github.com/fpco/typed-process#readme),
"it's highly recommended that you compile any program
using this library with the multi-threaded runtime,
usually by adding ghc-options: -threaded to your executable stanza
in your cabal or package.yaml file.
The single-threaded runtime necessitates some inefficient polling
to be used under the surface."

-}
gitClone :: GitCloneArgs -> ProcessConfig () () ()
gitClone = proc "git" . (["clone"] <>) . cloneArgsToList