Warning, /sdk/kde-dev-scripts/colorsvn is written in an unsupported language. File is not indexed.

0001 #! /usr/bin/env perl
0002 
0003 # colorsvn
0004 #
0005 # based on colorgcc
0006 #
0007 # Requires the ANSIColor module from CPAN.
0008 #
0009 # Usage:
0010 #
0011 # In a directory that occurs in your PATH _before_ the directory
0012 # where svn lives, create a softlink to colorsvn:
0013 #
0014 #    svn -> colorsvn
0015 #
0016 # That's it. When "svn" is invoked, colorsvn is run instead.
0017 #
0018 # The default settings can be overridden with ~/.colorcvsrc.
0019 # See the colorcvsrc-sample for more information.
0020 #
0021 # Note:
0022 #
0023 # colorsvn will only emit color codes if:
0024 # 
0025 #    (1) tts STDOUT is a tty.
0026 #    (2) the value of $TERM is not listed in the "nocolor" option.
0027 #    (3) the svn command is not a commit or import (as the text editor
0028 #    opened by svn will often be hampered by colorsvn).
0029 #
0030 # If colorsvn colorizes the output, svn's STDERR will be
0031 # combined with STDOUT. Otherwise, colorsvn just passes the output from
0032 # svn through without modification.
0033 #
0034 # Copyright 2002 Neil Stevens <neil@qualityassistant.com>
0035 #
0036 # Copyright 1999 Jamie Moyers <jmoyers@geeks.com>
0037 #
0038 # This program is free software; you can redistribute it and/or modify
0039 # it under the terms of the GNU General Public License as published by
0040 # the Free Software Foundation; version 2 of the License as published
0041 # by the Free Software Foundation.
0042 #
0043 # This program is distributed in the hope that it will be useful,
0044 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0045 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0046 # GNU General Public License for more details.
0047 #
0048 # You should have received a copy of the GNU General Public License
0049 # along with this program; if not, write to the Free Software
0050 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0051 
0052 use Term::ANSIColor;
0053 use IPC::Open3;
0054 use File::Spec::Functions qw/catfile file_name_is_absolute path/;
0055 
0056 sub initDefaults
0057 {
0058         $svnCmd = "svn";
0059 
0060         $nocolor{"dumb"} = "true";
0061 
0062         $colors{"P"} = color("reset");
0063         $colors{"U"} = color("reset");
0064         $colors{" "} = color("reset");
0065         $colors{"C"} = color("bold red");
0066         $colors{"M"} = color("bold yellow");
0067         $colors{'G'} = color("bold yellow");
0068         $colors{"A"} = color("cyan");
0069         $colors{"R"} = color("cyan");
0070         $colors{"D"} = color("red");
0071         $colors{"I"} = color("bold");
0072         $colors{"?"} = color("bold");
0073         $colors{"!"} = color("bold");
0074         $colors{"~"} = color("bold red");
0075         $colors{"server"} = color("bold green");
0076         $colors{"warning"} = color("bold cyan");
0077 
0078         # Applies when only the properties changed
0079         $propcolors{"C"} = color("bold red");
0080         $propcolors{"M"} = color("yellow");
0081 }
0082 
0083 sub loadPreferences
0084 {
0085 # Usage: loadPreferences("filename");
0086 
0087         my($filename) = @_;
0088 
0089         open(PREFS, "<$filename") || return;
0090 
0091         while(<PREFS>)
0092         {
0093                 next if (m/^\#.*/);          # It's a comment.
0094                 next if (!m/(.*):\s*(.*)/);  # It's not of the form "foo: bar".
0095 
0096                 $option = $1;
0097                 $value = $2;
0098 
0099                 if ($option =~ /svn/)
0100                 {
0101                         $svnCmd = $value;
0102                 }
0103                 elsif ($option eq "nocolor")
0104                 {
0105                         # The nocolor option lists terminal types, separated by
0106                         # spaces, not to do color on.
0107                         foreach $termtype (split(/\s+/, $value))
0108                         {
0109                                 $nocolor{$termtype} = "true";
0110                         }
0111                 }
0112                 elsif ($option =~ /prop (.)/)
0113                 {
0114                         # Property color
0115                         $propcolors{$1} = color($value);
0116                 }
0117                 else
0118                 {
0119                         $colors{$option} = color($value);
0120                 }
0121         }
0122         close(PREFS);
0123 }
0124 
0125 #
0126 # Main program
0127 #
0128 
0129 # Set up default values for colors and svn path.
0130 initDefaults();
0131 
0132 # Read the configuration file, if there is one.
0133 $configFile = $ENV{"HOME"} . "/.colorcvsrc";
0134 if (-f $configFile)
0135 {
0136         loadPreferences($configFile);
0137 }
0138 
0139 # Get the terminal type. 
0140 $terminal = $ENV{"TERM"} || "dumb";
0141 
0142 $commit = 0;
0143 foreach (@ARGV)
0144 {
0145         if(/^ci$/ || /^commit$/ || /^import$/ || /^prop/ || /^p[delsg]$/)
0146         {
0147                 $commit = 1;
0148                 break;
0149         }
0150         elsif (! /^-/)
0151         {
0152                 break;
0153         }
0154 }
0155 
0156 # If it's in the list of terminal types not to color, or if
0157 # we're writing to something that's not a tty, don't do color.
0158 if (! -t STDOUT || $commit == 1 || $nocolor{$terminal})
0159 {
0160         exec $svnCmd, @ARGV;
0161         die("Couldn't exec");
0162 }
0163 
0164 sub svn_not_found() {
0165         die ("$svnCmd not found, add svn=/full/path/to/svn to ~/.colorcvsrc");
0166 }
0167 
0168 # Check if we have SVN binary accessible. Of course, there could
0169 # be a race, but we don't care - all we want is to print out
0170 # nice error if executable SVN binary could not be found.
0171 if (file_name_is_absolute($svnCmd)) {
0172         svn_not_found unless -f $svnCmd and -x $svnCmd;
0173 } else {
0174         my $found = 0;
0175         foreach (path()) {
0176                 my $path = catfile($_, $svnCmd);
0177                 if (-f $path and -x $path) {
0178                         $found = 1;
0179                         last;
0180                 }
0181         }
0182         svn_not_found unless $found;
0183 }
0184 
0185 # Keep the pid of the svn process so we can get its return
0186 # code and use that as our return code.
0187 $svn_pid = open3('<&STDIN', \*SVNOUT, \*SVNOUT, $svnCmd, @ARGV);
0188 $svnName = $svnCmd;
0189 $svnName =~ s,.*/(.*)$,\1,;
0190 
0191 # Colorize the output from the svn program.
0192 while(<SVNOUT>)
0193 {
0194         chomp;
0195         if (m/^ (.).+/) # Property changed only
0196         {
0197                 print($propcolors{$1}, $_, color("reset"));
0198         }
0199         elsif (m/^(.).+/) # S filename
0200         {
0201                 print($colors{$1}, $_, color("reset"));
0202         }
0203         elsif (m/warning:/) # warning
0204         {
0205                 print($colors{"warning"}, $_, color("reset"));
0206         }
0207         elsif (m/^$svnName[^:]*: / || m/^svn server: /) # server message
0208         {
0209                 print($colors{"server"}, $_, color("reset"));
0210         }
0211         else # Anything else
0212         {
0213                 # Print normally.
0214                 print(color("reset"), $_);
0215         }
0216         print "\n";
0217 }
0218 
0219 # Get the return code of the svn program and exit with that.
0220 waitpid($svn_pid, 0);
0221 exit ($? >> 8);