diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 0c1f65317..83cfa1e1a 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -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 " \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 diff --git a/lib/git_refresh_changes.py b/lib/git_refresh_changes.py new file mode 100644 index 000000000..a0250243e --- /dev/null +++ b/lib/git_refresh_changes.py @@ -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}) diff --git a/lib/tasks/git_post_update.rake b/lib/tasks/git_post_update.rake new file mode 100644 index 000000000..170bd0467 --- /dev/null +++ b/lib/tasks/git_post_update.rake @@ -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 \ No newline at end of file