@@ -18,6 +18,7 @@ use warnings FATAL => 'all';
18
18
my $remove_file_add_header = 1;
19
19
my $remove_file_delete_header = 1;
20
20
my $clean_permission_changes = 1;
21
+ my $patch_mode = 0;
21
22
my $manually_color_lines = 0; # Usually git/hg colorizes the lines, but for raw patches we use this
22
23
my $change_hunk_indicators = git_config_boolean(" diff-so-fancy.changeHunkIndicators" ," true" );
23
24
my $strip_leading_indicators = git_config_boolean(" diff-so-fancy.stripLeadingSymbols" ," true" );
@@ -52,6 +53,13 @@ if ($args->{color_on}) {
52
53
$color_forced = 1;
53
54
}
54
55
56
+ # `git add --patch` requries our output to match the number of lines from the
57
+ # input. So, when patch mode is active, we print out empty lines to pad our
58
+ # output to match any lines we've consumed.
59
+ if ($args -> {patch }) {
60
+ $patch_mode = 1;
61
+ }
62
+
55
63
# We only process ARGV if we don't have STDIN
56
64
if (!$has_stdin ) {
57
65
if ($args -> {v } || $args -> {version }) {
@@ -179,6 +187,10 @@ sub do_dsf_stuff {
179
187
180
188
$last_file_seen =~ s | ^\w /|| ; # Remove a/ (and handle diff.mnemonicPrefix).
181
189
$in_hunk = 0;
190
+ if ($patch_mode ) {
191
+ # we are consuming one line, and the debt must be paid
192
+ print " \n " ;
193
+ }
182
194
# #######################################
183
195
# Find the first file: --- a/README.md #
184
196
# #######################################
@@ -282,6 +294,9 @@ sub do_dsf_stuff {
282
294
} elsif ($remove_file_delete_header && $line =~ / ^${ansi_color_regex} deleted file mode/ ) {
283
295
# Don't print the line (i.e. remove it from the output);
284
296
$last_file_mode = " delete" ;
297
+ if ($patch_mode ) {
298
+ print " \n " ;
299
+ }
285
300
# ###############################
286
301
# Look for binary file changes #
287
302
# ###############################
@@ -302,6 +317,10 @@ sub do_dsf_stuff {
302
317
}
303
318
304
319
my ($new_mode ) = $next =~ m / new mode (\d +)/ ;
320
+
321
+ if ($patch_mode ) {
322
+ print " \n " ;
323
+ }
305
324
print " $last_file_seen changed file mode from $old_mode to $new_mode \n " ;
306
325
307
326
# ##############
@@ -544,16 +563,23 @@ sub strip_leading_indicators {
544
563
if ($manually_color_lines ) {
545
564
if (defined ($5 ) && $5 eq " +" ) {
546
565
my $add_line_color = get_config_color(" add_line" );
547
- $line = $add_line_color . $line . $reset_color ;
566
+ $line = $add_line_color . insert_reset_at_line_end( $line ) ;
548
567
} elsif (defined ($5 ) && $5 eq " -" ) {
549
568
my $remove_line_color = get_config_color(" remove_line" );
550
- $line = $remove_line_color . $line . $reset_color ;
569
+ $line = $remove_line_color . insert_reset_at_line_end( $line ) ;
551
570
}
552
571
}
553
572
554
573
return $line ;
555
574
}
556
575
576
+ # Insert the color reset code at end of line, but before any newlines
577
+ sub insert_reset_at_line_end {
578
+ my $line = shift ();
579
+ $line =~ s / ^(.*)([\n\r ]+)?$/ ${1}${reset_color} ${2}/ ;
580
+ return $line ;
581
+ }
582
+
557
583
# Count the number of a given char in a string
558
584
sub char_count {
559
585
my ($needle ,$str ) = @_ ;
0 commit comments