@@ -73,6 +73,22 @@ sub colored {
7373# command line options
7474my $patch_mode ;
7575
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+
7692sub run_cmd_pipe {
7793 if ($^O eq ' MSWin32' || $^O eq ' msys' ) {
7894 my @invalid = grep {m /[" :*]/} @_ ;
@@ -613,12 +629,21 @@ sub add_untracked_cmd {
613629 print " \n " ;
614630}
615631
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+
616640sub parse_diff {
617641 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 );
619644 my @colored = ();
620645 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 );
622647 }
623648 my (@hunk ) = { TEXT => [], DISPLAY => [], TYPE => ' header' };
624649
@@ -877,14 +902,15 @@ sub edit_hunk_manually {
877902 or die " failed to open hunk edit file for writing: " . $! ;
878903 print $fh " # Manual hunk edit mode -- see bottom for a quick guide\n " ;
879904 print $fh @$oldtext ;
905+ my $participle = $patch_mode_flavour {PARTICIPLE };
880906 print $fh <<EOF ;
881907# ---
882908# To remove '-' lines, make them ' ' lines (context).
883909# To remove '+' lines, delete them.
884910# Lines starting with # will be removed.
885911#
886912# 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
888914# an opportunity to edit again. If all lines of the hunk are removed,
889915# then the edit is aborted and the hunk is left unchanged.
890916EOF
@@ -918,11 +944,8 @@ sub edit_hunk_manually {
918944
919945sub diff_applies {
920946 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 }} } @_ );
926949}
927950
928951sub _restore_terminal_and_die {
@@ -988,12 +1011,14 @@ sub edit_hunk_loop {
9881011}
9891012
9901013sub 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
9971022g - select a hunk to go to
9981023/ - search for a hunk matching the given regex
9991024j - leave this hunk undecided, see next undecided hunk
@@ -1006,8 +1031,17 @@ sub help_patch_cmd {
10061031EOF
10071032}
10081033
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+
10091043sub patch_update_cmd {
1010- my @all_mods = list_modified(' file-only ' );
1044+ my @all_mods = list_modified($patch_mode_flavour { FILTER } );
10111045 my @mods = grep { !($_ -> {BINARY }) } @all_mods ;
10121046 my @them ;
10131047
@@ -1138,8 +1172,9 @@ sub patch_update_file {
11381172 for (@{$hunk [$ix ]{DISPLAY }}) {
11391173 print ;
11401174 }
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 },
11431178 " [y,n,q,a,d,/$other ,?]? " ;
11441179 my $line = prompt_single_character;
11451180 if ($line ) {
@@ -1313,16 +1348,9 @@ sub patch_update_file {
13131348
13141349 if (@result ) {
13151350 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 );
13261354 refresh();
13271355 }
13281356
0 commit comments