Commit 2f594206 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'operation-service-merge' into 'master'

Prepare Repository#merge for migration to Gitaly

Closes gitaly#559

See merge request gitlab-org/gitlab-ce!14154
parents d86bbe01 902b5347
...@@ -768,17 +768,23 @@ class Repository ...@@ -768,17 +768,23 @@ class Repository
multi_action(**options) multi_action(**options)
end end
def with_branch(user, *args) def with_cache_hooks
result = Gitlab::Git::OperationService.new(user, raw_repository).with_branch(*args) do |start_commit| result = yield
yield start_commit
end
newrev, should_run_after_create, should_run_after_create_branch = result return unless result
after_create if should_run_after_create after_create if result.repo_created?
after_create_branch if should_run_after_create_branch after_create_branch if result.branch_created?
newrev result.newrev
end
def with_branch(user, *args)
with_cache_hooks do
Gitlab::Git::OperationService.new(user, raw_repository).with_branch(*args) do |start_commit|
yield start_commit
end
end
end end
# rubocop:disable Metrics/ParameterLists # rubocop:disable Metrics/ParameterLists
...@@ -843,30 +849,13 @@ class Repository ...@@ -843,30 +849,13 @@ class Repository
end end
end end
def merge(user, source, merge_request, options = {}) def merge(user, source_sha, merge_request, message)
with_branch( with_cache_hooks do
user, raw_repository.merge(user, source_sha, merge_request.target_branch, message) do |commit_id|
merge_request.target_branch) do |start_commit| merge_request.update(in_progress_merge_commit_sha: commit_id)
our_commit = start_commit.sha nil # Return value does not matter.
their_commit = source end
raise 'Invalid merge target' unless our_commit
raise 'Invalid merge source' unless their_commit
merge_index = rugged.merge_commits(our_commit, their_commit)
break if merge_index.conflicts?
actual_options = options.merge(
parents: [our_commit, their_commit],
tree: merge_index.write_tree(rugged)
)
commit_id = create_commit(actual_options)
merge_request.update(in_progress_merge_commit_sha: commit_id)
commit_id
end end
rescue Gitlab::Git::CommitError # when merge_index.conflicts?
false
end end
def revert( def revert(
...@@ -1157,12 +1146,6 @@ class Repository ...@@ -1157,12 +1146,6 @@ class Repository
Gitlab::Metrics.add_event(event, { path: full_path }.merge(tags)) Gitlab::Metrics.add_event(event, { path: full_path }.merge(tags))
end end
def create_commit(params = {})
params[:message].delete!("\r")
Rugged::Commit.create(rugged, params)
end
def last_commit_for_path_by_gitaly(sha, path) def last_commit_for_path_by_gitaly(sha, path)
c = raw_repository.gitaly_commit_client.last_commit_for_path(sha, path) c = raw_repository.gitaly_commit_client.last_commit_for_path(sha, path)
commit(c) commit(c)
......
...@@ -38,15 +38,9 @@ module MergeRequests ...@@ -38,15 +38,9 @@ module MergeRequests
private private
def commit def commit
committer = repository.user_to_committer(current_user) message = params[:commit_message] || merge_request.merge_commit_message
options = { commit_id = repository.merge(current_user, source, merge_request, message)
message: params[:commit_message] || merge_request.merge_commit_message,
author: committer,
committer: committer
}
commit_id = repository.merge(current_user, source, merge_request, options)
raise MergeError, 'Conflicts detected during merge' unless commit_id raise MergeError, 'Conflicts detected during merge' unless commit_id
......
module Gitlab module Gitlab
module Git module Git
class OperationService class OperationService
WithBranchResult = Struct.new(:newrev, :repo_created, :branch_created) do
alias_method :repo_created?, :repo_created
alias_method :branch_created?, :branch_created
end
attr_reader :user, :repository attr_reader :user, :repository
def initialize(user, new_repository) def initialize(user, new_repository)
...@@ -107,7 +112,7 @@ module Gitlab ...@@ -107,7 +112,7 @@ module Gitlab
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
update_ref_in_hooks(ref, newrev, oldrev) update_ref_in_hooks(ref, newrev, oldrev)
[newrev, was_empty, was_empty || Gitlab::Git.blank_ref?(oldrev)] WithBranchResult.new(newrev, was_empty, was_empty || Gitlab::Git.blank_ref?(oldrev))
end end
def find_oldrev_from_branch(newrev, branch) def find_oldrev_from_branch(newrev, branch)
......
...@@ -653,6 +653,43 @@ module Gitlab ...@@ -653,6 +653,43 @@ module Gitlab
tags.find { |tag| tag.name == name } tags.find { |tag| tag.name == name }
end end
def merge(user, source_sha, target_branch, message)
committer = Gitlab::Git.committer_hash(email: user.email, name: user.name)
OperationService.new(user, self).with_branch(target_branch) do |start_commit|
our_commit = start_commit.sha
their_commit = source_sha
raise 'Invalid merge target' unless our_commit
raise 'Invalid merge source' unless their_commit
merge_index = rugged.merge_commits(our_commit, their_commit)
break if merge_index.conflicts?
options = {
parents: [our_commit, their_commit],
tree: merge_index.write_tree(rugged),
message: message,
author: committer,
committer: committer
}
commit_id = create_commit(options)
yield commit_id
commit_id
end
rescue Gitlab::Git::CommitError # when merge_index.conflicts?
nil
end
def create_commit(params = {})
params[:message].delete!("\r")
Rugged::Commit.create(rugged, params)
end
# Delete the specified branch from the repository # Delete the specified branch from the repository
def delete_branch(branch_name) def delete_branch(branch_name)
gitaly_migrate(:delete_branch) do |is_enabled| gitaly_migrate(:delete_branch) do |is_enabled|
......
...@@ -1761,17 +1761,9 @@ describe Gitlab::Diff::PositionTracer do ...@@ -1761,17 +1761,9 @@ describe Gitlab::Diff::PositionTracer do
let(:merge_commit) do let(:merge_commit) do
update_file_again_commit update_file_again_commit
committer = repository.user_to_committer(current_user)
options = {
message: "Merge branches",
author: committer,
committer: committer
}
merge_request = create(:merge_request, source_branch: second_create_file_commit.sha, target_branch: branch_name, source_project: project) merge_request = create(:merge_request, source_branch: second_create_file_commit.sha, target_branch: branch_name, source_project: project)
repository.merge(current_user, merge_request.diff_head_sha, merge_request, options) repository.merge(current_user, merge_request.diff_head_sha, merge_request, "Merge branches")
project.commit(branch_name) project.commit(branch_name)
end end
......
...@@ -207,11 +207,6 @@ eos ...@@ -207,11 +207,6 @@ eos
context 'of a merge commit' do context 'of a merge commit' do
let(:repository) { project.repository } let(:repository) { project.repository }
let(:commit_options) do
author = repository.user_to_committer(user)
{ message: 'Test message', committer: author, author: author }
end
let(:merge_request) do let(:merge_request) do
create(:merge_request, create(:merge_request,
source_branch: 'video', source_branch: 'video',
...@@ -224,7 +219,7 @@ eos ...@@ -224,7 +219,7 @@ eos
merge_commit_id = repository.merge(user, merge_commit_id = repository.merge(user,
merge_request.diff_head_sha, merge_request.diff_head_sha,
merge_request, merge_request,
commit_options) 'Test message')
repository.commit(merge_commit_id) repository.commit(merge_commit_id)
end end
......
...@@ -10,10 +10,7 @@ describe Repository, models: true do ...@@ -10,10 +10,7 @@ describe Repository, models: true do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:git_user) { Gitlab::Git::User.from_gitlab(user) } let(:git_user) { Gitlab::Git::User.from_gitlab(user) }
let(:commit_options) do let(:message) { 'Test message' }
author = repository.user_to_committer(user)
{ message: 'Test message', committer: author, author: author }
end
let(:merge_commit) do let(:merge_commit) do
merge_request = create(:merge_request, source_branch: 'feature', target_branch: 'master', source_project: project) merge_request = create(:merge_request, source_branch: 'feature', target_branch: 'master', source_project: project)
...@@ -21,7 +18,7 @@ describe Repository, models: true do ...@@ -21,7 +18,7 @@ describe Repository, models: true do
merge_commit_id = repository.merge(user, merge_commit_id = repository.merge(user,
merge_request.diff_head_sha, merge_request.diff_head_sha,
merge_request, merge_request,
commit_options) message)
repository.commit(merge_commit_id) repository.commit(merge_commit_id)
end end
...@@ -1287,10 +1284,7 @@ describe Repository, models: true do ...@@ -1287,10 +1284,7 @@ describe Repository, models: true do
describe '#merge' do describe '#merge' do
let(:merge_request) { create(:merge_request, source_branch: 'feature', target_branch: 'master', source_project: project) } let(:merge_request) { create(:merge_request, source_branch: 'feature', target_branch: 'master', source_project: project) }
let(:commit_options) do let(:message) { 'Test \r\n\r\n message' }
author = repository.user_to_committer(user)
{ message: 'Test \r\n\r\n message', committer: author, author: author }
end
it 'merges the code and returns the commit id' do it 'merges the code and returns the commit id' do
expect(merge_commit).to be_present expect(merge_commit).to be_present
...@@ -1298,19 +1292,19 @@ describe Repository, models: true do ...@@ -1298,19 +1292,19 @@ describe Repository, models: true do
end end
it 'sets the `in_progress_merge_commit_sha` flag for the given merge request' do it 'sets the `in_progress_merge_commit_sha` flag for the given merge request' do
merge_commit_id = merge(repository, user, merge_request, commit_options) merge_commit_id = merge(repository, user, merge_request, message)
expect(merge_request.in_progress_merge_commit_sha).to eq(merge_commit_id) expect(merge_request.in_progress_merge_commit_sha).to eq(merge_commit_id)
end end
it 'removes carriage returns from commit message' do it 'removes carriage returns from commit message' do
merge_commit_id = merge(repository, user, merge_request, commit_options) merge_commit_id = merge(repository, user, merge_request, message)
expect(repository.commit(merge_commit_id).message).to eq(commit_options[:message].delete("\r")) expect(repository.commit(merge_commit_id).message).to eq(message.delete("\r"))
end end
def merge(repository, user, merge_request, options = {}) def merge(repository, user, merge_request, message)
repository.merge(user, merge_request.diff_head_sha, merge_request, options) repository.merge(user, merge_request.diff_head_sha, merge_request, message)
end end
end end
......
...@@ -150,9 +150,7 @@ describe MergeRequests::RefreshService do ...@@ -150,9 +150,7 @@ describe MergeRequests::RefreshService do
context 'manual merge of source branch' do context 'manual merge of source branch' do
before do before do
# Merge master -> feature branch # Merge master -> feature branch
author = { email: 'test@gitlab.com', time: Time.now, name: "Me" } @project.repository.merge(@user, @merge_request.diff_head_sha, @merge_request, 'Test message')
commit_options = { message: 'Test message', committer: author, author: author }
@project.repository.merge(@user, @merge_request.diff_head_sha, @merge_request, commit_options)
commit = @project.repository.commit('feature') commit = @project.repository.commit('feature')
service.new(@project, @user).execute(@oldrev, commit.id, 'refs/heads/feature') service.new(@project, @user).execute(@oldrev, commit.id, 'refs/heads/feature')
reload_mrs reload_mrs
......
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