2016-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class FileCollection
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_reader :merge_request, :resolver
|
|
|
|
|
|
|
|
def initialize(merge_request)
|
|
|
|
our_commit = merge_request.source_branch_head.raw
|
|
|
|
their_commit = merge_request.target_branch_head.raw
|
|
|
|
target_repo = merge_request.target_project.repository.raw
|
|
|
|
@source_repo = merge_request.source_project.repository.raw
|
|
|
|
@resolver = Gitlab::Git::Conflict::Resolver.new(target_repo, our_commit.id, their_commit.id)
|
|
|
|
@merge_request = merge_request
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def resolve(user, commit_message, files)
|
|
|
|
msg = commit_message || default_commit_message
|
|
|
|
resolution = Gitlab::Git::Conflict::Resolution.new(user, files, msg)
|
|
|
|
args = {
|
|
|
|
source_branch: merge_request.source_branch,
|
|
|
|
target_branch: merge_request.target_branch
|
|
|
|
}
|
|
|
|
resolver.resolve_conflicts(@source_repo, resolution, args)
|
|
|
|
ensure
|
|
|
|
@merge_request.clear_memoized_shas
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def files
|
2018-03-17 18:26:18 +05:30
|
|
|
@files ||= resolver.conflicts.map do |conflict_file|
|
|
|
|
Gitlab::Conflict::File.new(conflict_file, merge_request: merge_request)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def file_for_path(old_path, new_path)
|
|
|
|
files.find { |file| file.their_path == old_path && file.our_path == new_path }
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def as_json(opts = nil)
|
|
|
|
{
|
|
|
|
target_branch: merge_request.target_branch,
|
|
|
|
source_branch: merge_request.source_branch,
|
|
|
|
commit_sha: merge_request.diff_head_sha,
|
|
|
|
commit_message: default_commit_message,
|
|
|
|
files: files
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_commit_message
|
2018-03-17 18:26:18 +05:30
|
|
|
conflict_filenames = files.map do |conflict|
|
|
|
|
"# #{conflict.our_path}"
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
<<EOM.chomp
|
|
|
|
Merge branch '#{merge_request.target_branch}' into '#{merge_request.source_branch}'
|
|
|
|
|
|
|
|
# Conflicts:
|
|
|
|
#{conflict_filenames.join("\n")}
|
|
|
|
EOM
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|