Skip to content

Commit

Permalink
Implement acquire repository command
Browse files Browse the repository at this point in the history
Port `git-elegant-acquire-repository` command to haskell.
`unordered-containers` provide HashMap.

#297
  • Loading branch information
teggotic committed Feb 7, 2023
1 parent 60c9f93 commit d48eb9c
Show file tree
Hide file tree
Showing 13 changed files with 924 additions and 158 deletions.
12 changes: 12 additions & 0 deletions elegant-git.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source-repository head

library
exposed-modules:
Elegit.Cli.Action.AcquireRepository
Elegit.Cli.Action.ShowWork
Elegit.Cli.Command
Elegit.Cli.Parser
Expand All @@ -43,6 +44,7 @@ library
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Werror=incomplete-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
base >=4.7 && <5
, containers
, dlist
, fmt
, free
Expand All @@ -57,6 +59,8 @@ library
, transformers
, typed-process
, universum
, unordered-containers
, utility-ht
default-language: Haskell2010

executable git-elegant
Expand All @@ -72,6 +76,7 @@ executable git-elegant
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Werror=incomplete-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, dlist
, elegant-git
, fmt
Expand All @@ -87,13 +92,17 @@ executable git-elegant
, transformers
, typed-process
, universum
, unordered-containers
, utility-ht
default-language: Haskell2010

test-suite elegant-git-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Elegit.Cli.Action.AcquireRepositorySpec
Elegit.Cli.Action.ShowWorkSpec
Elegit.Cli.Parser.AcquireRepositorySpec
Elegit.Cli.Parser.ShowWorkSpec
Elegit.Cli.Parser.Util
Elegit.Git.Runner.SimulatedSpec
Expand All @@ -107,6 +116,7 @@ test-suite elegant-git-test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Werror=incomplete-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, dlist
, elegant-git
, fmt
Expand All @@ -123,4 +133,6 @@ test-suite elegant-git-test
, transformers
, typed-process
, universum
, unordered-containers
, utility-ht
default-language: Haskell2010
3 changes: 3 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ dependencies:
- mtl
- free
- dlist
- containers
- unordered-containers
- microlens
- microlens-mtl
- microlens-th
- utility-ht

