add--interactive: allow custom diff highlighting programs

The patch hunk selector of add--interactive knows how ask git for colorized diffs, and correlate them with the uncolored diffs we apply. But there's not any way for somebody who uses a diff-filter tool like contrib's diff-highlight to see their normal highlighting. This patch lets users define an arbitrary shell command to pipe the colorized diff through. The exact output shouldn't matter (since we just show the result to humans) as long as it is line-compatible with the original diff (so that hunk-splitting can split the colorized version, too). I left two minor issues with the new system that I don't think are worth fixing right now, but could be done later: 1. We only filter colorized diffs. Theoretically a user could want to filter a non-colorized diff, but I find it unlikely in practice. Users who are doing things like diff-highlighting are likely to want color, too. 2. add--interactive will re-colorize a diff which has been hand-edited, but it won't have run through the filter. Fixing this is conceptually easy (just pipe the diff through the filter), but practically hard to do without using tempfiles (it would need to feed data to and read the result from the filter without deadlocking; this raises portability questions with respect to Windows). I've punted on both issues for now, and if somebody really cares later, they can do a patch on top. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 27, 2016 at 00:37 UTC 01143847dbf4fbf27268650f3ace16eac03b3130
2 files changed +18 -2
Documentation/config.txt
+8
@@ -1886,6 +1886,14 @@ interactive.singleKey::
1886 setting is silently ignored if portable keystroke input
1887 is not available; requires the Perl module Term::ReadKey.
1888
1889 +interactive.diffFilter::
1890 + When an interactive command (such as `git add --patch`) shows
1891 + a colorized diff, git will pipe the diff through the shell
1892 + command defined by this configuration variable. The command may
1893 + mark up the diff further for human consumption, provided that it
1894 + retains a one-to-one correspondence with the lines in the
1895 + original diff. Defaults to disabled (no filtering).
1896 +
1897 log.abbrevCommit::
1898 If true, makes linkgit:git-log[1], linkgit:git-show[1], and
1899 linkgit:git-whatchanged[1] assume `--abbrev-commit`. You may
git-add--interactive.perl
+10 -2
@@ -45,6 +45,7 @@ my ($diff_new_color) =
45 my $normal_color = $repo->get_color("", "reset");
46
47 my $diff_algorithm = $repo->config('diff.algorithm');
48 +my $diff_filter = $repo->config('interactive.difffilter');
49
50 my $use_readkey = 0;
51 my $use_termcap = 0;
@@ -754,7 +755,14 @@ sub parse_diff {
755 my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
756 my @colored = ();
757 if ($diff_use_color) {
757 - @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path);
758 + my @display_cmd = ("git", @diff_cmd, qw(--color --), $path);
759 + if (defined $diff_filter) {
760 + # quotemeta is overkill, but sufficient for shell-quoting
761 + my $diff = join(' ', map { quotemeta } @display_cmd);
762 + @display_cmd = ("$diff | $diff_filter");
763 + }
764 +
765 + @colored = run_cmd_pipe(@display_cmd);
766 }
767 my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
768
@@ -765,7 +773,7 @@ sub parse_diff {
773 }
774 push @{$hunk[-1]{TEXT}}, $diff[$i];
775 push @{$hunk[-1]{DISPLAY}},
768 - ($diff_use_color ? $colored[$i] : $diff[$i]);
776 + (@colored ? $colored[$i] : $diff[$i]);
777 }
778 return @hunk;
779 }