Commit 2a6f522c authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Implement merge from forks without satellites

parent 2be3c949
...@@ -149,7 +149,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -149,7 +149,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
return access_denied! unless @merge_request.can_be_merged_by?(current_user) return access_denied! unless @merge_request.can_be_merged_by?(current_user)
if @merge_request.automergeable? if @merge_request.automergeable?
AutoMergeWorker.perform_async(@merge_request.id, current_user.id, params) #AutoMergeWorker.perform_async(@merge_request.id, current_user.id, params)
@merge_request.automerge!(current_user, params[:commit_message])
@status = true @status = true
else else
@status = false @status = false
......
...@@ -206,11 +206,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -206,11 +206,7 @@ class MergeRequest < ActiveRecord::Base
def check_if_can_be_merged def check_if_can_be_merged
can_be_merged = can_be_merged =
if for_fork? project.repository.can_be_merged?(source_sha, target_branch)
raise 'Implement me'
else
project.repository.can_be_merged?(source_branch, target_branch)
end
if can_be_merged if can_be_merged
mark_as_mergeable mark_as_mergeable
...@@ -274,14 +270,14 @@ class MergeRequest < ActiveRecord::Base ...@@ -274,14 +270,14 @@ class MergeRequest < ActiveRecord::Base
# #
# see "git diff" # see "git diff"
def to_diff(current_user) def to_diff(current_user)
raise 'Implement me' target_project.repository.diff_text(target_branch, source_sha)
end end
# Returns the commit as a series of email patches. # Returns the commit as a series of email patches.
# #
# see "git format-patch" # see "git format-patch"
def to_patch(current_user) def to_patch(current_user)
raise 'Implement me' target_project.repository.format_patch(target_branch, source_sha)
end end
def hook_attrs def hook_attrs
...@@ -432,4 +428,13 @@ class MergeRequest < ActiveRecord::Base ...@@ -432,4 +428,13 @@ class MergeRequest < ActiveRecord::Base
"Open" "Open"
end end
end end
def target_sha
@target_sha ||= target_project.
repository.commit(target_branch).sha
end
def source_sha
commits.first.sha
end
end end
...@@ -160,10 +160,12 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -160,10 +160,12 @@ class MergeRequestDiff < ActiveRecord::Base
private private
def compare_result def compare_result
source_sha = merge_request.source_project.commit(source_branch).sha
@compare_result ||= CompareService.new.execute( @compare_result ||= CompareService.new.execute(
merge_request.author, merge_request.author,
merge_request.source_project, merge_request.target_project,
merge_request.source_branch, source_sha,
merge_request.target_project, merge_request.target_project,
merge_request.target_branch, merge_request.target_branch,
) )
......
...@@ -422,15 +422,34 @@ class Repository ...@@ -422,15 +422,34 @@ class Repository
} }
end end
def can_be_merged?(source_branch, target_branch) def can_be_merged?(source_sha, target_branch)
our_commit = rugged.branches[target_branch].target our_commit = rugged.branches[target_branch].target
their_commit = rugged.branches[source_branch].target their_commit = rugged.lookup(source_sha)
if our_commit && their_commit if our_commit && their_commit
!rugged.merge_commits(our_commit, their_commit).conflicts? !rugged.merge_commits(our_commit, their_commit).conflicts?
end end
end end
def merge(source_sha, target_branch, options = {})
our_commit = rugged.branches[target_branch].target
their_commit = rugged.lookup(source_sha)
raise "Invalid merge target" if our_commit.nil?
raise "Invalid merge source" if their_commit.nil?
merge_index = rugged.merge_commits(our_commit, their_commit)
return false if merge_index.conflicts?
actual_options = options.merge(
parents: [our_commit, their_commit],
tree: merge_index.write_tree(rugged),
update_ref: "refs/heads/#{target_branch}"
)
Rugged::Commit.create(rugged, actual_options)
end
def search_files(query, ref) def search_files(query, ref)
offset = 2 offset = 2
args = %W(git grep -i -n --before-context #{offset} --after-context #{offset} #{query} #{ref || root_ref}) args = %W(git grep -i -n --before-context #{offset} --after-context #{offset} #{query} #{ref || root_ref})
......
require 'securerandom'
# Compare 2 branches for one repo or between repositories # Compare 2 branches for one repo or between repositories
# and return Gitlab::CompareResult object that responds to commits and diffs # and return Gitlab::CompareResult object that responds to commits and diffs
class CompareService class CompareService
...@@ -12,7 +14,22 @@ class CompareService ...@@ -12,7 +14,22 @@ class CompareService
) )
) )
else else
raise 'Implement me' random_string = SecureRandom.hex
target_project.repository.fetch_ref(
source_project.repository.path_to_repo,
"refs/heads/#{source_branch}",
"refs/tmp/#{random_string}/head"
)
source_sha = source_project.commit(source_branch).sha
Gitlab::CompareResult.new(
Gitlab::Git::Compare.new(
target_project.repository.raw_repository,
target_branch,
source_sha,
)
)
end end
end end
end end
...@@ -31,12 +31,8 @@ module MergeRequests ...@@ -31,12 +31,8 @@ module MergeRequests
end end
def merge! def merge!
if merge_request.for_fork? if sha = commit
raise 'Implement me' after_commit(sha, merge_request.target_branch)
else
if sha = commit
after_commit(sha, merge_request.target_branch)
end
end end
end end
...@@ -49,7 +45,7 @@ module MergeRequests ...@@ -49,7 +45,7 @@ module MergeRequests
committer: committer committer: committer
} }
repository.merge(merge_request.source_branch, merge_request.target_branch, options) repository.merge(merge_request.source_sha, merge_request.target_branch, options)
end end
def after_commit(sha, branch) def after_commit(sha, branch)
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
= icon('history') = icon('history')
Commits Commits
%span.badge= @commits.size %span.badge= @commits.size
%li.diffs-tab %li.diffs-tab.active
= link_to url_for(params), data: {target: '#diffs', action: 'diffs', toggle: 'tab'} do = link_to url_for(params), data: {target: '#diffs', action: 'diffs', toggle: 'tab'} do
= icon('list-alt') = icon('list-alt')
Changes Changes
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
.tab-content .tab-content
#commits.commits.tab-pane #commits.commits.tab-pane
= render "projects/commits/commits", project: @project = render "projects/commits/commits", project: @project
#diffs.diffs.tab-pane #diffs.diffs.tab-pane.active
- if @diffs.present? - if @diffs.present?
= render "projects/diffs/diffs", diffs: @diffs, project: @project = render "projects/diffs/diffs", diffs: @diffs, project: @project
- elsif @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE - elsif @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE
......
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