2016-07-27 11:42:18 +00:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class FileCollection
|
2018-03-07 01:25:12 +00:00
|
|
|
include Gitlab::RepositoryCacheAdapter
|
|
|
|
|
2017-10-09 19:40:37 +00:00
|
|
|
attr_reader :merge_request, :resolver
|
2017-10-07 01:41:23 +00:00
|
|
|
|
|
|
|
def initialize(merge_request)
|
|
|
|
our_commit = merge_request.source_branch_head.raw
|
|
|
|
their_commit = merge_request.target_branch_head.raw
|
2018-03-07 01:25:12 +00:00
|
|
|
@target_repo = merge_request.target_project.repository
|
2017-12-13 01:55:30 +00:00
|
|
|
@source_repo = merge_request.source_project.repository.raw
|
2018-03-07 01:25:12 +00:00
|
|
|
@our_commit_id = our_commit.id
|
|
|
|
@their_commit_id = their_commit.id
|
|
|
|
@resolver = Gitlab::Git::Conflict::Resolver.new(@target_repo.raw, @our_commit_id, @their_commit_id)
|
2017-10-07 01:41:23 +00:00
|
|
|
@merge_request = merge_request
|
2017-10-05 23:22:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(user, commit_message, files)
|
2017-12-27 19:49:05 +00:00
|
|
|
msg = commit_message || default_commit_message
|
|
|
|
resolution = Gitlab::Git::Conflict::Resolution.new(user, files, msg)
|
2017-10-07 01:41:23 +00:00
|
|
|
args = {
|
|
|
|
source_branch: merge_request.source_branch,
|
2017-12-27 19:49:05 +00:00
|
|
|
target_branch: merge_request.target_branch
|
2017-10-07 01:41:23 +00:00
|
|
|
}
|
2017-12-27 19:49:05 +00:00
|
|
|
resolver.resolve_conflicts(@source_repo, resolution, args)
|
2017-12-11 15:38:16 +00:00
|
|
|
ensure
|
|
|
|
@merge_request.clear_memoized_shas
|
2016-07-27 16:54:04 +00:00
|
|
|
end
|
|
|
|
|
2016-07-27 11:42:18 +00:00
|
|
|
def files
|
2017-10-09 19:40:37 +00:00
|
|
|
@files ||= resolver.conflicts.map do |conflict_file|
|
2017-10-07 01:41:23 +00:00
|
|
|
Gitlab::Conflict::File.new(conflict_file, merge_request: merge_request)
|
2016-07-27 11:42:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-07 01:25:12 +00:00
|
|
|
def can_be_resolved_in_ui?
|
|
|
|
# Try to parse each conflict. If the MR's mergeable status hasn't been
|
|
|
|
# updated, ensure that we don't say there are conflicts to resolve
|
|
|
|
# when there are no conflict files.
|
|
|
|
files.each(&:lines)
|
|
|
|
files.any?
|
|
|
|
rescue Gitlab::Git::CommandError, Gitlab::Git::Conflict::Parser::UnresolvableError, Gitlab::Git::Conflict::Resolver::ConflictSideMissing
|
|
|
|
false
|
|
|
|
end
|
|
|
|
cache_method :can_be_resolved_in_ui?
|
|
|
|
|
2016-09-01 12:59:10 +00:00
|
|
|
def file_for_path(old_path, new_path)
|
|
|
|
files.find { |file| file.their_path == old_path && file.our_path == new_path }
|
|
|
|
end
|
|
|
|
|
2016-07-27 11:42:18 +00:00
|
|
|
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
|
2017-10-07 01:41:23 +00:00
|
|
|
conflict_filenames = files.map do |conflict|
|
|
|
|
"# #{conflict.our_path}"
|
2016-07-27 11:42:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
<<EOM.chomp
|
2016-08-05 11:15:06 +00:00
|
|
|
Merge branch '#{merge_request.target_branch}' into '#{merge_request.source_branch}'
|
2016-07-27 11:42:18 +00:00
|
|
|
|
|
|
|
# Conflicts:
|
|
|
|
#{conflict_filenames.join("\n")}
|
|
|
|
EOM
|
|
|
|
end
|
2018-03-07 01:25:12 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cache
|
|
|
|
@cache ||= begin
|
|
|
|
# Use the commit ids as a namespace so if the MR branches get
|
|
|
|
# updated we instantiate the cache under a different namespace. That
|
|
|
|
# way don't have to worry about explicitly invalidating the cache
|
|
|
|
namespace = "#{@our_commit_id}:#{@their_commit_id}"
|
|
|
|
|
|
|
|
Gitlab::RepositoryCache.new(@target_repo, extra_namespace: namespace)
|
|
|
|
end
|
|
|
|
end
|
2016-07-27 11:42:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|