Skip to content

Commit f5b9dcf

Browse files
committed
Cabal 3.4: copy D.S.Util.CabalRevisions from hackage-cli (#1016)
The module Distribution.Server.Util.CabalRevisions is shared between hackage-server and hackage-cli and has been updated at the latter first.
1 parent 562dd90 commit f5b9dcf

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/Distribution/Server/Util/CabalRevisions.hs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
-- |
1010
-- Module : Distribution.Server.Util.CabalRevisions
1111
-- Copyright : Duncan Coutts et al.
12-
-- License : BSD3
12+
-- SPDX-License-Identifier: BSD-3-Clause
1313
--
1414
-- Maintainer : [email protected]
1515
-- Stability : provisional
@@ -18,12 +18,13 @@
1818
-- Validation and helpers for Cabal revision handling
1919
module Distribution.Server.Util.CabalRevisions
2020
( diffCabalRevisions
21+
, diffCabalRevisions'
2122
, Change(..)
2223
, insertRevisionField
2324
) where
2425

2526
-- NB: This module avoids to import any hackage-server modules
26-
import Distribution.CabalSpecVersion (cabalSpecLatest)
27+
import Distribution.CabalSpecVersion (CabalSpecVersion(..), cabalSpecLatest, showCabalSpecVersion)
2728
import Distribution.Types.Dependency
2829
import Distribution.Types.ExeDependency
2930
import Distribution.Types.PkgconfigDependency
@@ -43,7 +44,6 @@ import Distribution.PackageDescription.Parsec (parseGenericPackageDescription, r
4344
import Distribution.PackageDescription.FieldGrammar (sourceRepoFieldGrammar)
4445
import Distribution.PackageDescription.Check
4546
import Distribution.Parsec (showPWarning, showPError, PWarning (..))
46-
import Distribution.Simple.LocalBuildInfo (showComponentName)
4747
import Distribution.Utils.ShortText
4848
import Text.PrettyPrint as Doc
4949
((<+>), colon, text, Doc, hsep, punctuate)
@@ -54,6 +54,7 @@ import Control.Monad.Except (ExceptT, runExceptT, throwError)
5454
import Control.Monad.Writer (MonadWriter(..), Writer, runWriter)
5555
import Data.Foldable (for_)
5656
import Data.List
57+
((\\), deleteBy, intercalate)
5758
import Data.ByteString.Lazy (ByteString)
5859
import qualified Data.ByteString as BS
5960
import qualified Data.ByteString.Lazy.Char8 as LBS8
@@ -72,8 +73,16 @@ import qualified Control.Monad.Fail as Fail
7273
-- 'String' and performs validations. Returns either a validation
7374
-- error or a list of detected changes.
7475
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
7786

7887
newtype CheckM a = CheckM { unCheckM :: ExceptT String (Writer [Change]) a }
7988
deriving (Functor, Applicative)
@@ -127,15 +136,15 @@ logChange change = CheckM (tell [change])
127136

128137
type Check a = a -> a -> CheckM ()
129138

130-
checkCabalFileRevision :: Check BS.ByteString
131-
checkCabalFileRevision old new = do
139+
checkCabalFileRevision :: Bool -> Check BS.ByteString
140+
checkCabalFileRevision checkXRevision old new = do
132141
(pkg, warns) <- parseCabalFile old
133142
(pkg', warns') <- parseCabalFile new
134143

135144
let pkgid = packageId pkg
136145
filename = prettyShow pkgid ++ ".cabal"
137146

138-
checkGenericPackageDescription pkg pkg'
147+
checkGenericPackageDescription checkXRevision pkg pkg'
139148
checkParserWarnings filename warns warns'
140149
checkPackageChecks pkg pkg'
141150

@@ -170,12 +179,12 @@ checkCabalFileRevision old new = do
170179
[] -> return ()
171180
newchecks -> fail $ unlines (map explanation newchecks)
172181

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
177186

178-
checkPackageDescriptions descrA descrB
187+
checkPackageDescriptions checkXRevision descrA descrB
179188

180189
checkList "Cannot add or remove flags" checkFlag flagsA flagsB
181190

@@ -213,7 +222,7 @@ checkGenericPackageDescription
213222
withComponentName' f condTree = (f, condTree)
214223

215224

216-
checkFlag :: Check Flag
225+
checkFlag :: Check PackageFlag
217226
checkFlag flagOld flagNew = do
218227
-- This check is applied via 'checkList' and for simplicity we
219228
-- disallow renaming/reordering flags (even though reordering
@@ -248,10 +257,10 @@ checkFlag flagOld flagNew = do
248257
changesOk ("description of flag '" ++ fname ++ "'") id
249258
(flagDescription flagOld) (flagDescription flagNew)
250259

251-
checkPackageDescriptions :: Check PackageDescription
252-
checkPackageDescriptions
260+
checkPackageDescriptions :: Bool -> Check PackageDescription
261+
checkPackageDescriptions checkXRevision
253262
pdA@(PackageDescription
254-
{ specVersionRaw = _specVersionRawA
263+
{ specVersion = _specVersionA
255264
, package = packageIdA
256265
, licenseRaw = licenseRawA
257266
, licenseFiles = licenseFilesA
@@ -283,7 +292,7 @@ checkPackageDescriptions
283292
, extraDocFiles = extraDocFilesA
284293
})
285294
pdB@(PackageDescription
286-
{ specVersionRaw = _specVersionRawB
295+
{ specVersion = _specVersionB
287296
, package = packageIdB
288297
, licenseRaw = licenseRawB
289298
, licenseFiles = licenseFilesB
@@ -354,14 +363,14 @@ checkPackageDescriptions
354363
checkSpecVersionRaw pdA pdB
355364
checkSetupBuildInfo setupBuildInfoA setupBuildInfoB
356365

357-
checkRevision customFieldsPDA customFieldsPDB
366+
when checkXRevision $ checkRevision customFieldsPDA customFieldsPDB
358367
checkCuration customFieldsPDA customFieldsPDB
359368

360369
checkSpecVersionRaw :: Check PackageDescription
361370
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
365374

366375
| otherwise
367376
= checkSame "Cannot change the Cabal spec version"
@@ -372,8 +381,7 @@ checkSpecVersionRaw pdA pdB
372381

373382
-- nothing interesting changed within the Cabal >=1.10 && <1.21 range
374383
-- 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
377385

378386
checkRevision :: Check [(String, String)]
379387
checkRevision customFieldsA customFieldsB =
@@ -467,7 +475,7 @@ instance IsDependency VersionRange Dependency where
467475
depKey (Dependency pkgname _ _) = pkgname
468476
depKeyShow Proxy = prettyShow''
469477
depVerRg (Dependency _ vr _) = vr
470-
reconstructDep = \n vr -> Dependency n vr Set.empty
478+
reconstructDep = \n vr -> Dependency n vr mainLibSet
471479

472480
depInAddWhitelist (Dependency pn _ _) = pn `elem`
473481
-- Special case: there are some pretty weird broken packages out there, see

0 commit comments

Comments
 (0)