Commit 57dddacf authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '32983-merge-conflict-resolution-removed-the-newline-in-the-end-of-file' into 'master'

Resolve "Merge conflict resolution removed the newline in the end of file"

Closes #32983

See merge request !11817
parents 1bf76c76 f99941d7
...@@ -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