From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
Dragan Simic <dsimic@manjaro.org>, Jeff King <peff@peff.net>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH v2 4/4] add-patch: render hunks through the pager
Date: Sun, 14 Jul 2024 01:30:03 +0900 [thread overview]
Message-ID: <4556095f-47d7-4849-b6d7-a08cd00ad865@gmail.com> (raw)
In-Reply-To: <ebcba08f-3fbb-4130-93eb-d0e62bfe0a8a@gmail.com>
Make the print command trigger the pager when invoked using a capital
'P', to make it easier for the user to review long hunks.
Note that if the PAGER ends unexpectedly before we've been able to send
the payload, perhaps because the user is not interested in the whole
thing, we might receive a SIGPIPE, which would abruptly and unexpectedly
terminate the interactive session for the user.
Therefore, we need to ignore a possible SIGPIPE signal. Add a test for
this, in addition to the test for normal operation.
For the SIGPIPE test, we need to make sure that we completely fill the
operating system's buffer, otherwise we might not trigger the SIGPIPE
signal. The normal size of this buffer in different OSs varies from a
few KBs to 1MB. Use a payload large enough to guarantee that we exceed
this limit.
Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
add-patch.c | 18 +++++++++++++++---
t/t3701-add-interactive.sh | 28 ++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index 6e176cd21a..f2c76b7d83 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -7,9 +7,11 @@
#include "environment.h"
#include "gettext.h"
#include "object-name.h"
+#include "pager.h"
#include "read-cache-ll.h"
#include "repository.h"
#include "strbuf.h"
+#include "sigchain.h"
#include "run-command.h"
#include "strvec.h"
#include "pathspec.h"
@@ -1391,7 +1393,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
"/ - search for a hunk matching the given regex\n"
"s - split the current hunk into smaller hunks\n"
"e - manually edit the current hunk\n"
- "p - print the current hunk\n"
+ "p - print the current hunk, 'P' to use the pager\n"
"? - print help\n");
static int patch_update_file(struct add_p_state *s,
@@ -1402,7 +1404,7 @@ static int patch_update_file(struct add_p_state *s,
struct hunk *hunk;
char ch;
struct child_process cp = CHILD_PROCESS_INIT;
- int colored = !!s->colored.len, quit = 0;
+ int colored = !!s->colored.len, quit = 0, use_pager = 0;
enum prompt_mode_type prompt_mode_type;
enum {
ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
@@ -1452,9 +1454,18 @@ static int patch_update_file(struct add_p_state *s,
strbuf_reset(&s->buf);
if (file_diff->hunk_nr) {
if (rendered_hunk_index != hunk_index) {
+ if (use_pager) {
+ setup_pager();
+ sigchain_push(SIGPIPE, SIG_IGN);
+ }
render_hunk(s, hunk, 0, colored, &s->buf);
fputs(s->buf.buf, stdout);
rendered_hunk_index = hunk_index;
+ if (use_pager) {
+ sigchain_pop(SIGPIPE);
+ wait_for_pager();
+ use_pager = 0;
+ }
}
strbuf_reset(&s->buf);
@@ -1675,8 +1686,9 @@ static int patch_update_file(struct add_p_state *s,
hunk->use = USE_HUNK;
goto soft_increment;
}
- } else if (s->answer.buf[0] == 'p') {
+ } else if (ch == 'p') {
rendered_hunk_index = -1;
+ use_pager = (s->answer.buf[0] == 'P') ? 1 : 0;
} else if (s->answer.buf[0] == '?') {
const char *p = _(help_patch_remainder), *eol = p;
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 6daf3a6be0..c89b984751 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -591,6 +591,34 @@ test_expect_success 'print again the hunk' '
test_cmp expect actual.trimmed
'
+test_expect_success TTY 'print again the hunk (PAGER)' '
+ test_when_finished "git reset" &&
+ cat >expect <<-EOF &&
+ <GREEN>+<RESET><GREEN>15<RESET>
+ 20<RESET>
+ <BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
+ PAGER 10<RESET>
+ PAGER <GREEN>+<RESET><GREEN>15<RESET>
+ PAGER 20<RESET>
+ <BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
+ EOF
+ test_write_lines s y g 1 P |
+ (
+ GIT_PAGER="sed s/^/PAGER\ /" &&
+ export GIT_PAGER &&
+ test_terminal git add -p >actual
+ ) &&
+ tail -n 7 <actual | test_decode_color >actual.trimmed &&
+ test_cmp expect actual.trimmed
+'
+
+test_expect_success TTY 'P does not break if pager ends unexpectedly' '
+ test_when_finished "rm -f huge_file; git reset" &&
+ printf "%2500000s" Y >huge_file &&
+ git add -N huge_file &&
+ test_write_lines P q | GIT_PAGER="head -c 1" test_terminal git add -p >actual
+'
+
test_expect_success 'split hunk "add -p (edit)"' '
# Split, say Edit and do nothing. Then:
#
--
2.45.2.831.g9e4974e3d4
next prev parent reply other threads:[~2024-07-13 16:30 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-12 0:57 [PATCH 0/4] use the pager in 'add -p' Rubén Justo
2024-07-12 1:00 ` [PATCH 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-12 1:00 ` [PATCH 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-12 1:00 ` [PATCH 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-12 13:17 ` Phillip Wood
2024-07-12 1:00 ` [PATCH 4/4] add-patch: render hunks through the pager Rubén Justo
2024-07-12 8:58 ` Dragan Simic
2024-07-12 13:26 ` Phillip Wood
2024-07-12 16:24 ` Rubén Justo
2024-07-13 3:23 ` Rubén Justo
2024-07-13 9:12 ` Junio C Hamano
2024-07-13 13:17 ` phillip.wood123
2024-07-13 23:13 ` Rubén Justo
2024-07-12 8:56 ` [PATCH 0/4] use the pager in 'add -p' Dragan Simic
2024-07-13 16:26 ` [PATCH v2 " Rubén Justo
2024-07-13 16:29 ` [PATCH v2 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-13 16:29 ` [PATCH v2 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-13 16:29 ` [PATCH v2 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-13 16:30 ` Rubén Justo [this message]
2024-07-13 17:08 ` [PATCH v2 0/4] use the pager in 'add -p' Junio C Hamano
2024-07-13 23:21 ` Rubén Justo
2024-07-14 1:18 ` Junio C Hamano
2024-07-14 16:00 ` [PATCH v3 " Rubén Justo
2024-07-14 16:04 ` [PATCH v3 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-14 16:04 ` [PATCH v3 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-14 16:04 ` [PATCH v3 4/4] add-patch: render hunks through the pager Rubén Justo
2024-07-15 14:10 ` Phillip Wood
2024-07-15 23:54 ` Junio C Hamano
2024-07-17 17:20 ` Rubén Justo
2024-07-17 19:39 ` phillip.wood123
2024-07-17 20:03 ` Junio C Hamano
2024-07-17 20:09 ` Eric Sunshine
2024-07-17 20:21 ` Junio C Hamano
2024-07-20 22:37 ` Rubén Justo
2024-07-22 7:18 ` Eric Sunshine
2024-07-22 14:53 ` Rubén Justo
2024-07-18 9:48 ` phillip.wood123
2024-07-17 20:31 ` Junio C Hamano
2024-07-18 9:56 ` phillip.wood123
2024-07-20 22:39 ` Rubén Justo
2024-07-20 22:29 ` Rubén Justo
2024-07-22 10:18 ` Phillip Wood
2024-07-22 16:45 ` Rubén Justo
2024-07-22 17:22 ` Junio C Hamano
2024-07-22 19:06 ` Rubén Justo
2024-07-22 19:27 ` Junio C Hamano
2024-07-22 21:06 ` Re* " Junio C Hamano
2024-07-22 22:00 ` Rubén Justo
2024-07-22 23:12 ` Kyle Lippincott
2024-07-22 23:28 ` Junio C Hamano
2024-07-23 2:31 ` Junio C Hamano
2024-07-22 21:25 ` Junio C Hamano
2024-07-22 23:20 ` Rubén Justo
2024-07-22 23:24 ` [PATCH 1/2] t3701: avoid one-shot export for shell functions Rubén Justo
2024-07-22 23:44 ` Junio C Hamano
2024-07-22 23:57 ` Junio C Hamano
2024-07-22 23:24 ` [PATCH 2/2] pager: make wait_for_pager a no-op for "cat" Rubén Justo
2024-07-22 23:54 ` Junio C Hamano
2024-07-18 9:58 ` [PATCH v3 4/4] add-patch: render hunks through the pager phillip.wood123
2024-07-20 22:45 ` Rubén Justo
2024-07-14 16:04 ` [PATCH v3 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-15 14:13 ` Phillip Wood
2024-07-15 20:04 ` Rubén Justo
2024-07-17 14:58 ` phillip.wood123
2024-07-15 20:16 ` [PATCH v4 0/4] add-patch: render hunks through the pager Rubén Justo
2024-07-15 20:20 ` [PATCH v4 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-15 20:21 ` [PATCH v4 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-15 20:21 ` [PATCH v4 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-15 20:22 ` [PATCH v4 4/4] add-patch: render hunks through the pager Rubén Justo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4556095f-47d7-4849-b6d7-a08cd00ad865@gmail.com \
--to=rjusto@gmail.com \
--cc=dsimic@manjaro.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).