From 10ca39f5bddcb414dac2a3fcee4cc53844c74fc1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 27 Sep 2023 06:02:48 +0000 Subject: spawn: add popen_wr support This makes interesting parts of our code easier to read IMHO. We can take advantage of `local' while avoiding `fileno' calls since it's called in spawn() anyways to reduce LoC even further. --- lib/PublicInbox/LeiRediff.pm | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'lib/PublicInbox/LeiRediff.pm') diff --git a/lib/PublicInbox/LeiRediff.pm b/lib/PublicInbox/LeiRediff.pm index 824289d6..efd24d17 100644 --- a/lib/PublicInbox/LeiRediff.pm +++ b/lib/PublicInbox/LeiRediff.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # The "lei rediff" sub-command, regenerates diffs with new options @@ -7,7 +7,7 @@ use strict; use v5.10.1; use parent qw(PublicInbox::IPC PublicInbox::LeiInput); use File::Temp 0.19 (); # 0.19 for ->newdir -use PublicInbox::Spawn qw(run_wait spawn which); +use PublicInbox::Spawn qw(run_wait popen_wr which); use PublicInbox::MsgIter qw(msg_part_text); use PublicInbox::ViewDiff; use PublicInbox::LeiBlob; @@ -121,15 +121,11 @@ EOM close $fh or die "close $f: $!"; $rw = PublicInbox::Git->new($d); } - pipe(my ($r, $w)) or die "pipe: $!"; - my $pid = spawn(['git', "--git-dir=$rw->{git_dir}", + my $w = popen_wr(['git', "--git-dir=$rw->{git_dir}", qw(fast-import --quiet --done --date-format=raw)], - $lei->{env}, { 2 => $lei->{2}, 0 => $r }); - close $r or die "close r fast-import: $!"; + $lei->{env}, { 2 => $lei->{2} }); print $w $ta, "\n", $tb, "\ndone\n" or die "print fast-import: $!"; - close $w or die "close w fast-import: $!"; - waitpid($pid, 0); - die "fast-import failed: \$?=$?" if $?; + close $w or die "close w fast-import: \$?=$? \$!=$!"; my $cmd = [ 'diff' ]; _lei_diff_prepare($lei, $cmd); @@ -141,7 +137,7 @@ EOM undef; } -sub wait_requote ($$$) { # OnDestroy callback +sub wait_requote { # OnDestroy callback my ($lei, $pid, $old_1) = @_; $lei->{1} = $old_1; # closes stdin of `perl -pE 's/^/> /'` waitpid($pid, 0) == $pid or die "BUG(?) waitpid: \$!=$! \$?=$?"; @@ -150,13 +146,12 @@ sub wait_requote ($$$) { # OnDestroy callback sub requote ($$) { my ($lei, $pfx) = @_; - pipe(my($r, $w)) or die "pipe: $!"; - my $rdr = { 0 => $r, 1 => $lei->{1}, 2 => $lei->{2} }; - # $^X (perl) is overkill, but maybe there's a weird system w/o sed - my $pid = spawn([$^X, '-pE', "s/^/$pfx/"], $lei->{env}, $rdr); my $old_1 = $lei->{1}; + my $opt = { 1 => $old_1, 2 => $lei->{2} }; + # $^X (perl) is overkill, but maybe there's a weird system w/o sed + my ($w, $pid) = popen_wr([$^X, '-pE', "s/^/$pfx/"], $lei->{env}, $opt); $w->autoflush(1); - binmode $w, ':utf8'; + binmode $w, ':utf8'; # incompatible with ProcessPipe due to syswrite $lei->{1} = $w; PublicInbox::OnDestroy->new(\&wait_requote, $lei, $pid, $old_1); } -- cgit v1.2.3-24-ge0c7