Skip to content

Commit 9aca025

Browse files
Eric WongJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5cc2de commit 9aca025

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

git-svn.perl

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ sub nag_lib {
6060
my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
6161
my $sha1 = qr/[a-f\d]{40}/;
6262
my $sha1_short = qr/[a-f\d]{4,40}/;
63+
my $_esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
6364
my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
6465
$_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
6566
$_repack, $_repack_nr, $_repack_flags, $_q,
@@ -68,7 +69,8 @@ sub nag_lib {
6869
$_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
6970
$_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
7071
$_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);
7274
my (@_branch_from, %tree_map, %users, %rusers, %equiv);
7375
my ($_svn_co_url_revs, $_svn_pg_peg_revs);
7476
my @repo_path_split_cache;
@@ -135,6 +137,8 @@ sub nag_lib {
135137
'show-commit' => \$_show_commit,
136138
'non-recursive' => \$_non_recursive,
137139
'authors-file|A=s' => \$_authors,
140+
'color' => \$_color,
141+
'pager=s' => \$_pager,
138142
} ],
139143
'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
140144
{ 'message|m=s' => \$_message,
@@ -759,16 +763,17 @@ sub show_log {
759763
}
760764
}
761765

766+
config_pager();
762767
my $pid = open(my $log,'-|');
763768
defined $pid or croak $!;
764769
if (!$pid) {
765770
exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
766771
}
767-
setup_pager();
772+
run_pager();
768773
my (@k, $c, $d);
769774

770775
while (<$log>) {
771-
if (/^commit ($sha1_short)/o) {
776+
if (/^${_esc_color}commit ($sha1_short)/o) {
772777
my $cmt = $1;
773778
if ($c && cmt_showable($c) && $c->{r} != $r_last) {
774779
$r_last = $c->{r};
@@ -777,25 +782,25 @@ sub show_log {
777782
}
778783
$d = undef;
779784
$c = { c => $cmt };
780-
} elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
785+
} elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
781786
get_author_info($c, $1, $2, $3);
782-
} elsif (/^(?:tree|parent|committer) /) {
787+
} elsif (/^${_esc_color}(?:tree|parent|committer) /) {
783788
# ignore
784-
} elsif (/^:\d{6} \d{6} $sha1_short/o) {
789+
} elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
785790
push @{$c->{raw}}, $_;
786-
} elsif (/^[ACRMDT]\t/) {
791+
} elsif (/^${_esc_color}[ACRMDT]\t/) {
787792
# we could add $SVN->{svn_path} here, but that requires
788793
# remote access at the moment (repo_path_split)...
789-
s#^([ACRMDT])\t# $1 #;
794+
s#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
790795
push @{$c->{changed}}, $_;
791-
} elsif (/^diff /) {
796+
} elsif (/^${_esc_color}diff /) {
792797
$d = 1;
793798
push @{$c->{diff}}, $_;
794799
} elsif ($d) {
795800
push @{$c->{diff}}, $_;
796-
} elsif (/^ (git-svn-id:.+)$/) {
801+
} elsif (/^${_esc_color} (git-svn-id:.+)$/) {
797802
($c->{url}, $c->{r}, undef) = extract_metadata($1);
798-
} elsif (s/^ //) {
803+
} elsif (s/^${_esc_color} //) {
799804
push @{$c->{l}}, $_;
800805
}
801806
}
@@ -901,12 +906,30 @@ sub cmt_showable {
901906
return defined $c->{r};
902907
}
903908

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+
904926
sub git_svn_log_cmd {
905927
my ($r_min, $r_max) = @_;
906928
my @cmd = (qw/git-log --abbrev-commit --pretty=raw
907929
--default/, "refs/remotes/$GIT_SVN");
908930
push @cmd, '-r' unless $_non_recursive;
909931
push @cmd, qw/--raw --name-status/ if $_verbose;
932+
push @cmd, '--color' if log_use_color();
910933
return @cmd unless defined $r_max;
911934
if ($r_max == $r_min) {
912935
push @cmd, '--max-count=1';
@@ -2533,23 +2556,27 @@ sub tz_to_s_offset {
25332556
return ($1 * 60) + ($tz * 3600);
25342557
}
25352558

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;
25432566
}
2567+
}
2568+
2569+
sub run_pager {
2570+
return unless -t *STDOUT;
25442571
pipe my $rfd, my $wfd or return;
25452572
defined(my $pid = fork) or croak $!;
25462573
if (!$pid) {
25472574
open STDOUT, '>&', $wfd or croak $!;
25482575
return;
25492576
}
25502577
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";
25532580
}
25542581

25552582
sub get_author_info {

0 commit comments

Comments
 (0)