@@ -620,11 +620,12 @@ sub parse_diff {
620
620
if ($diff_use_color ) {
621
621
@colored = run_cmd_pipe(qw( git diff-files -p --color --) , $path );
622
622
}
623
- my (@hunk ) = { TEXT => [], DISPLAY => [] };
623
+ my (@hunk ) = { TEXT => [], DISPLAY => [], TYPE => ' header ' };
624
624
625
625
for (my $i = 0; $i < @diff ; $i ++) {
626
626
if ($diff [$i ] =~ / ^@@ / ) {
627
- push @hunk , { TEXT => [], DISPLAY => [] };
627
+ push @hunk , { TEXT => [], DISPLAY => [],
628
+ TYPE => ' hunk' };
628
629
}
629
630
push @{$hunk [-1]{TEXT }}, $diff [$i ];
630
631
push @{$hunk [-1]{DISPLAY }},
@@ -636,8 +637,8 @@ sub parse_diff {
636
637
sub parse_diff_header {
637
638
my $src = shift ;
638
639
639
- my $head = { TEXT => [], DISPLAY => [] };
640
- my $mode = { TEXT => [], DISPLAY => [] };
640
+ my $head = { TEXT => [], DISPLAY => [], TYPE => ' header ' };
641
+ my $mode = { TEXT => [], DISPLAY => [], TYPE => ' mode ' };
641
642
642
643
for (my $i = 0; $i < @{$src -> {TEXT }}; $i ++) {
643
644
my $dest = $src -> {TEXT }-> [$i ] =~ / ^(old|new) mode (\d +)$ / ?
@@ -684,6 +685,7 @@ sub split_hunk {
684
685
my $this = +{
685
686
TEXT => [],
686
687
DISPLAY => [],
688
+ TYPE => ' hunk' ,
687
689
OLD => $o_ofs ,
688
690
NEW => $n_ofs ,
689
691
OCNT => 0,
@@ -869,7 +871,11 @@ sub edit_hunk_loop {
869
871
if (!defined $text ) {
870
872
return undef ;
871
873
}
872
- my $newhunk = { TEXT => $text , USE => 1 };
874
+ my $newhunk = {
875
+ TEXT => $text ,
876
+ TYPE => $hunk -> [$ix ]-> {TYPE },
877
+ USE => 1
878
+ };
873
879
if (diff_applies($head ,
874
880
@{$hunk }[0..$ix -1],
875
881
$newhunk ,
@@ -890,6 +896,7 @@ sub help_patch_cmd {
890
896
print colored $help_color , <<\EOF ;
891
897
y - stage this hunk
892
898
n - do not stage this hunk
899
+ q - quit, do not stage this hunk nor any of the remaining ones
893
900
a - stage this and all the remaining hunks in the file
894
901
d - do not stage this hunk nor any of the remaining hunks in the file
895
902
g - select a hunk to go to
@@ -926,7 +933,7 @@ sub patch_update_cmd {
926
933
@mods );
927
934
}
928
935
for (@them ) {
929
- patch_update_file($_ -> {VALUE });
936
+ return 0 if patch_update_file($_ -> {VALUE });
930
937
}
931
938
}
932
939
@@ -972,6 +979,7 @@ sub display_hunks {
972
979
}
973
980
974
981
sub patch_update_file {
982
+ my $quit = 0;
975
983
my ($ix , $num );
976
984
my $path = shift ;
977
985
my ($head , @hunk ) = parse_diff($path );
@@ -981,32 +989,7 @@ sub patch_update_file {
981
989
}
982
990
983
991
if (@{$mode -> {TEXT }}) {
984
- while (1) {
985
- print @{$mode -> {DISPLAY }};
986
- print colored $prompt_color ,
987
- " Stage mode change [y/n/a/d/?]? " ;
988
- my $line = prompt_single_character;
989
- if ($line =~ / ^y/i ) {
990
- $mode -> {USE } = 1;
991
- last ;
992
- }
993
- elsif ($line =~ / ^n/i ) {
994
- $mode -> {USE } = 0;
995
- last ;
996
- }
997
- elsif ($line =~ / ^a/i ) {
998
- $_ -> {USE } = 1 foreach ($mode , @hunk );
999
- last ;
1000
- }
1001
- elsif ($line =~ / ^d/i ) {
1002
- $_ -> {USE } = 0 foreach ($mode , @hunk );
1003
- last ;
1004
- }
1005
- else {
1006
- help_patch_cmd(' ' );
1007
- next ;
1008
- }
1009
- }
992
+ unshift @hunk , $mode ;
1010
993
}
1011
994
1012
995
$num = scalar @hunk ;
@@ -1050,14 +1033,19 @@ sub patch_update_file {
1050
1033
}
1051
1034
last if (!$undecided );
1052
1035
1053
- if (hunk_splittable($hunk [$ix ]{TEXT })) {
1036
+ if ($hunk [$ix ]{TYPE } eq ' hunk' &&
1037
+ hunk_splittable($hunk [$ix ]{TEXT })) {
1054
1038
$other .= ' ,s' ;
1055
1039
}
1056
- $other .= ' ,e' ;
1040
+ if ($hunk [$ix ]{TYPE } eq ' hunk' ) {
1041
+ $other .= ' ,e' ;
1042
+ }
1057
1043
for (@{$hunk [$ix ]{DISPLAY }}) {
1058
1044
print ;
1059
1045
}
1060
- print colored $prompt_color , " Stage this hunk [y,n,a,d,/$other ,?]? " ;
1046
+ print colored $prompt_color , ' Stage ' ,
1047
+ ($hunk [$ix ]{TYPE } eq ' mode' ? ' mode change' : ' this hunk' ),
1048
+ " [y,n,a,d,/$other ,?]? " ;
1061
1049
my $line = prompt_single_character;
1062
1050
if ($line ) {
1063
1051
if ($line =~ / ^y/i ) {
@@ -1109,6 +1097,16 @@ sub patch_update_file {
1109
1097
}
1110
1098
next ;
1111
1099
}
1100
+ elsif ($line =~ / ^q/i ) {
1101
+ while ($ix < $num ) {
1102
+ if (!defined $hunk [$ix ]{USE }) {
1103
+ $hunk [$ix ]{USE } = 0;
1104
+ }
1105
+ $ix ++;
1106
+ }
1107
+ $quit = 1;
1108
+ next ;
1109
+ }
1112
1110
elsif ($line =~ m | ^/(.*)| ) {
1113
1111
my $regex = $1 ;
1114
1112
if ($1 eq " " ) {
@@ -1189,7 +1187,7 @@ sub patch_update_file {
1189
1187
$num = scalar @hunk ;
1190
1188
next ;
1191
1189
}
1192
- elsif ($line =~ / ^e/ ) {
1190
+ elsif ($other =~ / e / && $ line =~ / ^e/ ) {
1193
1191
my $newhunk = edit_hunk_loop($head , \@hunk , $ix );
1194
1192
if (defined $newhunk ) {
1195
1193
splice @hunk , $ix , 1, $newhunk ;
@@ -1210,9 +1208,6 @@ sub patch_update_file {
1210
1208
1211
1209
my $n_lofs = 0;
1212
1210
my @result = ();
1213
- if ($mode -> {USE }) {
1214
- push @result , @{$mode -> {TEXT }};
1215
- }
1216
1211
for (@hunk ) {
1217
1212
if ($_ -> {USE }) {
1218
1213
push @result , @{$_ -> {TEXT }};
@@ -1235,6 +1230,7 @@ sub patch_update_file {
1235
1230
}
1236
1231
1237
1232
print " \n " ;
1233
+ return $quit ;
1238
1234
}
1239
1235
1240
1236
sub diff_cmd {
0 commit comments