From 267cd3e3594dcb1555b5018723f3cc620873499d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Rodr=C3=ADguez?= Date: Wed, 15 Feb 2017 23:08:30 -0300 Subject: Incorporate Gitaly client for refs service --- lib/api/internal.rb | 5 +++- lib/gitlab/git.rb | 2 ++ lib/gitlab/git/repository.rb | 44 ++++++++++++++++++++++++++----- lib/gitlab/gitaly_client/notifications.rb | 13 +++------ lib/gitlab/gitaly_client/ref.rb | 35 ++++++++++++++++++++++++ lib/gitlab/gitaly_client/util.rb | 13 +++++++++ 6 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 lib/gitlab/gitaly_client/ref.rb create mode 100644 lib/gitlab/gitaly_client/util.rb (limited to 'lib') diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 523f38d129e..56c597dffcb 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -138,8 +138,11 @@ module API return unless Gitlab::GitalyClient.enabled? + relative_path = Gitlab::RepoPath.strip_storage_path(params[:repo_path]) + project = Project.find_by_full_path(relative_path.sub(/\.(git|wiki)\z/, '')) + begin - Gitlab::GitalyClient::Notifications.new(params[:repo_path]).post_receive + Gitlab::GitalyClient::Notifications.new(project.repository_storage, relative_path).post_receive rescue GRPC::Unavailable => e render_api_error(e, 500) end diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index d3df3f1bca1..936606152e9 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -4,6 +4,8 @@ module Gitlab TAG_REF_PREFIX = "refs/tags/".freeze BRANCH_REF_PREFIX = "refs/heads/".freeze + CommandError = Class.new(StandardError) + class << self def ref_name(ref) ref.sub(/\Arefs\/(tags|heads)\//, '') diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index 32aebb6f6f0..2e4314932c8 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -25,9 +25,13 @@ module Gitlab # 'path' must be the path to a _bare_ git repository, e.g. # /path/to/my-repo.git - def initialize(path) - @path = path - @name = path.split("/").last + def initialize(repository_storage, relative_path) + @repository_storage = repository_storage + @relative_path = relative_path + + storage_path = Gitlab.config.repositories.storages[@repository_storage]['path'] + @path = File.join(storage_path, @relative_path) + @name = @relative_path.split("/").last @attributes = Gitlab::Git::Attributes.new(path) end @@ -37,7 +41,15 @@ module Gitlab # Default branch in the repository def root_ref - @root_ref ||= discover_default_branch + @root_ref ||= Gitlab::GitalyClient.migrate(:root_ref) do |is_enabled| + if is_enabled + gitaly_ref_client.default_branch_name + else + discover_default_branch + end + end + rescue GRPC::BadStatus => e + raise CommandError.new(e) end # Alias to old method for compatibility @@ -54,7 +66,15 @@ module Gitlab # Returns an Array of branch names # sorted by name ASC def branch_names - branches.map(&:name) + Gitlab::GitalyClient.migrate(:branch_names) do |is_enabled| + if is_enabled + gitaly_ref_client.branch_names + else + branches.map(&:name) + end + end + rescue GRPC::BadStatus => e + raise CommandError.new(e) end # Returns an Array of Branches @@ -107,7 +127,15 @@ module Gitlab # Returns an Array of tag names def tag_names - rugged.tags.map { |t| t.name } + Gitlab::GitalyClient.migrate(:tag_names) do |is_enabled| + if is_enabled + gitaly_ref_client.tag_names + else + rugged.tags.map { |t| t.name } + end + end + rescue GRPC::BadStatus => e + raise CommandError.new(e) end # Returns an Array of Tags @@ -1202,6 +1230,10 @@ module Gitlab diff.find_similar!(break_rewrites: break_rewrites) diff.each_patch end + + def gitaly_ref_client + @gitaly_ref_client ||= Gitlab::GitalyClient::Ref.new(@repository_storage, @relative_path) + end end end end diff --git a/lib/gitlab/gitaly_client/notifications.rb b/lib/gitlab/gitaly_client/notifications.rb index cbfb129c002..f0d93ded91b 100644 --- a/lib/gitlab/gitaly_client/notifications.rb +++ b/lib/gitlab/gitaly_client/notifications.rb @@ -3,18 +3,13 @@ module Gitlab class Notifications attr_accessor :stub - def initialize(repo_path) - full_path = Gitlab::RepoPath.strip_storage_path(repo_path). - sub(/\.git\z/, '').sub(/\.wiki\z/, '') - @project = Project.find_by_full_path(full_path) - - channel = GitalyClient.get_channel(@project.repository_storage) - @stub = Gitaly::Notifications::Stub.new(nil, nil, channel_override: channel) + def initialize(repository_storage, relative_path) + @channel, @repository = Util.process_path(repository_storage, relative_path) + @stub = Gitaly::Notifications::Stub.new(nil, nil, channel_override: @channel) end def post_receive - repository = Gitaly::Repository.new(path: @project.repository.path_to_repo) - request = Gitaly::PostReceiveRequest.new(repository: repository) + request = Gitaly::PostReceiveRequest.new(repository: @repository) @stub.post_receive(request) end end diff --git a/lib/gitlab/gitaly_client/ref.rb b/lib/gitlab/gitaly_client/ref.rb new file mode 100644 index 00000000000..bfc5fa573c7 --- /dev/null +++ b/lib/gitlab/gitaly_client/ref.rb @@ -0,0 +1,35 @@ +module Gitlab + module GitalyClient + class Ref + attr_accessor :stub + + def initialize(repository_storage, relative_path) + @channel, @repository = Util.process_path(repository_storage, relative_path) + @stub = Gitaly::Ref::Stub.new(nil, nil, channel_override: @channel) + end + + def default_branch_name + request = Gitaly::FindDefaultBranchNameRequest.new(repository: @repository) + stub.find_default_branch_name(request).name.gsub(/^refs\/heads\//, '') + end + + def branch_names + request = Gitaly::FindAllBranchNamesRequest.new(repository: @repository) + consume_refs_response(stub.find_all_branch_names(request), prefix: 'refs/heads/') + end + + def tag_names + request = Gitaly::FindAllTagNamesRequest.new(repository: @repository) + consume_refs_response(stub.find_all_tag_names(request), prefix: 'refs/tags/') + end + + private + + def consume_refs_response(response, prefix:) + response.flat_map do |r| + r.names.map { |name| name.sub(/\A#{Regexp.escape(prefix)}/, '') } + end + end + end + end +end diff --git a/lib/gitlab/gitaly_client/util.rb b/lib/gitlab/gitaly_client/util.rb new file mode 100644 index 00000000000..d272c25d1f9 --- /dev/null +++ b/lib/gitlab/gitaly_client/util.rb @@ -0,0 +1,13 @@ +module Gitlab + module GitalyClient + module Util + def self.process_path(repository_storage, relative_path) + channel = GitalyClient.get_channel(repository_storage) + storage_path = Gitlab.config.repositories.storages[repository_storage]['path'] + repository = Gitaly::Repository.new(path: File.join(storage_path, relative_path)) + + [channel, repository] + end + end + end +end -- cgit v1.2.1