IMPROVE: prevent loosing changes on deactivate

- Uncommitted changes got lost on deactivate. A prompt is show when uncommitted changes are present and a force flag is added to discard and deactivate
This commit is contained in:
Jean-Claude 2022-03-03 22:50:40 +01:00
parent 469a8dddcf
commit 52eee8a38a
Signed by: jeanclaude
GPG Key ID: 8A300F57CBB9F63E
5 changed files with 42 additions and 9 deletions

View File

@ -8,6 +8,7 @@ All notable changes to MIAnex are documented in this file.
- Command `list` to list all open imports
- Command `activate IMPORT` to switch to a current import
- Command `deactivate` to go back to main
- Flag `--force` to discard uncommitted changes and go back to main
- Command `import [IMAGES]` to create a new import
- Flag `--identifier TEXT` adds an optional identifier
- Flag `--current` to use the current branch for the import

View File

@ -4,6 +4,7 @@ module Command (
ImportImagesOptions(importImagesIdentifier, importImagesCurrentBranch),
ImportOption(ImportImages),
ActivateOption(ActivateImport),
DeactivateOption(DeactivateForce),
mainParser
) where
@ -22,7 +23,7 @@ data Options = Options
data Command
= Import ImportOption
| Activate ActivateOption
| Deactivate
| Deactivate DeactivateOption
| List
| Test
| NotImplemented
@ -44,6 +45,11 @@ data ActivateOption
= ActivateImport Git.Branch
deriving (Eq, Show)
-- Deactivate
data DeactivateOption
= DeactivateForce Bool
deriving (Eq, Show)
importCommand :: Mod CommandFields Command
importCommand =
command "import" (info importParser (progDesc "Import to archive"))
@ -93,7 +99,18 @@ activateParser =
deactivateCommand :: Mod CommandFields Command
deactivateCommand =
command "deactivate" (info (pure Deactivate) (progDesc "Deactivate import"))
command "deactivate" (info deactivateParser (progDesc "Deactivate import"))
deactivateParser :: Parser Command
deactivateParser =
Deactivate <$>
( DeactivateForce
<$> switch
( long "force"
<> short 'f'
<> help "Force deactivate. All uncommitted changes are lost."
)
)
listCommand :: Mod CommandFields Command
listCommand =

View File

@ -2,12 +2,18 @@ module Command.Deactivate (
runDeactivate
) where
import Command (DeactivateOption(DeactivateForce))
import qualified Config as Conf
import qualified Git as Git
runDeactivate :: IO ()
runDeactivate = do
mainBranch <- Conf.mainBranch
_ <- Git.runGit ["checkout", mainBranch]
return ()
runDeactivate :: DeactivateOption -> IO ()
runDeactivate (DeactivateForce force) = do
isDirty <- Git.existUncommittedChanges
if (isDirty && (not force)) then do
putStrLn "Deactivation not possible as there are uncommitted changes. Commit or discard them, or use the force flag to discard them."
return ()
else do
mainBranch <- Conf.mainBranch
_ <- Git.runGit ["checkout", mainBranch]
return ()

11
Git.hs
View File

@ -4,7 +4,8 @@ module Git(
getBranches,
getCurrentBranch,
branchExists,
checkoutBranch
checkoutBranch,
existUncommittedChanges
) where
import qualified Config as Conf
@ -60,3 +61,11 @@ checkoutBranch branch arg = do
return ()
return ()
existUncommittedChanges :: IO Bool
existUncommittedChanges = do
out <- runGit $ ["status", "-s"]
if out == "" then
return False
else
return True

View File

@ -14,7 +14,7 @@ main = do
case optCommand options of
(Import opts) -> runImport opts
(Activate opts) -> runActivate opts
Deactivate -> runDeactivate
(Deactivate opts) -> runDeactivate opts
List -> runList
Test -> runTest
_ -> putStrLn "Invalid"