Skip to content

Instantly share code, notes, and snippets.

@nega0
Forked from scrooloose/svnhacks.sh
Last active November 11, 2019 21:28
Show Gist options
  • Save nega0/2a466f33c7377863b2755ec3d5fa0b3d to your computer and use it in GitHub Desktop.
Save nega0/2a466f33c7377863b2755ec3d5fa0b3d to your computer and use it in GitHub Desktop.
This script hijacks calls to svn and adds color and pagination to some svn commands. Source it from your ~/.zshrc.
#Author: Martin Grenfell [http://github.com/scrooloose]
#License:
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
#This script hijacks calls to svn and adds color and pagination to
#some svn commands. Source it from your ~/.zshrc.
#
#colordiff must be installed.
#-------------------------------------------------------------------
#intercept calls to svn
svn () {
#bail if the user didnt specify which subversion command to invoke
if [ $# -lt 1 ]; then
command svn
return
fi
local sub_cmd=$1
shift
## define colors
local esc=$(printf "\033")
local bred="${esc}[1;31m"
local bgreen="${esc}[1;32m"
local bblue="${esc}[1;34m"
local bmagenta="${esc}[1;35m"
local clear="${esc}[0m"
#intercept svn diff commands
if [[ $sub_cmd == diff ]]; then
#colorize the diff
#remove stupid ^M dos line endings
#page it if there's more one screen
command svn diff "$@" | colordiff | sed -e 's/\r//g' | less -RFX
#add some color to svn status output and page if needed:
#M = blue
#A = green
#D/!/~ = red
#C = magenta
#
#note that C and M can be preceded by whitespace - see $svn help status
elif [[ $sub_cmd =~ ^(status|st)$ ]]; then
command svn status "$@" | sed -e "s/^\(\([A-Z]\s\+\(+\s\+\)\?\)\?C .*\)$/${bmagenta}\1${clear}/" \
-e "s/^\(\s*M.*\)$/${bblue}\1${clear}/" \
-e "s/^\(A.*\)$/${bgreen}\1${clear}/" \
-e "s/^\(\(D\|!\|~\).*\)$/${bred}\1${clear}/" | less -RFX
#page some stuff I often end up paging manually
elif [[ $sub_cmd =~ ^(blame|help|h|cat)$ ]]; then
command svn $sub_cmd "$@" | less -F
#colorize and page svn log
#rearrange the date field from:
# 2010-10-08 21:19:24 +1300 (Fri, 08 Oct 2010)
#to:
# 2010-10-08 21:19 (Fri, +1300)
elif [[ $sub_cmd == log ]]; then
command svn log "$@" | sed -e "s/^\(.*\)|\(.*\)| \(.*\) \(.*\):[0-9]\{2\} \(.*\) (\(...\).*) |\(.*\)$/${bgreen}\1${clear}|${bblue}\2${clear}| ${bmagenta}\3 \4 (\6, \5)${clear} |\7/" | less -RFX
#let svn handle it as normal
else
command svn $sub_cmd "$@"
fi
}
# vi:sw=2:et:sts=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment