Skip to content

Commit a47b0e3

Browse files
committed
4267 Add --exact flag to the list command
This PR adds `--exact` flag to the cabal list command which enables exact match on the package name while searching.
1 parent cf25742 commit a47b0e3

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

Cabal/Distribution/Simple/Program/HcPkg.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ listInvocation hpi _verbosity packagedb =
473473
progInvokeOutputEncoding = IOEncodingUTF8
474474
}
475475
where
476-
args = ["list", "--simple-output", packageDbOpts hpi packagedb]
476+
args = ["list", "--simple-output", "--exact", packageDbOpts hpi packagedb]
477477
++ verbosityOpts hpi silent
478478
-- We use verbosity level 'silent' because it is important that we
479479
-- do not contaminate the output with info/debug messages.

cabal-install/Distribution/Client/List.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Distribution.Client.List (
1414
) where
1515

1616
import Distribution.Package
17-
( PackageName, Package(..), packageName
17+
( PackageName, Package(..), packageName, unPackageName
1818
, packageVersion, UnitId )
1919
import Distribution.Types.Dependency
2020
import Distribution.Types.UnqualComponentName
@@ -106,11 +106,11 @@ getPkgList verbosity packageDBs repoCtxt comp progdb listFlags pats = do
106106
( PackageIndex.allPackages sourcePkgIndex)
107107

108108
-- gather info for packages matching search term
109-
| otherwise = pkgsInfoMatching
109+
| otherwise = pkgsInfoMatching exactMatch
110110

111111
pkgsInfoMatching ::
112-
[(PackageName, [Installed.InstalledPackageInfo], [UnresolvedSourcePackage])]
113-
pkgsInfoMatching =
112+
Bool -> [(PackageName, [Installed.InstalledPackageInfo], [UnresolvedSourcePackage])]
113+
pkgsInfoMatching exact =
114114
let matchingInstalled = matchingPackages
115115
InstalledPackageIndex.searchByNameSubstring
116116
installedPkgIndex
@@ -119,7 +119,10 @@ getPkgList verbosity packageDBs repoCtxt comp progdb listFlags pats = do
119119
concatMap snd
120120
(PackageIndex.searchByNameSubstring idx n))
121121
sourcePkgIndex
122-
in mergePackages matchingInstalled matchingSource
122+
mergedPackages = mergePackages matchingInstalled matchingSource
123+
in if exact
124+
then filterPackages mergedPackages
125+
else mergedPackages
123126

124127
matches :: [PackageDisplayInfo]
125128
matches = [ mergePackageInfo pref
@@ -131,6 +134,13 @@ getPkgList verbosity packageDBs repoCtxt comp progdb listFlags pats = do
131134
return matches
132135
where
133136
onlyInstalled = fromFlag (listInstalled listFlags)
137+
exactMatch = fromFlag (listExactMatch listFlags)
138+
filterPackages :: [(PackageName, a, b)] -> [(PackageName, a, b)]
139+
filterPackages =
140+
filter
141+
(\(package,_,_) ->
142+
unPackageName package `elem` pats
143+
)
134144
matchingPackages search index =
135145
[ pkg
136146
| pat <- pats

cabal-install/Distribution/Client/Setup.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@ instance Semigroup GetFlags where
16231623
data ListFlags = ListFlags {
16241624
listInstalled :: Flag Bool,
16251625
listSimpleOutput :: Flag Bool,
1626+
listExactMatch :: Flag Bool,
16261627
listVerbosity :: Flag Verbosity,
16271628
listPackageDBs :: [Maybe PackageDB]
16281629
} deriving Generic
@@ -1631,6 +1632,7 @@ defaultListFlags :: ListFlags
16311632
defaultListFlags = ListFlags {
16321633
listInstalled = Flag False,
16331634
listSimpleOutput = Flag False,
1635+
listExactMatch = Flag False,
16341636
listVerbosity = toFlag normal,
16351637
listPackageDBs = []
16361638
}
@@ -1666,6 +1668,10 @@ listCommand = CommandUI {
16661668
"Print in a easy-to-parse format"
16671669
listSimpleOutput (\v flags -> flags { listSimpleOutput = v })
16681670
trueArg
1671+
, option [] ["exact"]
1672+
"Print only exact match"
1673+
listExactMatch (\v flags -> flags { listExactMatch = v })
1674+
trueArg
16691675

16701676
, option "" ["package-db"]
16711677
( "Append the given package database to the list of package"

cabal-install/changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-*-change-log-*-
22

33
3.2.0.0 Someone <mailto:[email protected]> February 2020
4+
* Add `--exact` flag to `cabal list` command that enables exact match on the
5+
search package name (#4267)
46
* `v2-build` (and other `v2-`prefixed commands) now accept the
57
`--benchmark-option(s)` flags, which pass options to benchmark executables
68
(analogous to how `--test-option(s)` works). (#6209)

cabal-testsuite/Test/Cabal/Monad.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ mkNormalizerEnv = do
412412
-- Arguably we should use Cabal's APIs but I am too lazy
413413
-- to remember what it is
414414
list_out <- liftIO $ readProcess (programPath ghc_pkg_program)
415-
["list", "--global", "--simple-output"] ""
415+
["list", "--global", "--simple-output", "--exact"] ""
416416
return NormalizerEnv {
417417
normalizerRoot
418418
= addTrailingPathSeparator (testSourceDir env),

solver-benchmarks/HackageBenchmark.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ hackageBenchmarkMain = do
164164
if null argPackages
165165
then do
166166
putStrLn $ "Obtaining the package list (using " ++ argCabal1 ++ ") ..."
167-
list <- readProcess argCabal1 ["list", "--simple-output"] ""
167+
list <- readProcess argCabal1 ["list", "--simple-output", "--exact"] ""
168168
return $ nub [mkPackageName $ head (words line) | line <- lines list]
169169
else do
170170
putStrLn "Using given package list ..."

0 commit comments

Comments
 (0)