BigW Consortium Gitlab

Commit faa9bd40 by Alejandro Rodríguez

Create a Gitlab::Git submodule for conlict-related files

Rename classes to (hopefully) clearer names while we're doing that.
parent 3fcab51e
......@@ -53,7 +53,7 @@ class Projects::MergeRequests::ConflictsController < Projects::MergeRequests::Ap
flash[:notice] = 'All merge conflicts were resolved. The merge request can now be merged.'
render json: { redirect_to: project_merge_request_url(@project, @merge_request, resolved_conflicts: true) }
rescue Gitlab::Git::Merge::ResolutionError => e
rescue Gitlab::Git::Conflict::Resolver::ResolutionError => e
render status: :bad_request, json: { message: e.message }
end
end
......
......@@ -23,7 +23,7 @@ module MergeRequests
# when there are no conflict files.
conflicts.files.each(&:lines)
@conflicts_can_be_resolved_in_ui = conflicts.files.length > 0
rescue Rugged::OdbError, Gitlab::Git::ConflictParser::UnresolvableError, Gitlab::Git::Merge::ConflictSideMissing
rescue Rugged::OdbError, Gitlab::Git::Conflict::Parser::UnresolvableError, Gitlab::Git::Conflict::Resolver::ConflictSideMissing
@conflicts_can_be_resolved_in_ui = false
end
end
......
......@@ -186,7 +186,7 @@ module API
lines.each do |line|
next unless line.new_pos == params[:line] && line.type == params[:line_type]
break opts[:line_code] = Gitlab::Git::DiffLineCode.generate(diff.new_path, line.new_pos, line.old_pos)
break opts[:line_code] = Gitlab::Git::Conflict::LineCode.generate(diff.new_path, line.new_pos, line.old_pos)
end
break if opts[:line_code]
......
......@@ -173,7 +173,7 @@ module API
lines.each do |line|
next unless line.new_pos == params[:line] && line.type == params[:line_type]
break opts[:line_code] = Gitlab::Git::DiffLineCode.generate(diff.new_path, line.new_pos, line.old_pos)
break opts[:line_code] = Gitlab::Git::Conflict::LineCode.generate(diff.new_path, line.new_pos, line.old_pos)
end
break if opts[:line_code]
......
......@@ -23,7 +23,7 @@ module Github
private
def generate_line_code(line)
Gitlab::Git::DiffLineCode.generate(file_path, line.new_pos, line.old_pos)
Gitlab::Git::Conflict::LineCode.generate(file_path, line.new_pos, line.old_pos)
end
def on_diff?
......
......@@ -241,7 +241,7 @@ module Gitlab
end
def generate_line_code(pr_comment)
Gitlab::Git::DiffLineCode.generate(pr_comment.file_path, pr_comment.new_pos, pr_comment.old_pos)
Gitlab::Git::Conflict::LineCode.generate(pr_comment.file_path, pr_comment.new_pos, pr_comment.old_pos)
end
def pull_request_comment_attributes(comment)
......
......@@ -6,7 +6,10 @@ module Gitlab
CONTEXT_LINES = 3
attr_reader :merge_request, :raw
attr_reader :merge_request
# 'raw' holds the Gitlab::Git::Conflict::File that this instance wraps
attr_reader :raw
delegate :type, :content, :their_path, :our_path, :our_mode, :our_blob, :repository, to: :raw
......@@ -107,7 +110,7 @@ module Gitlab
end
def line_code(line)
Gitlab::Git::DiffLineCode.generate(our_path, line.new_pos, line.old_pos)
Gitlab::Git::Conflict::LineCode.generate(our_path, line.new_pos, line.old_pos)
end
def create_match_line(line)
......
module Gitlab
module Conflict
class FileCollection
attr_reader :merge_request, :merge
attr_reader :merge_request, :resolver
def initialize(merge_request)
source_repo = merge_request.source_project.repository.raw
our_commit = merge_request.source_branch_head.raw
their_commit = merge_request.target_branch_head.raw
target_repo = merge_request.target_project.repository.raw
@merge = Gitlab::Git::Merge.new(source_repo, our_commit, target_repo, their_commit)
@resolver = Gitlab::Git::Conflict::Resolver.new(source_repo, our_commit, target_repo, their_commit)
@merge_request = merge_request
end
......@@ -18,11 +18,11 @@ module Gitlab
target_branch: merge_request.target_branch,
commit_message: commit_message || default_commit_message
}
merge.resolve_conflicts(user, files, args)
resolver.resolve_conflicts(user, files, args)
end
def files
@files ||= merge.conflicts.map do |conflict_file|
@files ||= resolver.conflicts.map do |conflict_file|
Gitlab::Conflict::File.new(conflict_file, merge_request: merge_request)
end
end
......
......@@ -49,7 +49,7 @@ module Gitlab
def line_code(line)
return if line.meta?
Gitlab::Git::DiffLineCode.generate(file_path, line.new_pos, line.old_pos)
Gitlab::Git::Conflict::LineCode.generate(file_path, line.new_pos, line.old_pos)
end
def line_for_line_code(code)
......
module Gitlab
module Git
class ConflictFile
module Conflict
class File
attr_reader :content, :their_path, :our_path, :our_mode, :repository
def initialize(repository, commit_oid, conflict, content)
......@@ -17,10 +18,10 @@ module Gitlab
begin
@type = 'text'
@lines = Gitlab::Git::ConflictParser.parse(content,
@lines = Gitlab::Git::Conflict::Parser.parse(content,
our_path: our_path,
their_path: their_path)
rescue Gitlab::Git::ConflictParser::ParserError
rescue Gitlab::Git::Conflict::Parser::ParserError
@type = 'text-editor'
@lines = nil
end
......@@ -45,7 +46,7 @@ module Gitlab
end
def line_code(line)
Gitlab::Git::DiffLineCode.generate(our_path, line[:line_new], line[:line_old])
Gitlab::Git::Conflict::LineCode.generate(our_path, line[:line_new], line[:line_old])
end
def resolve_lines(resolution)
......@@ -65,7 +66,7 @@ module Gitlab
when 'origin'
next unless line[:type] == 'old'
else
raise Gitlab::Git::Merge::ResolutionError, "Missing resolution for section ID: #{section_id}"
raise Gitlab::Git::Conflict::Resolver::ResolutionError, "Missing resolution for section ID: #{section_id}"
end
line
......@@ -74,11 +75,12 @@ module Gitlab
def resolve_content(resolution)
if resolution == content
raise Gitlab::Git::Merge::ResolutionError, "Resolved content has no changes for file #{our_path}"
raise Gitlab::Git::Conflict::Resolver::ResolutionError, "Resolved content has no changes for file #{our_path}"
end
resolution
end
end
end
end
end
module Gitlab
module Git
class DiffLineCode
module Conflict
class LineCode
def self.generate(file_path, new_line_position, old_line_position)
"#{Digest::SHA1.hexdigest(file_path)}_#{old_line_position}_#{new_line_position}"
end
end
end
end
end
module Gitlab
module Git
class ConflictParser
module Conflict
class Parser
UnresolvableError = Class.new(StandardError)
UnmergeableFile = Class.new(UnresolvableError)
UnsupportedEncoding = Class.new(UnresolvableError)
......@@ -86,4 +87,5 @@ module Gitlab
end
end
end
end
end
module Gitlab
module Git
class Merge
module Conflict
class Resolver
ConflictSideMissing = Class.new(StandardError)
ResolutionError = Class.new(StandardError)
......@@ -20,7 +21,7 @@ module Gitlab
target_index.conflicts.map do |conflict|
raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours]
Gitlab::Git::ConflictFile.new(
Gitlab::Git::Conflict::File.new(
@target_repository,
@our_commit.oid,
conflict,
......@@ -86,4 +87,5 @@ module Gitlab
end
end
end
end
end
......@@ -38,7 +38,7 @@ module Gitlab
end
def generate_line_code(line)
Gitlab::Git::DiffLineCode.generate(file_path, line.new_pos, line.old_pos)
Gitlab::Git::Conflict::LineCode.generate(file_path, line.new_pos, line.old_pos)
end
def on_diff?
......
......@@ -17,8 +17,8 @@ describe Projects::MergeRequests::ConflictsController do
describe 'GET show' do
context 'when the conflicts cannot be resolved in the UI' do
before do
allow(Gitlab::Git::ConflictParser).to receive(:parse)
.and_raise(Gitlab::Git::ConflictParser::UnmergeableFile)
allow(Gitlab::Git::Conflict::Parser).to receive(:parse)
.and_raise(Gitlab::Git::Conflict::Parser::UnmergeableFile)
get :show,
namespace_id: merge_request_with_conflicts.project.namespace.to_param,
......@@ -109,8 +109,8 @@ describe Projects::MergeRequests::ConflictsController do
context 'when the conflicts cannot be resolved in the UI' do
before do
allow(Gitlab::Git::ConflictParser).to receive(:parse)
.and_raise(Gitlab::Git::ConflictParser::UnmergeableFile)
allow(Gitlab::Git::Conflict::Parser).to receive(:parse)
.and_raise(Gitlab::Git::Conflict::Parser::UnmergeableFile)
conflict_for_path('files/ruby/regex.rb')
end
......
......@@ -10,7 +10,7 @@ describe Gitlab::Conflict::File do
let(:index) { rugged.merge_commits(our_commit, their_commit) }
let(:rugged_conflict) { index.conflicts.last }
let(:raw_conflict_content) { index.merge_file('files/ruby/regex.rb')[:data] }
let(:raw_conflict_file) { Gitlab::Git::ConflictFile.new(repository, our_commit.oid, rugged_conflict, raw_conflict_content) }
let(:raw_conflict_file) { Gitlab::Git::Conflict::File.new(repository, our_commit.oid, rugged_conflict, raw_conflict_content) }
let(:conflict_file) { described_class.new(raw_conflict_file, merge_request: merge_request) }
describe '#resolve_lines' do
......@@ -54,13 +54,13 @@ describe Gitlab::Conflict::File do
invalid_hash = section_keys.map { |key| [key, 'invalid'] }.to_h
expect { conflict_file.resolve_lines({}) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
expect { conflict_file.resolve_lines(empty_hash) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
expect { conflict_file.resolve_lines(invalid_hash) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
end
end
......
......@@ -40,7 +40,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, 0)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, 0)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -108,7 +108,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, 15)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, 15)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -149,7 +149,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -189,7 +189,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, 13, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, 13, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -233,7 +233,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, 5)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, 5)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -274,7 +274,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -314,7 +314,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, 4, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, 4, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -357,7 +357,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, 0, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, 0, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -399,7 +399,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, subject.new_line, 0)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, subject.new_line, 0)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......@@ -447,7 +447,7 @@ describe Gitlab::Diff::Position do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(subject.file_path, 0, subject.old_line)
line_code = Gitlab::Git::Conflict::LineCode.generate(subject.file_path, 0, subject.old_line)
expect(subject.line_code(project.repository)).to eq(line_code)
end
......
require 'spec_helper'
describe Gitlab::Git::ConflictParser do
describe Gitlab::Git::Conflict::Parser do
describe '.parse' do
def parse_text(text)
described_class.parse(text, our_path: 'README.md', their_path: 'README.md')
......@@ -125,12 +125,12 @@ CONFLICT
context 'when there is a non-start delimiter first' do
it 'raises UnexpectedDelimiter when there is a middle delimiter first' do
expect { parse_text('=======') }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'raises UnexpectedDelimiter when there is an end delimiter first' do
expect { parse_text('>>>>>>> README.md') }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'does not raise when there is an end delimiter for a different path first' do
......@@ -145,12 +145,12 @@ CONFLICT
it 'raises UnexpectedDelimiter when it is followed by an end delimiter' do
expect { parse_text(start_text + '>>>>>>> README.md' + end_text) }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'raises UnexpectedDelimiter when it is followed by another start delimiter' do
expect { parse_text(start_text + start_text + end_text) }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'does not raise when it is followed by a start delimiter for a different path' do
......@@ -165,12 +165,12 @@ CONFLICT
it 'raises UnexpectedDelimiter when it is followed by another middle delimiter' do
expect { parse_text(start_text + '=======' + end_text) }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'raises UnexpectedDelimiter when it is followed by a start delimiter' do
expect { parse_text(start_text + start_text + end_text) }
.to raise_error(Gitlab::Git::ConflictParser::UnexpectedDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::UnexpectedDelimiter)
end
it 'does not raise when it is followed by a start delimiter for another path' do
......@@ -183,25 +183,25 @@ CONFLICT
start_text = "<<<<<<< README.md\n=======\n"
expect { parse_text(start_text) }
.to raise_error(Gitlab::Git::ConflictParser::MissingEndDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::MissingEndDelimiter)
expect { parse_text(start_text + '>>>>>>> some-other-path.md') }
.to raise_error(Gitlab::Git::ConflictParser::MissingEndDelimiter)
.to raise_error(Gitlab::Git::Conflict::Parser::MissingEndDelimiter)
end
end
context 'other file types' do
it 'raises UnmergeableFile when lines is blank, indicating a binary file' do
expect { parse_text('') }
.to raise_error(Gitlab::Git::ConflictParser::UnmergeableFile)
.to raise_error(Gitlab::Git::Conflict::Parser::UnmergeableFile)
expect { parse_text(nil) }
.to raise_error(Gitlab::Git::ConflictParser::UnmergeableFile)
.to raise_error(Gitlab::Git::Conflict::Parser::UnmergeableFile)
end
it 'raises UnmergeableFile when the file is over 200 KB' do
expect { parse_text('a' * 204801) }
.to raise_error(Gitlab::Git::ConflictParser::UnmergeableFile)
.to raise_error(Gitlab::Git::Conflict::Parser::UnmergeableFile)
end
# All text from Rugged has an encoding of ASCII_8BIT, so force that in
......@@ -216,7 +216,7 @@ CONFLICT
context 'when the file contains non-UTF-8 characters' do
it 'raises UnsupportedEncoding' do
expect { parse_text("a\xC4\xFC".force_encoding(Encoding::ASCII_8BIT)) }
.to raise_error(Gitlab::Git::ConflictParser::UnsupportedEncoding)
.to raise_error(Gitlab::Git::Conflict::Parser::UnsupportedEncoding)
end
end
end
......
......@@ -105,7 +105,7 @@ describe DiffNote do
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Git::DiffLineCode.generate(position.file_path, position.formatter.new_line, 15)
line_code = Gitlab::Git::Conflict::LineCode.generate(position.file_path, position.formatter.new_line, 15)
expect(subject.line_code).to eq(line_code)
end
......
......@@ -204,16 +204,16 @@ describe MergeRequests::Conflicts::ResolveService do
it 'raises a ResolutionError error' do
expect { service.execute(user, invalid_params) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
end
end
context 'when the content of a file is unchanged' do
let(:merge) do
MergeRequests::Conflicts::ListService.new(merge_request).conflicts.merge
let(:resolver) do
MergeRequests::Conflicts::ListService.new(merge_request).conflicts.resolver
end
let(:regex_conflict) do
merge.conflict_for_path('files/ruby/regex.rb', 'files/ruby/regex.rb')
resolver.conflict_for_path('files/ruby/regex.rb', 'files/ruby/regex.rb')
end
let(:invalid_params) do
......@@ -235,7 +235,7 @@ describe MergeRequests::Conflicts::ResolveService do
it 'raises a ResolutionError error' do
expect { service.execute(user, invalid_params) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
end
end
......@@ -255,7 +255,7 @@ describe MergeRequests::Conflicts::ResolveService do
it 'raises a ResolutionError error' do
expect { service.execute(user, invalid_params) }
.to raise_error(Gitlab::Git::Merge::ResolutionError)
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
end
end
end
......
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