Skip to content

Commit 989bf45

Browse files
committed
Merge branch 'rj/add-p-explicit-reshow'
"git add -p" and other "interactive hunk selection" UI has learned to skip showing the hunk immediately after it has already been shown, and an additional action to explicitly ask to reshow the current hunk. * rj/add-p-explicit-reshow: add-patch: do not print hunks repeatedly add-patch: introduce 'p' in interactive-patch
2 parents 4b40810 + bab1f1c commit 989bf45

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

Documentation/git-add.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ patch::
348348
K - leave this hunk undecided, see previous hunk
349349
s - split the current hunk into smaller hunks
350350
e - manually edit the current hunk
351+
p - print the current hunk
351352
? - print help
352353
+
353354
After deciding the fate for all hunks, if there is any hunk

add-patch.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,13 +1388,14 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
13881388
"/ - search for a hunk matching the given regex\n"
13891389
"s - split the current hunk into smaller hunks\n"
13901390
"e - manually edit the current hunk\n"
1391+
"p - print the current hunk\n"
13911392
"? - print help\n");
13921393

13931394
static int patch_update_file(struct add_p_state *s,
13941395
struct file_diff *file_diff)
13951396
{
13961397
size_t hunk_index = 0;
1397-
ssize_t i, undecided_previous, undecided_next;
1398+
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
13981399
struct hunk *hunk;
13991400
char ch;
14001401
struct child_process cp = CHILD_PROCESS_INIT;
@@ -1447,8 +1448,11 @@ static int patch_update_file(struct add_p_state *s,
14471448

14481449
strbuf_reset(&s->buf);
14491450
if (file_diff->hunk_nr) {
1450-
render_hunk(s, hunk, 0, colored, &s->buf);
1451-
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+
}
14521456

14531457
strbuf_reset(&s->buf);
14541458
if (undecided_previous >= 0) {
@@ -1480,6 +1484,7 @@ static int patch_update_file(struct add_p_state *s,
14801484
permitted |= ALLOW_EDIT;
14811485
strbuf_addstr(&s->buf, ",e");
14821486
}
1487+
strbuf_addstr(&s->buf, ",p");
14831488
}
14841489
if (file_diff->deleted)
14851490
prompt_mode_type = PROMPT_DELETION;
@@ -1644,20 +1649,24 @@ static int patch_update_file(struct add_p_state *s,
16441649
hunk_index = i;
16451650
} else if (s->answer.buf[0] == 's') {
16461651
size_t splittable_into = hunk->splittable_into;
1647-
if (!(permitted & ALLOW_SPLIT))
1652+
if (!(permitted & ALLOW_SPLIT)) {
16481653
err(s, _("Sorry, cannot split this hunk"));
1649-
else if (!split_hunk(s, file_diff,
1650-
hunk - file_diff->hunk))
1654+
} else if (!split_hunk(s, file_diff,
1655+
hunk - file_diff->hunk)) {
16511656
color_fprintf_ln(stdout, s->s.header_color,
16521657
_("Split into %d hunks."),
16531658
(int)splittable_into);
1659+
rendered_hunk_index = -1;
1660+
}
16541661
} else if (s->answer.buf[0] == 'e') {
16551662
if (!(permitted & ALLOW_EDIT))
16561663
err(s, _("Sorry, cannot edit this hunk"));
16571664
else if (edit_hunk_loop(s, file_diff, hunk) >= 0) {
16581665
hunk->use = USE_HUNK;
16591666
goto soft_increment;
16601667
}
1668+
} else if (s->answer.buf[0] == 'p') {
1669+
rendered_hunk_index = -1;
16611670
} else {
16621671
const char *p = _(help_patch_remainder), *eol = p;
16631672

t/t3701-add-interactive.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ test_expect_success 'different prompts for mode change/deleted' '
325325
git -c core.filemode=true add -p >actual &&
326326
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
327327
cat >expect <<-\EOF &&
328-
(1/1) Stage deletion [y,n,q,a,d,?]?
329-
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
330-
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
328+
(1/1) Stage deletion [y,n,q,a,d,p,?]?
329+
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
330+
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
331331
EOF
332332
test_cmp expect actual.filtered
333333
'
@@ -514,13 +514,13 @@ test_expect_success 'split hunk setup' '
514514
test_expect_success 'goto hunk' '
515515
test_when_finished "git reset" &&
516516
tr _ " " >expect <<-EOF &&
517-
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
517+
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
518518
_ 2: -2,4 +3,8 +21
519519
go to which hunk? @@ -1,2 +1,3 @@
520520
_10
521521
+15
522522
_20
523-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
523+
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
524524
EOF
525525
test_write_lines s y g 1 | git add -p >actual &&
526526
tail -n 7 <actual >actual.trimmed &&
@@ -530,11 +530,11 @@ test_expect_success 'goto hunk' '
530530
test_expect_success 'navigate to hunk via regex' '
531531
test_when_finished "git reset" &&
532532
tr _ " " >expect <<-EOF &&
533-
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
533+
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
534534
_10
535535
+15
536536
_20
537-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
537+
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
538538
EOF
539539
test_write_lines s y /1,2 | git add -p >actual &&
540540
tail -n 5 <actual >actual.trimmed &&
@@ -715,21 +715,21 @@ test_expect_success 'colors can be overridden' '
715715
<BLUE>+<RESET><BLUE>new<RESET>
716716
<CYAN> more-context<RESET>
717717
<BLUE>+<RESET><BLUE>another-one<RESET>
718-
<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
718+
<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
719719
<MAGENTA>@@ -1,3 +1,3 @@<RESET>
720720
<CYAN> context<RESET>
721721
<BOLD>-old<RESET>
722722
<BLUE>+<RESET><BLUE>new<RESET>
723723
<CYAN> more-context<RESET>
724-
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
724+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
725725
<CYAN> more-context<RESET>
726726
<BLUE>+<RESET><BLUE>another-one<RESET>
727-
<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
727+
<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
728728
<CYAN> context<RESET>
729729
<BOLD>-old<RESET>
730730
<BLUE>+new<RESET>
731731
<CYAN> more-context<RESET>
732-
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
732+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
733733
EOF
734734
test_cmp expect actual
735735
'

0 commit comments

Comments
 (0)