Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@gitlab.com>2022-05-18 16:50:56 +0300
committerToon Claes <toon@gitlab.com>2022-05-18 16:50:56 +0300
commit03a411532099f31aabf477b49adc031c2a049b78 (patch)
treefd3bd177987a51fc0799662ad62b2f9be2e7e3cc
parent2106629e3af3e8949b23f20825d6bfee62c10992 (diff)
parentde8bbc7c68a20578e9c83fd7b7b4e48d4ea31112 (diff)
Merge branch 'pks-ruby-reduce-usage-of-git' into 'master'
ruby: Reduce usage of Git commands See merge request gitlab-org/gitaly!4559
-rw-r--r--internal/git/command_factory.go8
-rw-r--r--internal/git/command_factory_test.go7
-rw-r--r--ruby/lib/gitlab/git.rb2
-rw-r--r--ruby/lib/gitlab/git/committer_with_hooks.rb3
-rw-r--r--ruby/lib/gitlab/git/gitaly_remote_repository.rb113
-rw-r--r--ruby/lib/gitlab/git/operation_service.rb41
-rw-r--r--ruby/lib/gitlab/git/remote_repository.rb71
-rw-r--r--ruby/lib/gitlab/git/repository.rb53
-rw-r--r--ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb136
-rw-r--r--ruby/spec/lib/gitlab/git/remote_repository_spec.rb103
-rw-r--r--ruby/spec/lib/gitlab/git/repository_spec.rb101
-rw-r--r--ruby/spec/support/helpers/integration_helper.rb12
12 files changed, 30 insertions, 620 deletions
diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go
index 14f94ade8..9f9cb5c81 100644
--- a/internal/git/command_factory.go
+++ b/internal/git/command_factory.go
@@ -533,11 +533,9 @@ func (cf *ExecCommandFactory) SidecarGitConfiguration(ctx context.Context) ([]Co
}
// ... as well as all configuration that exists for specific Git subcommands. The sidecar
- // only executes git-fetch(1), git-symbolic-ref(1) and git-update-ref(1) nowadays, and this
- // set of commands is not expected to grow anymore. So while really intimate with how the
- // sidecar does this, it is good enough until we finally remove it.
- options = append(options, commandDescriptions["fetch"].opts...)
- options = append(options, commandDescriptions["symbolic-ref"].opts...)
+ // only executes git-update-ref(1) nowadays, and this set of commands is not expected to
+ // grow anymore. So while really intimate with how the sidecar does this, it is good enough
+ // until we finally remove it.
options = append(options, commandDescriptions["update-ref"].opts...)
// Convert the `GlobalOption`s into `ConfigPair`s.
diff --git a/internal/git/command_factory_test.go b/internal/git/command_factory_test.go
index ab617297d..8aeaaa7a8 100644
--- a/internal/git/command_factory_test.go
+++ b/internal/git/command_factory_test.go
@@ -499,13 +499,6 @@ func TestExecCommandFactory_SidecarGitConfiguration(t *testing.T) {
}
commonTail := []git.ConfigPair{
- {Key: "fetch.writeCommitGraph", Value: "false"},
- {Key: "http.followRedirects", Value: "false"},
- {Key: "fetch.recurseSubmodules", Value: "no"},
- {Key: "fetch.fsckObjects", Value: "true"},
- {Key: "fetch.fsck.badTimezone", Value: "ignore"},
- {Key: "fetch.fsck.missingSpaceBeforeDate", Value: "ignore"},
- {Key: "fetch.fsck.zeroPaddedFilemode", Value: "ignore"},
{Key: "custom.key", Value: "injected"},
}
diff --git a/ruby/lib/gitlab/git.rb b/ruby/lib/gitlab/git.rb
index ec2c97e3e..3e8c75da3 100644
--- a/ruby/lib/gitlab/git.rb
+++ b/ruby/lib/gitlab/git.rb
@@ -2,6 +2,7 @@
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'
@@ -21,7 +22,6 @@ 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/remote_repository.rb"
require_relative "#{dir}/git/popen.rb"
# Require all .rb files we can find in the gitlab lib directory
diff --git a/ruby/lib/gitlab/git/committer_with_hooks.rb b/ruby/lib/gitlab/git/committer_with_hooks.rb
index 90e98e729..57ca4ac5b 100644
--- a/ruby/lib/gitlab/git/committer_with_hooks.rb
+++ b/ruby/lib/gitlab/git/committer_with_hooks.rb
@@ -10,8 +10,7 @@ module Gitlab
def commit
result = Gitlab::Git::OperationService.new(git_user, gl_wiki.repository).with_branch(
- @wiki.ref,
- start_branch_name: @wiki.ref
+ @wiki.ref
) do |_start_commit|
super(false)
end
diff --git a/ruby/lib/gitlab/git/gitaly_remote_repository.rb b/ruby/lib/gitlab/git/gitaly_remote_repository.rb
deleted file mode 100644
index 9ed5b750c..000000000
--- a/ruby/lib/gitlab/git/gitaly_remote_repository.rb
+++ /dev/null
@@ -1,113 +0,0 @@
-require 'gitlab-labkit'
-
-module Gitlab
- module Git
- class GitalyRemoteRepository < RemoteRepository
- CLIENT_NAME = 'gitaly-ruby'.freeze
- PEM_REXP = /[-]+BEGIN CERTIFICATE[-]+.+?[-]+END CERTIFICATE[-]+/m.freeze
-
- attr_reader :gitaly_client
-
- def initialize(gitaly_repository, call)
- @gitaly_repository = gitaly_repository
- @storage = gitaly_repository.storage_name
- @gitaly_client = GitalyServer.client(call)
-
- @interceptors = []
- @interceptors << Labkit::Tracing::GRPC::ClientInterceptor.instance if Labkit::Tracing.enabled?
- end
-
- def path
- raise 'gitaly-ruby cannot access remote repositories by path'
- end
-
- def empty?
- !exists? || !has_visible_content?
- end
-
- def branch_exists?(branch_name)
- request = Gitaly::RefExistsRequest.new(repository: @gitaly_repository, ref: "refs/heads/#{branch_name}".b)
- stub = Gitaly::RefService::Stub.new(address, credentials)
- stub.ref_exists(request, request_kwargs).value
- end
-
- def commit_id(revision)
- request = Gitaly::FindCommitRequest.new(repository: @gitaly_repository, revision: revision.b)
- stub = Gitaly::CommitService::Stub.new(address, credentials)
- stub.find_commit(request, request_kwargs)&.commit&.id.presence
- end
-
- def certs
- raise 'SSL_CERT_DIR and/or SSL_CERT_FILE environment variable must be set' unless ENV['SSL_CERT_DIR'] || ENV['SSL_CERT_FILE']
-
- return @certs if @certs
-
- files = []
- files += Dir["#{ENV['SSL_CERT_DIR']}/*"] if ENV['SSL_CERT_DIR']
- files += [ENV['SSL_CERT_FILE']] if ENV['SSL_CERT_FILE']
- files.sort!
-
- @certs = files.flat_map do |cert_file|
- File.read(cert_file).scan(PEM_REXP).map do |cert|
- begin
- OpenSSL::X509::Certificate.new(cert).to_pem
- rescue OpenSSL::OpenSSLError => e
- Rails.logger.error "Could not load certificate #{cert_file} #{e}"
- nil
- end
- end.compact
- end.uniq.join("\n")
- end
-
- def credentials
- if URI(gitaly_client.address(storage)).scheme == 'tls'
- GRPC::Core::ChannelCredentials.new certs
- else
- :this_channel_is_insecure
- end
- end
-
- private
-
- def exists?
- request = Gitaly::RepositoryExistsRequest.new(repository: @gitaly_repository)
- stub = Gitaly::RepositoryService::Stub.new(address, credentials, interceptors: @interceptors)
- stub.repository_exists(request, request_kwargs).exists
- end
-
- def has_visible_content?
- request = Gitaly::HasLocalBranchesRequest.new(repository: @gitaly_repository)
- stub = Gitaly::RepositoryService::Stub.new(address, credentials, interceptors: @interceptors)
- stub.has_local_branches(request, request_kwargs).value
- end
-
- def address
- addr = gitaly_client.address(storage)
- addr = addr.sub(%r{^tcp://|^tls://}, '') if %w[tcp tls].include? URI(addr).scheme
- addr
- end
-
- def shared_secret
- gitaly_client.shared_secret(storage).to_s
- end
-
- def request_kwargs
- @request_kwargs ||= begin
- metadata = {
- 'authorization' => "Bearer #{authorization_token}",
- 'client_name' => CLIENT_NAME
- }
-
- { metadata: metadata }
- end
- end
-
- def authorization_token
- issued_at = Time.now.to_i.to_s
- hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, shared_secret, issued_at)
-
- "v2.#{hmac}.#{issued_at}"
- end
- end
- end
-end
diff --git a/ruby/lib/gitlab/git/operation_service.rb b/ruby/lib/gitlab/git/operation_service.rb
index 9a5d58219..902a707ad 100644
--- a/ruby/lib/gitlab/git/operation_service.rb
+++ b/ruby/lib/gitlab/git/operation_service.rb
@@ -25,41 +25,22 @@ module Gitlab
@repository = new_repository
end
- # Whenever `start_branch_name` or `start_sha` is passed, if `branch_name`
- # doesn't exist, it will be created from the commit pointed to by
- # `start_branch_name` or `start_sha`.
- #
- # If `start_repository` is passed, and the branch doesn't exist,
- # it would try to find the commits from it instead of current repository.
- def with_branch(branch_name,
- start_branch_name: nil,
- start_sha: nil,
- start_repository: repository,
- force: false,
- &block)
- start_repository = RemoteRepository.new(start_repository) unless start_repository.is_a?(RemoteRepository)
-
- start_branch_name = nil if start_repository.empty?
-
- if start_branch_name.present? && !start_repository.branch_exists?(start_branch_name)
- raise ArgumentError, "Cannot find branch '#{start_branch_name}'"
- elsif start_sha.present? && !start_repository.commit_id(start_sha)
- raise ArgumentError, "Cannot find commit '#{start_sha}'"
+ # Execute the block with the tip commit referenced by the given branch.
+ # The branch must exist before calling this function.
+ def with_branch(branch_name, &block)
+ if !repository.empty? && !repository.branch_exists?(branch_name)
+ raise ArgumentError, "Cannot find branch '#{branch_name}'"
end
- update_branch_with_hooks(branch_name, force) do
- repository.with_repo_branch_commit(
- start_repository,
- start_sha.presence || start_branch_name.presence || branch_name,
- &block
- )
+ update_branch_with_hooks(branch_name) do
+ repository.with_repo_branch_commit(branch_name, &block)
end
end
private
# Returns [newrev, should_run_after_create, should_run_after_create_branch]
- def update_branch_with_hooks(branch_name, force)
+ def update_branch_with_hooks(branch_name)
was_empty = repository.empty?
# Make commit
@@ -68,7 +49,7 @@ module Gitlab
raise Gitlab::Git::CommitError.new('Failed to create commit') unless newrev
branch = repository.find_branch(branch_name)
- oldrev = find_oldrev_from_branch(newrev, branch, force)
+ oldrev = find_oldrev_from_branch(newrev, branch)
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
update_ref_in_hooks(ref, newrev, oldrev)
@@ -76,13 +57,11 @@ module Gitlab
BranchUpdate.new(newrev, was_empty, was_empty || Gitlab::Git.blank_ref?(oldrev))
end
- def find_oldrev_from_branch(newrev, branch, force)
+ def find_oldrev_from_branch(newrev, branch)
return Gitlab::Git::BLANK_SHA unless branch
oldrev = branch.target
- return oldrev if force
-
merge_base = repository.merge_base(newrev, branch.target)
raise Gitlab::Git::Repository::InvalidRef unless merge_base
diff --git a/ruby/lib/gitlab/git/remote_repository.rb b/ruby/lib/gitlab/git/remote_repository.rb
deleted file mode 100644
index 50f07ad94..000000000
--- a/ruby/lib/gitlab/git/remote_repository.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-module Gitlab
- module Git
- #
- # When a Gitaly call involves two repositories instead of one we cannot
- # assume that both repositories are on the same Gitaly server. In this
- # case we need to make a distinction between the repository that the
- # call is being made on (a Repository instance), and the "other"
- # repository (a RemoteRepository instance). This is the reason why we
- # have the RemoteRepository class in Gitlab::Git.
- class RemoteRepository
- attr_reader :relative_path, :gitaly_repository
-
- def initialize(repository)
- @relative_path = repository.relative_path
- @gitaly_repository = repository.gitaly_repository
-
- @repository = repository
- end
-
- def empty?
- !@repository.exists? || @repository.empty?
- end
-
- def commit_id(revision)
- @repository.commit(revision)&.sha
- end
-
- def branch_exists?(name)
- @repository.branch_exists?(name)
- end
-
- # Compares self to a Gitlab::Git::Repository
- def same_repository?(other_repository)
- gitaly_repository.storage_name == other_repository.storage &&
- gitaly_repository.relative_path == other_repository.relative_path
- end
-
- def fetch_env(git_config_options: [])
- gitaly_ssh = File.absolute_path(File.join(Gitlab.config.gitaly.bin_dir, 'gitaly-ssh'))
- gitaly_address = gitaly_client.address(storage)
- shared_secret = gitaly_client.shared_secret(storage)
-
- request = Gitaly::SSHUploadPackRequest.new(repository: gitaly_repository, git_config_options: git_config_options)
- env = {
- 'GITALY_ADDRESS' => gitaly_address,
- 'GITALY_PAYLOAD' => request.to_json,
- 'GITALY_WD' => Dir.pwd,
- 'GIT_SSH_COMMAND' => "#{gitaly_ssh} upload-pack"
- }
- env['GITALY_TOKEN'] = shared_secret if shared_secret.present?
-
- env
- end
-
- def path
- @repository.path
- end
-
- private
-
- # Must return an object that responds to 'address' and 'storage'.
- def gitaly_client
- raise NotImplementedError.new("Can't perform remote operations on superclass")
- end
-
- def storage
- gitaly_repository.storage_name
- end
- end
- end
-end
diff --git a/ruby/lib/gitlab/git/repository.rb b/ruby/lib/gitlab/git/repository.rb
index ba0ae3507..cbe5bde19 100644
--- a/ruby/lib/gitlab/git/repository.rb
+++ b/ruby/lib/gitlab/git/repository.rb
@@ -176,26 +176,12 @@ module Gitlab
end
end
- def with_repo_branch_commit(start_repository, start_ref)
- start_repository = RemoteRepository.new(start_repository) unless start_repository.is_a?(RemoteRepository)
-
- if start_repository.empty?
- return yield nil
- elsif start_repository.same_repository?(self)
- # Directly return the commit from this repository
- return yield commit(start_ref)
- end
-
- # Find the commit from the remote repository (this triggers an RPC)
- commit_id = start_repository.commit_id(start_ref)
- return yield nil unless commit_id
-
- if existing_commit = commit(commit_id)
- # Commit is already present (e.g. in a fork, or through a previous fetch)
- yield existing_commit
+ def with_repo_branch_commit(start_ref)
+ if empty?
+ yield nil
else
- fetch_sha(start_repository, commit_id)
- yield commit(commit_id)
+ # Directly return the commit from this repository
+ yield commit(start_ref)
end
end
@@ -236,19 +222,6 @@ module Gitlab
nil
end
- # Fetch a commit from the given source repository
- def fetch_sha(source_repository, sha)
- source_repository = RemoteRepository.new(source_repository) unless source_repository.is_a?(RemoteRepository)
-
- env = source_repository.fetch_env(git_config_options: [GIT_ALLOW_SHA_UPLOAD])
-
- args = %W[fetch --no-tags #{GITALY_INTERNAL_URL} #{sha}]
- message, status = run_git(args, env: env, include_stderr: true)
- raise Gitlab::Git::CommandError, message unless status.zero?
-
- sha
- end
-
# Lookup for rugged object by oid or ref name
def lookup(oid_or_ref_name)
rugged.rev_parse(oid_or_ref_name)
@@ -277,25 +250,15 @@ module Gitlab
end
def head_symbolic_ref
- message, status = run_git(%w[symbolic-ref HEAD])
+ head = rugged.ref('HEAD')
- return 'main' if status.nonzero?
+ return 'main' if head.type != :symbolic
- Ref.extract_branch_name(message.squish)
+ Ref.extract_branch_name(head.target_id)
end
private
- def run_git(args, chdir: path, env: {}, nice: false, include_stderr: false, lazy_block: nil, &block)
- cmd = [Gitlab.config.git.bin_path, *args]
- cmd.unshift("nice") if nice
-
- object_directories = alternate_object_directories
- env['GIT_ALTERNATE_OBJECT_DIRECTORIES'] = object_directories.join(File::PATH_SEPARATOR) if object_directories.any?
-
- popen(cmd, chdir, env, include_stderr: include_stderr, lazy_block: lazy_block, &block)
- end
-
def branches_filter(filter: nil, sort_by: nil)
branches = rugged.branches.each(filter).map do |rugged_ref|
begin
diff --git a/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb b/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb
deleted file mode 100644
index 15943495e..000000000
--- a/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb
+++ /dev/null
@@ -1,136 +0,0 @@
-require 'spec_helper'
-
-describe Gitlab::Git::GitalyRemoteRepository do
- include TestRepo
- include IntegrationClient
-
- describe '#new' do
- let(:gitaly_repository) { double(storage_name: 'storage') }
- let(:call) do
- servers = Base64.strict_encode64({
- default: {
- address: 'address',
- token: 'the-secret-token'
- }
- }.to_json)
-
- double(metadata: { 'gitaly-servers' => servers })
- end
-
- context 'Labkit::Tracing enabled' do
- before do
- expect(Labkit::Tracing).to receive(:enabled?).and_return(true)
- end
-
- it 'initializes with an interceptor' do
- client = described_class.new(gitaly_repository, call)
- expect(client.instance_variable_get(:@interceptors).size).to eq 1
- end
- end
-
- context 'Labkit::Tracing disabled' do
- before do
- expect(Labkit::Tracing).to receive(:enabled?).and_return(false)
- end
-
- it 'initializes with no interceptors' do
- client = described_class.new(gitaly_repository, call)
- expect(client.instance_variable_get(:@interceptors).size).to eq 0
- end
- end
- end
-
- let(:repository) { gitlab_git_from_gitaly(new_mutable_test_repo) }
- describe 'certs' do
- let(:client) { get_client("tls://localhost:#{GitalyConfig.dynamic_port('tls')}") }
-
- context 'when neither SSL_CERT_FILE and SSL_CERT_DIR is set' do
- it 'Raises an error' do
- expect { client.certs }.to raise_error 'SSL_CERT_DIR and/or SSL_CERT_FILE environment variable must be set'
- end
- end
-
- context 'when SSL_CERT_FILE is set' do
- it 'Should return the correct certificate' do
- cert = File.join(File.dirname(__FILE__), "testdata/certs/gitalycert.pem")
- allow(ENV).to receive(:[]).with('GITLAB_TRACING').and_call_original
- allow(ENV).to receive(:[]).with('SSL_CERT_DIR').and_return(nil)
- allow(ENV).to receive(:[]).with('SSL_CERT_FILE').and_return(cert)
- certs = client.certs
- expect(certs).to eq File.read(cert)
- end
- end
-
- context 'when SSL_CERT_DIR is set' do
- it 'Should return concatenation of gitalycert and gitalycert2 and gitalycert3 omitting gitalycertdup.pem' do
- cert_pool_dir = File.join(File.dirname(__FILE__), "testdata/certs")
- allow(ENV).to receive(:[]).with('GITLAB_TRACING').and_call_original
- allow(ENV).to receive(:[]).with('SSL_CERT_DIR').and_return(cert_pool_dir)
- allow(ENV).to receive(:[]).with('SSL_CERT_FILE').and_return(nil)
- certs = client.certs
-
- # gitalycertdup.pem must exist and must be a duplicate of gitalycert.pem
- expect(File.exist?(File.join(cert_pool_dir, "gitalycertdup.pem"))).to be true
- expect(File.read(File.join(cert_pool_dir, "gitalycertdup.pem")))
- .to eq File.read(File.join(cert_pool_dir, "gitalycert.pem"))
-
- # No gitalycertdup.pem because duplicates should be removed
- expected_certs = [File.read(File.join(cert_pool_dir, "gitalycert.pem")),
- File.read(File.join(cert_pool_dir, "gitalycert2.pem")),
- File.read(File.join(cert_pool_dir, "gitalycert3.pem"))].join "\n"
-
- expect(certs).to eq expected_certs
- end
- end
-
- context 'when both SSL_CERT_DIR and SSL_CERT_FILE are set' do
- it 'Should return all certs in SSL_CERT_DIR + SSL_CERT_FILE' do
- cert_pool_dir = File.join(File.dirname(__FILE__), "testdata/certs")
- cert1_file = File.join(File.dirname(__FILE__), "testdata/gitalycert.pem")
- allow(ENV).to receive(:[]).with('GITLAB_TRACING').and_call_original
- allow(ENV).to receive(:[]).with('SSL_CERT_DIR').and_return(cert_pool_dir)
- allow(ENV).to receive(:[]).with('SSL_CERT_FILE').and_return(cert1_file)
- expected_certs_paths = [cert1_file, File.join(cert_pool_dir, "gitalycert2.pem"), File.join(cert_pool_dir, "gitalycert3.pem")]
-
- expected_certs = expected_certs_paths.map do |cert|
- File.read cert
- end.join("\n")
- certs = client.certs
- expect(certs).to eq expected_certs
- end
- end
- end
-
- describe 'Connectivity' do
- context 'tcp' do
- let(:client) do
- get_client("tcp://localhost:#{GitalyConfig.dynamic_port('tcp')}")
- end
-
- it 'Should connect over tcp' do
- expect(client).not_to be_empty
- end
- end
-
- context 'unix' do
- let(:client) { get_client("unix:#{File.join(TMP_DIR, SOCKET_PATH)}") }
-
- it 'Should connect over unix' do
- expect(client).not_to be_empty
- end
- end
-
- context 'tls' do
- let(:client) { get_client("tls://localhost:#{GitalyConfig.dynamic_port('tls')}") }
-
- it 'Should connect over tls' do
- cert = File.join(File.dirname(__FILE__), "testdata/certs/gitalycert.pem")
- allow(ENV).to receive(:[]).with('GITLAB_TRACING').and_call_original
- allow(ENV).to receive(:[]).with('SSL_CERT_DIR').and_return(nil)
- allow(ENV).to receive(:[]).with('SSL_CERT_FILE').and_return(cert)
-
- expect(client).not_to be_empty
- end
- end
- end
-end
diff --git a/ruby/spec/lib/gitlab/git/remote_repository_spec.rb b/ruby/spec/lib/gitlab/git/remote_repository_spec.rb
deleted file mode 100644
index 325ec5205..000000000
--- a/ruby/spec/lib/gitlab/git/remote_repository_spec.rb
+++ /dev/null
@@ -1,103 +0,0 @@
-require 'spec_helper'
-
-describe Gitlab::Git::RemoteRepository do
- include TestRepo
-
- let(:repository) { gitlab_git_from_gitaly(git_test_repo_read_only) }
- let(:non_existing_gitaly_repo) do
- Gitaly::Repository.new(storage_name: DEFAULT_STORAGE_NAME, relative_path: 'does-not-exist.git')
- end
-
- subject { described_class.new(repository) }
-
- describe '#empty?' do
- using RSpec::Parameterized::TableSyntax
-
- where(:repository, :result) do
- repository | false
- gitlab_git_from_gitaly(non_existing_gitaly_repo) | true
- end
-
- with_them do
- it { expect(subject.empty?).to eq(result) }
- end
- end
-
- describe '#commit_id' do
- it 'returns an OID if the revision exists' do
- expect(subject.commit_id('v1.0.0')).to eq('6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9')
- end
-
- it 'is nil when the revision does not exist' do
- expect(subject.commit_id('does-not-exist')).to be_nil
- end
- end
-
- describe '#branch_exists?' do
- using RSpec::Parameterized::TableSyntax
-
- where(:branch, :result) do
- 'master' | true
- 'does-not-exist' | false
- end
-
- with_them do
- it { expect(subject.branch_exists?(branch)).to eq(result) }
- end
- end
-
- describe '#same_repository?' do
- using RSpec::Parameterized::TableSyntax
-
- where(:other_repository, :result) do
- repository | true
- repository_from_relative_path(repository.relative_path) | true
- repository_from_relative_path('wrong/relative-path.git') | false
- end
-
- with_them do
- it { expect(subject.same_repository?(other_repository)).to eq(result) }
- end
- end
-
- describe '#fetch_env' do
- let(:remote_repository) { described_class.new(repository) }
-
- let(:gitaly_client) { double(:gitaly_client) }
- let(:address) { 'fake-address' }
- let(:shared_secret) { 'fake-secret' }
-
- subject { remote_repository.fetch_env }
-
- before do
- allow(remote_repository).to receive(:gitaly_client).and_return(gitaly_client)
-
- expect(gitaly_client).to receive(:address).with(repository.storage).and_return(address)
- expect(gitaly_client).to receive(:shared_secret).with(repository.storage).and_return(shared_secret)
- end
-
- it { expect(subject).to be_a(Hash) }
- it { expect(subject['GITALY_ADDRESS']).to eq(address) }
- it { expect(subject['GITALY_TOKEN']).to eq(shared_secret) }
- it { expect(subject['GITALY_WD']).to eq(Dir.pwd) }
-
- it 'creates a plausible GIT_SSH_COMMAND' do
- git_ssh_command = subject['GIT_SSH_COMMAND']
-
- expect(git_ssh_command).to start_with('/')
- expect(git_ssh_command).to end_with('/gitaly-ssh upload-pack')
- end
-
- it 'creates a plausible GITALY_PAYLOAD' do
- req = Gitaly::SSHUploadPackRequest.decode_json(subject['GITALY_PAYLOAD'])
-
- expect(remote_repository.gitaly_repository).to eq(req.repository)
- end
-
- context 'when the token is blank' do
- let(:shared_secret) { '' }
-
- it { expect(subject.keys).not_to include('GITALY_TOKEN') }
- end
- end
-end
diff --git a/ruby/spec/lib/gitlab/git/repository_spec.rb b/ruby/spec/lib/gitlab/git/repository_spec.rb
index b6e140cb1..573058b25 100644
--- a/ruby/spec/lib/gitlab/git/repository_spec.rb
+++ b/ruby/spec/lib/gitlab/git/repository_spec.rb
@@ -149,118 +149,31 @@ describe Gitlab::Git::Repository do # rubocop:disable Metrics/BlockLength
end
describe '#with_repo_branch_commit' do
- let(:start_repository) { Gitlab::Git::RemoteRepository.new(source_repository) }
- let(:start_commit) { source_repository.commit }
-
- context 'when start_repository is empty' do
- let(:source_repository) { gitlab_git_from_gitaly(new_empty_test_repo) }
-
- before do
- expect(start_repository).not_to receive(:commit_id)
- expect(repository).not_to receive(:fetch_sha)
- end
+ context 'when repository is empty' do
+ let(:repository) { gitlab_git_from_gitaly(new_empty_test_repo) }
it 'yields nil' do
expect do |block|
- repository.with_repo_branch_commit(start_repository, 'master', &block)
+ repository.with_repo_branch_commit('master', &block)
end.to yield_with_args(nil)
end
end
- context 'when start_repository is the same repository' do
- let(:source_repository) { repository }
-
- before do
- expect(start_repository).not_to receive(:commit_id)
- expect(repository).not_to receive(:fetch_sha)
- end
+ context 'when repository is not empty' do
+ let(:start_commit) { repository.commit }
it 'yields the commit for the SHA' do
expect do |block|
- repository.with_repo_branch_commit(start_repository, start_commit.sha, &block)
+ repository.with_repo_branch_commit(start_commit.sha, &block)
end.to yield_with_args(start_commit)
end
it 'yields the commit for the branch' do
expect do |block|
- repository.with_repo_branch_commit(start_repository, 'master', &block)
+ repository.with_repo_branch_commit('master', &block)
end.to yield_with_args(start_commit)
end
end
-
- context 'when start_repository is different' do
- let(:source_repository) { gitlab_git_from_gitaly(test_repo_read_only) }
-
- context 'when start commit already exists' do
- let(:start_commit) { repository.commit }
-
- before do
- expect(start_repository).to receive(:commit_id).and_return(start_commit.sha)
- expect(repository).not_to receive(:fetch_sha)
- end
-
- it 'yields the commit for the SHA' do
- expect do |block|
- repository.with_repo_branch_commit(start_repository, start_commit.sha, &block)
- end.to yield_with_args(start_commit)
- end
-
- it 'yields the commit for the branch' do
- expect do |block|
- repository.with_repo_branch_commit(start_repository, 'master', &block)
- end.to yield_with_args(start_commit)
- end
- end
-
- context 'when start commit does not exist' do
- before do
- expect(start_repository).to receive(:commit_id).and_return(start_commit.sha)
- expect(repository).to receive(:fetch_sha).with(start_repository, start_commit.sha)
- end
-
- it 'yields the fetched commit for the SHA' do
- expect do |block|
- repository.with_repo_branch_commit(start_repository, start_commit.sha, &block)
- end.to yield_with_args(nil) # since fetch_sha is mocked
- end
-
- it 'yields the fetched commit for the branch' do
- expect do |block|
- repository.with_repo_branch_commit(start_repository, 'master', &block)
- end.to yield_with_args(nil) # since fetch_sha is mocked
- end
- end
- end
- end
-
- describe '#fetch_sha' do
- let(:source_repository) { Gitlab::Git::RemoteRepository.new(repository) }
- let(:sha) { 'b971194ee2d047f24cb897b6fb0d7ae99c8dd0ca' }
- let(:git_args) { %W[fetch --no-tags ssh://gitaly/internal.git #{sha}] }
-
- before do
- expect(source_repository).to receive(:fetch_env)
- .with(git_config_options: ['uploadpack.allowAnySHA1InWant=true'])
- .and_return({})
- end
-
- it 'fetches the commit from the source repository' do
- expect(repository).to receive(:run_git)
- .with(git_args, env: {}, include_stderr: true)
- .and_return(['success', 0])
-
- expect(repository.fetch_sha(source_repository, sha)).to eq(sha)
- end
-
- it 'raises an error if the commit does not exist in the source repository' do
- expect(repository).to receive(:run_git)
- .with(git_args, env: {}, include_stderr: true)
- .and_return(['error', 1])
-
- expect do
- repository.fetch_sha(source_repository, sha)
- end.to raise_error(Gitlab::Git::CommandError, 'error')
- end
end
describe '#cleanup' do
diff --git a/ruby/spec/support/helpers/integration_helper.rb b/ruby/spec/support/helpers/integration_helper.rb
index 5d3a1ef7c..c4462e750 100644
--- a/ruby/spec/support/helpers/integration_helper.rb
+++ b/ruby/spec/support/helpers/integration_helper.rb
@@ -48,18 +48,6 @@ module IntegrationClient
def gitaly_repo(storage, relative_path)
Gitaly::Repository.new(storage_name: storage, relative_path: relative_path)
end
-
- def get_client(addr)
- servers = Base64.strict_encode64({
- default: {
- address: addr,
- token: 'the-secret-token'
- }
- }.to_json)
-
- call = double(metadata: { 'gitaly-servers' => servers })
- Gitlab::Git::GitalyRemoteRepository.new(repository.gitaly_repository, call)
- end
end
def start_gitaly