Skip to content

Commit 13d3f89

Browse files
committed
Add a patch mode for git add --patch
In this new patch mode, every time a line is consumed, we print out an empty line in its place. This keeps the same number of lines in the output, and appeases git.
1 parent 4f5a0e2 commit 13d3f89

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

diff-so-fancy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use warnings FATAL => 'all';
1717
my $remove_file_add_header = 1;
1818
my $remove_file_delete_header = 1;
1919
my $clean_permission_changes = 1;
20+
my $patch_mode = 0;
2021
my $manually_color_lines = 0; # Usually git/hg colorizes the lines, but for raw patches we use this
2122
my $change_hunk_indicators = git_config_boolean("diff-so-fancy.changeHunkIndicators","true");
2223
my $strip_leading_indicators = git_config_boolean("diff-so-fancy.stripLeadingSymbols","true");
@@ -51,6 +52,13 @@ if ($args->{color_on}) {
5152
$color_forced = 1;
5253
}
5354

55+
# `git add --patch` requries our output to match the number of lines from the
56+
# input. So, when patch mode is active, we print out empty lines to pad our
57+
# output to match any lines we've consumed.
58+
if ($args->{patch}) {
59+
$patch_mode = 1;
60+
}
61+
5462
# We only process ARGV if we don't have STDIN
5563
if (!$has_stdin) {
5664
if ($args->{v} || $args->{version}) {
@@ -178,6 +186,10 @@ sub do_dsf_stuff {
178186

179187
$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
180188
$in_hunk = 0;
189+
if ($patch_mode) {
190+
# we are consuming one line, and the debt must be paid
191+
print "\n";
192+
}
181193
########################################
182194
# Find the first file: --- a/README.md #
183195
########################################
@@ -281,6 +293,9 @@ sub do_dsf_stuff {
281293
} elsif ($remove_file_delete_header && $line =~ /^${ansi_color_regex}deleted file mode/) {
282294
# Don't print the line (i.e. remove it from the output);
283295
$last_file_mode = "delete";
296+
if ($patch_mode) {
297+
print "\n";
298+
}
284299
################################
285300
# Look for binary file changes #
286301
################################
@@ -301,6 +316,10 @@ sub do_dsf_stuff {
301316
}
302317

303318
my ($new_mode) = $next =~ m/new mode (\d+)/;
319+
320+
if ($patch_mode) {
321+
print "\n";
322+
}
304323
print "$last_file_seen changed file mode from $old_mode to $new_mode\n";
305324

306325
###############

0 commit comments

Comments
 (0)