Skip to content
Snippets Groups Projects
Commit 5d449d4a authored by Valery Sizov's avatar Valery Sizov
Browse files

Merge branch 'backport_from_ee_easy_commit_creation' into 'master'

Fix of 'Commits being passed to custom hooks are already reachable when using the UI'

This is back-port of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/563

## What does this MR do?
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/19771
This MR changes the way how we create commit, we create it directly in the branch and we don't use temporary ref anymore. This also works similar to how `git-receive-pack` works so it's a natural way to create commit.


## What are the relevant issue numbers?
https://gitlab.com/gitlab-org/gitlab-ce/issues/19771

See merge request !5337
parents a27212ab 501ce37f
No related branches found
No related tags found
No related merge requests found
Please view this file on the master branch, on stable branches it's out of date.
v 8.11.0 (unreleased)
- Fix of 'Commits being passed to custom hooks are already reachable when using the UI'
 
v 8.10.0 (unreleased)
- Fix profile activity heatmap to show correct day name (eanplatter)
Loading
Loading
Loading
Loading
@@ -704,6 +704,7 @@ class Repository
options[:commit] = {
message: message,
branch: ref,
update_ref: false,
}
 
raw_repository.mkdir(path, options)
Loading
Loading
@@ -719,6 +720,7 @@ class Repository
options[:commit] = {
message: message,
branch: ref,
update_ref: false,
}
 
options[:file] = {
Loading
Loading
@@ -739,7 +741,8 @@ class Repository
options[:author] = committer
options[:commit] = {
message: message,
branch: ref
branch: ref,
update_ref: false,
}
 
options[:file] = {
Loading
Loading
@@ -779,11 +782,10 @@ class Repository
merge_index = rugged.merge_commits(our_commit, their_commit)
return false if merge_index.conflicts?
 
commit_with_hooks(user, merge_request.target_branch) do |tmp_ref|
commit_with_hooks(user, merge_request.target_branch) do
actual_options = options.merge(
parents: [our_commit, their_commit],
tree: merge_index.write_tree(rugged),
update_ref: tmp_ref
)
 
commit_id = Rugged::Commit.create(rugged, actual_options)
Loading
Loading
@@ -798,15 +800,14 @@ class Repository
 
return false unless revert_tree_id
 
commit_with_hooks(user, base_branch) do |ref|
commit_with_hooks(user, base_branch) do
committer = user_to_committer(user)
source_sha = Rugged::Commit.create(rugged,
message: commit.revert_message,
author: committer,
committer: committer,
tree: revert_tree_id,
parents: [rugged.lookup(source_sha)],
update_ref: ref)
parents: [rugged.lookup(source_sha)])
end
end
 
Loading
Loading
@@ -816,7 +817,7 @@ class Repository
 
return false unless cherry_pick_tree_id
 
commit_with_hooks(user, base_branch) do |ref|
commit_with_hooks(user, base_branch) do
committer = user_to_committer(user)
source_sha = Rugged::Commit.create(rugged,
message: commit.message,
Loading
Loading
@@ -827,8 +828,7 @@ class Repository
},
committer: committer,
tree: cherry_pick_tree_id,
parents: [rugged.lookup(source_sha)],
update_ref: ref)
parents: [rugged.lookup(source_sha)])
end
end
 
Loading
Loading
@@ -929,20 +929,6 @@ class Repository
Gitlab::Popen.popen(args, path_to_repo)
end
 
def with_tmp_ref(oldrev = nil)
random_string = SecureRandom.hex
tmp_ref = "refs/tmp/#{random_string}/head"
if oldrev && !Gitlab::Git.blank_ref?(oldrev)
rugged.references.create(tmp_ref, oldrev)
end
# Make commit in tmp ref
yield(tmp_ref)
ensure
rugged.references.delete(tmp_ref) rescue nil
end
def commit_with_hooks(current_user, branch)
update_autocrlf_option
 
Loading
Loading
@@ -955,33 +941,31 @@ class Repository
oldrev = target_branch.target
end
 
with_tmp_ref(oldrev) do |tmp_ref|
# Make commit in tmp ref
newrev = yield(tmp_ref)
# Make commit
newrev = yield(ref)
 
unless newrev
raise CommitError.new('Failed to create commit')
end
unless newrev
raise CommitError.new('Failed to create commit')
end
GitHooksService.new.execute(current_user, path_to_repo, oldrev, newrev, ref) do
if was_empty || !target_branch
# Create branch
rugged.references.create(ref, newrev)
else
# Update head
current_head = find_branch(branch).target
 
GitHooksService.new.execute(current_user, path_to_repo, oldrev, newrev, ref) do
if was_empty || !target_branch
# Create branch
rugged.references.create(ref, newrev)
# Make sure target branch was not changed during pre-receive hook
if current_head == oldrev
rugged.references.update(ref, newrev)
else
# Update head
current_head = find_branch(branch).target
# Make sure target branch was not changed during pre-receive hook
if current_head == oldrev
rugged.references.update(ref, newrev)
else
raise CommitError.new('Commit was rejected because branch received new push')
end
raise CommitError.new('Commit was rejected because branch received new push')
end
end
newrev
end
newrev
end
 
def ls_files(ref)
Loading
Loading
Loading
Loading
@@ -15,21 +15,19 @@ class CreateBranchService < BaseService
return error('Branch already exists')
end
 
new_branch = nil
if source_project != @project
repository.with_tmp_ref do |tmp_ref|
repository.fetch_ref(
source_project.repository.path_to_repo,
"refs/heads/#{ref}",
tmp_ref
)
new_branch = repository.add_branch(current_user, branch_name, tmp_ref)
end
else
new_branch = repository.add_branch(current_user, branch_name, ref)
end
new_branch = if source_project != @project
repository.fetch_ref(
source_project.repository.path_to_repo,
"refs/heads/#{ref}",
"refs/heads/#{branch_name}"
)
repository.after_create_branch
repository.find_branch(branch_name)
else
repository.add_branch(current_user, branch_name, ref)
end
 
if new_branch
success(new_branch)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment