Skip to content

Commit 6a2b66d

Browse files
committed
Stack.Fetch: lookup with revision
1 parent 6fb4aa6 commit 6a2b66d

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

src/Stack/Fetch.hs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ import qualified Data.ByteString.Lazy as L
4949
import Data.Either (partitionEithers)
5050
import qualified Data.Foldable as F
5151
import Data.Function (fix)
52+
import Data.HashMap.Strict (HashMap)
5253
import qualified Data.HashMap.Strict as HashMap
54+
import Data.HashSet (HashSet)
55+
import qualified Data.HashSet as HashSet
5356
import Data.List (intercalate)
5457
import Data.List.NonEmpty (NonEmpty)
5558
import qualified Data.List.NonEmpty as NE
@@ -89,7 +92,7 @@ data FetchException
8992
| UnpackDirectoryAlreadyExists (Set FilePath)
9093
| CouldNotParsePackageSelectors [String]
9194
| UnknownPackageNames (Set PackageName)
92-
| UnknownPackageIdentifiers (Set PackageIdentifier) String
95+
| UnknownPackageIdentifiers (HashSet PackageIdentifierRevision) String
9396
deriving Typeable
9497
instance Exception FetchException
9598

@@ -117,7 +120,7 @@ instance Show FetchException where
117120
intercalate ", " (map packageNameString $ Set.toList names)
118121
show (UnknownPackageIdentifiers idents suggestions) =
119122
"The following package identifiers were not found in your indices: " ++
120-
intercalate ", " (map packageIdentifierString $ Set.toList idents) ++
123+
intercalate ", " (map packageIdentifierRevisionString $ HashSet.toList idents) ++
121124
(if null suggestions then "" else "\n" ++ suggestions)
122125

123126
-- | Fetch packages into the cache without unpacking
@@ -207,15 +210,15 @@ resolvePackages mSnapshotDef idents0 names0 = do
207210
go = r <$> resolvePackagesAllowMissing mSnapshotDef idents0 names0
208211
r (missingNames, missingIdents, idents)
209212
| not $ Set.null missingNames = Left $ UnknownPackageNames missingNames
210-
| not $ Set.null missingIdents = Left $ UnknownPackageIdentifiers missingIdents ""
213+
| not $ HashSet.null missingIdents = Left $ UnknownPackageIdentifiers missingIdents ""
211214
| otherwise = Right idents
212215

213216
resolvePackagesAllowMissing
214217
:: (StackMiniM env m, HasConfig env)
215218
=> Maybe SnapshotDef -- ^ when looking up by name, take from this build plan
216219
-> [PackageIdentifierRevision]
217220
-> Set PackageName
218-
-> m (Set PackageName, Set PackageIdentifier, [ResolvedPackage])
221+
-> m (Set PackageName, HashSet PackageIdentifierRevision, [ResolvedPackage])
219222
resolvePackagesAllowMissing mSnapshotDef idents0 names0 = do
220223
(res1, res2, resolved) <- inner
221224
if any (isJust . snd) resolved
@@ -266,11 +269,11 @@ resolvePackagesAllowMissing mSnapshotDef idents0 names0 = do
266269
(Set.toList names0)
267270
let (missingIdents, resolved) = partitionEithers $ map (goIdent caches shaCaches)
268271
$ idents0 <> idents1
269-
return (Set.fromList missingNames, Set.fromList missingIdents, resolved)
272+
return (Set.fromList missingNames, HashSet.fromList missingIdents, resolved)
270273

271-
goIdent caches shaCaches (PackageIdentifierRevision ident mcfi) =
274+
goIdent caches shaCaches identRev@(PackageIdentifierRevision ident mcfi) =
272275
case Map.lookup ident caches of
273-
Nothing -> Left ident
276+
Nothing -> Left identRev
274277
Just (index, cache) ->
275278
let (index', cache', missingCFI) =
276279
case mcfi of
@@ -333,7 +336,7 @@ withCabalFiles name pkgs f = do
333336
-- package indices.
334337
withCabalLoader
335338
:: (StackMiniM env m, HasConfig env, MonadBaseUnlift IO m)
336-
=> ((PackageIdentifier -> IO ByteString) -> m a)
339+
=> ((PackageIdentifierRevision -> IO ByteString) -> m a)
337340
-> m a
338341
withCabalLoader inner = do
339342
env <- ask
@@ -350,11 +353,11 @@ withCabalLoader inner = do
350353
unlift <- askRunBase
351354

