diff options
author | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2022-10-31 20:01:08 +0300 |
---|---|---|
committer | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2022-10-31 20:21:11 +0300 |
commit | 443964112b728193ac2796debcf5477826bc7876 (patch) | |
tree | 78ef4e2424b90ff7632263df553e4c720bef7ee9 /ruby | |
parent | a238cbfb8fd039b2fed350aa81f0c0154d9a01f5 (diff) |
ruby: Remove supporting methods and logger classes
After the wrappers for Rugged were removed, related supporting methods
become obsolete. One notable change in this commit is that we don't
need to create Gitlab::Git::Repository in find_license. Instead, we
can extract the repo path directly from the call argument.
Changelog: other
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/lib/gitaly_server.rb | 40 | ||||
-rw-r--r-- | ruby/lib/gitaly_server/client.rb | 28 | ||||
-rw-r--r-- | ruby/lib/gitaly_server/repository_service.rb | 6 | ||||
-rw-r--r-- | ruby/lib/gitlab/encoding_helper.rb | 98 | ||||
-rw-r--r-- | ruby/lib/gitlab/git.rb | 99 | ||||
-rw-r--r-- | ruby/lib/gitlab/git_logger.rb | 7 | ||||
-rw-r--r-- | ruby/lib/gitlab/rails_logger.rb | 10 | ||||
-rw-r--r-- | ruby/lib/gitlab/utils/strong_memoize.rb | 41 | ||||
-rw-r--r-- | ruby/spec/spec_helper.rb | 1 |
9 files changed, 18 insertions, 312 deletions
diff --git a/ruby/lib/gitaly_server.rb b/ruby/lib/gitaly_server.rb index bb293c7bf..168eb08e9 100644 --- a/ruby/lib/gitaly_server.rb +++ b/ruby/lib/gitaly_server.rb @@ -1,44 +1,34 @@ $:.unshift(File.expand_path('../proto', __dir__)) require 'gitaly' -require_relative 'gitlab/git.rb' +# External dependencies of Gitlab::Git +require 'linguist/blob_helper' +require 'securerandom' +require 'gitlab-labkit' +require 'rugged' + +# Ruby on Rails mix-ins that GitLab::Git code relies on +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/numeric/bytes' +require 'active_support/core_ext/numeric/time' +require 'active_support/core_ext/integer/time' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/enumerable' + +require_relative './gitlab/config.rb' -require_relative 'gitaly_server/client.rb' require_relative 'gitaly_server/utils.rb' require_relative 'gitaly_server/repository_service.rb' require_relative 'gitaly_server/health_service.rb' module GitalyServer - STORAGE_PATH_HEADER = 'gitaly-storage-path'.freeze REPO_PATH_HEADER = 'gitaly-repo-path'.freeze - GL_REPOSITORY_HEADER = 'gitaly-gl-repository'.freeze - REPO_ALT_DIRS_HEADER = 'gitaly-repo-alt-dirs'.freeze GITALY_SERVERS_HEADER = 'gitaly-servers'.freeze - def self.storage_path(call) - call.metadata.fetch(STORAGE_PATH_HEADER) - end - def self.repo_path(call) call.metadata.fetch(REPO_PATH_HEADER) end - def self.gl_repository(call) - call.metadata.fetch(GL_REPOSITORY_HEADER) - end - - def self.repo_alt_dirs(call) - call.metadata.fetch(REPO_ALT_DIRS_HEADER) - end - - def self.feature_flags(call) - FeatureFlags.new(call.metadata) - end - - def self.client(call) - Client.new(call.metadata[GITALY_SERVERS_HEADER]) - end - def self.register_handlers(server) server.handle(RepositoryService.new) server.handle(HealthService.new) diff --git a/ruby/lib/gitaly_server/client.rb b/ruby/lib/gitaly_server/client.rb deleted file mode 100644 index 49fab89ce..000000000 --- a/ruby/lib/gitaly_server/client.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'base64' -require 'json' - -module GitalyServer - class Client - ServerLookupError = Class.new(StandardError) - - def initialize(encoded_servers) - @servers = encoded_servers.present? ? JSON.parse(Base64.strict_decode64(encoded_servers)) : {} - end - - def shared_secret(storage) - server(storage)['token'] - end - - def address(storage) - server(storage)['address'] - end - - private - - def server(storage) - raise ServerLookupError.new("cannot find gitaly address for storage #{storage.inspect}") unless @servers.has_key?(storage) - - @servers[storage] - end - end -end diff --git a/ruby/lib/gitaly_server/repository_service.rb b/ruby/lib/gitaly_server/repository_service.rb index 98cb8b748..9cb39fd5d 100644 --- a/ruby/lib/gitaly_server/repository_service.rb +++ b/ruby/lib/gitaly_server/repository_service.rb @@ -4,11 +4,11 @@ module GitalyServer class RepositoryService < Gitaly::RepositoryService::Service include Utils - def find_license(request, call) - repo = Gitlab::Git::Repository.from_gitaly(request.repository, call) + def find_license(_request, call) + path = GitalyServer.repo_path(call) begin - project = ::Licensee.project(repo.path) + project = ::Licensee.project(path) return Gitaly::FindLicenseResponse.new(license_short_name: "") unless project&.license license = project.license diff --git a/ruby/lib/gitlab/encoding_helper.rb b/ruby/lib/gitlab/encoding_helper.rb deleted file mode 100644 index 039b0cda4..000000000 --- a/ruby/lib/gitlab/encoding_helper.rb +++ /dev/null @@ -1,98 +0,0 @@ -module Gitlab - module EncodingHelper - extend self - - # This threshold is carefully tweaked to prevent usage of encodings detected - # by CharlockHolmes with low confidence. If CharlockHolmes confidence is low, - # we're better off sticking with utf8 encoding. - # Reason: git diff can return strings with invalid utf8 byte sequences if it - # truncates a diff in the middle of a multibyte character. In this case - # CharlockHolmes will try to guess the encoding and will likely suggest an - # obscure encoding with low confidence. - # There is a lot more info with this merge request: - # https://gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193 - ENCODING_CONFIDENCE_THRESHOLD = 50 - - def encode!(message) - message = force_encode_utf8(message) - return message if message.valid_encoding? - - # return message if message type is binary - detect = CharlockHolmes::EncodingDetector.detect(message) - return message.force_encoding("BINARY") if detect_binary?(message, detect) - - if detect && detect[:encoding] && detect[:confidence] > ENCODING_CONFIDENCE_THRESHOLD - # force detected encoding if we have sufficient confidence. - message.force_encoding(detect[:encoding]) - end - - # encode and clean the bad chars - message.replace clean(message) - rescue ArgumentError => e - return unless e.message.include?('unknown encoding name') - - encoding = detect ? detect[:encoding] : "unknown" - "--broken encoding: #{encoding}" - end - - def detect_binary?(data, detect = nil) - detect ||= CharlockHolmes::EncodingDetector.detect(data) - detect && detect[:type] == :binary && detect[:confidence] == 100 - end - - def detect_libgit2_binary?(data) - # EncodingDetector checks the first 1024 * 1024 bytes for NUL byte, libgit2 checks - # only the first 8000 (https://github.com/libgit2/libgit2/blob/2ed855a9e8f9af211e7274021c2264e600c0f86b/src/filter.h#L15), - # which is what we use below to keep a consistent behavior. - detect = CharlockHolmes::EncodingDetector.new(8000).detect(data) - detect && detect[:type] == :binary - end - - def encode_utf8(message) - message = force_encode_utf8(message) - return message if message.valid_encoding? - - detect = CharlockHolmes::EncodingDetector.detect(message) - if detect && detect[:encoding] - begin - CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8') - rescue ArgumentError => e - Rails.logger.warn("Ignoring error converting #{detect[:encoding]} into UTF8: #{e.message}") - - '' - end - else - clean(message) - end - rescue ArgumentError - nil - end - - def encode_binary(str) - return "" if str.nil? - - str.dup.force_encoding(Encoding::ASCII_8BIT) - end - - def binary_stringio(str) - StringIO.new(str.freeze || '').tap { |io| io.set_encoding(Encoding::ASCII_8BIT) } - end - - private - - def force_encode_utf8(message) - raise ArgumentError unless message.respond_to?(:force_encoding) - return message if message.encoding == Encoding::UTF_8 && message.valid_encoding? - - message = message.dup if message.respond_to?(:frozen?) && message.frozen? - - message.force_encoding("UTF-8") - end - - def clean(message) - message.encode("UTF-16BE", undef: :replace, invalid: :replace, replace: "".encode("UTF-16BE")) - .encode("UTF-8") - .gsub("\0".encode("UTF-8"), "") - end - end -end diff --git a/ruby/lib/gitlab/git.rb b/ruby/lib/gitlab/git.rb deleted file mode 100644 index 1f99646ee..000000000 --- a/ruby/lib/gitlab/git.rb +++ /dev/null @@ -1,99 +0,0 @@ -# External dependencies of Gitlab::Git -require 'rugged' -require 'linguist/blob_helper' -require 'securerandom' -require 'gitlab-labkit' - -# Ruby on Rails mix-ins that GitLab::Git code relies on -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/numeric/bytes' -require 'active_support/core_ext/numeric/time' -require 'active_support/core_ext/integer/time' -require 'active_support/core_ext/module/delegation' -require 'active_support/core_ext/enumerable' - -require_relative 'git_logger.rb' -require_relative 'rails_logger.rb' -require_relative 'config.rb' - -dir = __dir__ - -# Some later requires are order-sensitive. Manually require whatever we need. -require_relative "#{dir}/encoding_helper.rb" -require_relative "#{dir}/utils/strong_memoize.rb" -require_relative "#{dir}/git/popen.rb" - -# Require all .rb files we can find in the gitlab lib directory -Dir["#{dir}/**/*.rb"].sort.each do |ruby_file| - require File.expand_path(ruby_file) -end - -class String - # Because we are not rendering HTML, this is a no-op in gitaly-ruby. - def html_safe - self - end -end - -module Gitlab - module Git - # The ID of empty tree. - # See http://stackoverflow.com/a/40884093/1856239 and - # https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012 - EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'.freeze - BLANK_SHA = ('0' * 40).freeze - TAG_REF_PREFIX = "refs/tags/".freeze - BRANCH_REF_PREFIX = "refs/heads/".freeze - - BaseError = Class.new(StandardError) - CommandError = Class.new(BaseError) - CommitError = Class.new(BaseError) - PreReceiveError = Class.new(BaseError) - - class << self - include Gitlab::EncodingHelper - - def ref_name(ref) - encode!(ref).sub(%r{\Arefs/(tags|heads|remotes)/}, '') - end - - def branch_name(ref) - ref = ref.to_s - self.ref_name(ref) if self.branch_ref?(ref) - end - - def tag_name(ref) - ref = ref.to_s - self.ref_name(ref) if self.tag_ref?(ref) - end - - def tag_ref?(ref) - ref.start_with?(TAG_REF_PREFIX) - end - - def branch_ref?(ref) - ref.start_with?(BRANCH_REF_PREFIX) - end - - def blank_ref?(ref) - ref == BLANK_SHA - end - - def version - Gitlab::Git::Version.git_version - end - end - end -end - -module Gitlab - module GlId - def self.gl_id(user) - user.gl_id - end - - def self.gl_id_from_id_value(id) - "user-#{id}" - end - end -end diff --git a/ruby/lib/gitlab/git_logger.rb b/ruby/lib/gitlab/git_logger.rb deleted file mode 100644 index 23a163ba6..000000000 --- a/ruby/lib/gitlab/git_logger.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'logger' - -module Gitlab - GitLogger = Logger.new(STDOUT) - GitLogger.progname = 'githost.log' - GitLogger.level = 'info' -end diff --git a/ruby/lib/gitlab/rails_logger.rb b/ruby/lib/gitlab/rails_logger.rb deleted file mode 100644 index 917bbb487..000000000 --- a/ruby/lib/gitlab/rails_logger.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'logger' - -module Rails - LOGGER = Logger.new(STDOUT) - LOGGER.level = 'info' - - def self.logger - LOGGER - end -end diff --git a/ruby/lib/gitlab/utils/strong_memoize.rb b/ruby/lib/gitlab/utils/strong_memoize.rb deleted file mode 100644 index fe091f461..000000000 --- a/ruby/lib/gitlab/utils/strong_memoize.rb +++ /dev/null @@ -1,41 +0,0 @@ -module Gitlab - module Utils - module StrongMemoize - # Instead of writing patterns like this: - # - # def trigger_from_token - # return @trigger if defined?(@trigger) - # - # @trigger = Ci::Trigger.find_by_token(params[:token].to_s) - # end - # - # We could write it like: - # - # include Gitlab::Utils::StrongMemoize - # - # def trigger_from_token - # strong_memoize(:trigger) do - # Ci::Trigger.find_by_token(params[:token].to_s) - # end - # end - # - def strong_memoize(name) - if instance_variable_defined?(ivar(name)) - instance_variable_get(ivar(name)) - else - instance_variable_set(ivar(name), yield) - end - end - - def clear_memoization(name) - remove_instance_variable(ivar(name)) if instance_variable_defined?(ivar(name)) - end - - private - - def ivar(name) - "@#{name}" - end - end - end -end diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb index dcd20550e..d65ae3b22 100644 --- a/ruby/spec/spec_helper.rb +++ b/ruby/spec/spec_helper.rb @@ -1,5 +1,4 @@ require_relative '../lib/gitaly_server.rb' -require_relative '../lib/gitlab/git.rb' require_relative 'support/sentry.rb' require 'timecop' require 'rspec-parameterized' |