From 1edf1807c570d74ee0039f5f58ef607ee797187a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 10 Jun 2019 15:50:23 -0700 Subject: [PATCH 1/2] Fix UTF-8 conversion issues when resolving conflicts Similar to https://gitlab.com/gitlab-org/gitlab-ce/issues/63030, when the commit message in the /resolve_conflicts endpoint contains a UTF-8 character, the conversion to the Gitaly ASCII-8BIT value may flag an error. To fix this, we run `force_encoding` on the commit message. Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/63062 --- .../sh-fix-utf-8-encoding-resolve-conflicts.yml | 5 +++++ lib/gitlab/gitaly_client/conflicts_service.rb | 2 +- .../gitlab/gitaly_client/conflicts_service_spec.rb | 14 +++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/sh-fix-utf-8-encoding-resolve-conflicts.yml diff --git a/changelogs/unreleased/sh-fix-utf-8-encoding-resolve-conflicts.yml b/changelogs/unreleased/sh-fix-utf-8-encoding-resolve-conflicts.yml new file mode 100644 index 00000000000..31039099788 --- /dev/null +++ b/changelogs/unreleased/sh-fix-utf-8-encoding-resolve-conflicts.yml @@ -0,0 +1,5 @@ +--- +title: Fix UTF-8 conversion issues when resolving conflicts +merge_request: 29453 +author: +type: fixed diff --git a/lib/gitlab/gitaly_client/conflicts_service.rb b/lib/gitlab/gitaly_client/conflicts_service.rb index 077b63205a8..3ab4ef4adfb 100644 --- a/lib/gitlab/gitaly_client/conflicts_service.rb +++ b/lib/gitlab/gitaly_client/conflicts_service.rb @@ -67,7 +67,7 @@ module Gitlab their_commit_oid: @their_commit_oid, source_branch: source_branch, target_branch: target_branch, - commit_message: resolution.commit_message, + commit_message: encode_binary(resolution.commit_message), user: Gitlab::Git::User.from_gitlab(resolution.user).to_gitaly ) end diff --git a/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb b/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb index e4fe01a671f..a8a6830ee2d 100644 --- a/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb +++ b/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb @@ -35,7 +35,7 @@ describe Gitlab::GitalyClient::ConflictsService do end let(:source_branch) { 'master' } let(:target_branch) { 'feature' } - let(:commit_message) { 'Solving conflicts' } + let(:commit_message) { 'Solving conflicts\n\nTést' } let(:resolution) do Gitlab::Git::Conflict::Resolution.new(user, files, commit_message) end @@ -51,6 +51,18 @@ describe Gitlab::GitalyClient::ConflictsService do subject end + it 'handles commit messages with UTF-8 characters' do + allow(::Gitlab::GitalyClient).to receive(:call).and_call_original + expect(::Gitlab::GitalyClient).to receive(:call).with(anything, :conflicts_service, :resolve_conflicts, any_args) do |*args| + # Force the generation of request messages by iterating through the enumerator + args[3].to_a + + double(resolution_error: nil) + end + + subject + end + it 'raises a relevant exception if resolution_error is present' do expect_any_instance_of(Gitaly::ConflictsService::Stub).to receive(:resolve_conflicts) .with(kind_of(Enumerator), kind_of(Hash)).and_return(double(resolution_error: "something happened")) From df650eaae5f94357f155c9b35c51a6b8c98fb6d7 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 10 Jun 2019 20:50:00 -0700 Subject: [PATCH 2/2] Force source and target branch to binary mode --- lib/gitlab/gitaly_client/conflicts_service.rb | 4 ++-- .../gitaly_client/conflicts_service_spec.rb | 23 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/gitlab/gitaly_client/conflicts_service.rb b/lib/gitlab/gitaly_client/conflicts_service.rb index 3ab4ef4adfb..d16e45c964d 100644 --- a/lib/gitlab/gitaly_client/conflicts_service.rb +++ b/lib/gitlab/gitaly_client/conflicts_service.rb @@ -65,8 +65,8 @@ module Gitlab our_commit_oid: @our_commit_oid, target_repository: target_repository.gitaly_repository, their_commit_oid: @their_commit_oid, - source_branch: source_branch, - target_branch: target_branch, + source_branch: encode_binary(source_branch), + target_branch: encode_binary(target_branch), commit_message: encode_binary(resolution.commit_message), user: Gitlab::Git::User.from_gitlab(resolution.user).to_gitaly ) diff --git a/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb b/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb index a8a6830ee2d..52630ba0223 100644 --- a/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb +++ b/spec/lib/gitlab/gitaly_client/conflicts_service_spec.rb @@ -51,16 +51,23 @@ describe Gitlab::GitalyClient::ConflictsService do subject end - it 'handles commit messages with UTF-8 characters' do - allow(::Gitlab::GitalyClient).to receive(:call).and_call_original - expect(::Gitlab::GitalyClient).to receive(:call).with(anything, :conflicts_service, :resolve_conflicts, any_args) do |*args| - # Force the generation of request messages by iterating through the enumerator - args[3].to_a + context 'with branches with UTF-8 characters' do + let(:source_branch) { 'testòbranch' } + let(:target_branch) { 'ábranch' } - double(resolution_error: nil) + it 'handles commit messages with UTF-8 characters' do + allow(::Gitlab::GitalyClient).to receive(:call).and_call_original + expect(::Gitlab::GitalyClient).to receive(:call).with(anything, :conflicts_service, :resolve_conflicts, any_args) do |*args| + # Force the generation of request messages by iterating through the enumerator + message = args[3].to_a.first + params = [message.header.commit_message, message.header.source_branch, message.header.target_branch] + expect(params.map(&:encoding).uniq).to eq([Encoding::ASCII_8BIT]) + + double(resolution_error: nil) + end + + subject end - - subject end it 'raises a relevant exception if resolution_error is present' do