PoshCode Logo PowerShell Code Repository

Colorize Subversion SVN (modification of post by Bishop view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1186"></script>download | new post

Colorize STAT, UPDATE and DIFF (without params) commands output for Subversion (svn) and Mercurial (hg).
Here’s a PowerShell functions that you can use to make those numerous commands you run every day via the PowerShell CLI a little easier to read by adding colors.
Autodetect for svn or hg.
Update autodetect to work with psdrives that are a UNC path. Also now works with repositorys in the root directory of drive.
Added $args to each function so you can do things like sd -r2 -rtip -U0

  1. # detect source control management software
  2. function findscm {
  3.         $scm = ''
  4.         :selectscm foreach ($_ in @('svn', 'hg')) {
  5.                 $dir = (Get-Location).ProviderPath.trimEnd("\")
  6.                 while ($dir.Length -gt 3) {                    
  7.                         if (Test-Path ([IO.Path]::combine(($dir), ".$_"))) {                   
  8.                                 $scm = $_
  9.                                 break selectscm
  10.                         }
  11.                         $dir = $dir -Replace '\\[^\\]+$', ''
  12.                 }
  13.         }
  14.         return $scm
  15. }
  16.  
  17. # draw output
  18. function drawlines($colors, $cmd) {
  19.         $scm = findscm
  20.         if (!$cmd -or !$scm) { return }
  21.         foreach ($line in (&$scm ($cmd).split())) {
  22.                 $color = $colors[[string]$line[0]]
  23.                 if ($color) {
  24.                         write-host $line -Fore $color
  25.                 } else {
  26.                         write-host $line
  27.                 }
  28.         }
  29. }
  30.  
  31. # svn stat
  32. function st {
  33.         drawlines @{ "A"="Magenta"; "D"="Red"; "C"="Yellow"; "G"="Blue"; "M"="Cyan"; "U"="Green"; "?"="DarkGray"; "!"="DarkRed" } "stat $args"
  34. }
  35.  
  36. # svn update
  37. function su {
  38.         drawlines @{ "A"="Magenta"; "D"="Red"; "U"="Green"; "C"="Yellow"; "G"="Blue"; } "up $args"
  39. }
  40.  
  41. # svn diff
  42. function sd {
  43.         drawlines @{ "@"="Magenta"; "-"="Red"; "+"="Green"; "="="DarkGray"; } "diff $args"
  44. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me