use strict;
use Git;
-# Prompt colors:
-my ($prompt_color, $header_color, $help_color, $normal_color);
-# Diff colors:
-my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_color);
-
-my ($use_color, $diff_use_color);
my $repo = Git->repository();
-$use_color = $repo->get_colorbool('color.interactive');
-
-if ($use_color) {
- # Set interactive colors:
+my $menu_use_color = $repo->get_colorbool('color.interactive');
+my ($prompt_color, $header_color, $help_color) =
+ $menu_use_color ? (
+ $repo->get_color('color.interactive.prompt', 'bold blue'),
+ $repo->get_color('color.interactive.header', 'bold'),
+ $repo->get_color('color.interactive.help', 'red bold'),
+ ) : ();
- # Grab the 3 main colors in git color string format, with sane
- # (visible) defaults:
- $prompt_color = $repo->get_color("color.interactive.prompt", "bold blue");
- $header_color = $repo->get_color("color.interactive.header", "bold");
- $help_color = $repo->get_color("color.interactive.help", "red bold");
- $normal_color = $repo->get_color("", "reset");
+my $diff_use_color = $repo->get_colorbool('color.diff');
+my ($fraginfo_color) =
+ $diff_use_color ? (
+ $repo->get_color('color.diff.frag', 'cyan'),
+ ) : ();
- # Do we also set diff colors?
- $diff_use_color = $repo->get_colorbool('color.diff');
- if ($diff_use_color) {
- $new_color = $repo->get_color("color.diff.new", "green");
- $old_color = $repo->get_color("color.diff.old", "red");
- $fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
- $metainfo_color = $repo->get_color("color.diff.meta", "bold");
- $whitespace_color = $repo->get_color("color.diff.whitespace", "normal red");
- }
-}
+my $normal_color = $repo->get_color("", "reset");
sub colored {
my $color = shift;
my $string = join("", @_);
- if ($use_color) {
+ if (defined $color) {
# Put a color code at the beginning of each line, a reset at the end
# color after newlines that are not at the end of the string
$string =~ s/(\n+)(.)/$1$color$2/g;
return "$prefix$remainder";
}
- if (!$use_color) {
+ if (!$menu_use_color) {
return "[$prefix]$remainder";
}
sub parse_diff {
my ($path) = @_;
my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
- my (@hunk) = { TEXT => [] };
-
- for (@diff) {
- if (/^@@ /) {
- push @hunk, { TEXT => [] };
- }
- push @{$hunk[-1]{TEXT}}, $_;
+ my @colored = ();
+ if ($diff_use_color) {
+ @colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
}
- return @hunk;
-}
+ my (@hunk) = { TEXT => [], DISPLAY => [] };
-sub colored_diff_hunk {
- my ($text) = @_;
- # return the text, so that it can be passed to print()
- my @ret;
- for (@$text) {
- if (!$diff_use_color) {
- push @ret, $_;
- next;
- }
-
- if (/^\+/) {
- push @ret, colored($new_color, $_);
- } elsif (/^\-/) {
- push @ret, colored($old_color, $_);
- } elsif (/^\@/) {
- push @ret, colored($fraginfo_color, $_);
- } elsif (/^ /) {
- push @ret, colored($normal_color, $_);
- } else {
- push @ret, colored($metainfo_color, $_);
+ for (my $i = 0; $i < @diff; $i++) {
+ if ($diff[$i] =~ /^@@ /) {
+ push @hunk, { TEXT => [], DISPLAY => [] };
}
+ push @{$hunk[-1]{TEXT}}, $diff[$i];
+ push @{$hunk[-1]{DISPLAY}},
+ ($diff_use_color ? $colored[$i] : $diff[$i]);
}
- return @ret;
+ return @hunk;
}
sub hunk_splittable {
}
sub split_hunk {
- my ($text) = @_;
+ my ($text, $display) = @_;
my @split = ();
-
+ if (!defined $display) {
+ $display = $text;
+ }
# If there are context lines in the middle of a hunk,
# it can be split, but we would need to take care of
# overlaps later.
my $i = $hunk_start - 1;
my $this = +{
TEXT => [],
+ DISPLAY => [],
OLD => $o_ofs,
NEW => $n_ofs,
OCNT => 0,
NCNT => 0,
ADDDEL => 0,
POSTCTX => 0,
+ USE => undef,
};
while (++$i < @$text) {
my $line = $text->[$i];
+ my $display = $display->[$i];
if ($line =~ /^ /) {
if ($this->{ADDDEL} &&
!defined $next_hunk_start) {
$next_hunk_start = $i;
}
push @{$this->{TEXT}}, $line;
+ push @{$this->{DISPLAY}}, $display;
$this->{OCNT}++;
$this->{NCNT}++;
if (defined $next_hunk_start) {
redo OUTER;
}
push @{$this->{TEXT}}, $line;
+ push @{$this->{DISPLAY}}, $display;
$this->{ADDDEL}++;
if ($line =~ /^-/) {
$this->{OCNT}++;
" +$n_ofs" .
(($n_cnt != 1) ? ",$n_cnt" : '') .
" @@\n");
+ my $display_head = $head;
unshift @{$hunk->{TEXT}}, $head;
+ if ($diff_use_color) {
+ $display_head = colored($fraginfo_color, $head);
+ }
+ unshift @{$hunk->{DISPLAY}}, $display_head;
}
- return map { $_->{TEXT} } @split;
+ return @split;
}
sub find_last_o_ctx {
my ($ix, $num);
my $path = shift;
my ($head, @hunk) = parse_diff($path);
- print colored_diff_hunk($head->{TEXT});
+ for (@{$head->{DISPLAY}}) {
+ print;
+ }
$num = scalar @hunk;
$ix = 0;
if (hunk_splittable($hunk[$ix]{TEXT})) {
$other .= '/s';
}
- print colored_diff_hunk($hunk[$ix]{TEXT});
+ for (@{$hunk[$ix]{DISPLAY}}) {
+ print;
+ }
print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
my $line = <STDIN>;
if ($line) {
next;
}
elsif ($other =~ /s/ && $line =~ /^s/) {
- my @split = split_hunk($hunk[$ix]{TEXT});
+ my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
if (1 < @split) {
print colored $header_color, "Split into ",
scalar(@split), " hunks.\n";
}
- splice(@hunk, $ix, 1,
- map { +{ TEXT => $_, USE => undef } }
- @split);
+ splice (@hunk, $ix, 1, @split);
$num = scalar @hunk;
next;
}