Commit 34690142 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Implement commit transaction with pre-receive and post-receive hooks for web editor

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 69c193e7
...@@ -38,7 +38,7 @@ gem "browser", '~> 0.8.0' ...@@ -38,7 +38,7 @@ gem "browser", '~> 0.8.0'
# Extracting information from a git repository # Extracting information from a git repository
# Provide access to Gitlab::Git library # Provide access to Gitlab::Git library
gem "gitlab_git", '~> 7.2.12' gem "gitlab_git", '~> 7.2.13'
# Ruby/Rack Git Smart-HTTP Server Handler # Ruby/Rack Git Smart-HTTP Server Handler
# GitLab fork with a lot of changes (improved thread-safety, better memory usage etc) # GitLab fork with a lot of changes (improved thread-safety, better memory usage etc)
......
...@@ -271,7 +271,7 @@ GEM ...@@ -271,7 +271,7 @@ GEM
mime-types (~> 1.19) mime-types (~> 1.19)
gitlab_emoji (0.1.0) gitlab_emoji (0.1.0)
gemojione (~> 2.0) gemojione (~> 2.0)
gitlab_git (7.2.12) gitlab_git (7.2.13)
activesupport (~> 4.0) activesupport (~> 4.0)
charlock_holmes (~> 0.6) charlock_holmes (~> 0.6)
gitlab-linguist (~> 3.0) gitlab-linguist (~> 3.0)
...@@ -783,7 +783,7 @@ DEPENDENCIES ...@@ -783,7 +783,7 @@ DEPENDENCIES
gitlab-grack (~> 2.0.2) gitlab-grack (~> 2.0.2)
gitlab-linguist (~> 3.0.1) gitlab-linguist (~> 3.0.1)
gitlab_emoji (~> 0.1) gitlab_emoji (~> 0.1)
gitlab_git (~> 7.2.12) gitlab_git (~> 7.2.13)
gitlab_meta (= 7.0) gitlab_meta (= 7.0)
gitlab_omniauth-ldap (= 1.2.1) gitlab_omniauth-ldap (= 1.2.1)
gollum-lib (~> 4.0.2) gollum-lib (~> 4.0.2)
...@@ -875,4 +875,4 @@ DEPENDENCIES ...@@ -875,4 +875,4 @@ DEPENDENCIES
wikicloth (= 0.8.1) wikicloth (= 0.8.1)
BUNDLED WITH BUNDLED WITH
1.10.4 1.10.6
...@@ -373,7 +373,7 @@ class Repository ...@@ -373,7 +373,7 @@ class Repository
options[:author] = committer options[:author] = committer
options[:commit] = { options[:commit] = {
message: message, message: message,
branch: ref branch: ref,
} }
options[:file] = { options[:file] = {
......
require 'securerandom'
class CommitService
def self.transaction(project, current_user, ref)
repository = project.repository
path_to_repo = repository.path_to_repo
# Create temporary ref
random_string = SecureRandom.hex
tmp_ref = "refs/tmp/#{random_string}/head"
target = repository.find_branch(ref).target
repository.rugged.references.create(tmp_ref, target)
# Make commit in tmp ref
sha = yield(tmp_ref)
unless sha
raise 'Failed to create commit'
end
# Run GitLab pre-receive hook
status = PreCommitService.new(project, current_user).execute(sha, ref)
if status
# Update head
repository.rugged.references.update(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
# Run GitLab post receive hook
PostCommitService.new(project, current_user).execute(sha, ref)
else
# Remove tmp ref and return error to user
repository.rugged.references.delete(tmp_ref)
raise 'Commit was rejected by pre-reveive hook'
end
end
end
...@@ -22,7 +22,6 @@ module Files ...@@ -22,7 +22,6 @@ module Files
end end
if sha = commit if sha = commit
after_commit(sha, @target_branch)
success success
else else
error("Something went wrong. Your changes were not committed") error("Something went wrong. Your changes were not committed")
...@@ -33,10 +32,6 @@ module Files ...@@ -33,10 +32,6 @@ module Files
private private
def after_commit(sha, branch)
PostCommitService.new(project, current_user).execute(sha, branch)
end
def current_branch def current_branch
@current_branch ||= params[:current_branch] @current_branch ||= params[:current_branch]
end end
......
...@@ -3,7 +3,9 @@ require_relative "base_service" ...@@ -3,7 +3,9 @@ require_relative "base_service"
module Files module Files
class CreateService < Files::BaseService class CreateService < Files::BaseService
def commit def commit
repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch) CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref)
end
end end
def validate def validate
......
...@@ -3,7 +3,9 @@ require_relative "base_service" ...@@ -3,7 +3,9 @@ require_relative "base_service"
module Files module Files
class DeleteService < Files::BaseService class DeleteService < Files::BaseService
def commit def commit
repository.remove_file(current_user, @file_path, @commit_message, @target_branch) CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.remove_file(current_user, @file_path, @commit_message, tmp_ref)
end
end end
end end
end end
...@@ -3,7 +3,9 @@ require_relative "base_service" ...@@ -3,7 +3,9 @@ require_relative "base_service"
module Files module Files
class UpdateService < Files::BaseService class UpdateService < Files::BaseService
def commit def commit
repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch) CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref)
end
end end
end end
end end
class PreCommitService < BaseService
include Gitlab::Popen
attr_reader :changes, :repo_path
def execute(sha, branch)
commit = repository.commit(sha)
full_ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
old_sha = commit.parent_id || Gitlab::Git::BLANK_SHA
@changes = "#{old_sha} #{sha} #{full_ref}"
@repo_path = repository.path_to_repo
pre_receive
end
private
def pre_receive
hook = hook_file('pre-receive', repo_path)
return true if hook.nil?
call_receive_hook(hook)
end
def call_receive_hook(hook)
# function will return true if succesful
exit_status = false
vars = {
'GL_ID' => Gitlab::ShellEnv.gl_id(current_user),
'PWD' => repo_path
}
options = {
chdir: repo_path
}
# we combine both stdout and stderr as we don't know what stream
# will be used by the custom hook
Open3.popen2e(vars, hook, options) do |stdin, stdout_stderr, wait_thr|
exit_status = true
stdin.sync = true
# in git, pre- and post- receive hooks may just exit without
# reading stdin. We catch the exception to avoid a broken pipe
# warning
begin
# inject all the changes as stdin to the hook
changes.lines do |line|
stdin.puts line
end
rescue Errno::EPIPE
end
# need to close stdin before reading stdout
stdin.close
# only output stdut_stderr if scripts doesn't return 0
unless wait_thr.value == 0
exit_status = false
end
end
exit_status
end
def hook_file(hook_type, repo_path)
hook_path = File.join(repo_path.strip, 'hooks')
hook_file = "#{hook_path}/#{hook_type}"
hook_file if File.exist?(hook_file)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment