diff options
3 files changed, 50 insertions, 8 deletions
diff --git a/app/services/merge_requests/conflicts/resolve_service.rb b/app/services/merge_requests/conflicts/resolve_service.rb index d74a82effd6..c2c335b8461 100644 --- a/app/services/merge_requests/conflicts/resolve_service.rb +++ b/app/services/merge_requests/conflicts/resolve_service.rb @@ -37,11 +37,13 @@ module MergeRequests private def write_resolved_file_to_index(merge_index, rugged, file, params) - new_file = if params[:sections] - file.resolve_lines(params[:sections]).map(&:text).join("\n") - elsif params[:content] - file.resolve_content(params[:content]) - end + if params[:sections] + new_file = file.resolve_lines(params[:sections]).map(&:text).join("\n") + + new_file << "\n" if file.our_blob.data.ends_with?("\n") + elsif params[:content] + new_file = file.resolve_content(params[:content]) + end our_path = file.our_path diff --git a/changelogs/unreleased/32983-merge-conflict-resolution-removed-the-newline-in-the-end-of-file.yml b/changelogs/unreleased/32983-merge-conflict-resolution-removed-the-newline-in-the-end-of-file.yml new file mode 100644 index 00000000000..eca42176501 --- /dev/null +++ b/changelogs/unreleased/32983-merge-conflict-resolution-removed-the-newline-in-the-end-of-file.yml @@ -0,0 +1,4 @@ +--- +title: Keep trailing newline when resolving conflicts by picking sides +merge_request: +author: diff --git a/spec/services/merge_requests/conflicts/resolve_service_spec.rb b/spec/services/merge_requests/conflicts/resolve_service_spec.rb index 19e8d5cc5f1..c77e6e9cd50 100644 --- a/spec/services/merge_requests/conflicts/resolve_service_spec.rb +++ b/spec/services/merge_requests/conflicts/resolve_service_spec.rb @@ -26,6 +26,10 @@ describe MergeRequests::Conflicts::ResolveService do describe '#execute' do let(:service) { described_class.new(merge_request) } + def blob_content(project, ref, path) + project.repository.blob_at(ref, path).data + end + context 'with section params' do let(:params) do { @@ -66,6 +70,35 @@ describe MergeRequests::Conflicts::ResolveService do end end + context 'when some files have trailing newlines' do + let!(:source_head) do + branch = 'conflict-resolvable' + path = 'files/ruby/popen.rb' + popen_content = blob_content(project, branch, path) + + project.repository.update_file( + user, + path, + popen_content.chomp("\n"), + message: 'Remove trailing newline from popen.rb', + branch_name: branch + ) + end + + before do + service.execute(user, params) + end + + it 'preserves trailing newlines from our side of the conflicts' do + head_sha = merge_request.source_branch_head.sha + popen_content = blob_content(project, head_sha, 'files/ruby/popen.rb') + regex_content = blob_content(project, head_sha, 'files/ruby/regex.rb') + + expect(popen_content).not_to end_with("\n") + expect(regex_content).to end_with("\n") + end + end + context 'when the source project is a fork and does not contain the HEAD of the target branch' do let!(:target_head) do project.repository.create_file( @@ -142,10 +175,13 @@ describe MergeRequests::Conflicts::ResolveService do end it 'sets the content to the content given' do - blob = merge_request.source_project.repository.blob_at(merge_request.source_branch_head.sha, - 'files/ruby/popen.rb') + blob = blob_content( + merge_request.source_project, + merge_request.source_branch_head.sha, + 'files/ruby/popen.rb' + ) - expect(blob.data).to eq(popen_content) + expect(blob).to eq(popen_content) end end |