[#79] Tell about rebase in 'hit status' (#83)

Resolves #79
This commit is contained in:
Dmitrii Kovanikov 2019-07-26 11:13:50 +08:00 committed by Veronika Romashkina
parent cda4a2e074
commit a941d5d7b3
3 changed files with 26 additions and 2 deletions

View File

@ -5,7 +5,8 @@ The changelog is available [on GitHub][2].
### Unreleased: 0.1.0.0
* [#63](https://github.com/kowainik/hit-on/issues/63):
* [#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.
* Bump up to GHC 8.6.5.
* Bump up to `relude-0.5.0`.

View File

@ -65,6 +65,7 @@ library
, github ^>= 0.20
, gitrev ^>= 1.3
, optparse-applicative ^>= 0.14
, process ^>= 1.6
, relude ^>= 0.5
, shellmet >= 0.0.0
, text

View File

@ -6,7 +6,8 @@ module Hit.Git.Status
( showPrettyDiff
) where
import Shellmet (($|))
import Shellmet (($|), ($?))
import System.Process (callCommand)
import Hit.ColorTerminal (blueCode, boldCode, cyanCode, greenCode, magentaCode, redCode, resetCode,
yellowCode)
@ -83,6 +84,10 @@ parseDiffStat = \case
showPrettyDiff :: Text -> IO ()
showPrettyDiff commit = do
-- 1. Check rebase in progress and tell about it
whenM isRebaseInProgress $ putTextLn gitRebaseHelp
-- 2. Output pretty diff
diffName <- map words . lines <$> "git" $| ["diff", commit, "--name-status"]
diffStat <- map toStats . lines <$> "git" $| ["diff", commit, "--stat", "--color=always"]
let fileTypes = sortWith diffNameFile $ mapMaybe parseDiffName diffName
@ -123,3 +128,20 @@ showPrettyDiff commit = do
maxOn :: (a -> Text) -> [a] -> Int
maxOn f = foldl' (\acc a -> max acc $ T.length $ f a) 0
{- | Returns 'True' if rebase is in progress. Calls magic comand and if this
command exits with code 1 then there's no rebase in progress.
-}
isRebaseInProgress :: IO Bool
isRebaseInProgress = do
let checkRebaseCmd = callCommand "ls `git rev-parse --git-dir` | grep rebase > /dev/null 2>&1"
True <$ checkRebaseCmd $? pure False
gitRebaseHelp :: Text
gitRebaseHelp = unlines
[ ""
, boldCode <> yellowCode <> "Rebase in progress! What you can do:" <> resetCode
, " " <> cyanCode <> "git rebase --continue " <> resetCode <> ": after fixing conflicts"
, " " <> cyanCode <> "git rebase --skip " <> resetCode <> ": to skip this patch"
, " " <> cyanCode <> "git rebase --abort " <> resetCode <> ": to abort to the original branch"
]