Commit f99941d7 authored by Sean McGivern's avatar Sean McGivern

Keep trailing newline when picking conflict sections

If our side of the conflict file has a trailing newline, and we are picking
sections, not editing the whole content, then add a trailing newline back.
parent 228926da
...@@ -37,10 +37,12 @@ module MergeRequests ...@@ -37,10 +37,12 @@ module MergeRequests
private private
def write_resolved_file_to_index(merge_index, rugged, file, params) def write_resolved_file_to_index(merge_index, rugged, file, params)
new_file = if params[:sections] if params[:sections]
file.resolve_lines(params[:sections]).map(&:text).join("\n") 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] elsif params[:content]
file.resolve_content(params[:content]) new_file = file.resolve_content(params[:content])
end end
our_path = file.our_path our_path = file.our_path
......
---
title: Keep trailing newline when resolving conflicts by picking sides
merge_request:
author:
...@@ -26,6 +26,10 @@ describe MergeRequests::Conflicts::ResolveService do ...@@ -26,6 +26,10 @@ describe MergeRequests::Conflicts::ResolveService do
describe '#execute' do describe '#execute' do
let(:service) { described_class.new(merge_request) } 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 context 'with section params' do
let(:params) do let(:params) do
{ {
...@@ -66,6 +70,35 @@ describe MergeRequests::Conflicts::ResolveService do ...@@ -66,6 +70,35 @@ describe MergeRequests::Conflicts::ResolveService do
end end
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 context 'when the source project is a fork and does not contain the HEAD of the target branch' do
let!(:target_head) do let!(:target_head) do
project.repository.create_file( project.repository.create_file(
...@@ -142,10 +175,13 @@ describe MergeRequests::Conflicts::ResolveService do ...@@ -142,10 +175,13 @@ describe MergeRequests::Conflicts::ResolveService do
end end
it 'sets the content to the content given' do it 'sets the content to the content given' do
blob = merge_request.source_project.repository.blob_at(merge_request.source_branch_head.sha, blob = blob_content(
'files/ruby/popen.rb') 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
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