- optparse-applicative
- typed-process
Expand Down
130 changes: 130 additions & 0 deletions src/Elegit/Cli/Action/AcquireRepository.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{-# LANGUAGE QuasiQuotes #-}
module Elegit.Cli.Action.AcquireRepository
( cli
, cmd
) where

import Control.Monad.Free.Class
import Data.String.QQ
import Elegit.Cli.Command
import qualified Elegit.Git.Action as GA
import Fmt
import Options.Applicative
import qualified Options.Applicative.Help.Pretty as OA
import Universum


site:: Text
site = "placeholder"

purpose :: OA.Doc
purpose = OA.text "Configures the current local Git repository."

description :: OA.Doc
description = OA.string $ [s|
Applies the "basics", "standards", "aliases", and "signature" configurations
to the current Git repository using `git config --local`. The command asks to
provide information that is needed for the current repository configuration.

The behavior of the command varies depend on `git elegant acquire-git`
execution (a global configuration). If the global configuration is applied,
then this command configures repository-related staffs only, otherwise, it
applies all configurations to the current local repository.

To find out what will be configured, please visit
|] ++ (fmt ""+|site|+"/en/latest/configuration/")


cli :: Mod CommandFields ElegitCommand
cli = command "acquire-repository" $ info (pure AcquireRepositoryCommand) $
mconcat [ progDescDoc (Just purpose )
, footerDoc (Just description )
]


data ConfigKey
= UserNameKey
| UserEmailKey
| CoreEditorKey
| DefaultBranchKey
| ProtectedBranchesKey


configName :: ConfigKey -> Text
configName UserNameKey = "user.name"
configName UserEmailKey = "user.email"
configName CoreEditorKey = "core.editor"
configName DefaultBranchKey = "elegant-git.default-branch"
configName ProtectedBranchesKey = "elegant-git.protected-branches"


configPrompt :: ConfigKey -> Text
configPrompt UserNameKey = "What is your user name?"
configPrompt UserEmailKey = "What is your email?"
configPrompt CoreEditorKey = "What is the command to launching an editor?"
configPrompt DefaultBranchKey = "What is the default branch?"
configPrompt ProtectedBranchesKey = "What are protected branches (split with space)"


configDefault :: (MonadFree GA.GitF m) => ConfigKey -> m (Maybe Text)
configDefault cKey = case cKey of
UserNameKey -> getFromConfig
UserEmailKey -> getFromConfig
CoreEditorKey -> getFromConfig
DefaultBranchKey -> return $ Just "master"
ProtectedBranchesKey -> return $ Just "master"

where
getFromConfig :: (MonadFree GA.GitF m) => m (Maybe Text)
getFromConfig = GA.readConfig GA.AutoConfig (configName cKey)


configureBasics :: (MonadFree GA.GitF m) => GA.ConfigScope -> m ()
configureBasics cScope = do
for_ basicConfigs $ \cKey -> do
keyDefault <- configDefault cKey
newValue <- GA.promptDefault (configPrompt cKey) keyDefault
GA.setConfigVerbose cScope (configName cKey) newValue

where
basicConfigs :: [ConfigKey]
basicConfigs =
[ UserNameKey
, UserEmailKey
, CoreEditorKey
, DefaultBranchKey
, ProtectedBranchesKey
]


configureStandards :: (MonadFree GA.GitF m) => GA.ConfigScope -> m ()
configureStandards cScope =
for_ standardConfigs $ \(cKey,cValue) -> do
GA.setConfigVerbose cScope cKey cValue
where
standardConfigs :: [(Text, Text)]
standardConfigs =
[ ("core.commentChar", "|")
, ("apply.whitespace", "fix")
, ("fetch.prune", "true")
, ("fetch.pruneTags", "false")
, ("core.autocrlf", "input")
, ("pull.rebase", "true")
, ("rebase.autoStash", "false")
, ("credential.helper", "osxkeychain")
]


-- | Execution description of the AcquireRepository action
cmd :: (MonadFree GA.GitF m) => m ()
cmd = do
GA.removeObsoleteConfiguration GA.LocalConfig
GA.print =<< GA.formatInfoBox "Configuring basics..."
configureBasics GA.LocalConfig
unlessM GA.isGitAcquired $ do
GA.print =<< GA.formatInfoBox "Configuring standards..."
configureStandards GA.LocalConfig
-- GA.print =<< GA.formatInfoBox "Configuring aliases..."
-- TODO: Setup aliases
-- GA.print =<< GA.formatInfoBox "Configuring signature..."
-- TODO: Setup gpg key
21 changes: 11 additions & 10 deletions src/Elegit/Cli/Action/ShowWork.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,25 @@ cmd = do
changes <- GA.status GA.StatusShort
stashes <- GA.stashList

GA.reportInfo ">>> Branch refs:"
GA.reportInfo (fmt "local: "+|currentBranch|+"")
GA.print =<< GA.formatInfo ">>> Branch refs:"
GA.print =<< GA.formatInfo (fmt "local: "+|currentBranch|+"")
case mCurrentUpstream of
Just currentUpstream -> GA.reportInfo (fmt "remote: "+|currentUpstream|+"")
Nothing -> pass
Nothing -> pass
Just currentUpstream ->
GA.print =<< GA.formatInfo (fmt "remote: "+|currentUpstream|+"")

GA.reportInfo ""
GA.print ""

unless (null logs) $ do
GA.reportInfo (fmt ">>> New commits (comparing to "+|branchWithLatestChanges|+" branch):")
GA.print =<< GA.formatInfo (fmt ">>> New commits (comparing to "+|branchWithLatestChanges|+" branch):")
GA.print $ T.intercalate "\n" logs
GA.reportInfo ""
GA.print ""

unless (null changes) $ do
GA.reportInfo ">>> Uncommitted modifications:"
GA.print =<< GA.formatInfo ">>> Uncommitted modifications:"
GA.print $ T.intercalate "\n" changes
GA.reportInfo ""
GA.print ""

unless (null stashes) $ do
GA.reportInfo ">>> Available stashes:"
GA.print =<< GA.formatInfo ">>> Available stashes:"
GA.print $ T.intercalate "\n" stashes
1 change: 1 addition & 0 deletions src/Elegit/Cli/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import Universum

data ElegitCommand
= ShowWorkCommand
| AcquireRepositoryCommand
deriving (Eq, Show)
4 changes: 3 additions & 1 deletion src/Elegit/Cli/Parser.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Elegit.Cli.Parser where

import qualified Elegit.Cli.Action.ShowWork as ShowWork
import qualified Elegit.Cli.Action.AcquireRepository as AcquireRepository
import qualified Elegit.Cli.Action.ShowWork as ShowWork
import Elegit.Cli.Command
import Options.Applicative
import Universum
Expand All @@ -13,6 +14,7 @@ dayToDayContributionsCommand :: Command ElegitCommand
dayToDayContributionsCommand =
commandGroup "make day-to-day contributions"
<> ShowWork.cli
<> AcquireRepository.cli


cli :: ParserInfo ElegitCommand
Expand Down
Loading

0 comments on commit d48eb9c

Please sign in to comment.