From 083e72a5939d33dc1fd7596aa441a9982332f776 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 15 Aug 2019 13:12:43 +0200 Subject: Add ?automatic and ?garbage patterns These patterns allow you to identify automatically installed packages, as well as automatically installed packages that are no longer reachable from the manually installed ones. --- apt-pkg/cachefilter-patterns.cc | 4 ++++ apt-pkg/cachefilter-patterns.h | 26 ++++++++++++++++++++++++++ doc/apt-patterns.7.xml | 11 +++++++++++ test/integration/test-apt-patterns | 6 ++++++ 4 files changed, 47 insertions(+) diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index dd5d741d3..6b506b740 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -210,8 +210,12 @@ std::unique_ptr PatternParser::aPattern(std::unique_p if (node->matches("?architecture", 1, 1)) return std::make_unique(aWord(node->arguments[0])); + if (node->matches("?automatic", 0, 0)) + return std::make_unique(file); if (node->matches("?false", 0, 0)) return std::make_unique(); + if (node->matches("?garbage", 0, 0)) + return std::make_unique(file); if (node->matches("?name", 1, 1)) return std::make_unique(aWord(node->arguments[0])); if (node->matches("?not", 1, 1)) diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 3097ba94b..4c2ef48df 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -113,6 +113,32 @@ struct PatternParser std::string aWord(std::unique_ptr &nodeP); }; +namespace Patterns +{ +using namespace APT::CacheFilter; + +struct PackageIsAutomatic : public PackageMatcher +{ + pkgCacheFile *Cache; + explicit PackageIsAutomatic(pkgCacheFile *Cache) : Cache(Cache) {} + bool operator()(pkgCache::PkgIterator const &Pkg) override + { + assert(Cache != nullptr); + return ((*Cache)[Pkg].Flags & pkgCache::Flag::Auto) != 0; + } +}; + +struct PackageIsGarbage : public PackageMatcher +{ + pkgCacheFile *Cache; + explicit PackageIsGarbage(pkgCacheFile *Cache) : Cache(Cache) {} + bool operator()(pkgCache::PkgIterator const &Pkg) override + { + assert(Cache != nullptr); + return (*Cache)[Pkg].Garbage; + } +}; +} // namespace Patterns } // namespace Internal } // namespace APT #endif diff --git a/doc/apt-patterns.7.xml b/doc/apt-patterns.7.xml index 5aa352f03..327ea17d5 100644 --- a/doc/apt-patterns.7.xml +++ b/doc/apt-patterns.7.xml @@ -69,6 +69,12 @@ ?architecture(WILDCARD) Selects packages matching the specified architecture, which may contain wildcards using any. + ?automatic + Selects packages that were installed automatically. + + ?garbage + Selects packages that can be removed automatically. + ?name(REGEX) Selects packages where the name matches the given regular expression. @@ -77,6 +83,11 @@ Examples + + apt remove ?garbage + Remove all packages that are automatically installed and no longer needed - same as apt autoremove + + Migrating from aptitude diff --git a/test/integration/test-apt-patterns b/test/integration/test-apt-patterns index 0d7b1540d..9a2c74f83 100755 --- a/test/integration/test-apt-patterns +++ b/test/integration/test-apt-patterns @@ -112,6 +112,12 @@ foreign/unstable 2.0 amd64" apt list '?architecture(amd64)' testsuccessequal "Listing..." apt list '?architecture(foreign)' testsuccessequal "Listing..." apt list '?architecture(native)' +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local] +automatic2/now 1.0 i386 [installed,local]" apt list '?automatic' + +testsuccessequal "Listing... +automatic2/now 1.0 i386 [installed,local]" apt list '?garbage' testsuccessequal "Listing... automatic1/now 1.0 i386 [installed,local] -- cgit v1.2.3-70-g09d2