-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-review
executable file
·270 lines (236 loc) · 7.77 KB
/
git-review
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env ruby
require "rubygems"
require "trollop"
require "net/smtp"
require "highline/import"
# TODO
# - make watching and unwatching perform faster
# - use EDITOR env variable
@opts = Trollop::options do
banner <<-EOS
A code review utility for git.
Usage: git review [options] [author]
Common tasks:
View unreviewed commit count: git review
Watch an author: git review -w <author>
Review next commit by author: git review -n 1 <author>
Quickly review all commits: git review -p <author>
Review a specific commit: git review -c <commit>
Options:
EOS
opt :num_commits, "-n <num>: Number of commits to review", :type => :int
opt :set_commit, "-s <commit>: Set <commit> as your last-seen commit", :type => :string
opt :commit, "-c <commit>: Review <commit> instead of your last-seen commit", :type => :string
opt :watch, "Add specified <author> to your watch list", :default => false
opt :unwatch, "Remove specified <author> from your watch list", :default => false
opt :paged, "Review commits in a paged view instead of in an editor", :default => false
opt :keep, "Keep your review files in ~/.git-review/<repository> instead of deleting them", :default => false
end
def initialize_env
ENV["LESS"] = "-XRS"
ENV["PAGER"]= "less -XRS"
STDOUT.sync = true
root_path = `git rev-parse --show-toplevel`.strip
@workspace = "#{File::expand_path("~")}/.git-review/#{File.basename(root_path)}"
@watch_file = "#{@workspace}/watches.txt"
system("mkdir -p #{@workspace}")
system("touch #{@watch_file}")
end
def initialize_smtp(server = "smtp.gmail.com", port = 587)
@smtp = Net::SMTP.new server, port
@smtp.enable_starttls
end
def initialize_reviewer_and_author_info
begin
@author = ARGV[0]
@author = author_from_commit(@opts[:commit]) if @opts[:commit]
return unless @author
author_info = `git log -n 1 --format="%an;%ae" --author=#{@author}`
@author_name, @author_email = author_info.strip.split(";")
@reviewer_name = `git config user.name`
@reviewer_email = `git config user.email`.strip
@password = nil
rescue
puts "unknown author specified"
exit
end
end
def author_from_commit(hash)
email = `git log -n 1 --format="%ae" #{hash}`
return email.match(/(.*)@/)[1]
end
def add_author_to_watch_list
File.open(@watch_file).each do |line|
author, commit = line.split
if author == @author
puts "you are already watching #{@author}"
return
end
end
log = `git log --oneline -n 11 --author=#{@author}`
unless log.empty?
hash = log.split("\n").last.split[0]
File.open(@watch_file, "a") { |file| file.puts "#{@author} #{hash}"}
puts "added #{@author} to your watch list"
end
end
def remove_author_from_watch_list
watch_text = ""
File.open(@watch_file, "r") { |file| watch_text = file.read }
unless watch_text.match(/^#{@author}\s/)
puts "you are not watching #{@author}"
return
end
watch_text = watch_text.gsub(/^#{@author}.*$\n/, "").strip
system("echo '#{watch_text}' > #{@watch_file}")
File.delete(@watch_file) if watch_text.empty?
puts "removed #{@author} from your watch list"
end
def num_commits_for_author(author)
watch_text = IO.read(@watch_file)
begin
commit = watch_text.match(/#{author} (.+)/)[1]
rescue
puts "you are not watching #{author}"
exit
end
num_commits = `git log --oneline --author=#{author} #{commit}..HEAD`.split("\n").size
num_commits
end
def show_watches
if File.zero?(@watch_file)
puts "you are not watching any authors"
puts "watch authors with git review -w <author>"
return
end
File.open(@watch_file).each do |line|
author, commit = line.split
num_commits = num_commits_for_author(author)
if num_commits.nil?
puts "#{author}'s last seen commit (#{commit}) not found, set with git review <author> -s <commit>"
else
puts "#{author} has #{num_commits} #{num_commits == 1 ? "commit" : "commits"} to review"
end
end
end
def cleanup
system("rm -f #{@workspace}/diff_*.tmp*")
system("rm -f #{@workspace}/review_*.txt*") unless @opts[:keep]
end
def get_commit_hashes
num_commits = num_commits_for_author(@author)
log = `git log -n #{num_commits} --oneline --author=#{@author}`
commits = log.split("\n").map { |line| line.split[0] }.reverse
commits = commits[0..@opts[:num_commits] - 1] if @opts[:num_commits]
commits
end
def get_commit_info(hash)
wc = `git whatchanged -n 1 --oneline #{hash}`
return { :subject => "", :files => [] } if wc == "" # May have been a merge
subject = wc.match(/ (.*)\n/)[1]
files = wc.split("\n")[1..-1].map do |line|
line.match(/\s(\S+)$/)[1]
end
return { :subject => subject, :files => files }
end
def set_author_hash(author, hash)
watch_text = ""
File.open(@watch_file, "r") { |file| watch_text = file.read }
watch_text = watch_text.gsub(/^#{author}.*$/, "#{author} #{hash}").strip
system("echo '#{watch_text}' > #{@watch_file}")
end
def update_watch_list
if @last_seen_hash
set_author_hash(@author, @last_seen_hash)
end
end
def send_email(subject, body, addresses)
@password ||= ask("Password: ") { |q| q.echo = false }
print "Sending mail..."
message = "From: #{@reviewer_name} <#{@reviewer_email}>\n"
message << "To: "
addresses.each { |address| message << "<#{address}>, " }
message << "\n"
#message << "To: #{@author_name} <#{@author_email}>\n"
message << "Subject: #{subject}\n\n"
message << body
@smtp.start("smtp.gmail.com", "#{@reviewer_email}", "#{@password}", :plain) do |smtp|
smtp.send_message(message, "#{@reviewer_email}", addresses)
end
puts "\nReview sent!"
end
def ask_to_send_email(subject, body)
print "Email review to #{@author_name} <#{@author_email}>? (Y/n): "
input = STDIN.gets.chomp
if ["", "y", "Y"].include? input
addresses = ask_for_additional_email_addresses
addresses << @author_email
send_email(subject, body, addresses) unless body.empty?
end
end
def ask_for_additional_email_addresses
addresses = []
while true
print "Also email to: "
input = STDIN.gets.chomp
break if input == ""
addresses << input
end
addresses
end
def process_commit(hash)
hash = short_hash(hash) # Use abbreviated hashes for readability
# TODO: open your own editor with convenient filenames?
if @opts[:paged]
system("clear; git show #{hash}")
else
commit_info = get_commit_info(hash)
review_file = "#{@workspace}/review_#{hash}.txt"
diff_file = "#{@workspace}/diff_#{hash}.tmp"
system("echo '#{hash} #{commit_info[:subject]}\n' > #{review_file}")
commit_info[:files].each { |filename| system("echo '#{filename}\n' >> #{review_file}") }
review_file_contents_old = IO.read(review_file)
system("git show #{hash} > #{diff_file}")
system("vi +'map q :qa<CR>' -O #{diff_file} #{review_file}")
review_file_contents_new = IO.read(review_file)
if review_file_contents_new != review_file_contents_old
subject = "Re: #{hash} by #{@author_name} #{commit_info[:subject]}"
ask_to_send_email(subject, review_file_contents_new)
end
end
@last_seen_hash = hash
end
def review_commits
commits = get_commit_hashes
if commits.empty?
puts "#{@author} has no commits to review"
else
commits.each do |hash|
process_commit(hash)
end
update_watch_list
end
end
def short_hash(hash)
`git log --format="%h" -n 1 #{hash}`.strip
end
if __FILE__ == $0
initialize_env
initialize_smtp
initialize_reviewer_and_author_info
if !@author
show_watches
elsif @opts[:watch]
add_author_to_watch_list
elsif @opts[:unwatch]
remove_author_from_watch_list
elsif @opts[:commit]
process_commit(@opts[:commit])
elsif @opts[:set_commit]
set_author_hash(@author, @opts[:set_commit])
puts "set #{@opts[:set_commit]} as last seen commit for #{@author}"
else
review_commits
end
cleanup
end