aboutsummaryrefslogtreecommitdiff
path: root/Opts.hs
blob: a32682f4c2eb36ff188435a1092920e5e34106a8 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}

module Opts where

import Data.Bool
import Data.List
import Data.Maybe
import Data.Version (showVersion)
import Options.Applicative
import Paths_werge (version)

data Tokenizer
  = TokenizeFilter String
  | TokenizeCharCategory
  | TokenizeCharCategorySimple
  deriving (Show)

tokenizer =
  asum
    [ TokenizeFilter
        <$> strOption
              (long "tok-filter"
                 <> short 'F'
                 <> metavar "FILTER"
                 <> help "External program to separate the text to tokens")
    , flag'
        TokenizeCharCategorySimple
        (long "simple-tokens"
           <> short 'i'
           <> help
                "Use wider character class to separate the tokens (results in larger tokens and ignores case)")
    , flag'
        TokenizeCharCategory
        (long "full-tokens"
           <> short 'I'
           <> help
                "Separate characters by all known character classes (default)")
    , pure TokenizeCharCategory
    ]

data ConflictMask = ConflictMask
  { cmResolveOverlaps :: Bool
  , cmResolveSeparate :: Bool
  } deriving (Show)

conflictMask label objs = do
  cmResolveOverlaps' <-
    fmap not . switch
      $ long (label ++ "-overlaps")
          <> help ("Never resolve overlapping changes in " ++ objs)
  cmResolveSeparate' <-
    fmap not . switch
      $ long (label ++ "-separate")
          <> help
               ("Never resolve separate (non-overlapping) changes in " ++ objs)
  cmAll <-
    fmap not . switch
      $ long (label ++ "-all") <> help ("Never resolve any changes in " ++ objs)
  pure
    ConflictMask
      { cmResolveSeparate = cmResolveSeparate' && cmAll
      , cmResolveOverlaps = cmResolveOverlaps' && cmAll
      }

data Resolution
  = ResolveKeep
  | ResolveMy
  | ResolveOld
  | ResolveYour
  deriving (Show, Eq)

resolutionMode x
  | x `isPrefixOf` "keep" = Right ResolveKeep
  | x `isPrefixOf` "my" = Right ResolveMy
  | x `isPrefixOf` "old" = Right ResolveOld
  | x `isPrefixOf` "your" = Right ResolveYour
  | otherwise =
    Left
      $ "Could not parse value `"
          ++ x
          ++ "', use one of `keep', `my', `old', and `your'"

data SpaceResolution
  = SpaceNormal
  | SpaceSpecial Resolution
  deriving (Show, Eq)

spaceMode x
  | x `isPrefixOf` "normal" = Right SpaceNormal
  | Right y <- resolutionMode x = Right (SpaceSpecial y)
  | otherwise =
    Left
      $ "Could not parse value `"
          ++ x
          ++ "', use one of `normal', `keep', `my', `old', and `your'"

data Config = Config
  { cfgTokenizer :: Tokenizer
  , cfgZealous :: Bool
  , cfgSpaceRetain :: Resolution
  , cfgSpaceResolution :: SpaceResolution
  , cfgSpaceConflicts :: ConflictMask
  , cfgContext :: Int
  , cfgResolution :: Resolution
  , cfgConflicts :: ConflictMask
  , cfgLabelStart :: String
  , cfgLabelMyOld :: String
  , cfgLabelOldYour :: String
  , cfgLabelEnd :: String
  } deriving (Show)

