9
9
-- |
10
10
-- Module : Distribution.Server.Util.CabalRevisions
11
11
-- Copyright : Duncan Coutts et al.
12
- -- License : BSD3
12
+ -- SPDX- License-Identifier: BSD-3-Clause
13
13
--
14
14
15
15
-- Stability : provisional
18
18
-- Validation and helpers for Cabal revision handling
19
19
module Distribution.Server.Util.CabalRevisions
20
20
( diffCabalRevisions
21
+ , diffCabalRevisions'
21
22
, Change (.. )
22
23
, insertRevisionField
23
24
) where
24
25
25
26
-- NB: This module avoids to import any hackage-server modules
26
- import Distribution.CabalSpecVersion (cabalSpecLatest )
27
+ import Distribution.CabalSpecVersion (CabalSpecVersion ( .. ), cabalSpecLatest , showCabalSpecVersion )
27
28
import Distribution.Types.Dependency
28
29
import Distribution.Types.ExeDependency
29
30
import Distribution.Types.PkgconfigDependency
@@ -43,7 +44,6 @@ import Distribution.PackageDescription.Parsec (parseGenericPackageDescription, r
43
44
import Distribution.PackageDescription.FieldGrammar (sourceRepoFieldGrammar )
44
45
import Distribution.PackageDescription.Check
45
46
import Distribution.Parsec (showPWarning , showPError , PWarning (.. ))
46
- import Distribution.Simple.LocalBuildInfo (showComponentName )
47
47
import Distribution.Utils.ShortText
48
48
import Text.PrettyPrint as Doc
49
49
((<+>) , colon , text , Doc , hsep , punctuate )
@@ -54,6 +54,7 @@ import Control.Monad.Except (ExceptT, runExceptT, throwError)
54
54
import Control.Monad.Writer (MonadWriter (.. ), Writer , runWriter )
55
55
import Data.Foldable (for_ )
56
56
import Data.List
57
+ ((\\) , deleteBy , intercalate )
57
58
import Data.ByteString.Lazy (ByteString )
58
59
import qualified Data.ByteString as BS
59
60
import qualified Data.ByteString.Lazy.Char8 as LBS8
@@ -72,8 +73,16 @@ import qualified Control.Monad.Fail as Fail
72
73
-- 'String' and performs validations. Returns either a validation
73
74
-- error or a list of detected changes.
74
75
diffCabalRevisions :: BS. ByteString -> BS. ByteString -> Either String [Change ]
75
- diffCabalRevisions oldVersion newRevision = runCheck $
76
- checkCabalFileRevision oldVersion newRevision
76
+ diffCabalRevisions = diffCabalRevisions' True
77
+
78
+ -- | Like 'diffCabalRevisions' but only optionally check @x-revision@ field modifications.
79
+ diffCabalRevisions'
80
+ :: Bool -- ^ check @x-revision@
81
+ -> BS. ByteString -- ^ old revision
82
+ -> BS. ByteString -- ^ new revision
83
+ -> Either String [Change ]
84
+ diffCabalRevisions' checkXRevision oldVersion newRevision = runCheck $
85
+ checkCabalFileRevision checkXRevision oldVersion newRevision
77
86
78
87
newtype CheckM a = CheckM { unCheckM :: ExceptT String (Writer [Change ]) a }
79
88
deriving (Functor , Applicative )
@@ -127,15 +136,15 @@ logChange change = CheckM (tell [change])
127
136
128
137
type Check a = a -> a -> CheckM ()
129
138
130
- checkCabalFileRevision :: Check BS. ByteString
131
- checkCabalFileRevision old new = do
139
+ checkCabalFileRevision :: Bool -> Check BS. ByteString
140
+ checkCabalFileRevision checkXRevision old new = do
132
141
(pkg, warns) <- parseCabalFile old
133
142
(pkg', warns') <- parseCabalFile new
134
143
135
144
let pkgid = packageId pkg
136
145
filename = prettyShow pkgid ++ " .cabal"
137
146
138
- checkGenericPackageDescription pkg pkg'
147
+ checkGenericPackageDescription checkXRevision pkg pkg'
139
148
checkParserWarnings filename warns warns'
140
149
checkPackageChecks pkg pkg'
141
150
@@ -170,12 +179,12 @@ checkCabalFileRevision old new = do
170
179
[] -> return ()
171
180
newchecks -> fail $ unlines (map explanation newchecks)
172
181
173
- checkGenericPackageDescription :: Check GenericPackageDescription
174
- checkGenericPackageDescription
175
- (GenericPackageDescription descrA flagsA libsA sublibsA flibsA exesA testsA benchsA)
176
- (GenericPackageDescription descrB flagsB libsB sublibsB flibsB exesB testsB benchsB) = do
182
+ checkGenericPackageDescription :: Bool -> Check GenericPackageDescription
183
+ checkGenericPackageDescription checkXRevision
184
+ (GenericPackageDescription descrA _versionA flagsA libsA sublibsA flibsA exesA testsA benchsA)
185
+ (GenericPackageDescription descrB _versionB flagsB libsB sublibsB flibsB exesB testsB benchsB) = do
177
186
178
- checkPackageDescriptions descrA descrB
187
+ checkPackageDescriptions checkXRevision descrA descrB
179
188
180
189
checkList " Cannot add or remove flags" checkFlag flagsA flagsB
181
190
@@ -213,7 +222,7 @@ checkGenericPackageDescription
213
222
withComponentName' f condTree = (f, condTree)
214
223
215
224
216
- checkFlag :: Check Flag
225
+ checkFlag :: Check PackageFlag
217
226
checkFlag flagOld flagNew = do
218
227
-- This check is applied via 'checkList' and for simplicity we
219
228
-- disallow renaming/reordering flags (even though reordering
@@ -248,10 +257,10 @@ checkFlag flagOld flagNew = do
248
257
changesOk (" description of flag '" ++ fname ++ " '" ) id
249
258
(flagDescription flagOld) (flagDescription flagNew)
250
259
251
- checkPackageDescriptions :: Check PackageDescription
252
- checkPackageDescriptions
260
+ checkPackageDescriptions :: Bool -> Check PackageDescription
261
+ checkPackageDescriptions checkXRevision
253
262
pdA@ (PackageDescription
254
- { specVersionRaw = _specVersionRawA
263
+ { specVersion = _specVersionA
255
264
, package = packageIdA
256
265
, licenseRaw = licenseRawA
257
266
, licenseFiles = licenseFilesA
@@ -283,7 +292,7 @@ checkPackageDescriptions
283
292
, extraDocFiles = extraDocFilesA
284
293
})
285
294
pdB@ (PackageDescription
286
- { specVersionRaw = _specVersionRawB
295
+ { specVersion = _specVersionB
287
296
, package = packageIdB
288
297
, licenseRaw = licenseRawB
289
298
, licenseFiles = licenseFilesB
@@ -354,14 +363,14 @@ checkPackageDescriptions
354
363
checkSpecVersionRaw pdA pdB
355
364
checkSetupBuildInfo setupBuildInfoA setupBuildInfoB
356
365
357
- checkRevision customFieldsPDA customFieldsPDB
366
+ when checkXRevision $ checkRevision customFieldsPDA customFieldsPDB
358
367
checkCuration customFieldsPDA customFieldsPDB
359
368
360
369
checkSpecVersionRaw :: Check PackageDescription
361
370
checkSpecVersionRaw pdA pdB
362
- | specVersionA `withinRange` range110To120
363
- , specVersionB `withinRange` range110To120
364
- = changesOk " cabal-version" prettyShow specVersionA specVersionB
371
+ | range110To120 specVersionA
372
+ , range110To120 specVersionB
373
+ = changesOk " cabal-version" showCabalSpecVersion specVersionA specVersionB
365
374
366
375
| otherwise
367
376
= checkSame " Cannot change the Cabal spec version"
@@ -372,8 +381,7 @@ checkSpecVersionRaw pdA pdB
372
381
373
382
-- nothing interesting changed within the Cabal >=1.10 && <1.21 range
374
383
-- therefore we allow to change the spec version within this interval
375
- range110To120 = (orLaterVersion (mkVersion [1 ,10 ])) `intersectVersionRanges`
376
- (earlierVersion (mkVersion [1 ,21 ]))
384
+ range110To120 v = CabalSpecV1_10 >= v && v <= CabalSpecV1_20
377
385
378
386
checkRevision :: Check [(String , String )]
379
387
checkRevision customFieldsA customFieldsB =
@@ -467,7 +475,7 @@ instance IsDependency VersionRange Dependency where
467
475
depKey (Dependency pkgname _ _) = pkgname
468
476
depKeyShow Proxy = prettyShow''
469
477
depVerRg (Dependency _ vr _) = vr
470
- reconstructDep = \ n vr -> Dependency n vr Set. empty
478
+ reconstructDep = \ n vr -> Dependency n vr mainLibSet
471
479
472
480
depInAddWhitelist (Dependency pn _ _) = pn `elem`
473
481
-- Special case: there are some pretty weird broken packages out there, see
0 commit comments