From ed9969ab367fbddd11ac2f6ae6c0c15114b43a5f Mon Sep 17 00:00:00 2001 From: Veronika Romashkina Date: Sun, 28 Jul 2019 15:45:13 +0800 Subject: [PATCH] [#82] Add force flag to hit fix (#88) * [#82] Add force flag to hit fix Resolves #82 * Fix stupid HLint rule * Fix --- CHANGELOG.md | 2 ++ hit-on.cabal | 1 + src/Hit/Cli.hs | 37 ++++++++++++++++++++++++------------- src/Hit/Core.hs | 13 +++++++++++++ src/Hit/Git.hs | 23 +++++++++++++---------- 5 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 src/Hit/Core.hs diff --git a/CHANGELOG.md b/CHANGELOG.md index 09f1f55..b0376df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ The changelog is available [on GitHub][2]. * [#63](https://github.com/kowainik/hit-on/issues/63), [#79](https://github.com/kowainik/hit-on/issues/79): Implement `hit status` command with pretty output. +* [#82](https://github.com/kowainik/hit-on/issues/82): + Add `--force` flag to `hit fix` command. * Bump up to GHC 8.6.5. * Bump up to `relude-0.5.0`. diff --git a/hit-on.cabal b/hit-on.cabal index 01c2e7d..5ae5de3 100644 --- a/hit-on.cabal +++ b/hit-on.cabal @@ -52,6 +52,7 @@ library exposed-modules: Hit Hit.Cli Hit.ColorTerminal + Hit.Core Hit.Git Hit.Git.Status Hit.Issue diff --git a/src/Hit/Cli.hs b/src/Hit/Cli.hs index f1a882b..8896cc2 100644 --- a/src/Hit/Cli.hs +++ b/src/Hit/Cli.hs @@ -9,11 +9,12 @@ module Hit.Cli import Data.Version (showVersion) import Development.GitRev (gitCommitDate, gitDirty, gitHash) -import Options.Applicative (Parser, ParserInfo, argument, auto, command, execParser, fullDesc, help, - helper, info, infoOption, long, metavar, progDesc, short, strArgument, - subparser, switch) +import Options.Applicative (Parser, ParserInfo, argument, auto, command, execParser, flag, fullDesc, + help, helper, info, infoOption, long, metavar, progDesc, short, + strArgument, subparser, switch) import Hit.ColorTerminal (arrow, blueCode, boldCode, redCode, resetCode) +import Hit.Core (PushBool (..)) import Hit.Git (getUsername, runAmend, runClone, runCommit, runCurrent, runFix, runFresh, runHop, runNew, runPush, runResolve, runStatus, runSync) import Hit.Issue (runIssue) @@ -31,7 +32,7 @@ hit = execParser cliParser >>= \case then getUsername >>= runIssue issueNum . Just else runIssue issueNum Nothing Commit message noIssue -> runCommit message noIssue - Fix message -> runFix message + Fix message pushBool -> runFix message pushBool Amend -> runAmend Resolve branchName -> runResolve branchName Push isForce -> runPush isForce @@ -56,10 +57,12 @@ data HitCommand | New Int | Issue (Maybe Int) Bool | Commit Text Bool - | Fix (Maybe Text) + | Fix + (Maybe Text) -- ^ Text of the fix commit + PushBool -- ^ Force push | Amend | Resolve (Maybe Text) - | Push Bool + | Push PushBool | Sync | Current | Status (Maybe Text) @@ -109,18 +112,18 @@ commitP = do <> help "Do not add [#ISSUE_NUMBER] prefix when specified" pure $ Commit msg noIssue +{- HLINT ignore "Use <$>"-} fixP :: Parser HitCommand -fixP = Fix <$> commitMessageP +fixP = do + commitMsg <- commitMessageP + isForce <- pushBoolP + pure $ Fix commitMsg isForce amendP :: Parser HitCommand amendP = pure Amend pushP :: Parser HitCommand -pushP = Push <$> switch - ( long "force" - <> short 'f' - <> help "Force push" - ) +pushP = Push <$> pushBoolP syncP :: Parser HitCommand syncP = pure Sync @@ -141,10 +144,18 @@ cloneP = Clone <$> strArgument (metavar "REPOSITORY") maybeBranchP :: Parser (Maybe Text) maybeBranchP = optional $ strArgument (metavar "BRANCH_NAME") --- / Parse optional commit message as an argument +-- | Parse optional commit message as an argument commitMessageP :: Parser (Maybe Text) commitMessageP = optional $ strArgument (metavar "COMMIT_MESSAGE") +-- | Parse flag of force push. +pushBoolP :: Parser PushBool +pushBoolP = flag Simple Force + ( long "force" + <> short 'f' + <> help "Force push" + ) + -- | Parse issue number as an argument. issueNumP :: Parser Int issueNumP = argument auto $ metavar "ISSUE_NUMBER" diff --git a/src/Hit/Core.hs b/src/Hit/Core.hs new file mode 100644 index 0000000..f01d26c --- /dev/null +++ b/src/Hit/Core.hs @@ -0,0 +1,13 @@ +{- | This module contains core data types used in the package. +-} + +module Hit.Core + ( PushBool (..) + ) where + + +-- | Data type to represent the type of @push@: force-push or not. +data PushBool + = Simple + | Force + deriving (Show, Eq) diff --git a/src/Hit/Git.hs b/src/Hit/Git.hs index 0d9bf64..ec66bf9 100644 --- a/src/Hit/Git.hs +++ b/src/Hit/Git.hs @@ -23,6 +23,7 @@ import Data.Char (isAlphaNum, isDigit, isSpace) import Shellmet (($|)) import Hit.ColorTerminal (arrow, errorMessage, greenCode, resetCode) +import Hit.Core (PushBool (..)) import Hit.Git.Status (showPrettyDiff) import Hit.Issue (getIssueTitle, mkIssueId) @@ -75,14 +76,15 @@ runCommit (T.strip -> msg) (not -> hasIssue) let issue = "#" <> show n in "[" <> issue <> "] " <> msg <> "\n\nResolves " <> issue --- / @hit fix@ command -runFix :: Maybe Text -> IO () -runFix msg = do +-- | @hit fix@ command +runFix :: Maybe Text -> PushBool -> IO () +runFix msg pushBool = do "git" ["add", "."] "git" ["commit", "-m", message] - runPush False - where - message = fromMaybe "Fix after review" msg + runPush pushBool + where + message :: Text + message = fromMaybe "Fix" msg -- | @hit amend@ command. @@ -90,17 +92,18 @@ runAmend :: IO () runAmend = do "git" ["add", "."] "git" ["commit", "--amend", "--no-edit"] - runPush True + runPush Force -- | @hit push@ command. -runPush :: Bool -> IO () +runPush :: PushBool -> IO () runPush isForce = getCurrentBranch >>= \branch -> "git" $ ["push", "--set-upstream", "origin", branch] - ++ ["--force" | isForce] + ++ ["--force" | isForce == Force] -- | @hit sync@ command. runSync :: IO () -runSync = getCurrentBranch >>= \branch -> "git" ["pull", "--rebase", "origin", branch] +runSync = getCurrentBranch >>= \branch -> + "git" ["pull", "--rebase", "origin", branch] -- | @hit resolve@ command. runResolve :: Maybe Text -> IO ()