Skip to content

Commit 1f5b191

Browse files
mergify[bot]jaspervdjulysses4ever
authored
Backport #10316: Remove hashable dependency (#10536)
* Remove hashable dependency (#10316) Co-authored-by: Artem Pelenitsyn <a.pelenitsyn@gmail.com> (cherry picked from commit 5aaf8ea) # Conflicts: # cabal.bootstrap.project # cabal.release.project * fixup! conflicts --------- Co-authored-by: Jasper Van der Jeugt <jaspervdj@gmail.com> Co-authored-by: Artem Pelenitsyn <a.pelenitsyn@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 3428eb8 commit 1f5b191

File tree

10 files changed

+133
-70
lines changed

10 files changed

+133
-70
lines changed

cabal-install/cabal-install.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ library
231231
edit-distance >= 0.2.2 && < 0.3,
232232
exceptions >= 0.10.4 && < 0.11,
233233
filepath >= 1.4.0.0 && < 1.6,
234-
hashable >= 1.0 && < 1.6,
235234
HTTP >= 4000.1.5 && < 4000.5,
236235
mtl >= 2.0 && < 2.4,
237236
network-uri >= 2.6.0.2 && < 2.7,
@@ -431,7 +430,6 @@ test-suite long-tests
431430
containers,
432431
directory,
433432
filepath,
434-
hashable,
435433
mtl,
436434
network-uri >= 2.6.2.0 && <2.7,
437435
random,

cabal-install/src/Distribution/Client/FileMonitor.hs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import Prelude ()
3535

3636
import Data.Binary.Get (runGetOrFail)
3737
import qualified Data.ByteString.Lazy as BS
38-
import qualified Data.Hashable as Hashable
3938
import qualified Data.Map.Strict as Map
4039

4140
import Control.Exception
@@ -51,6 +50,7 @@ import qualified Control.Monad.State as State
5150
import Control.Monad.Trans (MonadIO, liftIO)
5251

5352
import Distribution.Client.Glob
53+
import Distribution.Client.HashValue
5454
import Distribution.Client.Utils (MergeResult (..), mergeBy)
5555
import Distribution.Compat.Time
5656
import Distribution.Simple.FileMonitor.Types
@@ -83,8 +83,6 @@ data MonitorStateFileSet
8383
instance Binary MonitorStateFileSet
8484
instance Structured MonitorStateFileSet
8585

86-
type Hash = Int
87-
8886
-- | The state necessary to determine whether a monitored file has changed.
8987
--
9088
-- This covers all the cases of 'MonitorFilePath' except for globs which is
@@ -107,7 +105,7 @@ data MonitorStateFileStatus
107105
| -- | cached file mtime
108106
MonitorStateFileModTime !ModTime
109107
| -- | cached mtime and content hash
110-
MonitorStateFileHashed !ModTime !Hash
108+
MonitorStateFileHashed !ModTime !HashValue
111109
| MonitorStateDirExists
112110
| -- | cached dir mtime
113111
MonitorStateDirModTime !ModTime
@@ -961,21 +959,21 @@ buildMonitorStateGlobRel
961959
-- updating a file monitor the set of files is the same or largely the same so
962960
-- we can grab the previously known content hashes with their corresponding
963961
-- mtimes.
964-
type FileHashCache = Map FilePath (ModTime, Hash)
962+
type FileHashCache = Map FilePath (ModTime, HashValue)
965963

966964
-- | We declare it a cache hit if the mtime of a file is the same as before.
967-
lookupFileHashCache :: FileHashCache -> FilePath -> ModTime -> Maybe Hash
965+
lookupFileHashCache :: FileHashCache -> FilePath -> ModTime -> Maybe HashValue
968966
lookupFileHashCache hashcache file mtime = do
969967
(mtime', hash) <- Map.lookup file hashcache
970968
guard (mtime' == mtime)
971969
return hash
972970

973971
-- | Either get it from the cache or go read the file
974-
getFileHash :: FileHashCache -> FilePath -> FilePath -> ModTime -> IO Hash
972+
getFileHash :: FileHashCache -> FilePath -> FilePath -> ModTime -> IO HashValue
975973
getFileHash hashcache relfile absfile mtime =
976974
case lookupFileHashCache hashcache relfile mtime of
977975
Just hash -> return hash
978-
Nothing -> readFileHash absfile
976+
Nothing -> readFileHashValue absfile
979977

980978
-- | Build a 'FileHashCache' from the previous 'MonitorStateFileSet'. While
981979
-- in principle we could preserve the structure of the previous state, given
@@ -998,7 +996,7 @@ readCacheFileHashes monitor =
998996
collectAllFileHashes singlePaths
999997
`Map.union` collectAllGlobHashes globPaths
1000998

1001-
collectAllFileHashes :: [MonitorStateFile] -> Map FilePath (ModTime, Hash)
999+
collectAllFileHashes :: [MonitorStateFile] -> Map FilePath (ModTime, HashValue)
10021000
collectAllFileHashes singlePaths =
10031001
Map.fromList
10041002
[ (fpath, (mtime, hash))
@@ -1010,15 +1008,15 @@ readCacheFileHashes monitor =
10101008
singlePaths
10111009
]
10121010

1013-
collectAllGlobHashes :: [MonitorStateGlob] -> Map FilePath (ModTime, Hash)
1011+
collectAllGlobHashes :: [MonitorStateGlob] -> Map FilePath (ModTime, HashValue)
10141012
collectAllGlobHashes globPaths =
10151013
Map.fromList
10161014
[ (fpath, (mtime, hash))
10171015
| MonitorStateGlob _ _ _ gstate <- globPaths
10181016
, (fpath, (mtime, hash)) <- collectGlobHashes "" gstate
10191017
]
10201018

1021-
collectGlobHashes :: FilePath -> MonitorStateGlobRel -> [(FilePath, (ModTime, Hash))]
1019+
collectGlobHashes :: FilePath -> MonitorStateGlobRel -> [(FilePath, (ModTime, HashValue))]
10221020
collectGlobHashes dir (MonitorStateGlobDirs _ _ _ entries) =
10231021
[ res
10241022
| (subdir, fstate) <- entries
@@ -1043,13 +1041,13 @@ probeFileModificationTime root file mtime = do
10431041
unless unchanged (somethingChanged file)
10441042

10451043
-- | Within the @root@ directory, check if @file@ has its 'ModTime' and
1046-
-- 'Hash' is the same as @mtime@ and @hash@, short-circuiting if it is
1044+
-- 'HashValue' is the same as @mtime@ and @hash@, short-circuiting if it is
10471045
-- different.
10481046
probeFileModificationTimeAndHash
10491047
:: FilePath
10501048
-> FilePath
10511049
-> ModTime
1052-
-> Hash
1050+
-> HashValue
10531051
-> ChangedM ()
10541052
probeFileModificationTimeAndHash root file mtime hash = do
10551053
unchanged <-
@@ -1092,28 +1090,22 @@ checkModificationTimeUnchanged root file mtime =
10921090
return (mtime == mtime')
10931091

10941092
-- | Returns @True@ if, inside the @root@ directory, @file@ has the
1095-
-- same 'ModTime' and 'Hash' as @mtime and @chash@.
1093+
-- same 'ModTime' and 'HashValue' as @mtime and @chash@.
10961094
checkFileModificationTimeAndHashUnchanged
10971095
:: FilePath
10981096
-> FilePath
10991097
-> ModTime
1100-
-> Hash
1098+
-> HashValue
11011099
-> IO Bool
11021100
checkFileModificationTimeAndHashUnchanged root file mtime chash =
11031101
handleIOException False $ do
11041102
mtime' <- getModTime (root </> file)
11051103
if mtime == mtime'
11061104
then return True
11071105
else do
1108-
chash' <- readFileHash (root </> file)
1106+
chash' <- readFileHashValue (root </> file)
11091107
return (chash == chash')
11101108

1111-
-- | Read a non-cryptographic hash of a @file@.
1112-
readFileHash :: FilePath -> IO Hash
1113-
readFileHash file =
1114-
withBinaryFile file ReadMode $ \hnd ->
1115-
evaluate . Hashable.hash =<< BS.hGetContents hnd
1116-
11171109
-- | Given a directory @dir@, return @Nothing@ if its 'ModTime'
11181110
-- is the same as @mtime@, and the new 'ModTime' if it is not.
11191111
checkDirectoryModificationTime :: FilePath -> ModTime -> IO (Maybe ModTime)

cabal-install/src/Distribution/Client/ProjectConfig.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import Distribution.Client.GlobalFlags
9696
( RepoContext (..)
9797
, withRepoContext'
9898
)
99+
import Distribution.Client.HashValue
99100
import Distribution.Client.HttpUtils
100101
( HttpTransport
101102
, configureTransport
@@ -185,6 +186,10 @@ import Distribution.Types.PackageVersionConstraint
185186
import Distribution.Types.SourceRepo
186187
( RepoType (..)
187188
)
189+
import Distribution.Utils.Generic
190+
( toUTF8BS
191+
, toUTF8LBS
192+
)
188193
import Distribution.Utils.NubList
189194
( fromNubList
190195
)
@@ -203,11 +208,9 @@ import Control.Exception (handle)
203208
import Control.Monad.Trans (liftIO)
204209
import qualified Data.ByteString as BS
205210
import qualified Data.ByteString.Lazy as LBS
206-
import qualified Data.Hashable as Hashable
207211
import qualified Data.List.NonEmpty as NE
208212
import qualified Data.Map as Map
209213
import qualified Data.Set as Set
210-
import Numeric (showHex)
211214

212215
import Network.URI
213216
( URI (..)
@@ -1655,7 +1658,7 @@ localFileNameForRemoteTarball :: URI -> FilePath
16551658
localFileNameForRemoteTarball uri =
16561659
mangleName uri
16571660
++ "-"
1658-
++ showHex locationHash ""
1661+
++ showHashValue locationHash
16591662
where
16601663
mangleName =
16611664
truncateString 10
@@ -1665,15 +1668,15 @@ localFileNameForRemoteTarball uri =
16651668
. dropTrailingPathSeparator
16661669
. uriPath
16671670

1668-
locationHash :: Word
1669-
locationHash = fromIntegral (Hashable.hash (uriToString id uri ""))
1671+
locationHash :: HashValue
1672+
locationHash = hashValue (toUTF8LBS (uriToString id uri ""))
16701673

16711674
-- | The name to use for a local file or dir for a remote 'SourceRepo'.
16721675
-- This is deterministic based on the source repo identity details, and
16731676
-- intended to produce non-clashing file names for different repos.
16741677
localFileNameForRemoteRepo :: SourceRepoList -> FilePath
16751678
localFileNameForRemoteRepo SourceRepositoryPackage{srpType, srpLocation} =
1676-
mangleName srpLocation ++ "-" ++ showHex locationHash ""
1679+
mangleName srpLocation ++ "-" ++ showHashValue locationHash
16771680
where
16781681
mangleName =
16791682
truncateString 10
@@ -1682,9 +1685,10 @@ localFileNameForRemoteRepo SourceRepositoryPackage{srpType, srpLocation} =
16821685
. dropTrailingPathSeparator
16831686

16841687
-- just the parts that make up the "identity" of the repo
1685-
locationHash :: Word
1688+
locationHash :: HashValue
16861689
locationHash =
1687-
fromIntegral (Hashable.hash (show srpType, srpLocation))
1690+
hashValue $
1691+
LBS.fromChunks [toUTF8BS srpLocation, toUTF8BS (show srpType)]
16881692

16891693
-- | Truncate a string, with a visual indication that it is truncated.
16901694
truncateString :: Int -> String -> String

cabal-install/tests/UnitTests/Distribution/Client/FileMonitor.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tests :: Int -> [TestTree]
3232
tests mtimeChange =
3333
[ testGroup
3434
"Structured hashes"
35-
[ testCase "MonitorStateFile" $ structureHash (Proxy :: Proxy MonitorStateFile) @?= Fingerprint 0xe4108804c34962f6 0x06e94f8fc9e48e13
35+
[ testCase "MonitorStateFile" $ structureHash (Proxy :: Proxy MonitorStateFile) @?= Fingerprint 0xe1339b9dcfdfe19d 0x9135a5f30da7ca82
3636
, testCase "MonitorStateGlob" $ structureHash (Proxy :: Proxy MonitorStateGlob) @?= Fingerprint fingerprintStateGlob1 fingerprintStateGlob2
3737
, testCase "MonitorStateFileSet" $ structureHash (Proxy :: Proxy MonitorStateFileSet) @?= Fingerprint fingerprintStateFileSet1 fingerprintStateFileSet2
3838
]
@@ -88,10 +88,10 @@ tests mtimeChange =
8888
Windows -> expectFailBecause msg
8989
_ -> id
9090
fingerprintStateGlob1, fingerprintStateGlob2, fingerprintStateFileSet1, fingerprintStateFileSet2 :: Word64
91-
fingerprintStateGlob1 = 0x8d6292a27f48ab78
92-
fingerprintStateGlob2 = 0xa69393cf17cb6c71
93-
fingerprintStateFileSet1 = 0x441fcb5eaf403013
94-
fingerprintStateFileSet2 = 0x129db82bba47f56f
91+
fingerprintStateGlob1 = 0x1f9edda22b7e8de6
92+
fingerprintStateGlob2 = 0xda1d085c9fc6f5db
93+
fingerprintStateFileSet1 = 0x00ac4a0df546905d
94+
fingerprintStateFileSet2 = 0x5b2b2df018b1fa83
9595

9696
-- Check the file system behaves the way we expect it to
9797

cabal-install/tests/UnitTests/Distribution/Solver/Modular/QuickCheck.hs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Prelude ()
1010

1111
import Control.Arrow ((&&&))
1212
import Data.Either (lefts)
13-
import Data.Hashable (Hashable (..))
1413
import Data.List (groupBy, isInfixOf)
1514

1615
import Text.Show.Pretty (parseValue, valToStr)
@@ -20,7 +19,7 @@ import Test.QuickCheck.Instances.Cabal ()
2019
import Test.Tasty (TestTree)
2120

2221
import Distribution.Types.Flag (FlagName)
23-
import Distribution.Utils.ShortText (ShortText)
22+
import Distribution.Utils.ShortText (ShortText, fromShortText)
2423

2524
import Distribution.Client.Setup (defaultMaxBackjumps)
2625

@@ -47,7 +46,8 @@ import Distribution.Version
4746

4847
import UnitTests.Distribution.Solver.Modular.DSL
4948
import UnitTests.Distribution.Solver.Modular.QuickCheck.Utils
50-
( testPropertyWithSeed
49+
( ArbitraryOrd (..)
50+
, testPropertyWithSeed
5151
)
5252

5353
tests :: [TestTree]
@@ -223,6 +223,9 @@ newtype VarOrdering = VarOrdering
223223
{ unVarOrdering :: Variable P.QPN -> Variable P.QPN -> Ordering
224224
}
225225

226+
instance Arbitrary VarOrdering where
227+
arbitrary = VarOrdering <$> arbitraryCompare
228+
226229
solve
227230
:: EnableBackjumping
228231
-> FineGrainedConflicts
@@ -618,22 +621,18 @@ instance Arbitrary OptionalStanza where
618621
shrink BenchStanzas = [TestStanzas]
619622
shrink TestStanzas = []
620623

621-
-- Randomly sorts solver variables using 'hash'.
622-
-- TODO: Sorting goals with this function is very slow.
623-
instance Arbitrary VarOrdering where
624-
arbitrary = do
625-
f <- arbitrary :: Gen (Int -> Int)
626-
return $ VarOrdering (comparing (f . hash))
627-
628-
instance Hashable pn => Hashable (Variable pn)
629-
instance Hashable a => Hashable (P.Qualified a)
630-
instance Hashable P.PackagePath
631-
instance Hashable P.Qualifier
632-
instance Hashable P.Namespace
633-
instance Hashable OptionalStanza
634-
instance Hashable FlagName
635-
instance Hashable PackageName
636-
instance Hashable ShortText
624+
instance ArbitraryOrd pn => ArbitraryOrd (Variable pn)
625+
instance ArbitraryOrd a => ArbitraryOrd (P.Qualified a)
626+
instance ArbitraryOrd P.PackagePath
627+
instance ArbitraryOrd P.Qualifier
628+
instance ArbitraryOrd P.Namespace
629+
instance ArbitraryOrd OptionalStanza
630+
instance ArbitraryOrd FlagName
631+
instance ArbitraryOrd PackageName
632+
instance ArbitraryOrd ShortText where
633+
arbitraryCompare = do
634+
strc <- arbitraryCompare
635+
pure $ \l r -> strc (fromShortText l) (fromShortText r)
637636

638637
deriving instance Generic (Variable pn)
639638
deriving instance Generic (P.Qualified a)

0 commit comments

Comments
 (0)