summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2019-08-15 13:12:43 +0200
committerJulian Andres Klode <julian.klode@canonical.com>2019-08-15 20:21:34 +0200
commit083e72a5939d33dc1fd7596aa441a9982332f776 (patch)
treeb50d465b812d8155af0a79e994f0bd7ad8d73534
parentc64a85dd7524546864603b955f601bf64c9a4bcf (diff)
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.
-rw-r--r--apt-pkg/cachefilter-patterns.cc4
-rw-r--r--apt-pkg/cachefilter-patterns.h26
-rw-r--r--doc/apt-patterns.7.xml11
-rwxr-xr-xtest/integration/test-apt-patterns6
4 files changed, 47 insertions, 0 deletions
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<APT::CacheFilter::Matcher> PatternParser::aPattern(std::unique_p
if (node->matches("?architecture", 1, 1))
return std::make_unique<APT::CacheFilter::PackageArchitectureMatchesSpecification>(aWord(node->arguments[0]));
+ if (node->matches("?automatic", 0, 0))
+ return std::make_unique<Patterns::PackageIsAutomatic>(file);
if (node->matches("?false", 0, 0))
return std::make_unique<APT::CacheFilter::FalseMatcher>();
+ if (node->matches("?garbage", 0, 0))
+ return std::make_unique<Patterns::PackageIsGarbage>(file);
if (node->matches("?name", 1, 1))
return std::make_unique<APT::CacheFilter::PackageNameMatchesRegEx>(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<PatternTreeParser::Node> &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 @@
<varlistentry><term><code>?architecture(WILDCARD)</code></term>
<listitem><para>Selects packages matching the specified architecture, which may contain wildcards using any.</para></listitem>
</varlistentry>
+ <varlistentry><term><code>?automatic</code></term>
+ <listitem><para>Selects packages that were installed automatically.</para></listitem>
+ </varlistentry>
+ <varlistentry><term><code>?garbage</code></term>
+ <listitem><para>Selects packages that can be removed automatically.</para></listitem>
+ </varlistentry>
<varlistentry><term><code>?name(REGEX)</code></term>
<listitem><para>Selects packages where the name matches the given regular expression.</para></listitem>
</varlistentry>
@@ -77,6 +83,11 @@
<refsect1><title>Examples</title>
+ <variablelist>
+ <varlistentry><term><code>apt remove ?garbage</code></term>
+ <listitem><para>Remove all packages that are automatically installed and no longer needed - same as apt autoremove</para></listitem>
+ </varlistentry>
+ </variablelist>
</refsect1>
<refsect1><title>Migrating from aptitude</title>
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]