2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-27 07:42:18 -04:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class FileCollection
|
2018-03-06 20:25:12 -05:00
|
|
|
include Gitlab::RepositoryCacheAdapter
|
|
|
|
|
2017-10-09 15:40:37 -04:00
|
|
|
attr_reader :merge_request, :resolver
|
2017-10-06 21:41:23 -04:00
|
|
|
|
2021-07-21 17:10:10 -04:00
|
|
|
def initialize(merge_request, allow_tree_conflicts: false)
|
2017-10-06 21:41:23 -04:00
|
|
|
our_commit = merge_request.source_branch_head.raw
|
|
|
|
their_commit = merge_request.target_branch_head.raw
|
2018-03-06 20:25:12 -05:00
|
|
|
@target_repo = merge_request.target_project.repository
|
2017-12-12 20:55:30 -05:00
|
|
|
@source_repo = merge_request.source_project.repository.raw
|
2018-03-06 20:25:12 -05:00
|
|
|
@our_commit_id = our_commit.id
|
|
|
|
@their_commit_id = their_commit.id
|
2021-07-21 17:10:10 -04:00
|
|
|
@resolver = Gitlab::Git::Conflict::Resolver.new(@target_repo.raw, @our_commit_id, @their_commit_id, allow_tree_conflicts: allow_tree_conflicts)
|
2017-10-06 21:41:23 -04:00
|
|
|
@merge_request = merge_request
|
2017-10-05 19:22:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(user, commit_message, files)
|
2017-12-27 14:49:05 -05:00
|
|
|
msg = commit_message || default_commit_message
|
|
|
|
resolution = Gitlab::Git::Conflict::Resolution.new(user, files, msg)
|
2020-09-29 20:09:53 -04:00
|
|
|
|
|
|
|
resolver.resolve_conflicts(
|
|
|
|
@source_repo,
|
|
|
|
resolution,
|
2017-10-06 21:41:23 -04:00
|
|
|
source_branch: merge_request.source_branch,
|
2017-12-27 14:49:05 -05:00
|
|
|
target_branch: merge_request.target_branch
|
2020-09-29 20:09:53 -04:00
|
|
|
)
|
2017-12-11 10:38:16 -05:00
|
|
|
ensure
|
|
|
|
@merge_request.clear_memoized_shas
|
2016-07-27 12:54:04 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 07:42:18 -04:00
|
|
|
def files
|
2017-10-09 15:40:37 -04:00
|
|
|
@files ||= resolver.conflicts.map do |conflict_file|
|
2017-10-06 21:41:23 -04:00
|
|
|
Gitlab::Conflict::File.new(conflict_file, merge_request: merge_request)
|
2016-07-27 07:42:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-06 20:25:12 -05: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?
|
2018-03-23 11:02:05 -04:00
|
|
|
rescue Gitlab::Git::CommandError,
|
|
|
|
Gitlab::Git::Conflict::Parser::UnresolvableError,
|
|
|
|
Gitlab::Git::Conflict::Resolver::ConflictSideMissing,
|
|
|
|
Gitlab::Git::Conflict::File::UnsupportedEncoding
|
2018-03-06 20:25:12 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
cache_method :can_be_resolved_in_ui?
|
|
|
|
|
2016-09-01 08:59:10 -04: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 07:42:18 -04: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-06 21:41:23 -04:00
|
|
|
conflict_filenames = files.map do |conflict|
|
|
|
|
"# #{conflict.our_path}"
|
2016-07-27 07:42:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
<<EOM.chomp
|
2016-08-05 07:15:06 -04:00
|
|
|
Merge branch '#{merge_request.target_branch}' into '#{merge_request.source_branch}'
|
2016-07-27 07:42:18 -04:00
|
|
|
|
|
|
|
# Conflicts:
|
|
|
|
#{conflict_filenames.join("\n")}
|
|
|
|
EOM
|
|
|
|
end
|
2018-03-06 20:25:12 -05: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 07:42:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|