Skip to content

please merge #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config/previously.txt
44 changes: 0 additions & 44 deletions README.rdoc

This file was deleted.

17 changes: 1 addition & 16 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,28 +24,13 @@ 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)
execute_cmd "mv #{hooks_dir}post-receive #{hooks_dir}post-receive.old.#{Time.now.to_i}" if File.exist?("#{hooks_dir}post-receive")
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 [email protected] 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?
Expand Down
4 changes: 3 additions & 1 deletion post-receive → bin/post-receive
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
projects:

email:
delivery_method: sendmail # smtp or sendmail
delivery_method: sendmail # smtp or sendmail

smtp_server:
address: localhost
Expand Down
48 changes: 0 additions & 48 deletions git_commit_notifier/lib/commit_hook.rb

This file was deleted.

17 changes: 0 additions & 17 deletions git_commit_notifier/lib/git.rb

This file was deleted.

34 changes: 34 additions & 0 deletions lib/commit_hook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.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
emailer.send
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
25 changes: 21 additions & 4 deletions git_commit_notifier/lib/diff_to_html.rb → lib/diff_to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,39 @@ 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

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

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? || defined?(Test::Unit)

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

Expand Down
60 changes: 29 additions & 31 deletions git_commit_notifier/lib/emailer.rb → lib/emailer.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
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_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_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
Expand All @@ -23,28 +28,12 @@ def boundary
@boundary = Digest::SHA1.hexdigest(seed)
end

def format_html(html_diff)
<<EOF
<html><head>
<style>#{read_css}
</style>
</head>
<body>
#{html_diff}
</body>
</html>
EOF
end

def read_css
out = ''
File.open(File.dirname(__FILE__) + '/../stylesheets/styles.css').each { |line|
out += line
}
out
def stylesheet_string
stylesheet = File.join(File.dirname(__FILE__), '/../template/styles.css')
File.read(stylesheet)
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]
Expand All @@ -60,13 +49,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 = nil)
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
Expand All @@ -77,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}",
Expand All @@ -93,10 +84,17 @@ def send
"Content-Disposition: inline\n\n\n",
@html_message,
"--#{boundary}--"]
if @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'])
else
perform_delivery_sendmail(content, @config['sendmail_options'])
end
end

end
35 changes: 35 additions & 0 deletions lib/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Git
def self.show(rev)
`git show #{rev.strip} -w`
end

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?
dir_name = `pwd`.chomp.split("/").last.gsub(/\.git$/, '')
return "#{dir_name}"
end

def self.mailing_list_address
`git config hooks.mailinglist`.strip
end
end
File renamed without changes.
Loading