@@ -73,6 +73,22 @@ sub colored {
73
73
# command line options
74
74
my $patch_mode ;
75
75
76
+ sub apply_patch ;
77
+
78
+ my %patch_modes = (
79
+ ' stage' => {
80
+ DIFF => ' diff-files -p' ,
81
+ APPLY => sub { apply_patch ' apply --cached' , @_ ; },
82
+ APPLY_CHECK => ' apply --cached' ,
83
+ VERB => ' Stage' ,
84
+ TARGET => ' ' ,
85
+ PARTICIPLE => ' staging' ,
86
+ FILTER => ' file-only' ,
87
+ },
88
+ );
89
+
90
+ my %patch_mode_flavour = %{$patch_modes {stage }};
91
+
76
92
sub run_cmd_pipe {
77
93
if ($^O eq ' MSWin32' || $^O eq ' msys' ) {
78
94
my @invalid = grep {m /[" :*]/} @_ ;
@@ -613,12 +629,21 @@ sub add_untracked_cmd {
613
629
print " \n " ;
614
630
}
615
631
632
+ sub run_git_apply {
633
+ my $cmd = shift ;
634
+ my $fh ;
635
+ open $fh , ' | git ' . $cmd ;
636
+ print $fh @_ ;
637
+ return close $fh ;
638
+ }
639
+
616
640
sub parse_diff {
617
641
my ($path ) = @_ ;
618
- my @diff = run_cmd_pipe(qw( git diff-files -p --) , $path );
642
+ my @diff_cmd = split (" " , $patch_mode_flavour {DIFF });
643
+ my @diff = run_cmd_pipe(" git" , @diff_cmd , " --" , $path );
619
644
my @colored = ();
620
645
if ($diff_use_color ) {
621
- @colored = run_cmd_pipe(qw( git diff-files -p --color --) , $path );
646
+ @colored = run_cmd_pipe(" git" , @diff_cmd , qw( --color --) , $path );
622
647
}
623
648
my (@hunk ) = { TEXT => [], DISPLAY => [], TYPE => ' header' };
624
649
@@ -877,14 +902,15 @@ sub edit_hunk_manually {
877
902
or die " failed to open hunk edit file for writing: " . $! ;
878
903
print $fh " # Manual hunk edit mode -- see bottom for a quick guide\n " ;
879
904
print $fh @$oldtext ;
905
+ my $participle = $patch_mode_flavour {PARTICIPLE };
880
906
print $fh <<EOF ;
881
907
# ---
882
908
# To remove '-' lines, make them ' ' lines (context).
883
909
# To remove '+' lines, delete them.
884
910
# Lines starting with # will be removed.
885
911
#
886
912
# If the patch applies cleanly, the edited hunk will immediately be
887
- # marked for staging . If it does not apply cleanly, you will be given
913
+ # marked for $participle . If it does not apply cleanly, you will be given
888
914
# an opportunity to edit again. If all lines of the hunk are removed,
889
915
# then the edit is aborted and the hunk is left unchanged.
890
916
EOF
@@ -918,11 +944,8 @@ sub edit_hunk_manually {
918
944
919
945
sub diff_applies {
920
946
my $fh ;
921
- open $fh , ' | git apply --recount --cached --check' ;
922
- for my $h (@_ ) {
923
- print $fh @{$h -> {TEXT }};
924
- }
925
- return close $fh ;
947
+ return run_git_apply($patch_mode_flavour {APPLY_CHECK } . ' --recount --check' ,
948
+ map { @{$_ -> {TEXT }} } @_ );
926
949
}
927
950
928
951
sub _restore_terminal_and_die {
@@ -988,12 +1011,14 @@ sub edit_hunk_loop {
988
1011
}
989
1012
990
1013
sub help_patch_cmd {
991
- print colored $help_color , <<\EOF ;
992
- y - stage this hunk
993
- n - do not stage this hunk
994
- q - quit, do not stage this hunk nor any of the remaining ones
995
- a - stage this and all the remaining hunks in the file
996
- d - do not stage this hunk nor any of the remaining hunks in the file
1014
+ my $verb = lc $patch_mode_flavour {VERB };
1015
+ my $target = $patch_mode_flavour {TARGET };
1016
+ print colored $help_color , <<EOF ;
1017
+ y - $verb this hunk$target
1018
+ n - do not $verb this hunk$target
1019
+ q - quit, do not $verb this hunk nor any of the remaining ones
1020
+ a - $verb this and all the remaining hunks in the file
1021
+ d - do not $verb this hunk nor any of the remaining hunks in the file
997
1022
g - select a hunk to go to
998
1023
/ - search for a hunk matching the given regex
999
1024
j - leave this hunk undecided, see next undecided hunk
@@ -1006,8 +1031,17 @@ sub help_patch_cmd {
1006
1031
EOF
1007
1032
}
1008
1033
1034
+ sub apply_patch {
1035
+ my $cmd = shift ;
1036
+ my $ret = run_git_apply $cmd . ' --recount' , @_ ;
1037
+ if (!$ret ) {
1038
+ print STDERR @_ ;
1039
+ }
1040
+ return $ret ;
1041
+ }
1042
+
1009
1043
sub patch_update_cmd {
1010
- my @all_mods = list_modified(' file-only ' );
1044
+ my @all_mods = list_modified($patch_mode_flavour { FILTER } );
1011
1045
my @mods = grep { !($_ -> {BINARY }) } @all_mods ;
1012
1046
my @them ;
1013
1047
@@ -1138,8 +1172,9 @@ sub patch_update_file {
1138
1172
for (@{$hunk [$ix ]{DISPLAY }}) {
1139
1173
print ;
1140
1174
}
1141
- print colored $prompt_color , ' Stage ' ,
1142
- ($hunk [$ix ]{TYPE } eq ' mode' ? ' mode change' : ' this hunk' ),
1175
+ print colored $prompt_color , $patch_mode_flavour {VERB },
1176
+ ($hunk [$ix ]{TYPE } eq ' mode' ? ' mode change' : ' this hunk' ),
1177
+ $patch_mode_flavour {TARGET },
1143
1178
" [y,n,q,a,d,/$other ,?]? " ;
1144
1179
my $line = prompt_single_character;
1145
1180
if ($line ) {
@@ -1313,16 +1348,9 @@ sub patch_update_file {
1313
1348
1314
1349
if (@result ) {
1315
1350
my $fh ;
1316
-
1317
- open $fh , ' | git apply --cached --recount' ;
1318
- for (@{$head -> {TEXT }}, @result ) {
1319
- print $fh $_ ;
1320
- }
1321
- if (!close $fh ) {
1322
- for (@{$head -> {TEXT }}, @result ) {
1323
- print STDERR $_ ;
1324
- }
1325
- }
1351
+ my @patch = (@{$head -> {TEXT }}, @result );
1352
+ my $apply_routine = $patch_mode_flavour {APPLY };
1353
+ &$apply_routine (@patch );
1326
1354
refresh();
1327
1355
}
1328
1356
0 commit comments