352355
-- TODO in the future, keep all of the necessary @Handle@s open
353-
let doLookup :: PackageIdentifier
356+
let doLookup :: PackageIdentifierRevision
354357
-> IO ByteString
355358
doLookup ident = do
356-
(caches, _gitSHACaches) <- loadCaches
357-
eres <- unlift $ lookupPackageIdentifierExact ident env caches
359+
(caches, cachesRev) <- loadCaches
360+
eres <- unlift $ lookupPackageIdentifierExact ident env caches cachesRev
358361
case eres of
359362
Just bs -> return bs
360363
-- Update the cache and try again
@@ -374,7 +377,7 @@ withCabalLoader inner = do
374377
runInBase $ do
375378
$logInfo $ T.concat
376379
[ "Didn't see "
377-
, T.pack $ packageIdentifierString ident
380+
, T.pack $ packageIdentifierRevisionString ident
378381
, " in your package indices.\n"
379382
, "Updating and trying again."
380383
]
@@ -384,17 +387,24 @@ withCabalLoader inner = do
384387
return (False, doLookup ident)
385388
else return (toUpdate,
386389
throwM $ UnknownPackageIdentifiers
387-
(Set.singleton ident) (T.unpack suggestions))
390+
(HashSet.singleton ident) (T.unpack suggestions))
388391
inner doLookup
389392

390393
lookupPackageIdentifierExact
391394
:: (StackMiniM env m, HasConfig env)
392-
=> PackageIdentifier
395+
=> PackageIdentifierRevision
393396
-> env
394397
-> PackageCaches
398+
-> HashMap CabalHash (PackageIndex, OffsetSize)
395399
-> m (Maybe ByteString)
396-
lookupPackageIdentifierExact ident env caches =
397-
case Map.lookup ident caches of
400+
lookupPackageIdentifierExact (PackageIdentifierRevision ident mcfi) env caches cachesRev = do
401+
let mpair =
402+
case mcfi of
403+
Nothing -> Map.lookup ident caches
404+
Just cfi -> fmap
405+
(\(index, size) -> (index, PackageCache size Nothing))
406+
(HashMap.lookup (cfiHash cfi) cachesRev)
407+
case mpair of
398408
Nothing -> return Nothing
399409
Just (index, cache) -> do
400410
[bs] <- flip runReaderT env
@@ -411,10 +421,10 @@ lookupPackageIdentifierExact ident env caches =
411421
-- with the same name and the same two first version number components found
412422
-- in the caches.
413423
fuzzyLookupCandidates
414-
:: PackageIdentifier
424+
:: PackageIdentifierRevision
415425
-> PackageCaches
416426
-> Maybe (NonEmpty PackageIdentifier)
417-
fuzzyLookupCandidates (PackageIdentifier name ver) caches =
427+
fuzzyLookupCandidates (PackageIdentifierRevision (PackageIdentifier name ver) _rev) caches =
418428
let (_, zero, bigger) = Map.splitLookup zeroIdent caches
419429
zeroIdent = PackageIdentifier name $(mkVersion "0.0")
420430
sameName (PackageIdentifier n _) = n == name
@@ -426,10 +436,10 @@ fuzzyLookupCandidates (PackageIdentifier name ver) caches =
426436
-- package caches. This should be called before giving up, i.e. when
427437
-- 'fuzzyLookupCandidates' cannot return anything.
428438
typoCorrectionCandidates
429-
:: PackageIdentifier
439+
:: PackageIdentifierRevision
430440
-> PackageCaches
431441
-> Maybe (NonEmpty T.Text)
432-
typoCorrectionCandidates ident =
442+
typoCorrectionCandidates (PackageIdentifierRevision ident _mcfi) =
433443
let getName = packageNameText . packageIdentifierName
434444
name = getName ident
435445
in NE.nonEmpty

0 commit comments

Comments
 (0)