Skip to content

Commit bab1f1c

Browse files
rjustogitster
authored andcommitted
add-patch: do not print hunks repeatedly
The interactive-patch is a sequential process where, on each step, we print one hunk from a patch and then ask the user how to proceed. There is a possibility of repeating a step, for example if the user enters a non-applicable option, i.e: "s" $ git add -p diff --git a/add-patch.c b/add-patch.c index 52be1ddb15..8fb75e82e2 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" static int patch_update_file(struct add_p_state *s, struct file_diff *file_diff) { - size_t hunk_index = 0; + size_t hunk_index = 0, prev_hunk_index = -1; ssize_t i, undecided_previous, undecided_next; struct hunk *hunk; char ch; (1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? s Sorry, cannot split this hunk @@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" static int patch_update_file(struct add_p_state *s, struct file_diff *file_diff) { - size_t hunk_index = 0; + size_t hunk_index = 0, prev_hunk_index = -1; ssize_t i, undecided_previous, undecided_next; struct hunk *hunk; char ch; (1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? ... or an invalid option, i.e: "U" $ git add -p diff --git a/add-patch.c b/add-patch.c index 52be1ddb15..8fb75e82e2 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" static int patch_update_file(struct add_p_state *s, struct file_diff *file_diff) { - size_t hunk_index = 0; + size_t hunk_index = 0, prev_hunk_index = -1; ssize_t i, undecided_previous, undecided_next; struct hunk *hunk; char ch; (1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? U y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any of the later hunks in the file j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk g - select a hunk to go to / - search for a hunk matching the given regex e - manually edit the current hunk p - print again the current hunk ? - print help @@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" static int patch_update_file(struct add_p_state *s, struct file_diff *file_diff) { - size_t hunk_index = 0; + size_t hunk_index = 0, prev_hunk_index = -1; ssize_t i, undecided_previous, undecided_next; struct hunk *hunk; char ch; (1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? Printing the chunk again followed by the question can be confusing as the user has to pay special attention to notice that the same chunk is being reconsidered. It can also be problematic if the chunk is longer than one screen height because the result of the previous iteration is lost off the screen (the help guide in the previous example). To avoid such problems, stop printing the chunk if the iteration does not advance to a different chunk. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66c14ab commit bab1f1c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

add-patch.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ static int patch_update_file(struct add_p_state *s,
13951395
struct file_diff *file_diff)
13961396
{
13971397
size_t hunk_index = 0;
1398-
ssize_t i, undecided_previous, undecided_next;
1398+
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
13991399
struct hunk *hunk;
14001400
char ch;
14011401
struct child_process cp = CHILD_PROCESS_INIT;
@@ -1448,8 +1448,11 @@ static int patch_update_file(struct add_p_state *s,
14481448

14491449
strbuf_reset(&s->buf);
14501450
if (file_diff->hunk_nr) {
1451-
render_hunk(s, hunk, 0, colored, &s->buf);
1452-
fputs(s->buf.buf, stdout);
1451+
if (rendered_hunk_index != hunk_index) {
1452+
render_hunk(s, hunk, 0, colored, &s->buf);
1453+
fputs(s->buf.buf, stdout);
1454+
rendered_hunk_index = hunk_index;
1455+
}
14531456

14541457
strbuf_reset(&s->buf);
14551458
if (undecided_previous >= 0) {
@@ -1646,13 +1649,15 @@ static int patch_update_file(struct add_p_state *s,
16461649
hunk_index = i;
16471650
} else if (s->answer.buf[0] == 's') {
16481651
size_t splittable_into = hunk->splittable_into;
1649-
if (!(permitted & ALLOW_SPLIT))
1652+
if (!(permitted & ALLOW_SPLIT)) {
16501653
err(s, _("Sorry, cannot split this hunk"));
1651-
else if (!split_hunk(s, file_diff,
1652-
hunk - file_diff->hunk))
1654+
} else if (!split_hunk(s, file_diff,
1655+
hunk - file_diff->hunk)) {
16531656
color_fprintf_ln(stdout, s->s.header_color,
16541657
_("Split into %d hunks."),
16551658
(int)splittable_into);
1659+
rendered_hunk_index = -1;
1660+
}
16561661
} else if (s->answer.buf[0] == 'e') {
16571662
if (!(permitted & ALLOW_EDIT))
16581663
err(s, _("Sorry, cannot edit this hunk"));
@@ -1661,7 +1666,7 @@ static int patch_update_file(struct add_p_state *s,
16611666
goto soft_increment;
16621667
}
16631668
} else if (s->answer.buf[0] == 'p') {
1664-
/* nothing special is needed */
1669+
rendered_hunk_index = -1;
16651670
} else {
16661671
const char *p = _(help_patch_remainder), *eol = p;
16671672

0 commit comments

Comments
 (0)