Skip to content

Commit 927f984

Browse files
author
root
committed
Swich to GRack 2.x
1 parent 83e1880 commit 927f984

File tree

4 files changed

+140
-119
lines changed

4 files changed

+140
-119
lines changed

Diff for: Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Gitolite Admin repository management
22
gem 'gitolite-rugged', git: 'https://github.com/jbox-web/gitolite-rugged.git', tag: '1.2.0'
33

4-
# Ruby/Rack Git Smart-HTTP Server Handler (use our own repository because Redmine uses Rails 4.2 and Rack 1.6)
5-
gem 'gitlab-grack', git: 'https://github.com/jbox-web/grack.git', require: 'grack', branch: 'fix_rails4'
4+
# Ruby/Rack Git Smart-HTTP Server Handler
5+
gem 'gitlab-grack', '~> 2.0.0', git: 'https://github.com/jbox-web/grack.git', require: 'grack', branch: 'fix_gemfile'
66

77
# HAML views
88
gem 'haml-rails'

Diff for: lib/redmine_git_hosting.rb

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def loglevel
6565
# Redmine SCM adapter
6666
require_dependency 'redmine/scm/adapters/xitolite_adapter'
6767

68-
# Hrack for Git Hooks
6968
require 'hrack/init'
7069

7170
# Extensions for Faker

Diff for: lib/redmine_git_hosting/patches/grack_git_patch.rb

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
require 'grack/git'
2+
3+
module RedmineGitHosting
4+
module Patches
5+
module GrackGitPatch
6+
7+
def initialize(git_path, repo_path, user)
8+
@user = user
9+
super(git_path, repo_path)
10+
end
11+
12+
13+
# Override original *valid_repo?* method to test directory presence with Sudo
14+
def valid_repo?
15+
directory_exists?(repo)
16+
end
17+
18+
19+
# Override original *command* method to prefix the command with Sudo and other args.
20+
#
21+
def command(cmd)
22+
git_command_with_sudo(cmd)
23+
end
24+
25+
26+
# Override original *capture* method because the original IO.popen().read let zombie process behind.
27+
# This method is called :
28+
# * to get repository Git config (http.uploadpack || http.receivepack)
29+
# * to get repository info refs :
30+
# 0087deab8f3d612a47e7e153ed21bbc52a480205035a refs/heads/devel report-status delete-refs side-band-64k quiet ofs-delta agent=git/1.9.1
31+
# * to get repository refs :
32+
# 003f91a7b1dad21020e96d52119c585881c02f2fae45 refs/heads/master
33+
34+
# Note : *service_rpc* also calls IO.popen but pass a block !.
35+
# Passing a block to IO.popen auto-close the pipe/thread.
36+
37+
def capture(command)
38+
# Extract Args
39+
cmd = command.shift
40+
args = command
41+
42+
begin
43+
RedmineGitHosting::Utils::Exec.capture(cmd, args)
44+
rescue => e
45+
logger.error('Problems while getting SmartHttp params')
46+
# Return empty string since the next method will call *chomp* on it
47+
''
48+
end
49+
end
50+
51+
52+
# Override original *popen_options* method.
53+
# The original one try to chdir before executing the command by
54+
# passing 'chdir: @dir' option to IO.popen.
55+
# This is wrong as we can't chdir to Gitolite directory.
56+
# Notes : this method is called in *service_rpc* (not overriden)
57+
#
58+
def popen_options
59+
{ unsetenv_others: true }
60+
end
61+
62+
63+
# Override original *popen_env* method.
64+
# The original one passes useless arg (GL_ID) to IO.popen.
65+
# Notes : this method is called in *service_rpc* (not overriden)
66+
#
67+
def popen_env
68+
{ 'PATH' => ENV['PATH'] }
69+
end
70+
71+
72+
private
73+
74+
75+
def directory_exists?(dir)
76+
RedmineGitHosting::Commands.sudo_dir_exists?(dir)
77+
end
78+
79+
80+
# We sometimes need to add *--git-dir* arg to Git command otherwise
81+
# Git looks for the repository in the current path.
82+
def git_command_with_sudo(params)
83+
if command_require_chdir?(params.last)
84+
git_command_with_chdir.concat(params)
85+
else
86+
git_command_without_chdir.concat(params)
87+
end
88+
end
89+
90+
91+
def command_require_chdir?(cmd)
92+
cmd == 'update-server-info' || cmd == 'http.receivepack' || cmd == 'http.uploadpack' || cmd == 'rev-parse'
93+
end
94+
95+
96+
def git_command_without_chdir
97+
RedmineGitHosting::Commands.sudo_git_cmd(smart_http_args)
98+
end
99+
100+
101+
def git_command_with_chdir
102+
RedmineGitHosting::Commands.sudo_git_args_for_repo(@repo, smart_http_args)
103+
end
104+
105+
106+
def smart_http_args
107+
[
108+
'env',
109+
"GL_BINDIR=#{RedmineGitHosting::Config.gitolite_bin_dir}",
110+
"GL_LIBDIR=#{RedmineGitHosting::Config.gitolite_lib_dir}",
111+
"GL_REPO=#{repository_object.gitolite_repository_name}",
112+
"GL_USER=#{@user}"
113+
]
114+
end
115+
116+
117+
def logger
118+
RedmineGitHosting.logger
119+
end
120+
121+
122+
def repository_object
123+
@repository_object ||= Repository::Xitolite.find_by_path(@repo, loose: true)
124+
end
125+
126+
end
127+
end
128+
end
129+
130+
unless Grack::Git.included_modules.include?(RedmineGitHosting::Patches::GrackGitPatch)
131+
Grack::Git.send(:prepend, RedmineGitHosting::Patches::GrackGitPatch)
132+
end

