1. git仓库新建时加入hook代码

2. 对现有的旧hook代码进行更新的rake任务
This commit is contained in:
guange 2015-03-18 11:22:05 +08:00
parent 0ec826dd49
commit dc29ccf3b7
3 changed files with 76 additions and 7 deletions

View File

@ -92,6 +92,17 @@ class RepositoriesController < ApplicationController
render :action => 'show', :layout => 'base_projects'
end
HOOK_TEMPLATE = %Q{#!/bin/sh
exec git update-server-info
CMD_PATH=`dirname $0`
cd $CMD_PATH
PY_PATH=$PWD/../../git_refresh_changes.py
[[ -s "$PY_PATH" ]] && $(which python) $PY_PATH $(dirname $PWD)
cd -
}
def create
if params[:repository_scm].to_s == 'Gitlab'
# add by nwb
@ -127,7 +138,6 @@ class RepositoriesController < ApplicationController
if attrs[:attrs_extra].keys.any?
@repository.merge_extra_info(attrs[:attrs_extra])
end
#by xianbo
@repository.project = @project
if request.post? && @repository.save
@ -145,12 +155,11 @@ class RepositoriesController < ApplicationController
"</Limit> \n ' >> "+
@root_path+"htdocs/"+ @repository_name+"/.htaccess"
system "cd "+@project_path+" ;git update-server-info"
# if(create_repo_file&&create_passwd&&create_group&&init_repository&&add_privilege&&init_server_info)
# else
# logger.info "An error occured when authenticating "+"create passwd"+@creat_passwd+"create_group"+
# crate_group+"create repository file "+create_repo_file+"init repository"+init_repostory+
# "aad privilege to rpository"+add_privilege+"init server infos"+init_server_info
# end
File.open(@project_path+"/hooks/post-update", "w+") do |f|
f.write(HOOK_TEMPLATE)
end
@repository.update_attributes(:login => User.current.login.to_s)
end
redirect_to settings_project_url(@project, :tab => 'repositories')
@ -160,6 +169,8 @@ class RepositoriesController < ApplicationController
render :action => 'new', :layout =>'base_projects'
end
end
end
end

View File

@ -0,0 +1,30 @@
#coding=utf-8
#!/usr/bin/env python
# 脚本用于刷新版本库由git hooks里进行调用传入参数为git仓库路径
# 需要配置rails项目地址
# 必须装此文件放在git的存放目录现在是 /home/pdl/redmine-2.3.2-0/apache2/htdocs
import sys
import os
import urllib
import urllib2
RAILS_URL = 'http://192.168.128.128:3000/'
def get_git_path():
return sys.argv[1]
path=os.path.realpath(sys.argv[0])
if os.path.isfile(path):
path=os.path.dirname(os.path.dirname(path))
return os.path.abspath(path)
def post_http_data(url, data):
data_urlencode = urllib.urlencode(data)
req = urllib2.Request(url = url,data =data_urlencode)
res_data = urllib2.urlopen(req)
#res = res_data.read()
#return res
path = get_git_path()
post_http_data(RAILS_URL + 'git_callback/post_update', {'root_url': path})

View File

@ -0,0 +1,28 @@
namespace :git_post_update do
HOOK_TEMPLATE = %Q{#!/bin/sh
exec git update-server-info
CMD_PATH=`dirname $0`
cd $CMD_PATH
PY_PATH=$PWD/../../../git_refresh_changes.py
[[ -s "$PY_PATH" ]] && $(which python) $PY_PATH $(dirname $PWD)
cd -
}
desc "update old post-update file, REP_PATH"
task :update_old_file do
raise "please set REP_PATH" unless ENV["REP_PATH"]
Dir.chdir(ENV["REP_PATH"]) do
Dir.glob("**/post-update").each do |filename|
File.open(filename, "w+") do |f|
f.write(HOOK_TEMPLATE)
puts " file #{filename} changed"
end
end
end
end
end