From 9c38d7f9a5d2575dc6f7179c4e69fb1cb3d6b871 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 2 Feb 2021 22:11:38 -1000 Subject: lei q: -I/--exclude/--only support globs and basenames 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/LeiExternal.pm | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'lib/PublicInbox/LeiExternal.pm') diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm index 3853cfc1..6b4c7fb0 100644 --- a/lib/PublicInbox/LeiExternal.pm +++ b/lib/PublicInbox/LeiExternal.pm @@ -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); -- cgit v1.2.3-24-ge0c7