]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
config: glob2re supports `**' to match multiple path components
authorEric Wong <e@80x24.org>
Fri, 17 Mar 2023 20:31:37 +0000 (20:31 +0000)
committerEric Wong <e@80x24.org>
Sat, 18 Mar 2023 04:17:47 +0000 (04:17 +0000)
This should match behavior documented in gitglossary(7)

lib/PublicInbox/Config.pm
t/config.t

index 34abcea3a140abe1d879393f5502f0b2baf8bf14..4065b256539a1e523091cf1a68057d1ae52eb064 100644 (file)
@@ -580,6 +580,7 @@ sub squote_maybe ($) {
 }
 
 my %re_map = ( '*' => '[^/]*?', '?' => '[^/]',
+               '/**' => '/.*', '**/' => '.*/', '/**/' => '/.*?',
                '[' => '[', ']' => ']', ',' => ',' );
 
 sub glob2re ($) {
@@ -593,7 +594,7 @@ sub glob2re ($) {
        if ($re =~ s!\A([a-z0-9\+]+://\[[a-f0-9\:]+\](?::[0-9]+)?/)!!i) {
                $schema_host_port = quotemeta $1; # "http://[::1]:1234"
        }
-       my $changes = ($re =~ s!(.)!
+       my $changes = ($re =~ s!(/\*\*/|\A\*\*/|/\*\*\z|.)!
                $re_map{$p eq '\\' ? '' : do {
                        if ($1 eq '[') { ++$in_bracket }
                        elsif ($1 eq ']') { --$in_bracket }
index d67931da170c788057f4f0089ff50e4ec94897fd..80f214cd611342b8beae618ba0b1319a9c0961af 100644 (file)
@@ -274,5 +274,10 @@ is_deeply($glob2re->('*.[ch]'), '[^/]*?\\.[ch]', 'suffix glob');
 is_deeply($glob2re->('{[a-z],9,}'), '([a-z]|9|)' , 'brace with range');
 is_deeply($glob2re->('\\{a,b\\}'), undef, 'escaped brace');
 is_deeply($glob2re->('\\\\{a,b}'), '\\\\\\\\(a|b)', 'fake escape brace');
+is_deeply($glob2re->('**/foo'), '.*/foo', 'double asterisk start');
+is_deeply($glob2re->('foo/**'), 'foo/.*', 'double asterisk end');
+my $re = $glob2re->('a/**/b');
+is_deeply($re, 'a/.*?b', 'double asterisk middle');
+like($_, qr!$re!, "a/**/b matches $_") for ('a/b', 'a/c/b', 'a/c/a/b');
 
 done_testing();