]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LEI.pm
lei q: import flags when clobbering/augmenting Maildirs
[public-inbox.git] / lib / PublicInbox / LEI.pm
index 8eb96e78bc1a12593b15eb6de9175cdb91fc65eb..1e5b04cac3bdf5bb896daf1184852fe214e84b70 100644 (file)
@@ -27,7 +27,7 @@ use Time::HiRes qw(stat); # ctime comparisons for config cache
 use File::Path qw(mkpath);
 use File::Spec;
 our $quit = \&CORE::exit;
-our ($current_lei, $errors_log, $listener);
+our ($current_lei, $errors_log, $listener, $oldset);
 my ($recv_cmd, $send_cmd);
 my $GLP = Getopt::Long::Parser->new;
 $GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
@@ -108,11 +108,12 @@ sub index_opt {
 # see lei__complete() and PublicInbox::LeiHelp
 # command => [ positional_args, 1-line description, Getopt::Long option spec ]
 our %CMD = ( # sorted in order of importance/use:
-'q' => [ '--stdin|SEARCH_TERMS...', 'search for messages matching terms', qw(
-       save-as=s output|mfolder|o=s format|f=s dedupe|d=s threads|t augment|a
+'q' => [ '--stdin|SEARCH_TERMS...', 'search for messages matching terms',
+       'stdin|', # /|\z/ must be first for lone dash
+       qw(save-as=s output|mfolder|o=s format|f=s dedupe|d=s threads|t+
        sort|s=s reverse|r offset=i remote! local! external! pretty
-       include|I=s@ exclude=s@ only=s@ jobs|j=s globoff|g stdin|
-       import-remote!
+       include|I=s@ exclude=s@ only=s@ jobs|j=s globoff|g augment|a
+       import-remote! import-augment! lock=s@
        alert=s@ mua=s no-torsocks torsocks=s verbose|v+ quiet|q C=s@),
        PublicInbox::LeiQuery::curl_opt(), opt_dash('limit|n=i', '[0-9]+') ],
 
@@ -172,13 +173,16 @@ our %CMD = ( # sorted in order of importance/use:
 'import' => [ 'LOCATION...|--stdin',
        'one-time import/update from URL or filesystem',
        qw(stdin| offset=i recursive|r exclude=s include|I=s
-       in-format|F=s kw|keywords|flags! C=s@),
+       lock=s@ in-format|F=s kw|keywords|flags! C=s@),
        ],
 'convert' => [ 'LOCATION...|--stdin',
        'one-time conversion from URL or filesystem to another format',
        qw(stdin| in-format|F=s out-format|f=s output|mfolder|o=s quiet|q
-       kw|keywords|flags! C=s@),
+       lock=s@ kw|keywords|flags! C=s@),
        ],
+'p2q' => [ 'FILE|COMMIT_OID|--stdin',
+       "use a patch to generate a query for `lei q --stdin'",
+       qw(stdin| want|w=s@ uri debug) ],
 'config' => [ '[...]', sub {
                'git-config(1) wrapper for '._config_path($_[0]);
        }, qw(config-file|system|global|file|f=s), # for conflict detection
@@ -218,6 +222,9 @@ my %OPTDESC = (
 'help|h' => 'show this built-in help',
 'C=s@' => [ 'DIR', 'chdir to specify to directory' ],
 'quiet|q' => 'be quiet',
+'lock=s@' => [ 'METHOD|dotlock|fcntl|flock|none',
+       'mbox(5) locking method(s) to use (default: fcntl,dotlock)' ],
+
 'globoff|g' => "do not match locations using '*?' wildcards ".
                "and\xa0'[]'\x{a0}ranges",
 'verbose|v+' => 'be more verbose',
@@ -233,8 +240,12 @@ my %OPTDESC = (
 'dedupe|d=s' => ['STRATEGY|content|oid|mid|none',
                'deduplication strategy'],
 'show  threads|t' => 'display entire thread a message belongs to',
-'q     threads|t' =>
+'q     threads|t+' =>
        'return all messages in the same threads as the actual match(es)',
+
+'want|w=s@' => [ 'PREFIX|dfpost|dfn', # common ones in help...
+               'search prefixes to extract (default: dfpost7)' ],
+
 'alert=s@' => ['CMD,:WINCH,:bell,<any command>',
        'run command(s) or perform ops when done writing to output ' .
        '(default: ":WINCH,:bell" with --mua and Maildir/IMAP output, ' .
@@ -328,7 +339,7 @@ my %CONFIG_KEYS = (
        'leistore.dir' => 'top-level storage location',
 );
 
-my @WQ_KEYS = qw(lxs l2m imp mrr cnv); # internal workers
+my @WQ_KEYS = qw(lxs l2m imp mrr cnv p2q); # internal workers
 
 # pronounced "exit": x_it(1 << 8) => exit(1); x_it(13) => SIGPIPE
 sub x_it ($$) {
@@ -407,11 +418,13 @@ sub check_input_format ($;$) {
                my $err = $files ? "regular file(s):\n@$files" : '--stdin';
                return fail($self, "--$opt_key unset for $err");
        }
+       require PublicInbox::MboxLock if $files;
        return 1 if $fmt eq 'eml';
        # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail
        require PublicInbox::MboxReader;
-       PublicInbox::MboxReader->can($fmt) ||
-                               fail($self, "--$opt_key=$fmt unrecognized");
+       PublicInbox::MboxReader->can($fmt) or
+               return fail($self, "--$opt_key=$fmt unrecognized");
+       1;
 }
 
 sub out ($;@) {
@@ -668,6 +681,11 @@ sub lei_convert {
        PublicInbox::LeiConvert->call(@_);
 }
 
+sub lei_p2q {
+       require PublicInbox::LeiP2q;
+       PublicInbox::LeiP2q->call(@_);
+}
+
 sub lei_init {
        my ($self, $dir) = @_;
        my $cfg = _lei_cfg($self, 1);
@@ -849,6 +867,22 @@ sub poke_mua { # forces terminal MUAs to wake up and hopefully notice new mail
        }
 }
 
+my %path_to_fd = ('/dev/stdin' => 0, '/dev/stdout' => 1, '/dev/stderr' => 2);
+$path_to_fd{"/dev/fd/$_"} = $path_to_fd{"/proc/self/fd/$_"} for (0..2);
+sub fopen {
+       my ($self, $mode, $path) = @_;
+       rel2abs($self, $path);
+       $path =~ tr!/!/!s;
+       if (defined(my $fd = $path_to_fd{$path})) {
+               return $self->{$fd};
+       }
+       if ($path =~ m!\A/(?:dev|proc/self)/fd/[0-9]+\z!) {
+               return fail($self, "cannot open $path from daemon");
+       }
+       open my $fh, $mode, $path or return;
+       $fh;
+}
+
 # caller needs to "-t $self->{1}" to check if tty
 sub start_pager {
        my ($self) = @_;
@@ -936,6 +970,9 @@ sub dclose {
                }
        }
        close(delete $self->{1}) if $self->{1}; # may reap_compress
+       if (my $sto = delete $self->{sto}) {
+               $sto->ipc_do('done');
+       }
        $self->close if $self->{sock}; # PublicInbox::DS::close
 }
 
@@ -976,7 +1013,7 @@ sub event_step_init {
 
 sub noop {}
 
-our $oldset; sub oldset { $oldset }
+sub oldset { $oldset }
 
 sub dump_and_clear_log {
        if (defined($errors_log) && -s STDIN && seek(STDIN, 0, SEEK_SET)) {