提交 a431ca0f 编写于 作者: K Kamil Trzcinski

Don't execute git hooks if you create branch as part of other change

Currently, our procedure for adding a commit requires us to execute `CreateBranchService` before file creation.
It's OK, but also we do execute `git hooks` (the `PostReceive` sidekiq job) as part of this process.
However, this hook is execute before the file is actually committed, so the ref is updated.

Secondly, we do execute a `git hooks` after committing file and updating ref.
This results in duplicate `PostReceive` jobs, where the first one is completely invalid.

This change makes the branch creation, something that is intermediate step of bigger process (file creation or update, commit cherry pick or revert) to not execute git hooks.
上级 b328c788
......@@ -160,16 +160,20 @@ class Repository
tags.find { |tag| tag.name == name }
end
def add_branch(user, branch_name, target)
def add_branch(user, branch_name, target, with_hooks: true)
oldrev = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
target = commit(target).try(:id)
return false unless target
if with_hooks
GitHooksService.new.execute(user, path_to_repo, oldrev, target, ref) do
update_ref!(ref, target, oldrev)
end
else
update_ref!(ref, target, oldrev)
end
after_create_branch
find_branch(branch_name)
......
......@@ -55,7 +55,7 @@ module Commits
return success if repository.find_branch(new_branch)
result = CreateBranchService.new(@project, current_user)
.execute(new_branch, @target_branch, source_project: @source_project)
.execute(new_branch, @target_branch, source_project: @source_project, with_hooks: false)
if result[:status] == :error
raise ChangeError, "There was an error creating the source branch: #{result[:message]}"
......
require_relative 'base_service'
class CreateBranchService < BaseService
def execute(branch_name, ref, source_project: @project)
def execute(branch_name, ref, source_project: @project, with_hooks: true)
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
unless valid_branch
......@@ -26,7 +26,7 @@ class CreateBranchService < BaseService
repository.find_branch(branch_name)
else
repository.add_branch(current_user, branch_name, ref)
repository.add_branch(current_user, branch_name, ref, with_hooks: with_hooks)
end
if new_branch
......
......@@ -74,7 +74,7 @@ module Files
end
def create_target_branch
result = CreateBranchService.new(project, current_user).execute(@target_branch, @source_branch, source_project: @source_project)
result = CreateBranchService.new(project, current_user).execute(@target_branch, @source_branch, source_project: @source_project, with_hooks: false)
unless result[:status] == :success
raise_error("Something went wrong when we tried to create #{@target_branch} for you: #{result[:message]}")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册