2016-07-27 07:42:18 -04:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class FileCollection
|
2017-03-01 06:00:37 -05:00
|
|
|
ConflictSideMissing = Class.new(StandardError)
|
2016-08-01 09:52:53 -04:00
|
|
|
|
2016-07-27 07:42:18 -04:00
|
|
|
attr_reader :merge_request, :our_commit, :their_commit
|
|
|
|
|
|
|
|
def initialize(merge_request)
|
|
|
|
@merge_request = merge_request
|
2016-07-29 06:05:33 -04:00
|
|
|
@our_commit = merge_request.source_branch_head.raw.raw_commit
|
2016-07-27 07:42:18 -04:00
|
|
|
@their_commit = merge_request.target_branch_head.raw.raw_commit
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
merge_request.project.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_index
|
|
|
|
@merge_index ||= repository.rugged.merge_commits(our_commit, their_commit)
|
2016-07-27 12:54:04 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 07:42:18 -04:00
|
|
|
def files
|
|
|
|
@files ||= merge_index.conflicts.map do |conflict|
|
2016-08-01 09:52:53 -04:00
|
|
|
raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours]
|
2016-07-27 07:42:18 -04:00
|
|
|
|
2016-08-01 09:52:53 -04:00
|
|
|
Gitlab::Conflict::File.new(merge_index.merge_file(conflict[:ours][:path]),
|
2016-07-27 07:42:18 -04:00
|
|
|
conflict,
|
2016-08-03 07:02:20 -04:00
|
|
|
merge_request: merge_request)
|
2016-07-27 07:42:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
conflict_filenames = merge_index.conflicts.map do |conflict|
|
|
|
|
"# #{conflict[:ours][:path]}"
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|