-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbzrgrep.rb
executable file
·82 lines (72 loc) · 2.46 KB
/
bzrgrep.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
# Encoding: ISO-8859-1
##
## Bazaar Grep Revision 2016.1.23
## Copyright (C) 2010, 2014 Renato Silva
## GNU GPLv2 licensed
##
## This script searches for a given pattern in the commit history of a Bazaar
## branch. This can be used to find what revisions have introduced, removed or
## changed a given piece of code.
##
## Usage: @script.name [options] PATTERN
##
## -i, --stdin Read commit log from standard input. The expected
## format for the log is that of bzr log --show-diff.
## --from=WHERE Branch or file where to search for the pattern
## (current directory by default).
## -s, --case-sensitive PATTERN is case-sensitive.
## --no-color Disable colors in output.
##
require 'easyoptions'
options, arguments = EasyOptions.all
unless arguments[0]
puts 'Pattern is required, see --help.'
exit
end
if !options[:no_color] && ENV['TERM'] =~ /xterm/ && (system('test -t 1') || STDOUT.tty?)
$red = "\e[1;31m"
$green = "\e[0;32m"
$cyan = "\e[0;36m"
$yellow = "\e[0;33m"
$normal = "\e[0m"
end
def puts(text = nil, color = nil)
pattern = EasyOptions.options[:case_sensitive]?
/(#{EasyOptions.arguments[0]})/:
/(#{EasyOptions.arguments[0]})/i
case color
when nil then super(text)
when $cyan then super(text.gsub(pattern, "#{$cyan}\\1#{$normal}"))
when $red then super(text.gsub(pattern, "#{$red}\\1#{$normal}"))
else super("#{color}#{text}#{$normal}")
end
end
pattern = options[:case_sensitive]?
/^\s*[+\-].*#{arguments[0]}.*$/:
/^\s*[+\-].*#{arguments[0]}.*$/i
log = options[:stdin] ? STDIN : `bzr log --show-diff #{options[:from]}`
last_rev, last_file = nil
first_match = true
output = []
log.scrub.each_line do |line|
rev = line[/^\s*revno: (.*)/, 1]
file = line[/^\s*=== .*'(.*)'/, 1]
last_rev = rev if last_rev.nil? || !rev.nil?
last_file = file if last_file.nil? || !file.nil?
if line =~ pattern
unless last_rev.nil?
puts unless first_match
puts "revision #{last_rev}", $green
first_match = false
last_rev = nil
end
unless last_file.nil?
puts "\t#{last_file}", $yellow
last_file = nil
end
line_color = line.start_with?('+') ? $cyan : $red
puts "\t\t#{line.gsub(/[\n\r]/, '')}", line_color
end
end
puts