Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 189 lines (168 sloc) 6.561 kB
e35e41e @kolmodin Make merging respect categories
kolmodin authored
1 module Diff
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored
2 ( runDiff
3 , DiffMode(..)
e35e41e @kolmodin Make merging respect categories
kolmodin authored
4 ) where
03de0f6 Adding diff command to HackPort
der_eq@freenet.de authored
5
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
6 import Control.Monad ( mplus )
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
7 import Control.Exception ( assert )
8 import Data.Maybe ( fromJust, listToMaybe )
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
9 import Data.List ( sortBy, groupBy )
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
10 import Data.Ord ( comparing )
5078263 @kolmodin Make diffing respect categories
kolmodin authored
11
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
12 import qualified Portage.Overlay as Portage
13 import qualified Portage.Cabal as Portage
14 import qualified Portage.PackageId as Portage
15
16 import qualified Data.Version as Cabal
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored
17
18 -- cabal
19 import Distribution.Verbosity
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored
20 import Distribution.Text(display)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
21 import qualified Distribution.Package as Cabal
60d04de @kolmodin adapt to cabal-1.8 and cabal-install-0.7.5 (HEADs)
kolmodin authored
22 import qualified Distribution.Client.PackageIndex as Index
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
23 import Distribution.Simple.Utils (equating)
24
25 -- cabal-install
ac5deac @trofi port to (bundled) Cabal-1.12
trofi authored
26 import qualified Distribution.Client.IndexUtils as Index (getSourcePackages)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
27 import qualified Distribution.Client.Types as Cabal
28 import Distribution.Client.Utils (mergeBy, MergeResult(..))
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored
29
30 data DiffMode
31 = ShowAll
32 | ShowMissing
33 | ShowAdditions
34 | ShowNewer
35 | ShowCommon
eef57f0 @kolmodin Allow to diff by package name
kolmodin authored
36 | ShowPackages [String]
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored
37 deriving Eq
03de0f6 Adding diff command to HackPort
der_eq@freenet.de authored
38
3eb19fc New diff style
der_eq@freenet.de authored
39
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
40 {-
41 type DiffState a = MergeResult a a
5078263 @kolmodin Make diffing respect categories
kolmodin authored
42 tabs :: String -> String
43 tabs str = let len = length str in str++(if len < 3*8
44 then replicate (3*8-len) ' '
45 else "")
46
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
47
48 -- TODO: is the new showPackageCompareInfo showing the packages in the same
49 -- way as showDiffState did?
50
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
51 showDiffState :: Cabal.PackageName -> DiffState Portage.Version -> String
52 showDiffState pkg st = (tabs (display pkg)) ++ " [" ++ (case st of
53 InBoth x y -> display x ++ (case compare x y of
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored
54 EQ -> "="
55 GT -> ">"
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored
56 LT -> "<") ++ display y
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
57 OnlyInLeft x -> display x ++ ">none"
58 OnlyInRight y -> "none<" ++ display y) ++ "]"
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
59 -}
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
60
61 runDiff :: Verbosity -> FilePath -> DiffMode -> Cabal.Repo -> IO ()
62 runDiff verbosity overlayPath dm repo = do
63 -- get package list from hackage
ac5deac @trofi port to (bundled) Cabal-1.12
trofi authored
64 pkgDB <- Index.getSourcePackages verbosity [ repo ]
65 let (Cabal.SourcePackageDb hackageIndex _) = pkgDB
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
66
67 -- get package list from the overlay
68 overlay0 <- (Portage.loadLazy overlayPath)
69 let overlayIndex = Portage.fromOverlay (Portage.reduceOverlay overlay0)
70
71 let (subHackage, subOverlay)
72 = case dm of
73 ShowPackages pkgs ->
ac5deac @trofi port to (bundled) Cabal-1.12
trofi authored
74 (concatMap (concatMap snd . Index.searchByNameSubstring hackageIndex) pkgs
75 ,concatMap (concatMap snd . Index.searchByNameSubstring overlayIndex) pkgs)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
76 _ ->
60d04de @kolmodin adapt to cabal-1.8 and cabal-install-0.7.5 (HEADs)
kolmodin authored
77 (Index.allPackages hackageIndex
78 ,Index.allPackages overlayIndex)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
79 diff subHackage subOverlay dm
80
81 data PackageCompareInfo = PackageCompareInfo {
82 name :: Cabal.PackageName,
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
83 -- hackageVersions :: [ Cabal.Version ],
84 -- overlayVersions :: [ Cabal.Version ]
85 hackageVersion :: Maybe Cabal.Version,
86 overlayVersion :: Maybe Cabal.Version
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
87 } deriving Show
88
89 showPackageCompareInfo :: PackageCompareInfo -> String
90 showPackageCompareInfo pkgCmpInfo =
91 display (name pkgCmpInfo) ++ " ["
92 ++ hackageS ++ sign ++ overlayS ++ "]"
93 where
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
94 overlay = overlayVersion pkgCmpInfo
95 hackage = hackageVersion pkgCmpInfo
96 hackageS = maybe "none" display hackage
97 overlayS = maybe "none" display overlay
98 sign = case compare hackage overlay of
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
99 EQ -> "="
100 GT -> ">"
101 LT -> "<"
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
102
ac5deac @trofi port to (bundled) Cabal-1.12
trofi authored
103 diff :: [Cabal.SourcePackage]
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
104 -> [Portage.ExistingEbuild]
105 -> DiffMode
106 -> IO ()
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
107 diff hackage overlay dm = do
108 mapM_ (putStrLn . showPackageCompareInfo) pkgCmpInfos
109 where
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
110 merged = mergePackages (map (Portage.normalizeCabalPackageId . Cabal.packageId) hackage)
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
111 (map Portage.ebuildCabalId overlay)
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
112 pkgCmpInfos = filter pkgFilter (map (uncurry mergePackageInfo) merged)
113 pkgFilter :: PackageCompareInfo -> Bool
114 pkgFilter pkgCmpInfo =
115 let om = overlayVersion pkgCmpInfo
116 hm = hackageVersion pkgCmpInfo
117 st = case (om,hm) of
118 (Just ov, Just hv) -> InBoth ov hv
119 (Nothing, Just hv) -> OnlyInRight hv
120 (Just ov, Nothing) -> OnlyInLeft ov
ef321f4 @kolmodin Cleanup warnings in Diff.hs
kolmodin authored
121 _ -> error "impossible"
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
122 in
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
123 case dm of
124 ShowAll -> True
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
125 ShowPackages _ -> True -- already filtered
126 ShowNewer -> case st of
127 InBoth o h -> h>o
128 _ -> False
129 ShowMissing -> case st of
3b9e456 @kolmodin Fix 'hackport diff additions' and 'hackport diff common'
kolmodin authored
130 OnlyInLeft _ -> False
131 InBoth x y -> x < y
132 OnlyInRight _ -> True
133 ShowAdditions -> case st of
134 OnlyInLeft _ -> True
135 InBoth x y -> x > y
136 OnlyInRight _ -> False
137 ShowCommon -> case st of
138 OnlyInLeft _ -> False
139 InBoth x y -> x == y
140 OnlyInRight _ -> False
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
141
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
142 -- | We get the 'PackageCompareInfo' by combining the info for the overlay
143 -- and hackage versions of a package.
144 --
145 -- * We're building info about a various versions of a single named package so
146 -- the input package info records are all supposed to refer to the same
147 -- package name.
148 --
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
149 mergePackageInfo :: [Cabal.PackageIdentifier]
150 -> [Cabal.PackageIdentifier]
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
151 -> PackageCompareInfo
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
152 mergePackageInfo hackage overlay =
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
153 assert (length overlay + length hackage > 0) $
154 PackageCompareInfo {
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
155 name = combine Cabal.pkgName latestHackage
156 Cabal.pkgName latestOverlay,
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
157 -- hackageVersions = map Cabal.pkgVersion hackage,
158 -- overlayVersions = map Cabal.pkgVersion overlay
159 hackageVersion = fmap Cabal.pkgVersion latestHackage,
160 overlayVersion = fmap Cabal.pkgVersion latestOverlay
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
161 }
162 where
163 combine f x g y = fromJust (fmap f x `mplus` fmap g y)
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
164 latestHackage = latestOf hackage
165 latestOverlay = latestOf overlay
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
166 latestOf :: [Cabal.PackageIdentifier] -> Maybe Cabal.PackageIdentifier
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored
167 latestOf = listToMaybe . reverse . sortBy (comparing Cabal.pkgVersion)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
168
169 -- | Rearrange installed and available packages into groups referring to the
170 -- same package by name. In the result pairs, the lists are guaranteed to not
171 -- both be empty.
172 --
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
173 mergePackages :: [Cabal.PackageIdentifier] -> [Cabal.PackageIdentifier]
174 -> [([Cabal.PackageIdentifier], [Cabal.PackageIdentifier])]
175 mergePackages hackage overlay =
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
176 map collect
177 $ mergeBy (\i a -> fst i `compare` fst a)
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored
178 (groupOn Cabal.pkgName hackage)
179 (groupOn Cabal.pkgName overlay)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
180 where
181 collect (OnlyInLeft (_,is) ) = (is, [])
182 collect ( InBoth (_,is) (_,as)) = (is, as)
183 collect (OnlyInRight (_,as)) = ([], as)
5078263 @kolmodin Make diffing respect categories
kolmodin authored
184
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored
185 groupOn :: Ord key => (a -> key) -> [a] -> [(key,[a])]
186 groupOn key = map (\xs -> (key (head xs), xs))
187 . groupBy (equating key)
188 . sortBy (comparing key)
Something went wrong with that request. Please try again.