1
#!/usr/bin/perl -w
2
3
use strict;
4
+use Git;
5
+
6
+# Prompt colors:
7
+my ($prompt_color, $header_color, $help_color, $normal_color);
8
+# Diff colors:
9
+my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_color);
10
+
11
+my ($use_color, $diff_use_color);
12
+my $repo = Git->repository();
13
+
14
+$use_color = $repo->get_colorbool('color.interactive');
15
+
16
+if ($use_color) {
17
+ # Set interactive colors:
18
+
19
+ # Grab the 3 main colors in git color string format, with sane
20
+ # (visible) defaults:
21
+ $prompt_color = $repo->get_color("color.interactive.prompt", "bold blue");
22
+ $header_color = $repo->get_color("color.interactive.header", "bold");
23
+ $help_color = $repo->get_color("color.interactive.help", "red bold");
24
+ $normal_color = $repo->get_color("", "reset");
25
+
26
+ # Do we also set diff colors?
27
+ $diff_use_color = $repo->get_colorbool('color.diff');
28
+ if ($diff_use_color) {
29
+ $new_color = $repo->get_color("color.diff.new", "green");
30
+ $old_color = $repo->get_color("color.diff.old", "red");
31
+ $fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
32
+ $metainfo_color = $repo->get_color("color.diff.meta", "bold");
33
+ $whitespace_color = $repo->get_color("color.diff.whitespace", "normal red");
34
+ }
35
+}
36
+
37
+sub colored {
38
+ my $color = shift;
39
+ my $string = join("", @_);
40
+
41
+ if ($use_color) {
42
+ # Put a color code at the beginning of each line, a reset at the end
43
+ # color after newlines that are not at the end of the string
44
+ $string =~ s/(\n+)(.)/$1$color$2/g;
45
+ # reset before newlines
46
+ $string =~ s/(\n+)/$normal_color$1/g;
47
+ # codes at beginning and end (if necessary):
48
+ $string =~ s/^/$color/;
49
+ $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
50
+ }
51
+ return $string;
52
+}
53
54
# command line options
55
my $patch_mode;
295
sub highlight_prefix {
296
my $prefix = shift;
297
my $remainder = shift;
249
- return $remainder unless defined $prefix;
250
- return is_valid_prefix($prefix) ?
251
- "[$prefix]$remainder" :
252
- "$prefix$remainder";
298
+
299
+ if (!defined $prefix) {
300
+ return $remainder;
301
+ }
302
+
303
+ if (!is_valid_prefix($prefix)) {
304
+ return "$prefix$remainder";
305
+ }
306
+
307
+ if (!$use_color) {
308
+ return "[$prefix]$remainder";
309
+ }
310
+
311
+ return "$prompt_color$prefix$normal_color$remainder";
312
}
313
314
sub list_and_choose {
325
if (!$opts->{LIST_FLAT}) {
326
print " ";
327
}
269
- print "$opts->{HEADER}\n";
328
+ print colored $header_color, "$opts->{HEADER}\n";
329
}
330
for ($i = 0; $i < @stuff; $i++) {
331
my $chosen = $chosen[$i] ? '*' : ' ';
363
364
return if ($opts->{LIST_ONLY});
365
307
- print $opts->{PROMPT};
366
+ print colored $prompt_color, $opts->{PROMPT};
367
if ($opts->{SINGLETON}) {
368
print "> ";
369
}
430
}
431
432
sub singleton_prompt_help_cmd {
374
- print <<\EOF ;
433
+ print colored $help_color, <<\EOF ;
434
Prompt help:
435
1 - select a numbered item
436
foo - select item based on unique prefix
439
}
440
441
sub prompt_help_cmd {
383
- print <<\EOF ;
442
+ print colored $help_color, <<\EOF ;
443
Prompt help:
444
1 - select a single item
445
3-5 - select a range of items
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
+ }
548
+
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, $_);
559
+ }
560
+ }
561
+ return @ret;
562
+}
563
+
564
sub hunk_splittable {
565
my ($text) = @_;
566
755
}
756
757
sub help_patch_cmd {
674
- print <<\EOF ;
758
+ print colored $help_color, <<\EOF ;
759
y - stage this hunk
760
n - do not stage this hunk
761
a - stage this and all the remaining hunks in the file
794
my ($ix, $num);
795
my $path = shift;
796
my ($head, @hunk) = parse_diff($path);
713
- for (@{$head->{TEXT}}) {
714
- print;
715
- }
797
+ print colored_diff_hunk($head->{TEXT});
798
$num = scalar @hunk;
799
$ix = 0;
800
836
if (hunk_splittable($hunk[$ix]{TEXT})) {
837
$other .= '/s';
838
}
757
- for (@{$hunk[$ix]{TEXT}}) {
758
- print;
759
- }
760
- print "Stage this hunk [y/n/a/d$other/?]? ";
839
+ print colored_diff_hunk($hunk[$ix]{TEXT});
840
+ print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
841
my $line = <STDIN>;
842
if ($line) {
843
if ($line =~ /^y/i) {
891
elsif ($other =~ /s/ && $line =~ /^s/) {
892
my @split = split_hunk($hunk[$ix]{TEXT});
893
if (1 < @split) {
814
- print "Split into ",
894
+ print colored $header_color, "Split into ",
895
scalar(@split), " hunks.\n";
896
}
897
splice(@hunk, $ix, 1,
974
HEADER => $status_head, },
975
@mods);
976
return if (!@them);
897
- system(qw(git diff-index -p --cached HEAD --),
898
- map { $_->{VALUE} } @them);
977
+ system(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);
978
}
979
980
sub quit_cmd {
983
}
984
985
sub help_cmd {
907
- print <<\EOF ;
986
+ print colored $help_color, <<\EOF ;
987
status - show paths with changes
988
update - add working tree state to the staged set of changes
989
revert - revert staged set of changes back to the HEAD version