about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-04-13 10:54:46 +0000
committerEric Wong <e@80x24.org>2021-04-13 15:06:09 -0400
commit3052acd27f3119510b3efa33d77b610d6c4f1c85 (patch)
tree0e57051122af65f0802074b83c8752c87020f925 /lib/PublicInbox
parent8ab43c1c27c725a8ef9307f5dba3e565169d48ca (diff)
downloadpublic-inbox-3052acd27f3119510b3efa33d77b610d6c4f1c85.tar.gz
The command isn't finalized, yet, but it's intended to update
an existing saved search.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/LEI.pm2
-rw-r--r--lib/PublicInbox/LeiQuery.pm2
-rw-r--r--lib/PublicInbox/LeiSavedSearch.pm24
-rw-r--r--lib/PublicInbox/LeiToMail.pm12
-rw-r--r--lib/PublicInbox/LeiUp.pm46
5 files changed, 72 insertions, 14 deletions
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 7292d0f2..4b87c104 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -135,6 +135,8 @@ our %CMD = ( # sorted in order of importance/use:
         sort|s=s reverse|r offset=i pretty jobs|j=s globoff|g augment|a
         import-before! lock=s@ rsyncable alert=s@ mua=s verbose|v+), @c_opt,
         opt_dash('limit|n=i', '[0-9]+') ],
+'up' => [ 'SEARCH_TERMS...', 'update saved search',
+        qw(jobs|j=s lock=s@ alert=s@ mua=s verbose|v+), @c_opt ],
 
 'blob' => [ 'OID', 'show a git blob, reconstructing from mail if necessary',
         qw(git-dir=s@ cwd! verbose|v+ mail! oid-a|A=s path-a|a=s path-b|b=s),
diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm
index 8bca1020..7456f7f9 100644
--- a/lib/PublicInbox/LeiQuery.pm
+++ b/lib/PublicInbox/LeiQuery.pm
@@ -12,7 +12,7 @@ sub prep_ext { # externals_each callback
         $lxs->prepare_external($loc) unless $exclude->{$loc};
 }
 
-sub _start_query {
+sub _start_query { # used by "lei q" and "lei up"
         my ($self) = @_;
         PublicInbox::LeiOverview->new($self) or return;
         my $opt = $self->{opt};
diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index ab9f393b..815008fd 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -17,18 +17,12 @@ use PublicInbox::Hval qw(to_filename);
 sub new {
         my ($cls, $lei, $dir) = @_;
         my $self = bless { ale => $lei->ale, -cfg => {} }, $cls;
-        if (defined $dir) { # updating existing saved search
+        if (defined $dir) { # updating existing saved search via "lei up"
                 my $f = $self->{'-f'} = "$dir/lei.saved-search";
                 -f $f && -r _ or
                         return $lei->fail("$f non-existent or unreadable");
                 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
-                my $q = $lei->{mset_opt}->{q_raw} = $self->{-cfg}->{'lei.q'} //
-                                        return $lei->fail("lei.q unset in $f");
-                my $lse = $lei->{lse} // die 'BUG: {lse} missing';
-                $lei->{mset_opt}->{qstr} = ref($q) ?
-                                $lse->query_argv_to_string($lse->git, $q) :
-                                $lse->query_approxidate($lse->git, $q);
-        } else { # new saved search
+        } else { # new saved search "lei q --save"
                 my $saved_dir = $lei->store_path . '/../saved-searches/';
                 my (@name) = ($lei->{ovv}->{dst} =~ m{([\w\-\.]+)/*\z});
                 push @name, to_filename($lei->{mset_opt}->{qstr});
@@ -42,6 +36,20 @@ sub new {
                 } else {
                         cfg_set($self, 'lei.q', $q);
                 }
+                my $fmt = $lei->{opt}->{'format'};
+                cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
+                cfg_set($self, 'lei.q.output', $lei->{opt}->{output});
+                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);
+                        }
+                }
+                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);
+                }
         }
         bless $self->{-cfg}, 'PublicInbox::Config';
         $self->{lock_path} = "$self->{-f}.flock";
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index bd2b714a..4ebaf8f3 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -349,11 +349,13 @@ sub new {
                 die "bad mail --format=$fmt\n";
         }
         $self->{dst} = $dst;
-        my $dd_cls = 'PublicInbox::'.
-                ($lei->{opt}->{save} ? 'LeiSavedSearch' : 'LeiDedupe');
-        eval "require $dd_cls";
-        die "$dd_cls: $@" if $@;
-        $lei->{dedupe} = $dd_cls->new($lei);
+        $lei->{dedupe} = $lei->{lss} // do {
+                my $dd_cls = 'PublicInbox::'.
+                        ($lei->{opt}->{save} ? 'LeiSavedSearch' : 'LeiDedupe');
+                eval "require $dd_cls";
+                die "$dd_cls: $@" if $@;
+                $dd_cls->new($lei);
+        };
         $self;
 }
 
diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm
new file mode 100644
index 00000000..386a7566
--- /dev/null
+++ b/lib/PublicInbox/LeiUp.pm
@@ -0,0 +1,46 @@
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# "lei up" - updates the result of "lei q --save"
+package PublicInbox::LeiUp;
+use strict;
+use v5.10.1;
+use PublicInbox::LeiSavedSearch;
+use PublicInbox::LeiOverview;
+
+sub lei_up {
+        my ($lei, $dir) = @_;
+        $lei->{lse} = $lei->_lei_store(1)->search;
+        my $lss = PublicInbox::LeiSavedSearch->new($lei, $dir) or return;
+        my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
+        $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
+        my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
+                                return $lei->fail("lei.q unset in $lss->{-f}");
+        my $lse = $lei->{lse} // die 'BUG: {lse} missing';
+        if (ref($q)) {
+                $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
+        } else {
+                $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
+        }
+        $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
+                return $lei->fail("lei.q.output unset in $lss->{-f}");
+        $lei->{opt}->{'format'} //= $lss->{-cfg}->{'lei.q.format'}; # optional
+
+        my $to_avref = $lss->{-cfg}->can('_array');
+        for my $k (qw(only include exclude)) {
+                my $v = $lss->{-cfg}->{"lei.q.$k"} // next;
+                $lei->{opt}->{$k} = $to_avref->($v);
+        }
+        for my $k (qw(external local remote
+                        import-remote import-before threads)) {
+                my $v = $lss->{-cfg}->{"lei.q.$k"} // next;
+                $lei->{opt}->{$k} = $v;
+        }
+        $lei->{lss} = $lss; # for LeiOverview->new
+        my $lxs = $lei->lxs_prepare or return;
+        $lei->ale->refresh_externals($lxs);
+        $lei->{opt}->{save} = 1;
+        $lei->_start_query;
+}
+
+1;