add -p: avoid use of undefined $key when ReadKey -> EOF
b5cc003253 (add -i: ignore terminal escape sequences, 2011-05-17) add an additional check to the original code to better handle keys for escape sequences, but failed to account for the possibility the first ReadKey call returned undef (ex: stdin closes) and that was being handled fine by the original code in ca6ac7f135 (add -p: prompt for single characters, 2009-02-05) Add a test for undefined and encapsulate the loop and the original print that relied on it within it. After this, the following command (in a suitable repository state) wouldn't print any error: $ git -c interactive.singleKey add -p </dev/null Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Carlo Marcelo Arenas Belón committed
Nov 28, 2021 at 09:49 UTC
fc8a8126df822f82ab65135296b5bd569af276c1
1 file changed
+9
-7
git-add--interactive.perl
+9
-7
@@ -1175,15 +1175,17 @@ sub prompt_single_character {
1175
ReadMode 'cbreak';
1176
my $key = ReadKey 0;
1177
ReadMode 'restore';
1178
- if ($use_termcap and $key eq "\e") {
1179
- while (!defined $term_escapes{$key}) {
1180
- my $next = ReadKey 0.5;
1181
- last if (!defined $next);
1182
- $key .= $next;
1178
+ if (defined $key) {
1179
+ if ($use_termcap and $key eq "\e") {
1180
+ while (!defined $term_escapes{$key}) {
1181
+ my $next = ReadKey 0.5;
1182
+ last if (!defined $next);
1183
+ $key .= $next;
1184
+ }
1185
+ $key =~ s/\e/^[/;
1186
}
1184
- $key =~ s/\e/^[/;
1187
+ print "$key";
1188
}
1186
- print "$key" if defined $key;
1189
print "\n";
1190
return $key;
1191
} else {