Skip to content

Commit dcb11fc

Browse files
committed
Merge branch 'ab/pager-exit-log'
When a pager spawned by us exited, the trace log did not record its exit status correctly, which has been corrected. * ab/pager-exit-log: pager: properly log pager exit code when signalled run-command: add braces for "if" block in wait_or_whine() pager: test for exit code with and without SIGPIPE pager: refactor wait_for_pager() function
2 parents dc24948 + be8fc53 commit dcb11fc

File tree

3 files changed

+142
-13
lines changed

3 files changed

+142
-13
lines changed

pager.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,25 @@
1111
static struct child_process pager_process = CHILD_PROCESS_INIT;
1212
static const char *pager_program;
1313

14-
static void wait_for_pager(int in_signal)
14+
static void close_pager_fds(void)
1515
{
16-
if (!in_signal) {
17-
fflush(stdout);
18-
fflush(stderr);
19-
}
2016
/* signal EOF to pager */
2117
close(1);
2218
close(2);
23-
if (in_signal)
24-
finish_command_in_signal(&pager_process);
25-
else
26-
finish_command(&pager_process);
2719
}
2820

2921
static void wait_for_pager_atexit(void)
3022
{
31-
wait_for_pager(0);
23+
fflush(stdout);
24+
fflush(stderr);
25+
close_pager_fds();
26+
finish_command(&pager_process);
3227
}
3328

3429
static void wait_for_pager_signal(int signo)
3530
{
36-
wait_for_pager(1);
31+
close_pager_fds();
32+
finish_command_in_signal(&pager_process);
3733
sigchain_pop(signo);
3834
raise(signo);
3935
}

run-command.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,11 @@ static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
551551

552552
while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
553553
; /* nothing */
554-
if (in_signal)
555-
return 0;
554+
if (in_signal) {
555+
if (WIFEXITED(status))
556+
code = WEXITSTATUS(status);
557+
return code;
558+
}
556559

557560
if (waiting < 0) {
558561
failed_errno = errno;

t/t7006-pager.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,134 @@ test_expect_success TTY 'git tag with auto-columns ' '
656656
test_cmp expect actual
657657
'
658658

659+
test_expect_success 'setup trace2' '
660+
GIT_TRACE2_BRIEF=1 &&
661+
export GIT_TRACE2_BRIEF
662+
'
663+
664+
test_expect_success TTY 'git returns SIGPIPE on early pager exit' '
665+
test_when_finished "rm pager-used trace.normal" &&
666+
test_config core.pager ">pager-used; head -n 1; exit 0" &&
667+
GIT_TRACE2="$(pwd)/trace.normal" &&
668+
export GIT_TRACE2 &&
669+
test_when_finished "unset GIT_TRACE2" &&
670+
671+
if test_have_prereq !MINGW
672+
then
673+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
674+
test_match_signal 13 "$OUT"
675+
else
676+
test_terminal git log
677+
fi &&
678+
679+
grep child_exit trace.normal >child-exits &&
680+
test_line_count = 1 child-exits &&
681+
grep " code:0 " child-exits &&
682+
test_path_is_file pager-used
683+
'
684+
685+
test_expect_success TTY 'git returns SIGPIPE on early pager non-zero exit' '
686+
test_when_finished "rm pager-used trace.normal" &&
687+
test_config core.pager ">pager-used; head -n 1; exit 1" &&
688+
GIT_TRACE2="$(pwd)/trace.normal" &&
689+
export GIT_TRACE2 &&
690+
test_when_finished "unset GIT_TRACE2" &&
691+
692+
if test_have_prereq !MINGW
693+
then
694+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
695+
test_match_signal 13 "$OUT"
696+
else
697+
test_terminal git log
698+
fi &&
699+
700+
grep child_exit trace.normal >child-exits &&
701+
test_line_count = 1 child-exits &&
702+
grep " code:1 " child-exits &&
703+
test_path_is_file pager-used
704+
'
705+
706+
test_expect_success TTY 'git discards pager non-zero exit without SIGPIPE' '
707+
test_when_finished "rm pager-used trace.normal" &&
708+
test_config core.pager "wc >pager-used; exit 1" &&
709+
GIT_TRACE2="$(pwd)/trace.normal" &&
710+
export GIT_TRACE2 &&
711+
test_when_finished "unset GIT_TRACE2" &&
712+
713+
if test_have_prereq !MINGW
714+
then
715+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
716+
test "$OUT" -eq 0
717+
else
718+
test_terminal git log
719+
fi &&
720+
721+
grep child_exit trace.normal >child-exits &&
722+
test_line_count = 1 child-exits &&
723+
grep " code:1 " child-exits &&
724+
test_path_is_file pager-used
725+
'
726+
727+
test_expect_success TTY 'git discards nonexisting pager without SIGPIPE' '
728+
test_when_finished "rm pager-used trace.normal" &&
729+
test_config core.pager "wc >pager-used; does-not-exist" &&
730+
GIT_TRACE2="$(pwd)/trace.normal" &&
731+
export GIT_TRACE2 &&
732+
test_when_finished "unset GIT_TRACE2" &&
733+
734+
if test_have_prereq !MINGW
735+
then
736+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
737+
test "$OUT" -eq 0
738+
else
739+
test_terminal git log
740+
fi &&
741+
742+
grep child_exit trace.normal >child-exits &&
743+
test_line_count = 1 child-exits &&
744+
grep " code:127 " child-exits &&
745+
test_path_is_file pager-used
746+
'
747+
748+
test_expect_success TTY 'git attempts to page to nonexisting pager command, gets SIGPIPE' '
749+
test_when_finished "rm trace.normal" &&
750+
test_config core.pager "does-not-exist" &&
751+
GIT_TRACE2="$(pwd)/trace.normal" &&
752+
export GIT_TRACE2 &&
753+
test_when_finished "unset GIT_TRACE2" &&
754+
755+
if test_have_prereq !MINGW
756+
then
757+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
758+
test_match_signal 13 "$OUT"
759+
else
760+
test_terminal git log
761+
fi &&
762+
763+
grep child_exit trace.normal >child-exits &&
764+
test_line_count = 1 child-exits &&
765+
grep " code:-1 " child-exits
766+
'
767+
768+
test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
769+
test_when_finished "rm pager-used trace.normal" &&
770+
test_config core.pager ">pager-used; test-tool sigchain" &&
771+
GIT_TRACE2="$(pwd)/trace.normal" &&
772+
export GIT_TRACE2 &&
773+
test_when_finished "unset GIT_TRACE2" &&
774+
775+
if test_have_prereq !MINGW
776+
then
777+
OUT=$( ((test_terminal git log; echo $? 1>&3) | :) 3>&1 ) &&
778+
test_match_signal 13 "$OUT"
779+
else
780+
test_terminal git log
781+
fi &&
782+
783+
grep child_exit trace.normal >child-exits &&
784+
test_line_count = 1 child-exits &&
785+
grep " code:143 " child-exits &&
786+
test_path_is_file pager-used
787+
'
788+
659789
test_done

0 commit comments

Comments
 (0)