]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei q: -I/--exclude/--only support globs and basenames
authorEric Wong <e@80x24.org>
Wed, 3 Feb 2021 08:11:38 +0000 (22:11 -1000)
committerEric Wong <e@80x24.org>
Thu, 4 Feb 2021 01:37:09 +0000 (01:37 +0000)
We can do basename matching when it's unambiguous.  Since '*?[]'
characters are rare in URLs and pathnames, we'll do glob
matching by default to support a (curl-inspired) --globoff/-g
option to disable globbing.

And fix --exclude while we're at it

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiExternal.pm
lib/PublicInbox/LeiQuery.pm

index 05a39cadffe3f4c08393d4b078e7241f2b230a94..3cb7a32779f168e7fbdae1e141d39da20fb4d22a 100644 (file)
@@ -104,7 +104,7 @@ our %CMD = ( # sorted in order of importance/use:
 'q' => [ 'SEARCH_TERMS...', 'search for messages matching terms', qw(
        save-as=s output|mfolder|o=s format|f=s dedupe|d=s thread|t augment|a
        sort|s=s reverse|r offset=i remote! local! external! pretty
-       include|I=s@ exclude=s@ only=s@ jobs|j=s
+       include|I=s@ exclude=s@ only=s@ jobs|j=s globoff|g
        mua-cmd|mua=s no-torsocks torsocks=s verbose|v quiet|q
        received-after=s received-before=s sent-after=s sent-since=s),
        PublicInbox::LeiQuery::curl_opt(), opt_dash('limit|n=i', '[0-9]+') ],
@@ -201,6 +201,7 @@ my $ls_format = [ 'OUT|plain|json|null', 'listing output format' ];
 my %OPTDESC = (
 'help|h' => 'show this built-in help',
 'quiet|q' => 'be quiet',
+'globoff|g' => "do not match locations using '*?' wildcards and '[]' ranges",
 'verbose|v' => 'be more verbose',
 'solve!' => 'do not attempt to reconstruct blobs from emails',
 'torsocks=s' => ['auto|no|yes',
index 3853cfc13c3ba372ab1568ea4310d6f087056f53..6b4c7fb0334388e2dabdf95a4f98e71a9f0ec72a 100644 (file)
@@ -39,7 +39,7 @@ sub lei_ls_external {
 }
 
 sub ext_canonicalize {
-       my ($location) = $_[-1];
+       my ($location) = @_;
        if ($location !~ m!\Ahttps?://!) {
                PublicInbox::Config::rel2abs_collapsed($location);
        } else {
@@ -52,6 +52,42 @@ sub ext_canonicalize {
        }
 }
 
+my %patmap = ('*' => '[^/]*?', '?' => '[^/]', '[' => '[', ']' => ']');
+sub glob2pat {
+       my ($glob) = @_;
+        $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
+        $glob;
+}
+
+sub get_externals {
+       my ($self, $loc, $exclude) = @_;
+       return (ext_canonicalize($loc)) if -e $loc;
+
+       my @m;
+       my @cur = externals_each($self);
+       my $do_glob = !$self->{opt}->{globoff}; # glob by default
+       if ($do_glob && ($loc =~ /[\*\?]/s || $loc =~ /\[.*\]/s)) {
+               my $re = glob2pat($loc);
+               @m = grep(m!$re!, @cur);
+               return @m if scalar(@m);
+       } elsif (index($loc, '/') < 0) { # exact basename match:
+               @m = grep(m!/\Q$loc\E/?\z!, @cur);
+               return @m if scalar(@m) == 1;
+       } elsif ($exclude) { # URL, maybe:
+               my $canon = ext_canonicalize($loc);
+               @m = grep(m!\A\Q$canon\E\z!, @cur);
+               return @m if scalar(@m) == 1;
+       } else { # URL:
+               return (ext_canonicalize($loc));
+       }
+       if (scalar(@m) == 0) {
+               $self->fail("`$loc' is unknown");
+       } else {
+               $self->fail("`$loc' is ambiguous:\n", map { "\t$_\n" } @m);
+       }
+       ();
+}
+
 sub lei_add_external {
        my ($self, $location) = @_;
        my $cfg = $self->_lei_cfg(1);
index 72a67c24f18dfc89d2a2dee32eb0d86afafece81..10b8d6fae68339ab81df2ed1e3c268f204ac102e 100644 (file)
@@ -31,17 +31,21 @@ sub lei_q {
        }
        if (@only) {
                for my $loc (@only) {
-                       $lxs->prepare_external($self->ext_canonicalize($loc));
+                       my @loc = $self->get_externals($loc) or return;
+                       $lxs->prepare_external($_) for @loc;
                }
        } else {
                for my $loc (@{$opt->{include} // []}) {
-                       $lxs->prepare_external($self->ext_canonicalize($loc));
+                       my @loc = $self->get_externals($loc) or return;
+                       $lxs->prepare_external($_) for @loc;
                }
                # --external is enabled by default, but allow --no-external
                if ($opt->{external} //= 1) {
-                       my %x = map {;
-                               ($self->ext_canonicalize($_), 1)
-                       } @{$self->{exclude} // []};
+                       my %x;
+                       for my $loc (@{$opt->{exclude} // []}) {
+                               my @l = $self->get_externals($loc, 1) or return;
+                               $x{$_} = 1 for @l;
+                       }
                        my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
                        $opt->{remote} //= !($lxs->locals - $opt->{'local'});
                        if ($opt->{'local'}) {