From a93169dfce8bb9895faf26de5e448813948ca051 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 12:20:23 -0800 Subject: [PATCH 01/19] Refactor, switch to an erb template, make config file optional --- README.rdoc | 44 --------------- Rakefile | 17 +----- post-receive => bin/post-receive | 4 +- .../config.yml => config/email.yml.sample | 2 - git_commit_notifier/lib/commit_hook.rb | 48 ----------------- lib/commit_hook.rb | 40 ++++++++++++++ .../lib => lib}/diff_to_html.rb | 0 {git_commit_notifier/lib => lib}/emailer.rb | 54 ++++++++----------- {git_commit_notifier/lib => lib}/git.rb | 4 ++ .../lib => lib}/result_processor.rb | 0 template/email.html.erb | 9 ++++ .../stylesheets => template}/styles.css | 0 .../test => test}/commit_hook_test.rb | 3 +- .../test => test}/diff_to_html_test.rb | 0 .../test => test}/fixtures/git_log | 0 ...w_51b986619d88f7ba98be7d271188785cbbb541a0 | 0 ...w_a4629e707d80a5769f7a71ca6ed9471015e14dc9 | 0 ...w_dce6ade4cdc2833b53bd600ef10f9bce83c7102d | 0 ...w_e28ad77bba0574241e6eb64dfd0c1291b221effe | 0 .../test => test}/result_processor_test.rb | 0 .../test => test}/test_helper.rb | 0 21 files changed, 80 insertions(+), 145 deletions(-) delete mode 100644 README.rdoc rename post-receive => bin/post-receive (58%) rename git_commit_notifier/config/config.yml => config/email.yml.sample (95%) delete mode 100644 git_commit_notifier/lib/commit_hook.rb create mode 100644 lib/commit_hook.rb rename {git_commit_notifier/lib => lib}/diff_to_html.rb (100%) rename {git_commit_notifier/lib => lib}/emailer.rb (65%) rename {git_commit_notifier/lib => lib}/git.rb (82%) rename {git_commit_notifier/lib => lib}/result_processor.rb (100%) create mode 100644 template/email.html.erb rename {git_commit_notifier/stylesheets => template}/styles.css (100%) rename {git_commit_notifier/test => test}/commit_hook_test.rb (89%) rename {git_commit_notifier/test => test}/diff_to_html_test.rb (100%) rename {git_commit_notifier/test => test}/fixtures/git_log (100%) rename {git_commit_notifier/test => test}/fixtures/git_show_51b986619d88f7ba98be7d271188785cbbb541a0 (100%) rename {git_commit_notifier/test => test}/fixtures/git_show_a4629e707d80a5769f7a71ca6ed9471015e14dc9 (100%) rename {git_commit_notifier/test => test}/fixtures/git_show_dce6ade4cdc2833b53bd600ef10f9bce83c7102d (100%) rename {git_commit_notifier/test => test}/fixtures/git_show_e28ad77bba0574241e6eb64dfd0c1291b221effe (100%) rename {git_commit_notifier/test => test}/result_processor_test.rb (100%) rename {git_commit_notifier/test => test}/test_helper.rb (100%) diff --git a/README.rdoc b/README.rdoc deleted file mode 100644 index 17a609a..0000000 --- a/README.rdoc +++ /dev/null @@ -1,44 +0,0 @@ -== Git Commit Notifier - - by Csoma Zoltan (Primalgrasp) (zoltan 'at' primalgrasp 'dot' com) - -Sends email commit messages splitting commits that were pushed in one -step. Email is delivered as text or HTML with changes refined per -word. Emails have a scanable subject containing the first sentence of -the commit as well as the author, project and branch name. - -For example: - - [rails][master] Fix Brasilia timezone. [#1180 state:resolved] - -A reply-to header is added containing the author of the commit. This -makes follow up really simple. If multiple commits are pushed at once, -emails are numbered in chronological order: - [rails][master][000] Added deprecated warning messages to Float#months and Float#years deprications. - [rails][master][001] Enhance testing for fractional days and weeks. Update changelog. - -== Requirements - -- Ruby -- RubyGems -- diff/lcs gem -- SMTP server or sendmail compatible mailer -- mocha, hpricot gems for testing - -== Installing and Configuring - -Make sure the following Git settings are correct: -- git config hooks.mailinglist (email address of the recipient, probably your mailing list address) -- git config hooks.emailprefix (application name, used in email subject) -- see /usr/local/share/git_commit_notifier/config/config.yml for overriding these settings - -Run the automated installation script: - sudo rake install - -To update already installed script, use: - sudo rake update - -See /usr/local/share/git_commit_notifier/config/config.yml for setting your SMTP server address and some other mail options. - -== License -MIT License, see the file LICENSE. diff --git a/Rakefile b/Rakefile index f28fa79..c584fe1 100644 --- a/Rakefile +++ b/Rakefile @@ -10,7 +10,7 @@ desc "Run tests" task :test do |test| Rake::TestTask.new do |t| t.libs << "test" - t.test_files = FileList['git_commit_notifier/test/*.rb'] + t.test_files = FileList['test/*.rb'] t.verbose = true end end @@ -24,15 +24,6 @@ task :install do |install| raise 'hooks directory not found for the specified project - cannot continue' unless File.exist?(hooks_dir) hooks_dir += '/' unless hooks_dir[-1,-1] == '/' - # load default config file - config = YAML::load_file('git_commit_notifier/config/config.yml') - - config.merge!({'projects' => - { project_path => - { 'application_name' => '', 'recipient_address' => ''} - } - }) - install_path = '/usr/local/share' install_script_files(install_path) @@ -40,12 +31,6 @@ task :install do |install| execute_cmd "cp post-receive #{hooks_dir}" execute_cmd "chmod a+x #{hooks_dir}post-receive" - # write config file - config_file = "#{install_path}/git_commit_notifier/config/config.yml" - File.open(config_file, 'w') do |f| - YAML.dump(config, f) - end - Dir.chdir(project_path) puts "Warning: no Git mailing list setting exists for your project. Please go to your project directory and set it with the git config hooks.mailinglist=you@yourdomain.com command or specify 'recipient_address' in the #{config_file} file else no emails can be sent out.\n\n" if `git config hooks.mailinglist`.empty? diff --git a/post-receive b/bin/post-receive similarity index 58% rename from post-receive rename to bin/post-receive index ac5533b..692ac35 100755 --- a/post-receive +++ b/bin/post-receive @@ -1,6 +1,8 @@ #!/usr/bin/env ruby # parameters: revision1, revision 2, branch -require '/usr/local/share/git_commit_notifier/lib/commit_hook' +THIS_FILE = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ +$:.unshift File.join(File.dirname(THIS_FILE), "../lib") +require "commit_hook" if ARGV[0].nil? param = STDIN.gets.strip.split diff --git a/git_commit_notifier/config/config.yml b/config/email.yml.sample similarity index 95% rename from git_commit_notifier/config/config.yml rename to config/email.yml.sample index 96a2ccd..318bd56 100644 --- a/git_commit_notifier/config/config.yml +++ b/config/email.yml.sample @@ -1,5 +1,3 @@ -projects: - email: delivery_method: sendmail # smtp or sendmail diff --git a/git_commit_notifier/lib/commit_hook.rb b/git_commit_notifier/lib/commit_hook.rb deleted file mode 100644 index 88097ff..0000000 --- a/git_commit_notifier/lib/commit_hook.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'rubygems' -require 'cgi' -require 'net/smtp' -require 'sha1' -require File.dirname(__FILE__) + '/diff_to_html' -require File.dirname(__FILE__) + '/emailer' -require File.dirname(__FILE__) + '/git' - -class CommitHook - - def self.run(rev1, rev2, ref_name, config_file = nil) - - config = YAML.load_file(config_file || '/usr/local/share/git_commit_notifier/config/config.yml') - project_path = Dir.getwd - - project_config = config['projects'] && config['projects'][project_path] ? config['projects'][project_path] : nil - - recipient = project_config ? project_config['recipient_address'] : '' - recipient = Git.mailing_list_address if recipient.empty? - - repo = project_config ? project_config['application_name'] : '' - repo = Git.prefix if repo.empty? - repo = 'scm' if repo.empty? - prefix = "[#{repo}][#{short_ref_name(ref_name)}]" - - diff2html = DiffToHtml.new - diff2html.diff_between_revisions rev1, rev2, repo, ref_name - unless recipient.empty? - diff2html.result.reverse.each_with_index do |result, i| - nr = number(diff2html.result.size, i) - emailer = Emailer.new config, project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "#{prefix}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name - emailer.send - end - end - end - - def self.number(total_entries, i) - return '' if total_entries <= 1 - digits = total_entries < 10 ? 1 : 3 - '[' + sprintf("%0#{digits}d", i) + ']' - end - - def self.short_ref_name(ref_name) - ref_name.strip.split('/').last - end - -end diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb new file mode 100644 index 0000000..758b7cc --- /dev/null +++ b/lib/commit_hook.rb @@ -0,0 +1,40 @@ +require 'rubygems' +require 'cgi' +require 'net/smtp' +require 'sha1' + +require 'diff_to_html' +require 'emailer' +require 'git' + +class CommitHook + + def self.run(rev1, rev2, ref_name) + + project_path = Dir.getwd + + recipient = Git.mailing_list_address + prefix = Git.prefix + prefix = 'git' if prefix.empty? + repo = Git.reponame + subject_prefix = "[#{prefix}][#{repo}]" + + diff2html = DiffToHtml.new + diff2html.diff_between_revisions rev1, rev2, repo, ref_name + unless recipient.empty? + diff2html.result.reverse.each_with_index do |result, i| + nr = number(diff2html.result.size, i) + emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], + "#{subject_prefix}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + emailer.send + end + end + end + + def self.number(total_entries, i) + return '' if total_entries <= 1 + digits = total_entries < 10 ? 1 : 3 + '[' + sprintf("%0#{digits}d", i) + ']' + end + +end diff --git a/git_commit_notifier/lib/diff_to_html.rb b/lib/diff_to_html.rb similarity index 100% rename from git_commit_notifier/lib/diff_to_html.rb rename to lib/diff_to_html.rb diff --git a/git_commit_notifier/lib/emailer.rb b/lib/emailer.rb similarity index 65% rename from git_commit_notifier/lib/emailer.rb rename to lib/emailer.rb index 0fff8b4..853ce0b 100644 --- a/git_commit_notifier/lib/emailer.rb +++ b/lib/emailer.rb @@ -1,19 +1,22 @@ require 'yaml' +require 'erb' class Emailer - def initialize(config, project_path, recipient, from_address, from_alias, subject, text_message, html_message, old_rev, new_rev, ref_name) - @config = config + def initialize(project_path, recipient, from_address, from_alias, subject, text_message, html_diff, old_rev, new_rev, ref_name) + @config = YAML::load_file('../config/email.yml') if File.exist?('../config/email.yml') @project_path = project_path @recipient = recipient @from_address = from_address @from_alias = from_alias @subject = subject @text_message = text_message - @html_message = format_html(html_message) @ref_name = ref_name @old_rev = old_rev @new_rev = new_rev + + template = File.join(File.dirname(__FILE__), '/../template/email.html.erb') + @html_message = ERB.new(File.read(template)).result(binding) end def boundary @@ -23,28 +26,12 @@ def boundary @boundary = Digest::SHA1.hexdigest(seed) end - def format_html(html_diff) -< - - - -#{html_diff} - - -EOF + def stylesheet_string + stylesheet = File.join(File.dirname(__FILE__), '/../template/styles.css') + File.read(stylesheet) end - def read_css - out = '' - File.open(File.dirname(__FILE__) + '/../stylesheets/styles.css').each { |line| - out += line - } - out - end - - def perform_delivery_smtp(content,smtp_settings) + def perform_delivery_smtp(content, smtp_settings) settings = { } %w(address port domain user_name password authentication).each do |key| val = smtp_settings[key].to_s.empty? ? nil : smtp_settings[key] @@ -60,13 +47,14 @@ def perform_delivery_smtp(content,smtp_settings) end end - def perform_delivery_sendmail(content, sendmail_settings) - args = '-i -t ' - args += sendmail_settings['arguments'].to_s - IO.popen("#{sendmail_settings['location']} #{args}","w+") do |f| - content.each do |line| - f.puts line - end + def perform_delivery_sendmail(content, options = {}) + sendmail_settings = { + 'location' => "/usr/sbin/sendmail", + 'arguments' => "-i -t" + }.merge(options) + command = "#{sendmail_settings['location']} #{sendmail_settings['arguments']}" + IO.popen(command, "w+") do |f| + f.write(content.join("\n")) f.flush end end @@ -93,10 +81,12 @@ def send "Content-Disposition: inline\n\n\n", @html_message, "--#{boundary}--"] - if @config['email']['delivery_method'] == 'smtp' + if @config && @config['email']['delivery_method'] == 'smtp' perform_delivery_smtp(content, @config['smtp_server']) - else + elsif @config && @config['sendmail_options'] perform_delivery_sendmail(content, @config['sendmail_options']) + else + perform_delivery_sendmail(content) end end end diff --git a/git_commit_notifier/lib/git.rb b/lib/git.rb similarity index 82% rename from git_commit_notifier/lib/git.rb rename to lib/git.rb index ced727c..b34dc2c 100644 --- a/git_commit_notifier/lib/git.rb +++ b/lib/git.rb @@ -10,6 +10,10 @@ def self.log(rev1, rev2) def self.prefix `git config hooks.emailprefix`.strip end + + def self.reponame + `pwd`.chomp.split("/").last + end def self.mailing_list_address `git config hooks.mailinglist`.strip diff --git a/git_commit_notifier/lib/result_processor.rb b/lib/result_processor.rb similarity index 100% rename from git_commit_notifier/lib/result_processor.rb rename to lib/result_processor.rb diff --git a/template/email.html.erb b/template/email.html.erb new file mode 100644 index 0000000..7798796 --- /dev/null +++ b/template/email.html.erb @@ -0,0 +1,9 @@ + + + + + <%= html_diff %> + + diff --git a/git_commit_notifier/stylesheets/styles.css b/template/styles.css similarity index 100% rename from git_commit_notifier/stylesheets/styles.css rename to template/styles.css diff --git a/git_commit_notifier/test/commit_hook_test.rb b/test/commit_hook_test.rb similarity index 89% rename from git_commit_notifier/test/commit_hook_test.rb rename to test/commit_hook_test.rb index de7fb9d..be712f1 100644 --- a/git_commit_notifier/test/commit_hook_test.rb +++ b/test/commit_hook_test.rb @@ -17,7 +17,6 @@ def test_hook emailer = mock('Emailer') Emailer.expects(:new).times(4).returns(emailer) # 4 commit, one email for each of them emailer.expects(:send).times(4) - config_file = File.dirname(__FILE__) + '/../config/config.yml' - CommitHook.run REVISIONS.first, REVISIONS.last, 'refs/heads/master', config_file + CommitHook.run REVISIONS.first, REVISIONS.last, 'refs/heads/master' end end diff --git a/git_commit_notifier/test/diff_to_html_test.rb b/test/diff_to_html_test.rb similarity index 100% rename from git_commit_notifier/test/diff_to_html_test.rb rename to test/diff_to_html_test.rb diff --git a/git_commit_notifier/test/fixtures/git_log b/test/fixtures/git_log similarity index 100% rename from git_commit_notifier/test/fixtures/git_log rename to test/fixtures/git_log diff --git a/git_commit_notifier/test/fixtures/git_show_51b986619d88f7ba98be7d271188785cbbb541a0 b/test/fixtures/git_show_51b986619d88f7ba98be7d271188785cbbb541a0 similarity index 100% rename from git_commit_notifier/test/fixtures/git_show_51b986619d88f7ba98be7d271188785cbbb541a0 rename to test/fixtures/git_show_51b986619d88f7ba98be7d271188785cbbb541a0 diff --git a/git_commit_notifier/test/fixtures/git_show_a4629e707d80a5769f7a71ca6ed9471015e14dc9 b/test/fixtures/git_show_a4629e707d80a5769f7a71ca6ed9471015e14dc9 similarity index 100% rename from git_commit_notifier/test/fixtures/git_show_a4629e707d80a5769f7a71ca6ed9471015e14dc9 rename to test/fixtures/git_show_a4629e707d80a5769f7a71ca6ed9471015e14dc9 diff --git a/git_commit_notifier/test/fixtures/git_show_dce6ade4cdc2833b53bd600ef10f9bce83c7102d b/test/fixtures/git_show_dce6ade4cdc2833b53bd600ef10f9bce83c7102d similarity index 100% rename from git_commit_notifier/test/fixtures/git_show_dce6ade4cdc2833b53bd600ef10f9bce83c7102d rename to test/fixtures/git_show_dce6ade4cdc2833b53bd600ef10f9bce83c7102d diff --git a/git_commit_notifier/test/fixtures/git_show_e28ad77bba0574241e6eb64dfd0c1291b221effe b/test/fixtures/git_show_e28ad77bba0574241e6eb64dfd0c1291b221effe similarity index 100% rename from git_commit_notifier/test/fixtures/git_show_e28ad77bba0574241e6eb64dfd0c1291b221effe rename to test/fixtures/git_show_e28ad77bba0574241e6eb64dfd0c1291b221effe diff --git a/git_commit_notifier/test/result_processor_test.rb b/test/result_processor_test.rb similarity index 100% rename from git_commit_notifier/test/result_processor_test.rb rename to test/result_processor_test.rb diff --git a/git_commit_notifier/test/test_helper.rb b/test/test_helper.rb similarity index 100% rename from git_commit_notifier/test/test_helper.rb rename to test/test_helper.rb From 224e4e24bc77bb72be9e8df545760fedde0165ab Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 12:56:55 -0800 Subject: [PATCH 02/19] Remove [git] from the subject line --- lib/commit_hook.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 758b7cc..74288d7 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -14,10 +14,8 @@ def self.run(rev1, rev2, ref_name) project_path = Dir.getwd recipient = Git.mailing_list_address - prefix = Git.prefix - prefix = 'git' if prefix.empty? repo = Git.reponame - subject_prefix = "[#{prefix}][#{repo}]" + subject_prefix = "[#{repo}]" diff2html = DiffToHtml.new diff2html.diff_between_revisions rev1, rev2, repo, ref_name From a93158794e9acb7f088b5492c5e1143ee025b33e Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 16:37:31 -0800 Subject: [PATCH 03/19] don't include .git at the end of the repo name --- lib/commit_hook.rb | 3 +-- lib/git.rb | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 74288d7..67d3d68 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -14,8 +14,7 @@ def self.run(rev1, rev2, ref_name) project_path = Dir.getwd recipient = Git.mailing_list_address - repo = Git.reponame - subject_prefix = "[#{repo}]" + subject_prefix = "[#{Git.prefix}]" diff2html = DiffToHtml.new diff2html.diff_between_revisions rev1, rev2, repo, ref_name diff --git a/lib/git.rb b/lib/git.rb index b34dc2c..cf1e9db 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -8,11 +8,9 @@ def self.log(rev1, rev2) end def self.prefix - `git config hooks.emailprefix`.strip - end - - def self.reponame - `pwd`.chomp.split("/").last + [ `git config hooks.emailprefix`.strip, + `pwd`.chomp.split("/").last.gsub(/\.git$/, '') + ].find{|p| !p.empty? } end def self.mailing_list_address From e48e472c9aa01b472d6560f84723bc94499d9f37 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 18:06:49 -0800 Subject: [PATCH 04/19] Fix the repo name variable --- lib/commit_hook.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 67d3d68..2d3c0b2 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -14,15 +14,15 @@ def self.run(rev1, rev2, ref_name) project_path = Dir.getwd recipient = Git.mailing_list_address - subject_prefix = "[#{Git.prefix}]" + prefix = Git.prefix diff2html = DiffToHtml.new - diff2html.diff_between_revisions rev1, rev2, repo, ref_name + diff2html.diff_between_revisions rev1, rev2, prefix, ref_name unless recipient.empty? diff2html.result.reverse.each_with_index do |result, i| nr = number(diff2html.result.size, i) emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "#{subject_prefix}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + "[#{prefix}]#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name emailer.send end end From 26d9c28843c321724aafe2b337213e581c700469 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 18:35:25 -0800 Subject: [PATCH 05/19] Fix the double brackets around the project name --- lib/commit_hook.rb | 2 +- lib/git.rb | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 2d3c0b2..48af699 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -22,7 +22,7 @@ def self.run(rev1, rev2, ref_name) diff2html.result.reverse.each_with_index do |result, i| nr = number(diff2html.result.size, i) emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "[#{prefix}]#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + "#{prefix}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name emailer.send end end diff --git a/lib/git.rb b/lib/git.rb index cf1e9db..34e5c3a 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -8,9 +8,10 @@ def self.log(rev1, rev2) end def self.prefix - [ `git config hooks.emailprefix`.strip, - `pwd`.chomp.split("/").last.gsub(/\.git$/, '') - ].find{|p| !p.empty? } + git_prefix = `git config hooks.emailprefix`.strip + return git_prefix unless git_prefix.empty? + dir_name = `pwd`.chomp.split("/").last.gsub(/\.git$/, '') + return "[#{dir_name}]" end def self.mailing_list_address From e655d3f4c8d884c73d63b794d1e89c9a3dca622a Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 6 Nov 2008 19:51:17 -0800 Subject: [PATCH 06/19] 11px text --- template/styles.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/styles.css b/template/styles.css index 390a4e3..bc60c4b 100644 --- a/template/styles.css +++ b/template/styles.css @@ -1,4 +1,4 @@ -* {font-size:12px;} +* {font-size:11px;} h2 {font-family:Verdana;font-size:12px;background-color: #bbb; font-weight: bold; line-height:25px; margin-bottom:2px; padding-left:5px} table {width:100%;border-collapse:collapse} .r {background-color: #fdd;} From 61c9e527e0bf47f913aa7cd9c356e2a73e3b1351 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 12 Feb 2009 07:59:59 -0800 Subject: [PATCH 07/19] First line for commit email subject instead of first sentence --- lib/diff_to_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index 1b2615d..d1792cc 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -239,7 +239,7 @@ def author_name_and_email(info) end def first_sentence(message_array) - msg = message_array.join("\n").split(/(\.\s)|\n/).first.to_s.strip + msg = message_array.first.to_s.strip return message_array.first if msg.empty? || msg =~ /^Merge\:/ msg end From 18e0034eeefb5fc279293ff16921c64b27b0c4a7 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 00:49:16 -0700 Subject: [PATCH 08/19] Include the branch name in the subject --- lib/commit_hook.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 48af699..e1ad2db 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -18,11 +18,12 @@ def self.run(rev1, rev2, ref_name) diff2html = DiffToHtml.new diff2html.diff_between_revisions rev1, rev2, prefix, ref_name + branch_name = (ref_name =~ /master$/i) ? "" : "[#{ref_name.split("/").last}]" unless recipient.empty? diff2html.result.reverse.each_with_index do |result, i| nr = number(diff2html.result.size, i) emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "#{prefix}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + "#{prefix}#{branch_name}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name emailer.send end end From e58ecaad8ec5980405c3a17d9e99d7d1d3ed7ed9 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 00:57:24 -0700 Subject: [PATCH 09/19] Do NOT take 'commit email' in the log to mean there is a commit named 'email' --- lib/diff_to_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index d1792cc..4e0dfbf 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -250,7 +250,7 @@ def diff_between_revisions(rev1, rev2, repo, branch) commits = [[rev1]] else log = Git.log(rev1, rev2) - commits = log.scan /commit\s([a-f0-9]+)/ + commits = log.scan /^commit\s([a-f0-9]+)/ end commits.each_with_index do |commit, i| From 88b4bb097921ca3005f69a5b8a1129e84436f2db Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 01:12:43 -0700 Subject: [PATCH 10/19] Print emails to STDOUT if recipient is blank --- config/email.yml.sample | 3 +-- lib/commit_hook.rb | 16 ++++++---------- lib/emailer.rb | 20 +++++++++++++------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/config/email.yml.sample b/config/email.yml.sample index 318bd56..5af9d03 100644 --- a/config/email.yml.sample +++ b/config/email.yml.sample @@ -1,5 +1,4 @@ -email: - delivery_method: sendmail # smtp or sendmail +delivery_method: sendmail # smtp or sendmail smtp_server: address: localhost diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index e1ad2db..22ded17 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -10,22 +10,18 @@ class CommitHook def self.run(rev1, rev2, ref_name) - project_path = Dir.getwd - recipient = Git.mailing_list_address prefix = Git.prefix + branch_name = (ref_name =~ /master$/i) ? "" : "[#{ref_name.split("/").last}]" diff2html = DiffToHtml.new diff2html.diff_between_revisions rev1, rev2, prefix, ref_name - branch_name = (ref_name =~ /master$/i) ? "" : "[#{ref_name.split("/").last}]" - unless recipient.empty? - diff2html.result.reverse.each_with_index do |result, i| - nr = number(diff2html.result.size, i) - emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "#{prefix}#{branch_name}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name - emailer.send - end + diff2html.result.reverse.each_with_index do |result, i| + nr = number(diff2html.result.size, i) + emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], + "#{prefix}#{branch_name}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + emailer.send end end diff --git a/lib/emailer.rb b/lib/emailer.rb index 853ce0b..092e08e 100644 --- a/lib/emailer.rb +++ b/lib/emailer.rb @@ -5,6 +5,7 @@ class Emailer def initialize(project_path, recipient, from_address, from_alias, subject, text_message, html_diff, old_rev, new_rev, ref_name) @config = YAML::load_file('../config/email.yml') if File.exist?('../config/email.yml') + @config ||= {} @project_path = project_path @recipient = recipient @from_address = from_address @@ -14,7 +15,7 @@ def initialize(project_path, recipient, from_address, from_alias, subject, text_ @ref_name = ref_name @old_rev = old_rev @new_rev = new_rev - + template = File.join(File.dirname(__FILE__), '/../template/email.html.erb') @html_message = ERB.new(File.read(template)).result(binding) end @@ -47,11 +48,11 @@ def perform_delivery_smtp(content, smtp_settings) end end - def perform_delivery_sendmail(content, options = {}) + def perform_delivery_sendmail(content, options = nil) sendmail_settings = { 'location' => "/usr/sbin/sendmail", 'arguments' => "-i -t" - }.merge(options) + }.merge(options || {}) command = "#{sendmail_settings['location']} #{sendmail_settings['arguments']}" IO.popen(command, "w+") do |f| f.write(content.join("\n")) @@ -81,12 +82,17 @@ def send "Content-Disposition: inline\n\n\n", @html_message, "--#{boundary}--"] - if @config && @config['email']['delivery_method'] == 'smtp' + + if @recipient.empty? + puts content.join("\n") + return + end + + if @config['delivery_method'] == 'smtp' perform_delivery_smtp(content, @config['smtp_server']) - elsif @config && @config['sendmail_options'] - perform_delivery_sendmail(content, @config['sendmail_options']) else - perform_delivery_sendmail(content) + perform_delivery_sendmail(content, @config['sendmail_options']) end end + end From 922686d90d061f4134026a249071a31dd213e746 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 01:40:00 -0700 Subject: [PATCH 11/19] Track commits that have already been emailed in previously.txt --- .gitignore | 1 + lib/diff_to_html.rb | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d25313d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config/previously.txt \ No newline at end of file diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index 4e0dfbf..17cfa81 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -253,6 +253,11 @@ def diff_between_revisions(rev1, rev2, repo, branch) commits = log.scan /^commit\s([a-f0-9]+)/ end + previous_file = '../config/previously.txt' + previous_list = (File.read(previous_file).split("\n") if File.exist?(previous_file)) || [] + commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } + File.open(previous_file, "a"){|f| f << commits.join("\n") << "\n" } unless commits.empty? + commits.each_with_index do |commit, i| raw_diff = Git.show(commit[0]) raise "git show output is empty" if raw_diff.empty? From 4b57d025e6c7b8323ebb56e966a8564a17f4798a Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 01:46:16 -0700 Subject: [PATCH 12/19] Try to actually know where the config dir is --- lib/diff_to_html.rb | 2 +- lib/emailer.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index 17cfa81..90c5820 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -253,7 +253,7 @@ def diff_between_revisions(rev1, rev2, repo, branch) commits = log.scan /^commit\s([a-f0-9]+)/ end - previous_file = '../config/previously.txt' + previous_file = THIS_FILE ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" previous_list = (File.read(previous_file).split("\n") if File.exist?(previous_file)) || [] commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } File.open(previous_file, "a"){|f| f << commits.join("\n") << "\n" } unless commits.empty? diff --git a/lib/emailer.rb b/lib/emailer.rb index 092e08e..74e1eef 100644 --- a/lib/emailer.rb +++ b/lib/emailer.rb @@ -4,7 +4,8 @@ class Emailer def initialize(project_path, recipient, from_address, from_alias, subject, text_message, html_diff, old_rev, new_rev, ref_name) - @config = YAML::load_file('../config/email.yml') if File.exist?('../config/email.yml') + config_file = File.join(File.dirname(THIS_FILE), '../config/email.yml') + @config = YAML::load_file(config_file) if File.exist?(config_file) @config ||= {} @project_path = project_path @recipient = recipient From 95d4c3bc05655171fb1e5400261bff9ada8774df Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Sat, 21 Mar 2009 22:54:43 -0700 Subject: [PATCH 13/19] Log the last 1000 commits circularly older than that and I bet we don't care. and I don't want a log file that will increase forever. --- lib/diff_to_html.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index 90c5820..b3222ec 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -256,7 +256,8 @@ def diff_between_revisions(rev1, rev2, repo, branch) previous_file = THIS_FILE ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" previous_list = (File.read(previous_file).split("\n") if File.exist?(previous_file)) || [] commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } - File.open(previous_file, "a"){|f| f << commits.join("\n") << "\n" } unless commits.empty? + current_list = (previous_list + commits.flatten)[-1000..-1] + File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? commits.each_with_index do |commit, i| raw_diff = Git.show(commit[0]) From 6a5b8e5d41d1c821cebab4a62b47ef987a2aa63f Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Wed, 25 Mar 2009 02:09:39 -0700 Subject: [PATCH 14/19] Just use git's notation, [pfc/branchname] and the like --- lib/commit_hook.rb | 6 +++--- lib/git.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/commit_hook.rb b/lib/commit_hook.rb index 22ded17..1587f06 100644 --- a/lib/commit_hook.rb +++ b/lib/commit_hook.rb @@ -12,15 +12,15 @@ class CommitHook def self.run(rev1, rev2, ref_name) project_path = Dir.getwd recipient = Git.mailing_list_address - prefix = Git.prefix - branch_name = (ref_name =~ /master$/i) ? "" : "[#{ref_name.split("/").last}]" + prefix = Git.repo_name + branch_name = (ref_name =~ /master$/i) ? "" : "/#{ref_name.split("/").last}" diff2html = DiffToHtml.new diff2html.diff_between_revisions rev1, rev2, prefix, ref_name diff2html.result.reverse.each_with_index do |result, i| nr = number(diff2html.result.size, i) emailer = Emailer.new project_path, recipient, result[:commit_info][:email], result[:commit_info][:author], - "#{prefix}#{branch_name}#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name + "[#{prefix}#{branch_name}]#{nr} #{result[:commit_info][:message]}", result[:text_content], result[:html_content], rev1, rev2, ref_name emailer.send end end diff --git a/lib/git.rb b/lib/git.rb index 34e5c3a..50a96fa 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -7,11 +7,11 @@ def self.log(rev1, rev2) `git log #{rev1}..#{rev2}`.strip end - def self.prefix + def self.repo_name git_prefix = `git config hooks.emailprefix`.strip return git_prefix unless git_prefix.empty? dir_name = `pwd`.chomp.split("/").last.gsub(/\.git$/, '') - return "[#{dir_name}]" + return "#{dir_name}" end def self.mailing_list_address From 2b6b084c997eca72662c530895e14ce1412d9a4e Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 26 Mar 2009 09:43:32 -0700 Subject: [PATCH 15/19] Fixes that current_list is nil when calling Array#[] with indicies that are out of range. --- lib/diff_to_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index b3222ec..ce7a0af 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -256,7 +256,7 @@ def diff_between_revisions(rev1, rev2, repo, branch) previous_file = THIS_FILE ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" previous_list = (File.read(previous_file).split("\n") if File.exist?(previous_file)) || [] commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } - current_list = (previous_list + commits.flatten)[-1000..-1] + current_list = (previous_list + commits.flatten).last(1000) File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? commits.each_with_index do |commit, i| From d145fd986286ad7da06d86bfba59e2c43065fb72 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 26 Mar 2009 10:22:18 -0700 Subject: [PATCH 16/19] Make sure we don't include the newlines in previous_list. --- lib/diff_to_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index ce7a0af..a93ed77 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -254,7 +254,7 @@ def diff_between_revisions(rev1, rev2, repo, branch) end previous_file = THIS_FILE ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" - previous_list = (File.read(previous_file).split("\n") if File.exist?(previous_file)) || [] + previous_list = (File.read(previous_file).to_a.map {|sha| sha.chomp!} if File.exist?(previous_file)) || [] commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } current_list = (previous_list + commits.flatten).last(1000) File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? From bc1a30db4d43e8069e65e50ef35643612977c68c Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Mon, 11 May 2009 16:36:13 -0700 Subject: [PATCH 17/19] Make creating a branch send the right emails. --- lib/diff_to_html.rb | 14 ++++++++++---- lib/git.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index a93ed77..8f0bcb2 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -247,20 +247,26 @@ def first_sentence(message_array) def diff_between_revisions(rev1, rev2, repo, branch) @result = [] if rev1 == rev2 - commits = [[rev1]] + commits = [rev1] + elsif rev1 =~ /^0+$/ + # creating a new remote branch + commits = Git.branch_commits(branch) + elsif rev2 =~ /^0+$/ + # deleting an existing remote branch + commits = [] else log = Git.log(rev1, rev2) - commits = log.scan /^commit\s([a-f0-9]+)/ + commits = log.scan(/^commit\s([a-f0-9]+)/).map{|match| match[0]} end - previous_file = THIS_FILE ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" + previous_file = (defined?(THIS_FILE) && THIS_FILE) ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" previous_list = (File.read(previous_file).to_a.map {|sha| sha.chomp!} if File.exist?(previous_file)) || [] commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } current_list = (previous_list + commits.flatten).last(1000) File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? commits.each_with_index do |commit, i| - raw_diff = Git.show(commit[0]) + raw_diff = Git.show(commit) raise "git show output is empty" if raw_diff.empty? @last_raw = raw_diff diff --git a/lib/git.rb b/lib/git.rb index 50a96fa..e315412 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -7,6 +7,21 @@ def self.log(rev1, rev2) `git log #{rev1}..#{rev2}`.strip end + def self.branch_commits(treeish) + args = Git.branch_heads - [Git.branch_head(treeish)] + args.map! {|tree| "^#{tree}"} + args << treeish + `git rev-list #{args.join(' ')}`.to_a.map{|commit| commit.chomp} + end + + def self.branch_heads + `git rev-parse --branches`.to_a.map{|head| head.chomp} + end + + def self.branch_head(treeish) + `git rev-parse #{treeish}`.strip + end + def self.repo_name git_prefix = `git config hooks.emailprefix`.strip return git_prefix unless git_prefix.empty? From 57dd7cbe568ead27d31245d018ea02dcc38120f3 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Mon, 11 May 2009 16:48:22 -0700 Subject: [PATCH 18/19] Fix the tests. --- lib/diff_to_html.rb | 11 ++++++++--- test/diff_to_html_test.rb | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/diff_to_html.rb b/lib/diff_to_html.rb index 8f0bcb2..8558388 100644 --- a/lib/diff_to_html.rb +++ b/lib/diff_to_html.rb @@ -259,11 +259,16 @@ def diff_between_revisions(rev1, rev2, repo, branch) commits = log.scan(/^commit\s([a-f0-9]+)/).map{|match| match[0]} end - previous_file = (defined?(THIS_FILE) && THIS_FILE) ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" - previous_list = (File.read(previous_file).to_a.map {|sha| sha.chomp!} if File.exist?(previous_file)) || [] + if defined?(Test::Unit) + previous_list = [] + else + previous_file = (defined?(THIS_FILE) && THIS_FILE) ? File.join(File.dirname(THIS_FILE), "../config/previously.txt") : "/tmp/previously.txt" + previous_list = (File.read(previous_file).to_a.map {|sha| sha.chomp!} if File.exist?(previous_file)) || [] + end + commits.reject!{|c| c.find{|sha| previous_list.include?(sha)} } current_list = (previous_list + commits.flatten).last(1000) - File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? + File.open(previous_file, "w"){|f| f << current_list.join("\n") } unless current_list.empty? || defined?(Test::Unit) commits.each_with_index do |commit, i| raw_diff = Git.show(commit) diff --git a/test/diff_to_html_test.rb b/test/diff_to_html_test.rb index d2140ba..9a9acbe 100644 --- a/test/diff_to_html_test.rb +++ b/test/diff_to_html_test.rb @@ -68,7 +68,7 @@ def test_single_commit diff = DiffToHtml.new diff.diff_between_revisions REVISIONS.first, REVISIONS.first, 'testproject', 'master' assert_equal 1, diff.result.size # single result for a single commit - assert_equal 'Allow use of :path_prefix and :name_prefix outside of namespaced routes', diff.result.first[:commit_info][:message] + assert_equal 'Allow use of :path_prefix and :name_prefix outside of namespaced routes. [#1188 state:resolved]', diff.result.first[:commit_info][:message] assert_equal 'Tom Stuart', diff.result.first[:commit_info][:author] assert_equal 'tom@experthuman.com', diff.result.first[:commit_info][:email] From fae95ea6f105309b927654fafe2bdf33779870a3 Mon Sep 17 00:00:00 2001 From: Andrian Jardan Date: Fri, 13 Aug 2010 19:36:28 +0800 Subject: [PATCH 19/19] Add an X-Mailer header to make filtering easier --- lib/emailer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/emailer.rb b/lib/emailer.rb index 74e1eef..2913ec0 100644 --- a/lib/emailer.rb +++ b/lib/emailer.rb @@ -67,6 +67,7 @@ def send "Reply-To: #{from}", "To: #{@recipient}", "Subject: #{@subject}", + "X-Mailer: git-commit-notifier", "X-Git-Refname: #{@ref_name}", "X-Git-Oldrev: #{@old_rev}", "X-Git-Newrev: #{@new_rev}",