Skip to content

Commit 78f0a5d

Browse files
rjustogitster
authored andcommitted
pager: die when paging to non-existing command
When trying to execute a non-existent program from GIT_PAGER, we display an error. However, we also send the complete text to the terminal and return a successful exit code. This can be confusing for the user and the displayed error could easily become obscured by a lengthy text. For example, here the error message would be very far above after sending 50 MB of text: $ GIT_PAGER=non-existent t/test-terminal.perl git log | wc -c error: cannot run non-existent: No such file or directory 50314363 Let's make the error clear by aborting the process and return an error so that the user can easily correct their mistake. This will be the result of the change: $ GIT_PAGER=non-existent t/test-terminal.perl git log | wc -c error: cannot run non-existent: No such file or directory fatal: unable to execute pager 'non-existent' 0 The behavior change we're introducing in this commit affects two tests in t7006, which is a good sign regarding test coverage and requires us to address it. The first test is 'git skips paging non-existing command'. This test comes from f7991f0 (t7006: clean up SIGPIPE handling in trace2 tests, 2021-11-21,) where a modification was made to a test that was originally introduced in c24b7f6 (pager: test for exit code with and without SIGPIPE, 2021-02-02). That original test was, IMHO, in the same direction we're going in this commit. At any rate, this test obviously needs to be adjusted to check the new behavior we are introducing. Do it. The second test being affected is: 'non-existent pager doesnt cause crash', introduced in f917f57 (pager: fix crash when pager program doesn't exist, 2021-11-24). As its name states, it has the intention of checking that we don't introduce a regression that produces a crash when GIT_PAGER points to a nonexistent program. This test could be considered redundant nowadays, due to us already having several tests checking implicitly what a non-existent command in GIT_PAGER produces. However, let's maintain a good belt-and-suspenders strategy; adapt it to the new world. Finally, it's worth noting that we are not changing the behavior if the command specified in GIT_PAGER is a shell command. In such cases, it is: $ GIT_PAGER=:\;non-existent t/test-terminal.perl git log :;non-existent: 1: non-existent: not found died of signal 13 at t/test-terminal.perl line 33. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit 78f0a5d

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

pager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void setup_pager(void)
137137
pager_process.in = -1;
138138
strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
139139
if (start_command(&pager_process))
140-
return;
140+
die("unable to execute pager '%s'", pager);
141141

142142
/* original process continues, but writes to the pipe */
143143
dup2(pager_process.in, 1);

t/t7006-pager.sh

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,11 @@ test_expect_success TTY 'git discards pager non-zero exit without SIGPIPE' '
725725
test_path_is_file pager-used
726726
'
727727

728-
test_expect_success TTY 'git skips paging nonexisting command' '
729-
test_when_finished "rm trace.normal" &&
728+
test_expect_success TTY 'git errors when asked to execute nonexisting pager' '
729+
test_when_finished "rm -f err" &&
730730
test_config core.pager "does-not-exist" &&
731-
GIT_TRACE2="$(pwd)/trace.normal" &&
732-
export GIT_TRACE2 &&
733-
test_when_finished "unset GIT_TRACE2" &&
734-
735-
test_terminal git log &&
736-
737-
grep child_exit trace.normal >child-exits &&
738-
test_line_count = 1 child-exits &&
739-
grep " code:-1 " child-exits
731+
test_must_fail test_terminal git log 2>err &&
732+
test_grep "unable to execute pager" err
740733
'
741734

742735
test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
@@ -762,7 +755,7 @@ test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
762755

763756
test_expect_success TTY 'non-existent pager doesnt cause crash' '
764757
test_config pager.show invalid-pager &&
765-
test_terminal git show
758+
test_must_fail test_terminal git show
766759
'
767760

768761
test_done

0 commit comments

Comments
 (0)