Skip to content

Instantly share code, notes, and snippets.

@brettstimmerman
Created April 9, 2011 01:19
Show Gist options
  • Save brettstimmerman/911010 to your computer and use it in GitHub Desktop.
Save brettstimmerman/911010 to your computer and use it in GitHub Desktop.
Quick n' dirty svn log search.
#!/usr/bin/env ruby
trap(:SIGINT){}
opts = {}
pattern = []
help = <<HELP
svn-slog filters output from `svn log`. It reads standard input if given,
otherwise `svn log` is run in the background.
Usage: svn-slog [options] [PATTERN]
Options:
-u, --user <user> Limit to commits by the specified user.
-h, --help Display this help message.
-v, --version Display version information.
Background `svn log` options:
-p, --path <path> Limit to the specified path.
-r, --revision <revision> Limit to the specified revision or range.
HELP
while arg = ARGV.shift do
unless arg.start_with?("-")
pattern << arg
next
end
case arg
when "--help", "-h", "-?"
puts help
exit
when "--path", "-p"
opts[:path] = ARGV.shift
next
when "--revision", "-rev", "-r"
rev = ARGV.shift
# Stupid hack to extract rare datetime format: {yyyy-mm-dd hh:ii:ss}
rev += " #{ARGV.shift}" until rev.count("{") == rev.count("}")
opts[:revision] = rev
next
when "--user", "-u"
opts[:user] = ARGV.shift
next
when "--version", "-v"
puts "svn-slog v0.1.0"
exit
else
abort("Invalid option: #{arg}")
end
end
pattern = pattern.join("")
path = opts[:path] || "."
revision = opts[:revision] && "-r '#{opts[:revision]}'"
user = opts[:user] && "| sed -n '/#{opts[:user]}/,/----$/ p'"
if $stdin.tty?
log = `svn log #{path} #{revision} #{user}`
else
log = []
$stdin.each_line {|line| log << line }
log = log.join("")
log = `echo #{log.inspect} #{user}` if user
end
results = log.split(/^-+$/).select do |log|
log.strip! && !log.empty? && (pattern.empty? || log.match(/#{pattern}/i))
end
unless results.empty?
bar = "#{'-' * 72}"
puts "#{bar}\n" + results.join("\n#{bar}\n") + "\n#{bar}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment