fix git-send-email with recent versions of Term::ReadLine::Gnu
cover fix git-send-email with recent versions of Term::ReadLine::Gnu
Using git-send-email with the latest release of Term::ReadLine::Gnu causes any invocations which prompt more than once to fail. Details are in the second patch, which fixes it. The first patch cleans up an obsolete workaround that obscured the root cause of the problem.
The affected version of Term::ReadLine::Gnu is v1.46, which hit Debian unstable in the past few weeks. So we should consider this for maint and perhaps even for the -rc candidates, since people will presumably start running into this more and more.
[1/2]: send-email: drop FakeTerm hack
[2/2]: send-email: avoid creating more than one Term::ReadLine object
git-send-email.perl | 32 +++++++++++--------------------- t/t9001-send-email.sh | 5 +++-- 2 files changed, 14 insertions(+), 23 deletions(-)
-Peff
PATCH 1/2 send-email: drop FakeTerm hack
Back in 280242d1cc (send-email: do not barf when Term::ReadLine does not like your terminal, 2006-07-02), we added a fallback for when Term::ReadLine's constructor failed: we'd have a FakeTerm object instead, which would then die if anybody actually tried to call readline() on it. Since we instantiated the $term variable at program startup, we needed this workaround to let the program run in modes when we did not prompt the user.
But later, in f4dc9432fd (send-email: lazily load modules for a big speedup, 2021-05-28), we started loading Term::ReadLine lazily only when ask() is called. So at that point we know we're trying to prompt the user, and we can just die if ReadLine instantiation fails, rather than making this fake object to lazily delay showing the error.
This should be OK even if there is no tty (e.g., we're in a cron job), because Term::ReadLine will return a stub object in that case whose "IN" and "OUT" functions return undef. And since 5906f54e47 (send-email:
don't attempt to prompt if tty is closed, 2009-03-31), we check for that case and skip prompting.
And we can be sure that FakeTerm was not kicking in for such a situation, because it has actually been broken since that commit! It does not define "IN" or "OUT" methods, so perl would barf with an error. If FakeTerm was in use, we were neither honoring what 5906f54e47 tried to do, nor producing the readable message that 280242d1cc intended.
So we're better off just dropping FakeTerm entirely, and letting the error reported by constructing Term::ReadLine through.
---
The smaller change is to implement:
sub IN { return undef }
for FakeTerm, which would then behave according to 5906f54e47's "no tty" behavior. But after reading through the relevant history, I'm fairly convinced that just dropping it is the right path. If it were actually kicking in ever, people would be complaining about the obscure "$term->IN is not defined" error message from perl.
git-send-email.perl | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-)
git-send-email.perl +2 -20
@@ -26,18 +26,6 @@ Getopt::Long::Configure qw/ pass_through /; -package FakeTerm; -sub new { - my ($class, $reason) = @_; - return bless \$reason, shift; -} -sub readline { - my $self = shift; - die "Cannot use readline on FakeTerm: $$self"; -} -package main; - - sub usage { print <<EOT; git send-email' [<options>] <file|directory>
@@ -972,16 +960,10 @@ sub get_patch_subject { } sub term { - my $term = eval { - require Term::ReadLine; - $ENV{"GIT_SEND_EMAIL_NOTTY"} + require Term::ReadLine; + return $ENV{"GIT_SEND_EMAIL_NOTTY"} ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT) : Term::ReadLine->new('git-send-email'); - }; - if ($@) { - $term = FakeTerm->new("$@: going non-interactive"); - } - return $term; } sub ask {
Signature · 1 line
2.42.0.rc0.376.g66bfc4f195
Re: [PATCH 1/2] send-email: drop FakeTerm hack
Quoted 9 lines · Jeff King
The smaller change is to implement:
sub IN { return undef }
for FakeTerm, which would then behave according to 5906f54e47's "no tty" behavior. But after reading through the relevant history, I'm fairly convinced that just dropping it is the right path. If it were actually kicking in ever, people would be complaining about the obscure "$term->IN is not defined" error message from perl.
All makes sense. Thanks for the detailed commit message, as always :-).
Thanks,
Taylor
PATCH 2/2 send-email: avoid creating more than one Term::ReadLine object
In reply to Jeff KingEvery time git-send-email calls its ask() function to prompt the user, we call term(), which instantiates a new Term::ReadLine object. But in v1.46 of Term::ReadLine::Gnu (which provides the Term::ReadLine interface on some platforms), its constructor refuses to create a second instance[1]. So on systems with that version of the module, most git-send-email instances will fail (as we usually prompt for both "to" and "in-reply-to" unless the user provided them on the command line).
We can fix this by keeping a single instance variable and returning it for each call to term(). In perl 5.10 and up, we could do that with a "state" variable. But since we only require 5.008, we'll do it the old-fashioned way, with a lexical "my" in its own scope.
Note that the tests in t9001 detect this problem as-is, since the failure mode is for the program to die. But let's also beef up the "Prompting works" test to check that it correctly handles multiple inputs (if we had chosen to keep our FakeTerm hack in the previous commit, then the failure mode would be incorrectly ignoring prompts after the first).
[1] For discussion of why multiple instances are forbidden, see:
https://github.com/hirooih/perl-trg/issues/16
--- git-send-email.perl | 18 +++++++++++++----- t/t9001-send-email.sh | 5 +++-- 2 files changed, 16 insertions(+), 7 deletions(-)
git-send-email.perl +13 -5
@@ -959,11 +959,19 @@ sub get_patch_subject { do_edit(@files); } -sub term { - require Term::ReadLine; - return $ENV{"GIT_SEND_EMAIL_NOTTY"} - ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT) - : Term::ReadLine->new('git-send-email'); +{ + # Only instantiate one $term per program run, since some + # Term::ReadLine providers refuse to create a second instance. + my $term; + sub term { + require Term::ReadLine; + if (!defined $term) { + $term = $ENV{"GIT_SEND_EMAIL_NOTTY"} + ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT) + : Term::ReadLine->new('git-send-email'); + } + return $term; + } } sub ask {
t/t9001-send-email.sh +3 -2
@@ -337,13 +337,14 @@ test_expect_success $PREREQ 'Show all headers' ' test_expect_success $PREREQ 'Prompting works' ' clean_fake_sendmail && (echo "[email protected]" && - echo "" + echo "[email protected]" ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \ --smtp-server="$(pwd)/fake.sendmail" \ $patches \ 2>errors && grep "^From: A U Thor <[email protected]>\$" msgtxt1 && - grep "^To: [email protected]\$" msgtxt1 + grep "^To: [email protected]\$" msgtxt1 && + grep "^In-Reply-To: <[email protected]>" msgtxt1 ' test_expect_success $PREREQ,AUTOIDENT 'implicit ident is allowed' '
Signature · 1 line
2.42.0.rc0.376.g66bfc4f195
Re: [PATCH 2/2] send-email: avoid creating more than one Term::ReadLine object
Quoted 7 lines · Jeff King
Every time git-send-email calls its
ask()function to prompt the user, we callterm(), which instantiates a newTerm::ReadLineobject. But in v1.46 ofTerm::ReadLine::Gnu(which provides theTerm::ReadLineinterface on some platforms), its constructor refuses to create a second instance[1]. So on systems with that version of the module, most git-send-email instances will fail (as we usually prompt for both "to" and "in-reply-to" unless the user provided them on the command line).
Nice one ;-). Everything you wrote here makes sense, as does initializing the Term::ReadLine object only once.
Quoted 6 lines
Note that the tests in t9001 detect this problem as-is, since the failure mode is for the program to die. But let's also beef up the "Prompting works" test to check that it correctly handles multiple inputs (if we had chosen to keep our FakeTerm hack in the previous commit, then the failure mode would be incorrectly ignoring prompts after the first).
Thanks for improving the test coverage while you're here.
Everything here LGTM.
Thanks,
Taylor