1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/env ruby
#--
# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
incpath = File.dirname(__FILE__)
$: << incpath
require "pre_receive_guard"
require 'logger'
logger = if ENV["GITORIOUS_BASE_DIR"]
Logger.new(File.join(ENV["GITORIOUS_BASE_DIR"], "log/pre_receive_guard.log"))
else
Logger.new("/tmp/pre_receive_guard.log")
end
hook_input_lines = ""
while data = gets
hook_input_lines << data
begin
pre_receive_guard = Gitorious::SSH::PreReceiveGuard.new(ENV.to_hash, data)
oldrev, newrev, ref = data.chomp.split(" ")
if pre_receive_guard.allow_push?
if pre_receive_guard.deny_merge_request_update_with_sha?(newrev)
# it's a request to delete a merge request ref
logger.info("delete-merge-request.ref denied for #{ENV['GITORIOUS_USER'].inspect}" +
" to #{pre_receive_guard.git_spec.inspect} via " +
"#{ENV['GITORIOUS_WRITABLE_BY_URL'].inspect}")
pre_receive_guard.gitorious_says "You can't delete a merge-request ref"
exit!(1)
end
if pre_receive_guard.deny_force_pushes?
if pre_receive_guard.null_sha?(newrev)
# it's a request to delete a ref
logger.info("delete-ref denied for #{ENV['GITORIOUS_USER'].inspect} to " +
"#{pre_receive_guard.git_spec.inspect} via " +
"#{ENV['GITORIOUS_WRITABLE_BY_URL'].inspect}")
pre_receive_guard.gitorious_says "This repository does not allow deleting refs"
exit!(1)
end
if !pre_receive_guard.null_sha?(oldrev)
merge_base = `git merge-base #{oldrev} #{newrev}`.chomp
if merge_base != oldrev
# it's a force-push
logger.info("force-push denied for #{ENV['GITORIOUS_USER'].inspect} to " +
"#{pre_receive_guard.git_spec.inspect} via "+
"#{ENV['GITORIOUS_WRITABLE_BY_URL'].inspect}")
pre_receive_guard.gitorious_says "This repository does only allow fast-forwards"
exit!(1)
end
end
end
# If we get here, everything should be good to go and we
# wait for the next line to be fed to us, or just let this
# script finish..
logger.info("Allowed #{ENV['GITORIOUS_USER'].inspect} to " +
"#{pre_receive_guard.git_spec.inspect} via " +
"#{ENV['GITORIOUS_WRITABLE_BY_URL'].inspect}")
else
pre_receive_guard.gitorious_says "You do not have write access to this repository"
logger.info("Denied #{ENV['GITORIOUS_USER'].inspect} to " +
"#{pre_receive_guard.git_spec.inspect} via " +
"#{ENV['GITORIOUS_WRITABLE_BY_URL'].inspect}")
exit!(1)
end
rescue Errno::ECONNREFUSED
logger.fatal "Connection refused"
pre_receive_guard.gitorious_says "Temporary interuption. Please try again shortly"
exit!(1)
end
end
require 'pathname'
def custom_hook_path
real_path_of_this = Pathname.new(File.dirname(__FILE__)).realpath
full_path_of_this = File.expand_path(real_path_of_this)
custom_prereceive_path = full_path_of_this + "/custom-pre-receive"
end
def run_custom_hook_if_present(input)
if File.exist?(custom_hook_path)
cmd = %Q{#{custom_hook_path} 2>&1}
IO.popen(cmd, 'w+') do |subprocess|
subprocess.write(input)
subprocess.close_write
subprocess.read.split("\n").each do |l|
puts "#{l}\n"
end
end
custom_hook_status = $?
if !custom_hook_status.success?
puts "not success, exiting with its code"
exit(custom_hook_status.exitstatus)
end
end
end
run_custom_hook_if_present(hook_input_lines)
|