@matheusbsbs / matheus-bagliano / commits / 4af756f31b
Fork 0

Teach "git add -i" to colorize whitespace errors

Rather than replicating the colorization logic of "git diff-files" we rely on "git diff-files" itself. This guarantees consistent colorization in and outside "git add -i". Seeing as speed is not a concern here (the bottleneck is how fast the user can read, not how fast "git diff-files" runs) we do this by actually running it twice, once without color and once with. In this way as the whitespace colorization provided by "git diff-files" evolves (per-path attributes, new classes of whitespace error), "git add -i" will automatically benefit from it and stay in synch. Also, by working with two sets of diff output (an uncolorized one for internal processing and a colorized one for display only) we minimize the risk of regressions because the changes required to implement this are minimally invasive. Signed-off-by: Wincent Colaiuta <win@wincent.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Wincent Colaiuta committed Dec 7, 2007 at 13:35 UTC 4af756f31b0696ed1ca6ad10dc6d7053477edc16
1 file changed +34 -39
git-add--interactive.perl
+34 -39
@@ -525,40 +525,21 @@ sub add_untracked_cmd {
525 sub parse_diff {
526 my ($path) = @_;
527 my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
528 - my (@hunk) = { TEXT => [] };
529 -
530 - for (@diff) {
531 - if (/^@@ /) {
532 - push @hunk, { TEXT => [] };
533 - }
534 - push @{$hunk[-1]{TEXT}}, $_;
528 + my @colored = ();
529 + if ($diff_use_color) {
530 + @colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
531 }
536 - return @hunk;
537 -}
538 -
539 -sub colored_diff_hunk {
540 - my ($text) = @_;
541 - # return the text, so that it can be passed to print()
542 - my @ret;
543 - for (@$text) {
544 - if (!$diff_use_color) {
545 - push @ret, $_;
546 - next;
547 - }
532 + my (@hunk) = { TEXT => [], DISPLAY => [] };
533
549 - if (/^\+/) {
550 - push @ret, colored($new_color, $_);
551 - } elsif (/^\-/) {
552 - push @ret, colored($old_color, $_);
553 - } elsif (/^\@/) {
554 - push @ret, colored($fraginfo_color, $_);
555 - } elsif (/^ /) {
556 - push @ret, colored($normal_color, $_);
557 - } else {
558 - push @ret, colored($metainfo_color, $_);
534 + for (my $i = 0; $i < @diff; $i++) {
535 + if ($diff[$i] =~ /^@@ /) {
536 + push @hunk, { TEXT => [], DISPLAY => [] };
537 }
538 + push @{$hunk[-1]{TEXT}}, $diff[$i];
539 + push @{$hunk[-1]{DISPLAY}},
540 + ($diff_use_color ? $colored[$i] : $diff[$i]);
541 }
561 - return @ret;
542 + return @hunk;
543 }
544
545 sub hunk_splittable {
@@ -578,9 +559,11 @@ sub parse_hunk_header {
559 }
560
561 sub split_hunk {
581 - my ($text) = @_;
562 + my ($text, $display) = @_;
563 my @split = ();
583 -
564 + if (!defined $display) {
565 + $display = $text;
566 + }
567 # If there are context lines in the middle of a hunk,
568 # it can be split, but we would need to take care of
569 # overlaps later.
@@ -594,16 +577,19 @@ sub split_hunk {
577 my $i = $hunk_start - 1;
578 my $this = +{
579 TEXT => [],
580 + DISPLAY => [],
581 OLD => $o_ofs,
582 NEW => $n_ofs,
583 OCNT => 0,
584 NCNT => 0,
585 ADDDEL => 0,
586 POSTCTX => 0,
587 + USE => undef,
588 };
589
590 while (++$i < @$text) {
591 my $line = $text->[$i];
592 + my $display = $display->[$i];
593 if ($line =~ /^ /) {
594 if ($this->{ADDDEL} &&
595 !defined $next_hunk_start) {
@@ -615,6 +601,7 @@ sub split_hunk {
601 $next_hunk_start = $i;
602 }
603 push @{$this->{TEXT}}, $line;
604 + push @{$this->{DISPLAY}}, $display;
605 $this->{OCNT}++;
606 $this->{NCNT}++;
607 if (defined $next_hunk_start) {
@@ -637,6 +624,7 @@ sub split_hunk {
624 redo OUTER;
625 }
626 push @{$this->{TEXT}}, $line;
627 + push @{$this->{DISPLAY}}, $display;
628 $this->{ADDDEL}++;
629 if ($line =~ /^-/) {
630 $this->{OCNT}++;
@@ -661,9 +649,14 @@ sub split_hunk {
649 " +$n_ofs" .
650 (($n_cnt != 1) ? ",$n_cnt" : '') .
651 " @@\n");
652 + my $display_head = $head;
653 unshift @{$hunk->{TEXT}}, $head;
654 + if ($diff_use_color) {
655 + $display_head = colored($fraginfo_color, $head);
656 + }
657 + unshift @{$hunk->{DISPLAY}}, $display_head;
658 }
666 - return map { $_->{TEXT} } @split;
659 + return @split;
660 }
661
662 sub find_last_o_ctx {
@@ -794,7 +787,9 @@ sub patch_update_file {
787 my ($ix, $num);
788 my $path = shift;
789 my ($head, @hunk) = parse_diff($path);
797 - print colored_diff_hunk($head->{TEXT});
790 + for (@{$head->{DISPLAY}}) {
791 + print;
792 + }
793 $num = scalar @hunk;
794 $ix = 0;
795
@@ -836,7 +831,9 @@ sub patch_update_file {
831 if (hunk_splittable($hunk[$ix]{TEXT})) {
832 $other .= '/s';
833 }
839 - print colored_diff_hunk($hunk[$ix]{TEXT});
834 + for (@{$hunk[$ix]{DISPLAY}}) {
835 + print;
836 + }
837 print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
838 my $line = <STDIN>;
839 if ($line) {
@@ -889,14 +886,12 @@ sub patch_update_file {
886 next;
887 }
888 elsif ($other =~ /s/ && $line =~ /^s/) {
892 - my @split = split_hunk($hunk[$ix]{TEXT});
889 + my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
890 if (1 < @split) {
891 print colored $header_color, "Split into ",
892 scalar(@split), " hunks.\n";
893 }
897 - splice(@hunk, $ix, 1,
898 - map { +{ TEXT => $_, USE => undef } }
899 - @split);
894 + splice (@hunk, $ix, 1, @split);
895 $num = scalar @hunk;
896 next;
897 }