Commit d3bcf8ac authored by Jacob Vosmaer's avatar Jacob Vosmaer

Fix gitaly ref encoding bugs

parent 4596ab5b
......@@ -7,8 +7,10 @@ module Git
CommandError = Class.new(StandardError)
class << self
include Gitlab::EncodingHelper
def ref_name(ref)
ref.sub(/\Arefs\/(tags|heads)\//, '')
encode! ref.sub(/\Arefs\/(tags|heads)\//, '')
end
def branch_name(ref)
......
......@@ -113,9 +113,7 @@ def find_branch(name, force_reload = false)
def local_branches(sort_by: nil)
gitaly_migrate(:local_branches) do |is_enabled|
if is_enabled
gitaly_ref_client.local_branches(sort_by: sort_by).map do |gitaly_branch|
Gitlab::Git::Branch.new(self, gitaly_branch.name, gitaly_branch)
end
gitaly_ref_client.local_branches(sort_by: sort_by)
else
branches(filter: :local, sort_by: sort_by)
end
......
module Gitlab
module GitalyClient
class Ref
include Gitlab::EncodingHelper
# 'repository' is a Gitlab::Git::Repository
def initialize(repository)
@repository = repository
@gitaly_repo = repository.gitaly_repository
@storage = repository.storage
end
......@@ -16,13 +19,13 @@ def default_branch_name
def branch_names
request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
response = GitalyClient.call(@storage, :ref, :find_all_branch_names, request)
consume_refs_response(response, prefix: 'refs/heads/')
consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) }
end
def tag_names
request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
response = GitalyClient.call(@storage, :ref, :find_all_tag_names, request)
consume_refs_response(response, prefix: 'refs/tags/')
consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) }
end
def find_ref_name(commit_id, ref_prefix)
......@@ -51,10 +54,8 @@ def local_branches(sort_by: nil)
private
def consume_refs_response(response, prefix:)
response.flat_map do |r|
r.names.map { |name| name.sub(/\A#{Regexp.escape(prefix)}/, '') }
end
def consume_refs_response(response)
response.flat_map { |message| message.names.map { |name| yield(name) } }
end
def sort_by_param(sort_by)
......@@ -64,7 +65,15 @@ def sort_by_param(sort_by)
end
def consume_branches_response(response)
response.flat_map { |r| r.branches }
response.flat_map do |message|
message.branches.map do |gitaly_branch|
Gitlab::Git::Branch.new(
@repository,
encode!(gitaly_branch.name.dup),
gitaly_branch.commit_id
)
end
end
end
end
end
......
......@@ -26,6 +26,10 @@
end
end
it 'returns UTF-8' do
expect(repository.root_ref.encoding).to eq(Encoding.find('UTF-8'))
end
context 'with gitaly enabled' do
before do
stub_gitaly
......@@ -123,6 +127,11 @@
it 'has SeedRepo::Repo::BRANCHES.size elements' do
expect(subject.size).to eq(SeedRepo::Repo::BRANCHES.size)
end
it 'returns UTF-8' do
expect(subject.first.encoding).to eq(Encoding.find('UTF-8'))
end
it { is_expected.to include("master") }
it { is_expected.not_to include("branch-from-space") }
......@@ -158,10 +167,15 @@
subject { repository.tag_names }
it { is_expected.to be_kind_of Array }
it 'has SeedRepo::Repo::TAGS.size elements' do
expect(subject.size).to eq(SeedRepo::Repo::TAGS.size)
end
it 'returns UTF-8' do
expect(subject.first.encoding).to eq(Encoding.find('UTF-8'))
end
describe '#last' do
subject { super().last }
it { is_expected.to eq("v1.2.1") }
......@@ -1276,6 +1290,16 @@ def commit_files(commit)
Gitlab::GitalyClient.clear_stubs!
end
it 'returns a Branch with UTF-8 fields' do
branches = @repo.local_branches.to_a
expect(branches.size).to be > 0
utf_8 = Encoding.find('utf-8')
branches.each do |branch|
expect(branch.name.encoding).to eq(utf_8)
expect(branch.target.encoding).to eq(utf_8) unless branch.target.nil?
end
end
it 'gets the branches from GitalyClient' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches)
.and_return([])
......
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