git-svn: color support for the log command
* match LESS environment settings to those in pager.c * parse diff.color and pager.color settings in the config file, and pass --color to git-log * --color and --pager= settings are supported Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Nov 28, 2006 at 18:51 UTC
9aca025849d417b04b49fead126c915f6ca47526
1 file changed
+47
-20
git-svn.perl
+47
-20
@@ -60,6 +60,7 @@ nag_lib() unless $_use_lib;
60
my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
61
my $sha1 = qr/[a-f\d]{40}/;
62
my $sha1_short = qr/[a-f\d]{4,40}/;
63
+my $_esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
64
my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
65
$_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
66
$_repack, $_repack_nr, $_repack_flags, $_q,
@@ -68,7 +69,8 @@ my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
69
$_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
70
$_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
71
$_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive,
71
- $_username, $_config_dir, $_no_auth_cache, $_xfer_delta);
72
+ $_username, $_config_dir, $_no_auth_cache, $_xfer_delta,
73
+ $_pager, $_color);
74
my (@_branch_from, %tree_map, %users, %rusers, %equiv);
75
my ($_svn_co_url_revs, $_svn_pg_peg_revs);
76
my @repo_path_split_cache;
@@ -135,6 +137,8 @@ my %cmd = (
137
'show-commit' => \$_show_commit,
138
'non-recursive' => \$_non_recursive,
139
'authors-file|A=s' => \$_authors,
140
+ 'color' => \$_color,
141
+ 'pager=s' => \$_pager,
142
} ],
143
'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
144
{ 'message|m=s' => \$_message,
@@ -759,16 +763,17 @@ sub show_log {
763
}
764
}
765
766
+ config_pager();
767
my $pid = open(my $log,'-|');
768
defined $pid or croak $!;
769
if (!$pid) {
770
exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
771
}
767
- setup_pager();
772
+ run_pager();
773
my (@k, $c, $d);
774
775
while (<$log>) {
771
- if (/^commit ($sha1_short)/o) {
776
+ if (/^${_esc_color}commit ($sha1_short)/o) {
777
my $cmt = $1;
778
if ($c && cmt_showable($c) && $c->{r} != $r_last) {
779
$r_last = $c->{r};
@@ -777,25 +782,25 @@ sub show_log {
782
}
783
$d = undef;
784
$c = { c => $cmt };
780
- } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
785
+ } elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
786
get_author_info($c, $1, $2, $3);
782
- } elsif (/^(?:tree|parent|committer) /) {
787
+ } elsif (/^${_esc_color}(?:tree|parent|committer) /) {
788
# ignore
784
- } elsif (/^:\d{6} \d{6} $sha1_short/o) {
789
+ } elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
790
push @{$c->{raw}}, $_;
786
- } elsif (/^[ACRMDT]\t/) {
791
+ } elsif (/^${_esc_color}[ACRMDT]\t/) {
792
# we could add $SVN->{svn_path} here, but that requires
793
# remote access at the moment (repo_path_split)...
789
- s#^([ACRMDT])\t# $1 #;
794
+ s#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
795
push @{$c->{changed}}, $_;
791
- } elsif (/^diff /) {
796
+ } elsif (/^${_esc_color}diff /) {
797
$d = 1;
798
push @{$c->{diff}}, $_;
799
} elsif ($d) {
800
push @{$c->{diff}}, $_;
796
- } elsif (/^ (git-svn-id:.+)$/) {
801
+ } elsif (/^${_esc_color} (git-svn-id:.+)$/) {
802
($c->{url}, $c->{r}, undef) = extract_metadata($1);
798
- } elsif (s/^ //) {
803
+ } elsif (s/^${_esc_color} //) {
804
push @{$c->{l}}, $_;
805
}
806
}
@@ -901,12 +906,30 @@ sub cmt_showable {
906
return defined $c->{r};
907
}
908
909
+sub log_use_color {
910
+ return 1 if $_color;
911
+ my $dc;
912
+ chomp($dc = `git-repo-config --get diff.color`);
913
+ if ($dc eq 'auto') {
914
+ if (-t *STDOUT || (defined $_pager &&
915
+ `git-repo-config --bool --get pager.color` !~ /^false/)) {
916
+ return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
917
+ }
918
+ return 0;
919
+ }
920
+ return 0 if $dc eq 'never';
921
+ return 1 if $dc eq 'always';
922
+ chomp($dc = `git-repo-config --bool --get diff.color`);
923
+ $dc eq 'true';
924
+}
925
+
926
sub git_svn_log_cmd {
927
my ($r_min, $r_max) = @_;
928
my @cmd = (qw/git-log --abbrev-commit --pretty=raw
929
--default/, "refs/remotes/$GIT_SVN");
930
push @cmd, '-r' unless $_non_recursive;
931
push @cmd, qw/--raw --name-status/ if $_verbose;
932
+ push @cmd, '--color' if log_use_color();
933
return @cmd unless defined $r_max;
934
if ($r_max == $r_min) {
935
push @cmd, '--max-count=1';
@@ -2533,14 +2556,18 @@ sub tz_to_s_offset {
2556
return ($1 * 60) + ($tz * 3600);
2557
}
2558
2536
-sub setup_pager { # translated to Perl from pager.c
2537
- return unless (-t *STDOUT);
2538
- my $pager = $ENV{PAGER};
2539
- if (!defined $pager) {
2540
- $pager = 'less';
2541
- } elsif (length $pager == 0 || $pager eq 'cat') {
2542
- return;
2559
+# adapted from pager.c
2560
+sub config_pager {
2561
+ $_pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
2562
+ if (!defined $_pager) {
2563
+ $_pager = 'less';
2564
+ } elsif (length $_pager == 0 || $_pager eq 'cat') {
2565
+ $_pager = undef;
2566
}
2567
+}
2568
+
2569
+sub run_pager {
2570
+ return unless -t *STDOUT;
2571
pipe my $rfd, my $wfd or return;
2572
defined(my $pid = fork) or croak $!;
2573
if (!$pid) {
@@ -2548,8 +2575,8 @@ sub setup_pager { # translated to Perl from pager.c
2575
return;
2576
}
2577
open STDIN, '<&', $rfd or croak $!;
2551
- $ENV{LESS} ||= '-S';
2552
- exec $pager or croak "Can't run pager: $!\n";;
2578
+ $ENV{LESS} ||= 'FRSX';
2579
+ exec $_pager or croak "Can't run pager: $! ($_pager)\n";
2580
}
2581
2582
sub get_author_info {