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 {
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.
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) {
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) {
624
redo OUTER;
625
}
626
push @{$this->{TEXT}}, $line;
627
+ push @{$this->{DISPLAY}}, $display;
628
$this->{ADDDEL}++;
629
if ($line =~ /^-/) {
630
$this->{OCNT}++;
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 {
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
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) {
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
}