-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub-rebase.hs
executable file
·42 lines (35 loc) · 1.23 KB
/
sub-rebase.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env stack
{-# LANGUAGE LambdaCase #-}
import GitUtils
import Control.Monad (when)
import Control.Monad.Extra (fromMaybeM)
import Data.Functor ((<&>))
import Data.Maybe (fromJust)
import Data.List (isInfixOf)
import qualified Options.Applicative as O
import System.Exit (ExitCode(..))
import System.Process
opts :: O.ParserInfo (Maybe String)
opts =
O.info
(O.helper <*>
O.optional (O.strArgument (O.metavar "BASE" <> O.help "Base branch to rebase over")))
(O.fullDesc <> O.progDesc "Gradually rebase to handle less merge conflicts at a time")
subRebase :: String -> IO ()
subRebase base =
firstCommitInPathTo base >>=
\case
Nothing -> putStrLn "Done"
Just commit -> do
putStrLn ("Rebasing over " <> commit)
spawnCommand ("git rebase " <> commit) >>= waitForProcess
>>= (`when` subRebase base) . (== ExitSuccess)
allConflictsFixed :: IO Bool
allConflictsFixed =
cmd "git status" <&> isInfixOf "all conflicts fixed: run \"git rebase --continue\"" . fromJust
main :: IO ()
main = do
base <- fromMaybeM defaultBaseBranch (O.execParser opts)
putStrLn ("Rebasing onto: " <> base)
allConflictsFixed >>= (`when` callCommand "git rebase --continue")
subRebase base