1
- {-# LANGUAGE DeriveFunctor #-}
2
- {-# LANGUAGE DerivingStrategies #-}
3
- {-# LANGUAGE FlexibleContexts #-}
1
+ {-# LANGUAGE DeriveFunctor #-}
2
+ {-# LANGUAGE DerivingStrategies #-}
3
+ {-# LANGUAGE DuplicateRecordFields #-}
4
+ {-# LANGUAGE FlexibleContexts #-}
4
5
5
6
-----------------------------------------------------------------------------
6
7
-- |
@@ -35,25 +36,125 @@ import qualified Data.Text as T
35
36
import Fmt
36
37
import Universum hiding (print )
37
38
39
+ -- TODO: maybe, cover with tests
40
+ class RenderGitCommand c where
41
+ renderGC :: c -> Text
42
+
43
+ data GCurrentBranchData
44
+ = GCurrentBranchData
45
+
46
+ instance RenderGitCommand GCurrentBranchData where
47
+ renderGC _ = " rev-parse --abbrev-ref @"
48
+
49
+ newtype GBranchUpstreamData
50
+ = GBranchUpstreamData { branch :: Text }
51
+
52
+ instance RenderGitCommand GBranchUpstreamData where
53
+ renderGC (GBranchUpstreamData branchName) = " rev-parse --abbrev-ref " +| branchName|+ " @{upstream}"
54
+
55
+ data GLogData
56
+ = GLogData
57
+ { logType :: LogType
58
+ , base :: Text
59
+ , target :: Text
60
+ }
61
+ instance RenderGitCommand GLogData where
62
+ renderGC (GLogData lType baseName targetName) = " log " +| logArg|+ " " +| baseName|+ " .." +| targetName|+ " "
63
+ where
64
+ logArg :: Text
65
+ logArg = case lType of
66
+ LogOneLine -> " --oneline"
67
+
68
+ newtype GStatusData
69
+ = GStatusData { statusType :: StatusType }
70
+
71
+ instance RenderGitCommand GStatusData where
72
+ renderGC (GStatusData sType) = " status " +| statusFormat|+ " "
73
+ where
74
+ statusFormat :: Text
75
+ statusFormat = case sType of
76
+ StatusShort -> " --short"
77
+
78
+ data GStashListData
79
+ = GStashListData
80
+ instance RenderGitCommand GStashListData where
81
+ renderGC _ = " stash list"
82
+
83
+ data GReadConfigData
84
+ = GReadConfigData
85
+ { scope :: ConfigScope
86
+ , key :: Text
87
+ }
88
+
89
+ instance RenderGitCommand GReadConfigData where
90
+ renderGC (GReadConfigData cScope cName) = " config " +| scopeText|+ " --get " +| cName|+ " "
91
+ where
92
+ scopeText :: Text
93
+ scopeText = case cScope of
94
+ LocalConfig -> " --local"
95
+ GlobalConfig -> " --global"
96
+ AutoConfig -> " "
97
+
98
+ data GSetConfigData
99
+ = GSetConfigData
100
+ { scope :: ConfigScope
101
+ , key :: Text
102
+ , value :: Text
103
+ }
104
+
105
+ instance RenderGitCommand GSetConfigData where
106
+ renderGC (GSetConfigData cScope cName cValue) = " config " +| scopeText|+ " " +| cName|+ " " +| cValue|+ " "
107
+ where
108
+ scopeText :: Text
109
+ scopeText = case cScope of
110
+ GlobalConfig -> " --global"
111
+ LocalConfig -> " --local"
112
+ AutoConfig -> " --local"
113
+
114
+ data GUnsetConfigData
115
+ = GUnsetConfigData
116
+ { scope :: ConfigScope
117
+ , key :: Text
118
+ }
119
+
120
+ instance RenderGitCommand GUnsetConfigData where
121
+ renderGC (GUnsetConfigData cScope cName) = " config " +| scopeText|+ " --unset " +| cName|+ " "
122
+ where
123
+ scopeText :: Text
124
+ scopeText = case cScope of
125
+ GlobalConfig -> " --global"
126
+ LocalConfig -> " --local"
127
+ AutoConfig -> " --local"
128
+
129
+ newtype GAliasesToRemoveData
130
+ = GAliasesToRemoveData { scope :: ConfigScope }
131
+
132
+ instance RenderGitCommand GAliasesToRemoveData where
133
+ renderGC (GAliasesToRemoveData cScope) = " config " +| scopeText|+ " --name-only --get-regexp \" ^alias.\" \" ^elegant ([-a-z]+)$\" "
134
+ where
135
+ scopeText :: Text
136
+ scopeText = case cScope of
137
+ GlobalConfig -> " --global"
138
+ LocalConfig -> " --local"
139
+ AutoConfig -> " "
140
+
38
141
-- | The declaration of all posible actions we can do in the git action.
39
142
--
40
143
-- This describes the data of the action, and whether it can return any value
41
144
-- for further computations.
42
145
--
43
146
-- We can use records later to better comunicate the purpose of each field by
44
147
-- providing a name.
45
- --
46
- -- TODO: Use records
47
148
data GitF a
48
- = CurrentBranch (Text -> a )
49
- | BranchUpstream Text (Maybe Text -> a )
50
- | Log LogType Text Text ([Text ] -> a )
51
- | Status StatusType ([Text ] -> a )
52
- | StashList ([Text ] -> a )
53
- | ReadConfig ConfigScope Text (Maybe Text -> a )
54
- | AliasesToRemove ConfigScope (Maybe (NonEmpty Text ) -> a )
55
- | SetConfig ConfigScope Text Text a
56
- | UnsetConfig ConfigScope Text a
149
+ = CurrentBranch GCurrentBranchData (Text -> a )
150
+ | BranchUpstream GBranchUpstreamData (Maybe Text -> a )
151
+ | Log GLogData ([Text ] -> a )
152
+ | Status GStatusData ([Text ] -> a )
153
+ | StashList GStashListData ([Text ] -> a )
154
+ | ReadConfig GReadConfigData (Maybe Text -> a )
155
+ | AliasesToRemove GAliasesToRemoveData (Maybe (NonEmpty Text ) -> a )
156
+ | SetConfig GSetConfigData a
157
+ | UnsetConfig GUnsetConfigData a
57
158
| Prompt Text (Maybe Text ) (Text -> a )
58
159
| FormatInfo Text (Text -> a )
59
160
| FormatCommand Text (Text -> a )
@@ -101,34 +202,34 @@ type FreeGit t = F GitF t
101
202
-- * Otherwise just use `id` function.
102
203
103
204
status :: MonadFree GitF m => StatusType -> m [Text ]
104
- status sType = liftF $ Status sType id
205
+ status sType = liftF $ Status ( GStatusData sType) id
105
206
106
207
log :: MonadFree GitF m => LogType -> Text -> Text -> m [Text ]
107
- log lType lBase lTarget = liftF $ Log lType lBase lTarget id
208
+ log lType lBase lTarget = liftF $ Log ( GLogData lType lBase lTarget) id
108
209
109
210
stashList :: MonadFree GitF m => m [Text ]
110
- stashList = liftF $ StashList id
211
+ stashList = liftF $ StashList GStashListData id
111
212
112
213
currentBranch :: MonadFree GitF m => m Text
113
- currentBranch = liftF $ CurrentBranch id
214
+ currentBranch = liftF $ CurrentBranch GCurrentBranchData id
114
215
115
216
branchUpstream :: MonadFree GitF m => Text -> m (Maybe Text )
116
- branchUpstream bName = liftF $ BranchUpstream bName id
217
+ branchUpstream bName = liftF $ BranchUpstream ( GBranchUpstreamData bName) id
117
218
118
219
readConfig :: MonadFree GitF m => ConfigScope -> Text -> m (Maybe Text )
119
- readConfig cScope cName = liftF $ ReadConfig cScope cName id
220
+ readConfig cScope cName = liftF $ ReadConfig ( GReadConfigData cScope cName) id
120
221
121
- -- TODO: Check if it's better or even possible to get all configurations and filter then ourself.
222
+ -- TODO: Check if it's better or even possible to get all configurations and filter them ourself.
122
223
-- This would improve testability of this, as now we rely on the fact that we make a correct cli call
123
224
-- to find config with regex in the `Real.hs`.
124
225
aliasesToRemove :: MonadFree GitF m => ConfigScope -> m (Maybe (NonEmpty Text ))
125
- aliasesToRemove cScope = liftF $ AliasesToRemove cScope id
226
+ aliasesToRemove cScope = liftF $ AliasesToRemove ( GAliasesToRemoveData cScope) id
126
227
127
228
setConfig :: MonadFree GitF m => ConfigScope -> Text -> Text -> m ()
128
- setConfig cScope cName cValue = liftF $ SetConfig cScope cName cValue ()
229
+ setConfig cScope cName cValue = liftF $ SetConfig ( GSetConfigData cScope cName cValue) ()
129
230
130
231
unsetConfig :: MonadFree GitF m => ConfigScope -> Text -> m ()
131
- unsetConfig cScope cName = liftF $ UnsetConfig cScope cName ()
232
+ unsetConfig cScope cName = liftF $ UnsetConfig ( GUnsetConfigData cScope cName) ()
132
233
133
234
promptDefault :: MonadFree GitF m => Text -> Maybe Text -> m Text
134
235
promptDefault pText pDefault = liftF $ Prompt pText pDefault id
@@ -144,22 +245,18 @@ print content = liftF $ PrintText content ()
144
245
145
246
-- Derived actions
146
247
147
- configScopeText :: ConfigScope -> Text
148
- configScopeText LocalConfig = " --local"
149
- configScopeText GlobalConfig = " --global"
150
- configScopeText AutoConfig = " "
248
+ formatGitCommand :: (RenderGitCommand gc , MonadFree GitF m ) => gc -> m Text
249
+ formatGitCommand gc = formatCommand (" git " +| renderGC gc|+ " " )
151
250
152
251
setConfigVerbose :: MonadFree GitF m => ConfigScope -> Text -> Text -> m ()
153
252
setConfigVerbose cScope cName cValue = do
154
253
setConfig cScope cName cValue
155
- -- TODO: Generate these messages only in a single place and reuse in `Real.hs`
156
- print =<< formatCommand (" git config " +| configScopeText cScope|+ " " +| cName|+ " " +| cValue|+ " " )
254
+ print =<< formatGitCommand (GSetConfigData cScope cName cValue)
157
255
158
256
unsetConfigVerbose :: MonadFree GitF m => ConfigScope -> Text -> m ()
159
257
unsetConfigVerbose cScope cName = do
160
258
unsetConfig cScope cName
161
- -- TODO: Generate these messages only in a single place and reuse in `Real.hs`
162
- print =<< formatCommand (" git config " +| configScopeText cScope|+ " --unset " +| cName|+ " " )
259
+ print =<< formatGitCommand (GUnsetConfigData cScope cName)
163
260
164
261
freshestDefaultBranch :: MonadFree GitF m => m Text
165
262
freshestDefaultBranch = do
0 commit comments