Skip to content

Commit

Permalink
Add a patch mode for git add --patch
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wren committed Dec 7, 2020
1 parent 4f5a0e2 commit 13d3f89
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use warnings FATAL => 'all';
my $remove_file_add_header = 1;
my $remove_file_delete_header = 1;
my $clean_permission_changes = 1;
my $patch_mode = 0;
my $manually_color_lines = 0; # Usually git/hg colorizes the lines, but for raw patches we use this
my $change_hunk_indicators = git_config_boolean("diff-so-fancy.changeHunkIndicators","true");
my $strip_leading_indicators = git_config_boolean("diff-so-fancy.stripLeadingSymbols","true");
Expand Down Expand Up @@ -51,6 +52,13 @@ if ($args->{color_on}) {
$color_forced = 1;
}

# `git add --patch` requries our output to match the number of lines from the
# input. So, when patch mode is active, we print out empty lines to pad our
# output to match any lines we've consumed.
if ($args->{patch}) {
$patch_mode = 1;
}

# We only process ARGV if we don't have STDIN
if (!$has_stdin) {
if ($args->{v} || $args->{version}) {
Expand Down Expand Up @@ -178,6 +186,10 @@ sub do_dsf_stuff {

$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
$in_hunk = 0;
if ($patch_mode) {
# we are consuming one line, and the debt must be paid
print "\n";
}
########################################
# Find the first file: --- a/README.md #
########################################
Expand Down Expand Up @@ -281,6 +293,9 @@ sub do_dsf_stuff {
} elsif ($remove_file_delete_header && $line =~ /^${ansi_color_regex}deleted file mode/) {
# Don't print the line (i.e. remove it from the output);
$last_file_mode = "delete";
if ($patch_mode) {
print "\n";
}
################################
# Look for binary file changes #
################################
Expand All @@ -301,6 +316,10 @@ sub do_dsf_stuff {
}

my ($new_mode) = $next =~ m/new mode (\d+)/;

if ($patch_mode) {
print "\n";
}
print "$last_file_seen changed file mode from $old_mode to $new_mode\n";

###############
Expand Down

0 comments on commit 13d3f89

Please sign in to comment.