]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiSavedSearch.pm
avoid wrong/needless squote_maybe shortcuts
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.pm
index 0f632d9378aacfb2b8625b66185fce131d6d44d4..3076d14c1b4ae3959ddc363e08b2e69325503ebf 100644 (file)
@@ -13,7 +13,13 @@ use PublicInbox::Spawn qw(run_die);
 use PublicInbox::ContentHash qw(git_sha);
 use Digest::SHA qw(sha256_hex);
 
-*squote_maybe = \&PublicInbox::Config::squote_maybe;
+# move this to PublicInbox::Config if other things use it:
+my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
+sub cquote_val ($) { # cf. git-config(1)
+       my ($val) = @_;
+       $val =~ s/([\n\t\b])/$cquote{$1}/g;
+       $val;
+}
 
 sub lss_dir_for ($$) {
        my ($lei, $dstref) = @_;
@@ -24,9 +30,10 @@ sub lss_dir_for ($$) {
                $$dstref = $$uri;
                @n = ($uri->mailbox);
        } else { # basename
-               @n = ($$dstref =~ m{([\w\-\.]+)/*\z});
                $$dstref = $lei->rel2abs($$dstref);
                $$dstref .= '/' if -d $$dstref;
+               $$dstref =~ tr!/!/!s;
+               @n = ($$dstref =~ m{([^/]+)/*\z});
        }
        push @n, sha256_hex($$dstref);
        $lei->share_path . '/saved-searches/' . join('-', @n);
@@ -40,7 +47,8 @@ sub new {
                my $f;
                $dir = $dst;
                output2lssdir($self, $lei, \$dir, \$f) or
-                       return $lei->fail("--save was not used with $dst");
+                       return $lei->fail("--save was not used with $dst cwd=".
+                                               $lei->rel2abs('.'));
                $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
                $self->{'-f'} = $f;
        } else { # new saved search "lei q --save"
@@ -51,30 +59,33 @@ sub new {
                $self->{-cfg} = {};
                my $f = $self->{'-f'} = "$dir/lei.saved-search";
                open my $fh, '>', $f or return $lei->fail("open $f: $!");
-               my $sq_dst = squote_maybe($dst);
-               print $fh <<EOM or return $lei->fail("print $f: $!");
-; to refresh with new results, run: lei up $sq_dst
-EOM
-               close $fh or return $lei->fail("close $f: $!");
+               my $sq_dst = PublicInbox::Config::squote_maybe($dst);
                my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
                if (ref $q) {
-                       cfg_set($self, '--add', 'lei.q', $_) for @$q;
+                       $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
                } else {
-                       cfg_set($self, 'lei.q', $q);
+                       $q = "\tq = ".cquote_val($q);
                }
                $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
-               cfg_set($self, 'lei.q.output', $dst);
+               print $fh <<EOM;
+; to refresh with new results, run: lei up $sq_dst
+[lei]
+$q
+[lei "q"]
+       output = $dst
+EOM
                for my $k (qw(only include exclude)) {
                        my $ary = $lei->{opt}->{$k} // next;
                        for my $x (@$ary) {
-                               cfg_set($self, '--add', "lei.q.$k", $x);
+                               print $fh "\t$k = ".cquote_val($x)."\n";
                        }
                }
                for my $k (qw(external local remote import-remote
                                import-before threads)) {
                        my $val = $lei->{opt}->{$k} // next;
-                       cfg_set($self, "lei.q.$k", $val);
+                       print $fh "\t$k = ".cquote_val($val)."\n";
                }
+               close($fh) or return $lei->fail("close $f: $!");
        }
        bless $self->{-cfg}, 'PublicInbox::Config';
        $self->{lock_path} = "$self->{-f}.flock";
@@ -84,7 +95,7 @@ EOM
 
 sub description { $_[0]->{qstr} } # for WWW
 
-sub cfg_set {
+sub cfg_set { # called by LeiXSearch
        my ($self, @args) = @_;
        my $lk = $self->lock_for_scope; # git-config doesn't wait
        run_die([qw(git config -f), $self->{'-f'}, @args]);