Diff for: lib/redmine_git_hosting/patches/grack_server_patch.rb

+6-116
Original file line numberDiff line numberDiff line change
@@ -4,128 +4,18 @@ module RedmineGitHosting
44
module Patches
55
module GrackServerPatch
66

7-
# Override original *get_git_dir* method because the path is relative
8-
# and accessed via Sudo.
9-
#
10-
def get_git_dir(path)
7+
# Override original *get_git* method to set the right path for the repository.
8+
# Also pass the *@env['REMOTE_USER']* variable to the Git constructor so we
9+
# can pass it to Gitolite hooks later.
10+
def get_git(path)
1111
path = gitolite_path(path)
12-
if !directory_exists?(path)
13-
false
14-
else
15-
path # TODO: check is a valid git directory
16-
end
17-
end
18-
19-
20-
# Override original *git_command* method to prefix the command with Sudo and other args.
21-
#
22-
def git_command(params)
23-
git_command_with_sudo(params)
12+
Grack::Git.new(@config[:git_path], path, @env['REMOTE_USER'])
2413
end
2514

26-
27-
# Override original *capture* method because the original IO.popen().read let zombie process behind.
28-
#
29-
# This method is called :
30-
# * to get repository Git config (http.uploadpack || http.receivepack)
31-
# * to get repository info refs :
32-
# 0087deab8f3d612a47e7e153ed21bbc52a480205035a refs/heads/devel report-status delete-refs side-band-64k quiet ofs-delta agent=git/1.9.1
33-
# * to get repository refs :
34-
# 003f91a7b1dad21020e96d52119c585881c02f2fae45 refs/heads/master
35-
#
36-
# Note : *service_rpc* also calls IO.popen but pass a block !.
37-
# Passing a block to IO.popen auto-close the pipe/thread.
38-
#
39-
def capture(command)
40-
# Extract Args
41-
cmd = command.shift
42-
args = command
43-
44-
begin
45-
RedmineGitHosting::Utils::Exec.capture(cmd, args)
46-
rescue => e
47-
logger.error('Problems while getting SmartHttp params')
48-
end
49-
end
50-
51-
52-
# Override original *popen_options* method.
53-
# The original one try to chdir before executing the command by
54-
# passing 'chdir: @dir' option to IO.popen.
55-
# This is wrong as we can't chdir to Gitolite directory.
56-
# Notes : this method is called in *service_rpc* (not overriden)
57-
#
58-
def popen_options
59-
{ unsetenv_others: true }
60-
end
61-
62-
63-
# Override original *popen_env* method.
64-
# The original one passes useless arg (GL_ID) to IO.popen.
65-
# Notes : this method is called in *service_rpc* (not overriden)
66-
#
67-
def popen_env
68-
{ 'PATH' => ENV['PATH'] }
69-
end
70-
71-
7215
private
7316

74-
7517
def gitolite_path(path)
76-
File.join(RedmineGitHosting::Config.gitolite_global_storage_dir, RedmineGitHosting::Config.gitolite_redmine_storage_dir, path)
77-
end
78-
79-
80-
def directory_exists?(dir)
81-
RedmineGitHosting::Commands.sudo_dir_exists?(dir)
82-
end
83-
84-
85-
# We sometimes need to add *--git-dir* arg to Git command otherwise
86-
# Git looks for the repository in the current path.
87-
def git_command_with_sudo(params)
88-
if command_require_chdir?(params.last)
89-
git_command_with_chdir.concat(params)
90-
else
91-
git_command_without_chdir.concat(params)
92-
end
93-
end
94-
95-
96-
def command_require_chdir?(cmd)
97-
cmd == 'update-server-info' || cmd == 'http.receivepack' || cmd == 'http.uploadpack'
98-
end
99-
100-
101-
def git_command_without_chdir
102-
RedmineGitHosting::Commands.sudo_git_cmd(smart_http_args)
103-
end
104-
105-
106-
def git_command_with_chdir
107-
RedmineGitHosting::Commands.sudo_git_args_for_repo(@dir, smart_http_args)
108-
end
109-
110-
111-
def smart_http_args
112-
[
113-
'env',
114-
"GL_BINDIR=#{RedmineGitHosting::Config.gitolite_bin_dir}",
115-
"GL_LIBDIR=#{RedmineGitHosting::Config.gitolite_lib_dir}",
116-
"GL_REPO=#{repository_object.gitolite_repository_name}",
117-
"GL_USER=#{@env['REMOTE_USER']}"
118-
]
119-
end
120-
121-
122-
def logger
123-
RedmineGitHosting.logger
124-
end
125-
126-
127-
def repository_object
128-
@repo ||= Repository::Xitolite.find_by_path(@dir, loose: true)
18+
File.join(RedmineGitHosting::Config.gitolite_home_dir, RedmineGitHosting::Config.gitolite_global_storage_dir, RedmineGitHosting::Config.gitolite_redmine_storage_dir, path)
12919
end
13020

13121
end

0 commit comments

Comments
 (0)