resolve-trivial-conflicts 0.3.1.2 → 0.3.2
raw patch · 3 files changed
+86/−61 lines, 3 filesdep +optparse-applicative
Dependencies added: optparse-applicative
Files
- Opts.hs +59/−0
- resolve-trivial-conflicts.cabal +3/−2
- resolve_trivial_conflicts.hs +24/−59
+ Opts.hs view
@@ -0,0 +1,59 @@+-- | Option parser++module Opts+ ( Options(..)+ , getOpts+ ) where++import Control.Applicative (Alternative(..))+import Data.Monoid ((<>))+import qualified Options.Applicative as O+import PPDiff (ColorEnable(..))++data Options = Options+ { shouldUseEditor :: Bool+ , shouldDumpDiffs :: Bool+ , shouldDumpDiff2 :: Bool+ , shouldUseColor :: Maybe ColorEnable+ , shouldSetConflictStyle :: Bool+ }++parser :: O.Parser Options+parser =+ Options+ <$> O.switch+ ( O.long "editor" <> O.short 'e'+ <> O.help "Execute $EDITOR for each conflicted file that remains conflicted"+ )+ <*> O.switch+ ( O.long "diff" <> O.short 'd'+ <> O.help "Dump the left/right diffs from base in each conflict remaining"+ )+ <*> O.switch+ ( O.long "diff2" <> O.short '2'+ <> O.help "Dump the diff between left and right in each conflict remaining"+ )+ <*> ( O.flag' (Just EnableColor)+ (O.long "color" <> O.short 'c' <> O.help "Enable color")+ <|> O.flag' (Just DisableColor)+ (O.long "no-color" <> O.short 'C' <> O.help "Disable color")+ <|> pure Nothing+ )+ <*> ( O.switch+ ( O.long "style" <> O.short 's'+ <> O.help "Configure git's global merge.conflictstyle to diff3 if needed"+ )+ )++opts :: O.ParserInfo Options+opts =+ O.info (O.helper <*> parser) $+ O.fullDesc+ <> O.progDesc+ "Resolve any git conflicts that have become trivial by editing operations.\n\+ \Go to http://github.com/ElastiLotem/resolve-trivial-conflicts for example use."+ <> O.header "resolve-trivial-conflicts - Become a conflicts hero"++getOpts :: IO Options+getOpts = O.execParser opts+
resolve-trivial-conflicts.cabal view
@@ -2,7 +2,7 @@ -- further documentation, see http://haskell.org/cabal/users-guide/ name: resolve-trivial-conflicts-version: 0.3.1.2+version: 0.3.2 synopsis: Remove trivial conflict markers in a git repository description: Remove trivial conflict markers in a git repository homepage: https://github.com/ElastiLotem/resolve-trivial-conflicts@@ -24,7 +24,7 @@ main-is: resolve_trivial_conflicts.hs ghc-options: -rtsopts -Wall ghc-prof-options: -rtsopts -Wall -auto-all -caf-all- other-modules: PPDiff+ other-modules: PPDiff, Opts -- other-extensions: build-depends: base >=4.6 && <5 , base-compat >= 0.8.2 && < 0.9@@ -35,5 +35,6 @@ , unix >=2.7 , Diff >=0.3 , ansi-terminal >=0.6.2+ , optparse-applicative >=0.11 && <0.13 -- hs-source-dirs: default-language: Haskell2010
resolve_trivial_conflicts.hs view
@@ -11,9 +11,11 @@ import Data.List (isPrefixOf, isSuffixOf) import Data.Maybe (mapMaybe) import qualified Data.Monoid as Monoid+import qualified Opts+import Opts (Options(..)) import PPDiff (ppDiff, ColorEnable(..)) import System.Directory (renameFile, removeFile, getCurrentDirectory)-import System.Environment (getProgName, getArgs, getEnv)+import System.Environment (getEnv) import System.Exit (ExitCode(..)) import System.FilePath ((<.>), makeRelative, joinPath, splitPath) import qualified System.FilePath as FilePath@@ -127,9 +129,14 @@ getConflictDiffs :: Conflict -> [SideDiff] getConflictDiffs Conflict{..} =- [ (A, cMarkerA, getDiff cLinesBase cLinesA) | not (null cLinesA) ] ++- [ (B, (fst cMarkerB, snd cMarkerEnd), getDiff cLinesBase cLinesB) | not (null cLinesB) ]+ [ (A, cMarkerA, getDiff cLinesBase cLinesA)+ | not (null cLinesA) ] +++ [ (B, (fst cMarkerB, snd cMarkerEnd), getDiff cLinesBase cLinesB)+ | not (null cLinesB) ] +getConflictDiff2s :: Conflict -> ((LineNo, String), (LineNo, String), [Diff String])+getConflictDiff2s Conflict{..} = (cMarkerA, cMarkerB, getDiff cLinesA cLinesB)+ resolveContent :: [Either String Conflict] -> NewContent resolveContent = asResult . mconcat . map go where@@ -149,50 +156,6 @@ gitAdd fileName = callProcess "git" ["add", "--", fileName] -data Options = Options- { shouldUseEditor :: Bool- , shouldDumpDiffs :: Bool- , shouldUseColor :: Maybe ColorEnable- , shouldSetConflictStyle :: Bool- }-instance Monoid Options where- mempty = Options False False Nothing False- Options oe0 od0 oc0 os0 `mappend` Options oe1 od1 oc1 os1 =- Options- (combineBool oe0 oe1 "-e")- (combineBool od0 od1 "-d")- (combineMaybe oc0 oc1 "-c or -C")- (os0 || os1)- where- err flag = error $ "Multiple " ++ flag ++ " flags used"- combineMaybe (Just _) (Just _) flag = err flag- combineMaybe Nothing Nothing _ = Nothing- combineMaybe (Just x) Nothing _ = Just x- combineMaybe Nothing (Just y) _ = Just y- combineBool True True flag = err flag- combineBool x y _ = x || y--getOpts :: [String] -> IO Options-getOpts = fmap mconcat . mapM parseArg- where- parseArg "-e" = return mempty { shouldUseEditor = True }- parseArg "-d" = return mempty { shouldDumpDiffs = True }- parseArg "-c" = return mempty { shouldUseColor = Just EnableColor }- parseArg "-C" = return mempty { shouldUseColor = Just DisableColor }- parseArg "-s" = return mempty { shouldSetConflictStyle = True }- parseArg arg =- do prog <- getProgName- putStr $ unlines- [ "Usage: " ++ prog ++ " [-e] [-d] [-c] [-C] [-s]"- , ""- , "-e Execute $EDITOR for each conflicted file that remains conflicted"- , "-d Dump the left/right diffs from base in each conflict remaining"- , "-c Enable color"- , "-C Disable color"- , "-s Configure git's global merge.conflictstyle to diff3 if needed"- ]- fail $ "Unknown argument: " ++ show arg- openEditor :: Options -> FilePath -> IO () openEditor opts path | shouldUseEditor opts =@@ -200,19 +163,24 @@ callProcess editor [path] | otherwise = return () -dumpDiffs :: ColorEnable -> Options -> FilePath -> [SideDiff] -> IO ()-dumpDiffs colorEnable opts filePath diffs- | shouldDumpDiffs opts = mapM_ dumpDiff diffs- | otherwise = return ()+dumpDiffs :: ColorEnable -> Options -> FilePath -> [Conflict] -> IO ()+dumpDiffs colorEnable opts filePath conflicts =+ do+ when (shouldDumpDiffs opts) $ mapM_ dumpDiff $ concatMap getConflictDiffs conflicts+ when (shouldDumpDiff2 opts) $ mapM_ dumpDiff2 $ map getConflictDiff2s conflicts where dumpDiff (side, (lineNo, marker), diff) = do putStrLn $ concat [filePath, ":", show lineNo, ":Diff", show side, ": ", marker] putStr $ unlines $ map (ppDiff colorEnable) diff+ dumpDiff2 ((lineNoA, markerA), (lineNoB, markerB), diff) =+ do putStrLn $ concat [filePath, ":", show lineNoA, " <->", markerA]+ putStrLn $ concat [filePath, ":", show lineNoB, ": ", markerB]+ putStr $ unlines $ map (ppDiff colorEnable) diff -dumpAndOpenEditor :: ColorEnable -> Options -> FilePath -> [SideDiff] -> IO ()-dumpAndOpenEditor colorEnable opts path diffs =- do dumpDiffs colorEnable opts path diffs+dumpAndOpenEditor :: ColorEnable -> Options -> FilePath -> [Conflict] -> IO ()+dumpAndOpenEditor colorEnable opts path conflicts =+ do dumpDiffs colorEnable opts path conflicts openEditor opts path overwrite :: FilePath -> String -> IO ()@@ -249,10 +217,7 @@ where doDump = dumpAndOpenEditor colorEnable opts fileName- [ cDiff- | Right conflict <- parseConflicts newContent- , cDiff <- getConflictDiffs conflict- ]+ [ conflict | Right conflict <- parseConflicts newContent ] stripNewline :: String -> String stripNewline x@@ -378,7 +343,7 @@ main :: IO () main =- do opts <- getOpts =<< getArgs+ do opts <- Opts.getOpts colorEnable <- case shouldUseColor opts of Nothing -> shouldUseColorByTerminal