]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei edit-search: support relocating lei.q.output
authorEric Wong <e@80x24.org>
Sat, 1 May 2021 06:21:17 +0000 (06:21 +0000)
committerEric Wong <e@80x24.org>
Sat, 1 May 2021 23:12:41 +0000 (23:12 +0000)
The contents of the old lei.q.output will not be removed,
but will be converted into the new one.

lib/PublicInbox/LeiConvert.pm
lib/PublicInbox/LeiEditSearch.pm
lib/PublicInbox/LeiSavedSearch.pm
script/lei

index cefcaf65563e9e2fd9bfb295f91253855413c2c1..5b27ec2d4dc7bbfbae4ea75e6ddba762c7d9fa47 100644 (file)
@@ -37,8 +37,11 @@ sub process_inputs { # via wq_do
        my ($self) = @_;
        local $PublicInbox::DS::in_loop = 0; # force synchronous dwaitpid
        $self->SUPER::process_inputs;
-       delete $self->{lei}->{1};
+       my $lei = $self->{lei};
+       delete $lei->{1};
        delete $self->{wcb}; # commit
+       my $nr = delete($lei->{-nr_write}) // 0;
+       $lei->err("# converted $nr messages") if $lei->{opt}->{verbose};
 }
 
 sub lei_convert { # the main "lei convert" method
index fb36fdcdeb22cc10beefc72b52b8af5e89b28677..30ac65bdc18c7d402abb410f203d27cddc25df15 100644 (file)
@@ -13,10 +13,19 @@ sub lei_edit_search {
        my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
        my @cmd = (qw(git config --edit -f), $lss->{'-f'});
        $lei->qerr("# spawning @cmd");
+       $lss->edit_begin($lei);
        if ($lei->{oneshot}) {
-               exec(@cmd) or die "exec @cmd: $!\n";
-       } else {
-               $lei->send_exec_cmd([], \@cmd, {});
+               require PublicInbox::Spawn;
+               waitpid(PublicInbox::Spawn::spawn(\@cmd), 0);
+               # non-fatal, editor could fail after successful write
+               $lei->child_error($?) if $?;
+               $lss->edit_done($lei);
+       } else { # run in script/lei foreground
+               require PublicInbox::PktOp;
+               my ($op_c, $op_p) = PublicInbox::PktOp->pair;
+               # $op_p will EOF when $EDITOR is done
+               $op_c->{ops} = { '' => [$lss->can('edit_done'), $lss, $lei] };
+               $lei->send_exec_cmd([ @$lei{qw(0 1 2)}, $op_p ], \@cmd, {});
        }
 }
 
index 79125214a06fdcc5dd6fafd08b6978c2ed786f55..8177c98e84182ec731937aee671e312003266a91 100644 (file)
@@ -259,6 +259,57 @@ sub output2lssdir {
        undef;
 }
 
+sub edit_begin {
+       my ($self, $lei) = @_;
+       if (ref($self->{-cfg}->{'lei.q.output'})) {
+               delete $self->{-cfg}->{'lei.q.output'}; # invalid
+               $lei->err(<<EOM);
+$self->{-f} has multiple values of lei.q.output
+please remove redundant ones
+EOM
+       }
+       $lei->{-lss_for_edit} = $self;
+}
+
+sub edit_done {
+       my ($self, $lei) = @_;
+       my $cfg = PublicInbox::Config->git_config_dump($self->{'-f'});
+       my $new_out = $cfg->{'lei.q.output'} // '';
+       return $lei->fail(<<EOM) if ref $new_out;
+$self->{-f} has multiple values of lei.q.output
+please edit again
+EOM
+       return $lei->fail(<<EOM) if $new_out eq '';
+$self->{-f} needs lei.q.output
+please edit again
+EOM
+       my $old_out = $self->{-cfg}->{'lei.q.output'} // '';
+       return if $old_out eq $new_out;
+       my $old_path = $old_out;
+       my $new_path = $new_out;
+       s!$LOCAL_PFX!! for ($old_path, $new_path);
+       my $dir_old = lss_dir_for($lei, \$old_path, 1);
+       my $dir_new = lss_dir_for($lei, \$new_path);
+       return if $dir_new eq $dir_old; # no change, likely
+       return $lei->fail(<<EOM) if -e $dir_new;
+lei.q.output changed from `$old_out' to `$new_out'
+However, $dir_new exists
+EOM
+       # start the conversion asynchronously
+       my $old_sq = PublicInbox::Config::squote_maybe($old_out);
+       my $new_sq = PublicInbox::Config::squote_maybe($new_out);
+       $lei->puts("lei.q.output changed from $old_sq to $new_sq");
+       $lei->qerr("# lei convert $old_sq -o $new_sq");
+       my $v = !$lei->{opt}->{quiet};
+       $lei->{opt} = { output => $new_out, verbose => $v };
+       require PublicInbox::LeiConvert;
+       PublicInbox::LeiConvert::lei_convert($lei, $old_out);
+
+       $lei->fail(<<EOM) if -e $dir_old && !rename($dir_old, $dir_new);
+E: rename($dir_old, $dir_new) error: $!
+EOM
+}
+
 no warnings 'once';
 *nntp_url = \&cloneurl;
 *base_url = \&PublicInbox::Inbox::base_url;
index 90a9383928f875faae25fa065a48a071a3273fef..bec6b00125fb4270d9c37152dce79ebe6972dcef 100755 (executable)
@@ -33,8 +33,15 @@ my $exec_cmd = sub {
                push @rdr, shift(@old), $newfh;
        }
        my $do_exec = sub {
+               my @non_std; # ex. $op_p from lei_edit_search
                while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
+                       my $old_io = !!$io;
                        open $io, '+<&', $newfh or die "open +<&=: $!";
+                       push @non_std, $io unless $old_io;
+               }
+               if (@non_std) {
+                       require Fcntl;
+                       fcntl($_, Fcntl::F_SETFD(), 0) for @non_std;
                }
                my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
                @ENV{keys %env} = values %env;