From 41861185c0874612b0c50dd6d1ee3ceacc06e190 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Wed, 15 Feb 2012 17:12:49 +0600 Subject: [PATCH] [refs #134] fixed git hook --- app/models/project.rb | 2 +- bin/post-receive-hook_dev | 15 ++++++++++++ bin/post-receive-hook_prod | 15 ++++++++++++ ...120131141651_write_git_hook_to_projects.rb | 13 ----------- lib/tasks/hook.rake | 23 +++++++++++++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 bin/post-receive-hook_dev create mode 100644 bin/post-receive-hook_prod delete mode 100644 db/migrate/20120131141651_write_git_hook_to_projects.rb create mode 100644 lib/tasks/hook.rake diff --git a/app/models/project.rb b/app/models/project.rb index 9238119fa..56105d2e1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -219,7 +219,7 @@ class Project < ActiveRecord::Base def write_hook hook_file = File.join(path, 'hooks', 'post-receive') - FileUtils.cp(File.join(::Rails.root.to_s, 'lib', 'post-receive-hook'), hook_file) + FileUtils.cp(File.join(::Rails.root.to_s, 'bin', "post-receive-hook#{ENV['RAILS_ENV'] == 'production' ? '_prod' : '_dev'}"), hook_file) #File.chmod(0775, hook_file) # need? rescue Exception # FIXME end diff --git a/bin/post-receive-hook_dev b/bin/post-receive-hook_dev new file mode 100644 index 000000000..b577daa5b --- /dev/null +++ b/bin/post-receive-hook_dev @@ -0,0 +1,15 @@ +#!/bin/bash + +# This file was placed here by rosa-team. It makes sure that your pushed commits will be processed properly. + +pwd=`pwd` +reponame=`basename $pwd .git` +owner=`basename \`dirname $pwd\`` + +while read oldrev newrev ref +do + newrev_type=$(git cat-file -t $newrev 2> /dev/null) + oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null) + + /bin/bash -l -c "cd /srv/rosa_build/current && bundle exec rails runner 'Project.delay.process_hook(\"$owner\", \"$reponame\", \"$newrev\", \"$oldrev\", \"$ref\", \"$newrev_type\", \"$oldrev_type\")'" +done \ No newline at end of file diff --git a/bin/post-receive-hook_prod b/bin/post-receive-hook_prod new file mode 100644 index 000000000..6e9fb4bd8 --- /dev/null +++ b/bin/post-receive-hook_prod @@ -0,0 +1,15 @@ +#!/bin/bash + +# This file was placed here by rosa-team. It makes sure that your pushed commits will be processed properly. + +pwd=`pwd` +reponame=`basename $pwd .git` +owner=`basename \`dirname $pwd\`` + +while read oldrev newrev ref +do + newrev_type=$(git cat-file -t $newrev 2> /dev/null) + oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null) + + /bin/bash -l -c "cd /srv/rosa_build/current && RAILS_ENV=production bundle exec rails runner 'Project.delay.process_hook(\"$owner\", \"$reponame\", \"$newrev\", \"$oldrev\", \"$ref\", \"$newrev_type\", \"$oldrev_type\")'" > /dev/null 2>&1 +done \ No newline at end of file diff --git a/db/migrate/20120131141651_write_git_hook_to_projects.rb b/db/migrate/20120131141651_write_git_hook_to_projects.rb deleted file mode 100644 index d8dded706..000000000 --- a/db/migrate/20120131141651_write_git_hook_to_projects.rb +++ /dev/null @@ -1,13 +0,0 @@ -class WriteGitHookToProjects < ActiveRecord::Migration - def self.up - origin_hook = File.join(::Rails.root.to_s, 'lib', 'post-receive-hook') - Project.all.each do |project| - hook_file = File.join(project.path, 'hooks', 'post-receive') - FileUtils.cp(origin_hook, hook_file) - end - end - - def self.down - Project.all.each { |project| FileUtils.rm_rf File.join(project.path, 'hooks', 'post-receive')} - end -end diff --git a/lib/tasks/hook.rake b/lib/tasks/hook.rake new file mode 100644 index 000000000..9b4a0212b --- /dev/null +++ b/lib/tasks/hook.rake @@ -0,0 +1,23 @@ +namespace :hook do + desc "Inserting hook to all repos" + task :install => :environment do + origin_hook = File.join(::Rails.root.to_s, 'bin', "post-receive-hook#{ENV['RAILS_ENV'] == 'production' ? '_prod' : '_dev'}") + say "process.. #{origin_hook}" + count = 0 + Project.all.each do |project| + hook_file = File.join(project.path, 'hooks', 'post-receive') + FileUtils.cp(origin_hook, hook_file) + count = count + 1 + end + say "Done! Writing to #{count.to_s} repo(s)" + end + + desc "remove with git hook" + task :remove => :environment do + say "process.." + count = 0 + Project.all.each { |project| FileUtils.rm_rf File.join(project.path, 'hooks', 'post-receive'); count = count + 1} + say "Done! Removing from #{count.to_s} repo(s)" + end +end +