config = do
  cfgTokenizer <- tokenizer
  cfgZealous <-
    asum
      [ flag' False $ long "no-zeal" <> help "avoid zealous mode (default)"
      , flag' True
          $ long "zeal"
              <> short 'z'
              <> help
                   "Try to zealously minify conflicts, potentially resolving them"
      , pure False
      ]
  cfgSpaceRetain <-
    option (eitherReader resolutionMode)
      $ long "space"
          <> short 'S'
          <> metavar "(keep|my|old|your)"
          <> help
               "Retain spacing from a selected version, or keep all space changes for merging (default: keep)"
          <> value ResolveKeep
  cfgSpaceResolution <-
    asum
      [ flag' (SpaceSpecial ResolveKeep)
          $ short 's'
              <> help
                   "Shortcut for `--resolve-space keep' (this separates space-only conflicts, enabling better automated resolution)"
      , option (eitherReader spaceMode)
          $ long "resolve-space"
              <> metavar ("(normal|keep|my|old|your)")
              <> value SpaceNormal
              <> help
                   "Resolve conflicts in space-only tokens separately, and either keep unresolved conflicts, or resolve in favor of a given version; `normal' resolves the spaces together with other tokens, ignoring choices in --resolve-space-* (default: normal)"
      ]
  cfgSpaceConflicts <- conflictMask "conflict-space" "space-only tokens"
  cfgContext <-
    option auto
      $ long "expand-context"
          <> short 'C'
          <> metavar "N"
          <> value 1
          <> showDefault
          <> help
               "Consider changes that are at most N tokens apart to be a single change. Zero may cause bad resolutions of near conflicting edits"
  cfgResolution <-
    option (eitherReader resolutionMode)
      $ long "resolve"
          <> metavar "(keep|my|old|your)"
          <> value ResolveKeep
          <> help
               "Resolve general conflicts in favor of a given version, or keep the conflicts (default: keep)"
  cfgConflicts <- conflictMask "conflict" "general tokens"
  color <-
    flag False True
      $ long "color"
          <> short 'G'
          <> help
               "Use shorter, gaily colored output markers by default (requires ANSI color support; good for terminals or `less -R')"
  labelStart <-
    optional . strOption
      $ long "label-start"
          <> metavar "\"<<<<<\""
          <> help "Label for beginning of the conflict"
  labelMyOld <-
    optional . strOption
      $ long "label-mo"
          <> metavar "\"|||||\""
          <> help "Separator of local edits and original"
  labelOldYour <-
    optional . strOption
      $ long "label-oy"
          <> metavar "\"=====\""
          <> help "Separator of original and other people's edits"
  labelEnd <-
    optional . strOption
      $ long "label-end"
          <> metavar "\">>>>>\""
          <> help "Label for end of the conflict"
  pure
    Config
      { cfgLabelStart =
          bool "<<<<<" "\ESC[1;37m<\ESC[0;31m" color `fromMaybe` labelStart
      , cfgLabelMyOld =
          bool "|||||" "\ESC[1;37m|\ESC[1;30m" color `fromMaybe` labelMyOld
      , cfgLabelOldYour =
          bool "=====" "\ESC[1;37m=\ESC[0;32m" color `fromMaybe` labelOldYour
      , cfgLabelEnd =
          bool ">>>>>" "\ESC[1;37m>\ESC[0m" color `fromMaybe` labelEnd
      , ..
      }

data Command
  = CmdDiff3
      { d3my :: FilePath
      , d3old :: FilePath
      , d3your :: FilePath
      }
  | CmdGitMerge
      { gmFiles :: Maybe [FilePath]
      , gmDoAdd :: Bool
      }
  deriving (Show)

cmdDiff3 = do
  d3my <- strArgument $ metavar "MYFILE" <> help "Version with local edits"
  d3old <- strArgument $ metavar "OLDFILE" <> help "Original file version"
  d3your <-
    strArgument $ metavar "YOURFILE" <> help "Version with other people's edits"
  pure CmdDiff3 {..}

cmdGitMerge = do
  gmFiles <-
    asum
      [ fmap Just . some
          $ strArgument
          $ metavar "UNMERGED"
              <> help "Unmerged git file (can be specified repeatedly)"
      , flag'
          Nothing
          (long "unmerged"
             <> short 'u'
             <> help "Process all files marked as unmerged by git")
      ]
  gmDoAdd <-
    asum
      [ flag'
          True
          (long "add"
             <> short 'a'
             <> help "Run `git add' for fully merged files")
      , flag' False (long "no-add" <> help "Prevent running `git add'")
      , pure False
      ]
  pure CmdGitMerge {..}

-- TODO have some option to output the (partially merged) my/old/your files so
-- that folks can continue with external program or so (such as meld)
cmd =
  hsubparser
    $ mconcat
        [ command "merge"
            $ info cmdDiff3
            $ progDesc "diff3-style merge of two changesets"
        , command "git"
            $ info cmdGitMerge
            $ progDesc "Automerge unmerged files in git conflict"
        ]

parseOpts :: IO (Config, Command)
parseOpts =
  customExecParser (prefs helpShowGlobals)
    $ info
        (liftA2 (,) config cmd
           <**> helper
           <**> simpleVersioner (showVersion version))
        (fullDesc
           <> header
                "werge -- blanks-friendly mergetool for tiny interdwindled changes"
           <> footer "werge is a free software, use it accordingly.")