1module AllCabalHashes where
3import Control.Lens hiding ((<.>))
5import Data.ByteString as BS
6import Data.ByteString.Lazy as BSL
9import Distribution.Nixpkgs.Hashes as NH
10import Distribution.Package
11import Distribution.PackageDescription
12import Distribution.PackageDescription.Parsec
13import Distribution.Text ( display )
15import Git.Libgit2 as Libgit2
16import OpenSSL.Digest as SSL ( digest, digestByName )
21type SHA256Hash = String
24 { _mHashes :: !(Map String String)
25 , _mLocations :: ![String]
31instance FromJSON Meta where
32 parseJSON (Object v) = Meta
33 <$> v .: "package-hashes"
34 <*> v .: "package-locations"
35 <*> v .: "package-size"
36 parseJSON o = fail ("invalid Cabal metadata: " ++ show o)
38readPackageByHash :: FilePath -> SHA1Hash -> IO (GenericPackageDescription, SHA256Hash)
39readPackageByHash repoDir sha1Hash = do
40 let repoOpts = defaultRepositoryOptions { repoPath = repoDir }
41 repo <- openLgRepository repoOpts
42 buf <- runLgRepository repo $ do
43 BlobObj (Blob _ contents) <- lookupObject =<< parseOid sha1Hash
45 BlobString bs -> return bs
46 BlobStringLazy bsl -> return $ BSL.toStrict bsl
47 _ -> fail $ "Git SHA1 " ++ show sha1Hash ++ ": expected single Blob"
48 cabal <- let (_, res) = runParseResult (parseGenericPackageDescription buf)
49 in case res of Right a -> return a
50 Left (_, err) -> fail ("Git SHA1 " ++ show sha1Hash ++ ": " ++ show err)
52 hash = printSHA256 (digest (digestByName "sha256") buf)
53 pkg = setCabalFileHash hash cabal
56readPackageByName :: FilePath -> PackageIdentifier -> IO (GenericPackageDescription, SHA256Hash)
57readPackageByName repoDir (PackageIdentifier name version) = do
58 let cabalFile = repoDir </> unPackageName name </> display version </> unPackageName name <.> "cabal"
59 buf <- BS.readFile cabalFile
60 cabal <- let (_, res) = runParseResult (parseGenericPackageDescription buf)
61 in case res of Right a -> return a
62 Left (_, err) -> fail (cabalFile ++ ": " ++ show err)
64 hash = NH.printSHA256 (SSL.digest (SSL.digestByName "sha256") buf)
65 pkg = setCabalFileHash hash cabal
68setCabalFileHash :: SHA256Hash -> GenericPackageDescription -> GenericPackageDescription
69setCabalFileHash hash cabal = cabal
70 { packageDescription = (packageDescription cabal)
71 { customFieldsPD = ("X-Cabal-File-Hash", hash) : customFieldsPD (packageDescription cabal)
75readPackageMeta :: FilePath -> PackageIdentifier -> IO Meta
76readPackageMeta dirPrefix (PackageIdentifier name version) = do
77 let metaFile = dirPrefix </> unPackageName name </> display version </> unPackageName name <.> "json"
78 buf <- BS.readFile metaFile
79 case eitherDecodeStrict buf of
80 Left msg -> fail (metaFile ++ ": " ++ msg)
81 Right x -> return $ over (mHashes . ix "SHA256") (printSHA256 . packHex) x
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list