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:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-11-17 13:43:10 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-11-17 13:43:10 +0300
commit3772b182b5b714770956de4d76f3ce09a220fb85 (patch)
tree00de2f1f33cd3d80973e5100b9d887193ef39ff9
parent84d250f45e8779a9e800763acb93c36983a58ec9 (diff)
parent26e212d676dceed1ee6add67eb75d85467ff858b (diff)
Merge branch 'pks-ruby-drop-dead-code' into 'master'
ruby: Clean up dead code and use temporary directory See merge request gitlab-org/gitaly!4079
-rw-r--r--.gitignore1
-rw-r--r--ruby/lib/gitlab/git/blob.rb131
-rw-r--r--ruby/lib/gitlab/git/gitlab_projects.rb105
-rw-r--r--ruby/lib/gitlab/git/repository.rb192
-rw-r--r--ruby/lib/gitlab/git/worktree.rb29
-rw-r--r--ruby/spec/lib/gitlab/git/blob_spec.rb216
-rw-r--r--ruby/spec/lib/gitlab/git/commit_spec.rb8
-rw-r--r--ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb97
-rw-r--r--ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb4
-rw-r--r--ruby/spec/lib/gitlab/git/repository_spec.rb88
-rw-r--r--ruby/spec/lib/gitlab/git/worktree_spec.rb21
-rw-r--r--ruby/spec/spec_helper.rb7
-rw-r--r--ruby/spec/support/helpers/integration_helper.rb4
-rw-r--r--ruby/spec/test_repo_helper.rb16
14 files changed, 17 insertions, 902 deletions
diff --git a/.gitignore b/.gitignore
index 8e0817d8f..3659a9723 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,6 @@
/.ruby-bundle
/ruby/vendor/bundle
/ruby/.bundle
-/ruby/tmp
/gitaly-*.gem
# Configuration and runtime data
diff --git a/ruby/lib/gitlab/git/blob.rb b/ruby/lib/gitlab/git/blob.rb
deleted file mode 100644
index 1fd69cf6c..000000000
--- a/ruby/lib/gitlab/git/blob.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-module Gitlab
- module Git
- class Blob
- include Linguist::BlobHelper
- include Gitlab::EncodingHelper
-
- # This number is the maximum amount of data that we want to display to
- # the user. We load as much as we can for encoding detection
- # (Linguist) and LFS pointer parsing.
- MAX_DATA_DISPLAY_SIZE = 10.megabytes
-
- attr_accessor :size, :mode, :id, :commit_id, :loaded_size, :binary
- attr_writer :data, :name, :path
-
- class << self
- def find(repository, sha, path, limit: MAX_DATA_DISPLAY_SIZE)
- return unless path
-
- # Strip any leading / characters from the path
- path = path.sub(%r{\A/*}, '')
-
- rugged_commit = repository.lookup(sha)
- root_tree = rugged_commit.tree
-
- blob_entry = find_entry_by_path(repository, root_tree.oid, *path.split('/'))
-
- return nil unless blob_entry
-
- if blob_entry[:type] == :commit
- submodule_blob(blob_entry, path, sha)
- else
- blob = repository.lookup(blob_entry[:oid])
-
- if blob
- new(
- id: blob.oid,
- name: blob_entry[:name],
- size: blob.size,
- # Rugged::Blob#content is expensive; don't call it if we don't have to.
- data: limit.zero? ? '' : blob.content(limit),
- mode: blob_entry[:filemode].to_s(8),
- path: path,
- commit_id: sha,
- binary: blob.binary?
- )
- end
- end
- rescue Rugged::ReferenceError
- nil
- end
-
- def binary?(data)
- EncodingHelper.detect_libgit2_binary?(data)
- end
-
- private
-
- # Recursive search of blob id by path
- #
- # Ex.
- # blog/ # oid: 1a
- # app/ # oid: 2a
- # models/ # oid: 3a
- # file.rb # oid: 4a
- #
- #
- # Blob.find_entry_by_path(repo, '1a', 'blog', 'app', 'file.rb') # => '4a'
- #
- def find_entry_by_path(repository, root_id, *path_parts)
- root_tree = repository.lookup(root_id)
-
- entry = root_tree.find do |entry|
- entry[:name] == path_parts[0]
- end
-
- return nil unless entry
-
- if path_parts.size > 1
- return nil unless entry[:type] == :tree
-
- path_parts.shift
- find_entry_by_path(repository, entry[:oid], *path_parts)
- else
- [:blob, :commit].include?(entry[:type]) ? entry : nil
- end
- end
-
- def submodule_blob(blob_entry, path, sha)
- new(
- id: blob_entry[:oid],
- name: blob_entry[:name],
- size: 0,
- data: '',
- path: path,
- commit_id: sha
- )
- end
- end
-
- def initialize(options)
- %w(id name path size data mode commit_id binary).each do |key|
- self.__send__("#{key}=", options[key.to_sym])
- end
-
- # Retain the actual size before it is encoded
- @loaded_size = @data.bytesize if @data
- @loaded_all_data = @loaded_size == size
- end
-
- def binary?
- @binary.nil? ? super : @binary == true
- end
-
- def data
- encode! @data
- end
-
- def name
- encode! @name
- end
-
- def path
- encode! @path
- end
-
- def truncated?
- size && (size > loaded_size)
- end
- end
- end
-end
diff --git a/ruby/lib/gitlab/git/gitlab_projects.rb b/ruby/lib/gitlab/git/gitlab_projects.rb
deleted file mode 100644
index d90bf7ae8..000000000
--- a/ruby/lib/gitlab/git/gitlab_projects.rb
+++ /dev/null
@@ -1,105 +0,0 @@
-require 'tempfile'
-
-module Gitlab
- module Git
- class GitlabProjects
- include Gitlab::Git::Popen
- include Gitlab::Utils::StrongMemoize
-
- BRANCHES_PER_PUSH = 10
-
- # Relative path is a directory name for repository with .git at the end.
- # Example: gitlab-org/gitlab-test.git
- attr_reader :repository_relative_path
-
- # This is the path at which the gitlab-shell hooks directory can be found.
- # It's essential for integration between git and GitLab proper. All new
- # repositories should have their hooks directory symlinked here.
- attr_reader :global_hooks_path
-
- attr_reader :logger
-
- def self.from_gitaly(gitaly_repository, call)
- storage_path = GitalyServer.storage_path(call)
-
- Gitlab::Git::GitlabProjects.new(
- storage_path,
- gitaly_repository.relative_path,
- global_hooks_path: Gitlab::Git::Hook.directory,
- logger: Rails.logger
- )
- end
-
- def initialize(shard_path, repository_relative_path, global_hooks_path:, logger:)
- @shard_path = shard_path
- @repository_relative_path = repository_relative_path
-
- @logger = logger
- @global_hooks_path = global_hooks_path
- @output = StringIO.new
- end
-
- def shard_path
- @shard_path
- end
-
- def output
- io = @output.dup
- io.rewind
- io.read
- end
-
- # Absolute path to the repository.
- # Example: /home/git/repositorities/gitlab-org/gitlab-test.git
- # Probably will be removed when we fully migrate to Gitaly, part of
- # https://gitlab.com/gitlab-org/gitaly/issues/1124.
- def repository_absolute_path
- strong_memoize(:repository_absolute_path) do
- File.join(shard_path, repository_relative_path)
- end
- end
-
- def push_branches(remote_name, timeout, force, branch_names, env: {})
- branch_names.each_slice(BRANCHES_PER_PUSH) do |branches|
- logger.info "Pushing #{branches.count} branches from #{repository_absolute_path} to remote #{remote_name}"
-
- cmd = %W(#{Gitlab.config.git.bin_path} push)
- cmd << '--force' if force
- cmd += %W(-- #{remote_name}).concat(branches)
-
- unless run_with_timeout(cmd, timeout, repository_absolute_path, env)
- logger.error("Pushing branches to remote #{remote_name} failed.")
- return false
- end
- end
-
- true
- end
-
- protected
-
- def run(*args)
- output, exitstatus = popen(*args)
- @output << output
-
- exitstatus&.zero?
- end
-
- def run_with_timeout(*args)
- output, exitstatus = popen_with_timeout(*args)
- @output << output
-
- exitstatus&.zero?
- rescue Timeout::Error
- @output.puts('Timed out')
-
- false
- end
-
- def remove_origin_in_repo
- cmd = %W(#{Gitlab.config.git.bin_path} remote rm origin)
- run(cmd, repository_absolute_path)
- end
- end
- end
-end
diff --git a/ruby/lib/gitlab/git/repository.rb b/ruby/lib/gitlab/git/repository.rb
index 02b9e05e9..92107b9c6 100644
--- a/ruby/lib/gitlab/git/repository.rb
+++ b/ruby/lib/gitlab/git/repository.rb
@@ -8,7 +8,6 @@ module Gitlab
include Gitlab::EncodingHelper
include Gitlab::Utils::StrongMemoize
- AM_WORKTREE_PREFIX = 'am'.freeze
GITALY_INTERNAL_URL = 'ssh://gitaly/internal.git'.freeze
AUTOCRLF_VALUES = { 'true' => true, 'false' => false, 'input' => :input }.freeze
RUGGED_KEY = :rugged_list
@@ -18,21 +17,12 @@ module Gitlab
InvalidRef = Class.new(StandardError)
GitError = Class.new(StandardError)
- class CreateTreeError < StandardError
- attr_reader :error
-
- def initialize(error)
- @error = error
- end
- end
-
class << self
def from_gitaly(gitaly_repository, call)
new(
gitaly_repository,
GitalyServer.repo_path(call),
GitalyServer.gl_repository(call),
- Gitlab::Git::GitlabProjects.from_gitaly(gitaly_repository, call),
GitalyServer.repo_alt_dirs(call),
GitalyServer.feature_flags(call)
)
@@ -54,9 +44,9 @@ module Gitlab
# Directory name of repo
attr_reader :name
- attr_reader :gitlab_projects, :storage, :gl_repository, :gl_project_path, :relative_path
+ attr_reader :storage, :gl_repository, :gl_project_path, :relative_path
- def initialize(gitaly_repository, path, gl_repository, gitlab_projects, combined_alt_dirs = "", feature_flags = GitalyServer::FeatureFlags.new({}))
+ def initialize(gitaly_repository, path, gl_repository, combined_alt_dirs = "", feature_flags = GitalyServer::FeatureFlags.new({}))
@gitaly_repository = gitaly_repository
@alternate_object_directories = combined_alt_dirs
@@ -68,7 +58,6 @@ module Gitlab
@path = path
@gl_repository = gl_repository
@gl_project_path = gitaly_repository.gl_project_path
- @gitlab_projects = gitlab_projects
@feature_flags = feature_flags
end
@@ -188,52 +177,6 @@ module Gitlab
end
end
- def diff_exists?(sha1, sha2)
- rugged.diff(sha1, sha2).size.positive?
- end
-
- def commit_patches(start_point, patches, extra_env: {})
- worktree = Gitlab::Git::Worktree.new(path, AM_WORKTREE_PREFIX, SecureRandom.hex)
-
- with_worktree(worktree, start_point, env: extra_env) do
- result, status = run_git(%w[am --quiet --3way], chdir: worktree.path, env: extra_env) do |stdin|
- loop { stdin.write(patches.next) }
- end
-
- raise Gitlab::Git::PatchError, result unless status == 0
-
- run_git!(
- %w[rev-parse --quiet --verify HEAD], chdir: worktree.path, env: extra_env
- ).chomp
- end
- end
-
- def update_submodule(submodule_path, commit_sha, branch, committer, message)
- target = rugged.rev_parse("refs/heads/" + branch)
- raise CommitError, 'Invalid branch' unless target.is_a?(Rugged::Commit)
-
- current_entry = rugged_submodule_entry(target, submodule_path)
- raise CommitError, 'Invalid submodule path' unless current_entry
- raise CommitError, "The submodule #{submodule_path} is already at #{commit_sha}" if commit_sha == current_entry[:oid]
-
- commit_tree = target.tree.update([action: :upsert,
- oid: commit_sha,
- filemode: 0o160000,
- path: submodule_path])
-
- options = {
- parents: [target.oid],
- tree: commit_tree,
- message: message,
- author: committer,
- committer: committer
- }
-
- create_commit(options).tap do |result|
- raise CommitError, 'Failed to create commit' unless result
- end
- end
-
def with_repo_branch_commit(start_repository, start_ref)
start_repository = RemoteRepository.new(start_repository) unless start_repository.is_a?(RemoteRepository)
@@ -274,10 +217,6 @@ module Gitlab
end
end
- def delete_refs(*ref_names)
- git_delete_refs(*ref_names)
- end
-
# Returns true if the given branch exists
#
# name - The name of the branch as a String.
@@ -298,10 +237,6 @@ module Gitlab
nil
end
- def user_to_committer(user, timestamp = nil)
- Gitlab::Git.committer_hash(email: user.email, name: user.name, timestamp: timestamp)
- 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)
@@ -320,20 +255,6 @@ module Gitlab
rugged.rev_parse(oid_or_ref_name)
end
- def commit_index(user, branch_name, index, options, timestamp = nil)
- committer = user_to_committer(user, timestamp)
-
- OperationService.new(user, self).with_branch(branch_name) do
- commit_params = options.merge(
- tree: index.write_tree(rugged),
- author: committer,
- committer: committer
- )
-
- create_commit(commit_params)
- end
- end
-
# Return the object that +revspec+ points to. If +revspec+ is an
# annotated tag, then return the tag's target instead.
def rev_parse_target(revspec)
@@ -358,10 +279,6 @@ module Gitlab
rugged.config['core.autocrlf'] = AUTOCRLF_VALUES.invert[value]
end
- def blob_at(sha, path)
- Gitlab::Git::Blob.find(self, sha, path) unless Gitlab::Git.blank_ref?(sha)
- end
-
def cleanup
# Opening a repository may be expensive, and we only need to close it
# if it's been open.
@@ -378,14 +295,6 @@ module Gitlab
private
- def sparse_checkout_empty?(output)
- output.include?("error: Sparse checkout leaves no entry on working directory")
- end
-
- def disable_sparse_checkout
- run_git!(%w[config core.sparseCheckout false], include_stderr: true)
- end
-
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
@@ -396,14 +305,6 @@ module Gitlab
popen(cmd, chdir, env, include_stderr: include_stderr, lazy_block: lazy_block, &block)
end
- def run_git!(args, chdir: path, env: {}, nice: false, include_stderr: false, lazy_block: nil, &block)
- output, status = run_git(args, chdir: chdir, env: env, nice: nice, include_stderr: include_stderr, lazy_block: lazy_block, &block)
-
- raise GitError, output unless status.zero?
-
- output
- end
-
def branches_filter(filter: nil, sort_by: nil)
branches = rugged.branches.each(filter).map do |rugged_ref|
begin
@@ -417,100 +318,11 @@ module Gitlab
sort_branches(branches, sort_by)
end
- def git_delete_refs(*ref_names)
- instructions = ref_names.map do |ref|
- "delete #{ref}\x00\x00"
- end
-
- message, status = run_git(%w[update-ref --stdin -z], include_stderr: true) do |stdin|
- stdin.write(instructions.join)
- end
-
- raise GitError, "Could not delete refs #{ref_names}: #{message}" unless status.zero?
- end
-
- def create_commit(params = {})
- params[:message].delete!("\r")
-
- Rugged::Commit.create(rugged, params)
- end
-
def rugged_head
rugged.head
rescue Rugged::ReferenceError
nil
end
-
- def with_worktree(worktree, branch, sparse_checkout_files: nil, env:)
- base_args = %w[worktree add --detach]
-
- run_git!(%w[config core.splitIndex false])
-
- # Note that we _don't_ want to test for `.present?` here: If the caller
- # passes an non nil empty value it means it still wants sparse checkout
- # but just isn't interested in any file, perhaps because it wants to
- # checkout files in by a changeset but that changeset only adds files.
- if sparse_checkout_files
- # Create worktree without checking out
- run_git!(base_args + ['--no-checkout', worktree.path], env: env, include_stderr: true)
- worktree_git_path = run_git!(%w[rev-parse --git-dir], chdir: worktree.path).chomp
-
- configure_sparse_checkout(worktree_git_path, sparse_checkout_files)
-
- # After sparse checkout configuration, checkout `branch` in worktree
- output, cmd_status = run_git(%W[checkout --detach #{branch}], chdir: worktree.path, env: env, include_stderr: true)
-
- # If sparse checkout fails, fall back to a regular checkout.
- if cmd_status.nonzero?
- if sparse_checkout_empty?(output)
- disable_sparse_checkout
- run_git!(%W[checkout --detach #{branch}], chdir: worktree.path, env: env, include_stderr: true)
- else
- raise GitError, output
- end
- end
- else
- # Create worktree and checkout `branch` in it
- run_git!(base_args + [worktree.path, branch], env: env, include_stderr: true)
- end
-
- yield
- ensure
- run_git(%W[worktree remove -f #{worktree.name}], include_stderr: true)
- end
-
- # Adding a worktree means checking out the repository. For large repos,
- # this can be very expensive, so set up sparse checkout for the worktree
- # to only check out the files we're interested in.
- def configure_sparse_checkout(worktree_git_path, files)
- run_git!(%w[config core.sparseCheckout true], include_stderr: true)
-
- return if files.empty?
-
- worktree_info_path = File.join(worktree_git_path, 'info')
- FileUtils.mkdir_p(worktree_info_path)
- File.write(File.join(worktree_info_path, 'sparse-checkout'), files)
- end
-
- def gitlab_projects_error
- raise CommandError, @gitlab_projects.output
- end
-
- def rugged_submodule_entry(target, submodule_path)
- parent_dir = File.dirname(submodule_path)
- parent_dir = '' if parent_dir == '.'
- parent_tree = rugged.rev_parse("#{target.oid}^{tree}:#{parent_dir}")
-
- return unless parent_tree.is_a?(Rugged::Tree)
-
- current_entry = parent_tree[File.basename(submodule_path)]
-
- valid_submodule_entry?(current_entry) ? current_entry : nil
- end
-
- def valid_submodule_entry?(entry)
- entry && entry[:type] == :commit
- end
end
end
end
diff --git a/ruby/lib/gitlab/git/worktree.rb b/ruby/lib/gitlab/git/worktree.rb
deleted file mode 100644
index 5e7cf9e04..000000000
--- a/ruby/lib/gitlab/git/worktree.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-# frozen_string_literal: true
-
-require 'securerandom'
-
-module Gitlab
- module Git
- class Worktree
- attr_reader :path, :name
-
- def initialize(repo_path, prefix, id)
- @repo_path = repo_path
- @prefix = prefix
- @suffix = SecureRandom.hex
- @id = id.to_s
- @name = "#{prefix}-#{id}-#{@suffix}"
- @path = worktree_path
- end
-
- private
-
- def worktree_path
- raise ArgumentError, "worktree id can't be empty" unless @id.present?
- raise ArgumentError, "worktree id can't contain slashes " if @id.include?("/")
-
- File.join(@repo_path, 'gitlab-worktree', @name)
- end
- end
- end
-end
diff --git a/ruby/spec/lib/gitlab/git/blob_spec.rb b/ruby/spec/lib/gitlab/git/blob_spec.rb
deleted file mode 100644
index aa0de8892..000000000
--- a/ruby/spec/lib/gitlab/git/blob_spec.rb
+++ /dev/null
@@ -1,216 +0,0 @@
-require "spec_helper"
-
-describe Gitlab::Git::Blob do
- include TestRepo
-
- let(:repository) { gitlab_git_from_gitaly(git_test_repo_read_only) }
- let(:rugged) do
- Rugged::Repository.new(GIT_TEST_REPO_PATH)
- end
-
- describe 'initialize' do
- let(:blob) { Gitlab::Git::Blob.new(name: 'test') }
-
- it 'handles nil data' do
- expect(blob.name).to eq('test')
- expect(blob.size).to eq(nil)
- expect(blob.loaded_size).to eq(nil)
- end
- end
-
- describe '.find' do
- context 'nil path' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, nil) }
-
- it { expect(blob).to eq(nil) }
- end
-
- context 'utf-8 branch' do
- let(:blob) { Gitlab::Git::Blob.find(repository, 'Ääh-test-utf-8', "files/ruby/popen.rb") }
-
- it { expect(blob.id).to eq(SeedRepo::RubyBlob::ID) }
- end
-
- context 'blank path' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, '') }
-
- it { expect(blob).to eq(nil) }
- end
-
- context 'file in subdir' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "files/ruby/popen.rb") }
-
- it { expect(blob.id).to eq(SeedRepo::RubyBlob::ID) }
- it { expect(blob.name).to eq(SeedRepo::RubyBlob::NAME) }
- it { expect(blob.path).to eq("files/ruby/popen.rb") }
- it { expect(blob.commit_id).to eq(SeedRepo::Commit::ID) }
- it { expect(blob.data[0..10]).to eq(SeedRepo::RubyBlob::CONTENT[0..10]) }
- it { expect(blob.size).to eq(669) }
- it { expect(blob.mode).to eq("100644") }
- end
-
- context 'file in root' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, ".gitignore") }
-
- it { expect(blob.id).to eq("dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82") }
- it { expect(blob.name).to eq(".gitignore") }
- it { expect(blob.path).to eq(".gitignore") }
- it { expect(blob.commit_id).to eq(SeedRepo::Commit::ID) }
- it { expect(blob.data[0..10]).to eq("*.rbc\n*.sas") }
- it { expect(blob.size).to eq(241) }
- it { expect(blob.mode).to eq("100644") }
- it { expect(blob).not_to be_binary }
- end
-
- context 'file in root with leading slash' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "/.gitignore") }
-
- it { expect(blob.id).to eq("dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82") }
- it { expect(blob.name).to eq(".gitignore") }
- it { expect(blob.path).to eq(".gitignore") }
- it { expect(blob.commit_id).to eq(SeedRepo::Commit::ID) }
- it { expect(blob.data[0..10]).to eq("*.rbc\n*.sas") }
- it { expect(blob.size).to eq(241) }
- it { expect(blob.mode).to eq("100644") }
- end
-
- context 'non-exist file' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "missing.rb") }
-
- it { expect(blob).to be_nil }
- end
-
- context 'six submodule' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, 'six') }
-
- it { expect(blob.id).to eq('409f37c4f05865e4fb208c771485f211a22c4c2d') }
- it { expect(blob.data).to eq('') }
-
- it 'does not mark the blob as binary' do
- expect(blob).not_to be_binary
- end
- end
-
- context 'large file' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, 'files/images/6049019_460s.jpg') }
- let(:blob_size) { 111_803 }
- let(:stub_limit) { 1000 }
-
- before do
- stub_const('Gitlab::Git::Blob::MAX_DATA_DISPLAY_SIZE', stub_limit)
- end
-
- it { expect(blob.size).to eq(blob_size) }
- it { expect(blob.data.length).to eq(stub_limit) }
-
- it 'check that this test is sane' do
- # It only makes sense to test limiting if the blob is larger than the limit.
- expect(blob.size).to be > Gitlab::Git::Blob::MAX_DATA_DISPLAY_SIZE
- end
-
- it 'marks the blob as binary' do
- expect(Gitlab::Git::Blob).to receive(:new)
- .with(hash_including(binary: true))
- .and_call_original
-
- expect(blob).to be_binary
- end
- end
- end
-
- describe 'encoding' do
- context 'file with russian text' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "encoding/russian.rb") }
-
- it { expect(blob.name).to eq("russian.rb") }
- it { expect(blob.data.lines.first).to eq("Хороший файл") }
- it { expect(blob.size).to eq(23) }
- it { expect(blob.truncated?).to be_falsey }
- # Run it twice since data is encoded after the first run
- it { expect(blob.truncated?).to be_falsey }
- it { expect(blob.mode).to eq("100755") }
- end
-
- context 'file with Chinese text' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "encoding/テスト.txt") }
-
- it { expect(blob.name).to eq("テスト.txt") }
- it { expect(blob.data).to include("これはテスト") }
- it { expect(blob.size).to eq(340) }
- it { expect(blob.mode).to eq("100755") }
- it { expect(blob.truncated?).to be_falsey }
- end
-
- context 'file with ISO-8859 text' do
- let(:blob) { Gitlab::Git::Blob.find(repository, SeedRepo::LastCommit::ID, "encoding/iso8859.txt") }
-
- it { expect(blob.name).to eq("iso8859.txt") }
- it { expect(blob.loaded_size).to eq(4) }
- it { expect(blob.size).to eq(4) }
- it { expect(blob.mode).to eq("100644") }
- it { expect(blob.truncated?).to be_falsey }
- end
- end
-
- describe 'mode' do
- context 'file regular' do
- let(:blob) do
- Gitlab::Git::Blob.find(
- repository,
- 'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
- 'files/ruby/regex.rb'
- )
- end
-
- it { expect(blob.name).to eq('regex.rb') }
- it { expect(blob.path).to eq('files/ruby/regex.rb') }
- it { expect(blob.size).to eq(1200) }
- it { expect(blob.mode).to eq("100644") }
- end
-
- context 'file binary' do
- let(:blob) do
- Gitlab::Git::Blob.find(
- repository,
- 'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
- 'files/executables/ls'
- )
- end
-
- it { expect(blob.name).to eq('ls') }
- it { expect(blob.path).to eq('files/executables/ls') }
- it { expect(blob.size).to eq(110_080) }
- it { expect(blob.mode).to eq("100755") }
- end
-
- context 'file symlink to regular' do
- let(:blob) do
- Gitlab::Git::Blob.find(
- repository,
- 'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
- 'files/links/ruby-style-guide.md'
- )
- end
-
- it { expect(blob.name).to eq('ruby-style-guide.md') }
- it { expect(blob.path).to eq('files/links/ruby-style-guide.md') }
- it { expect(blob.size).to eq(31) }
- it { expect(blob.mode).to eq("120000") }
- end
-
- context 'file symlink to binary' do
- let(:blob) do
- Gitlab::Git::Blob.find(
- repository,
- 'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
- 'files/links/touch'
- )
- end
-
- it { expect(blob.name).to eq('touch') }
- it { expect(blob.path).to eq('files/links/touch') }
- it { expect(blob.size).to eq(20) }
- it { expect(blob.mode).to eq("120000") }
- end
- end
-end
diff --git a/ruby/spec/lib/gitlab/git/commit_spec.rb b/ruby/spec/lib/gitlab/git/commit_spec.rb
index 3e9272478..bd2b5aeeb 100644
--- a/ruby/spec/lib/gitlab/git/commit_spec.rb
+++ b/ruby/spec/lib/gitlab/git/commit_spec.rb
@@ -109,13 +109,13 @@ describe Gitlab::Git::Commit do
end
it "should return nil for non-commit ids" do
- blob = Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "files/ruby/popen.rb")
- expect(described_class.find(repository, blob.id)).to be_nil
+ blob_id = repository.lookup("#{SeedRepo::Commit::ID}:files/ruby/popen.rb")
+ expect(described_class.find(repository, blob_id)).to be_nil
end
it "should return nil for parent of non-commit object" do
- blob = Gitlab::Git::Blob.find(repository, SeedRepo::Commit::ID, "files/ruby/popen.rb")
- expect(described_class.find(repository, "#{blob.id}^")).to be_nil
+ blob_id = repository.lookup("#{SeedRepo::Commit::ID}:files/ruby/popen.rb")
+ expect(described_class.find(repository, "#{blob_id}^")).to be_nil
end
it "should return nil for nonexisting ids" do
diff --git a/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb b/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb
deleted file mode 100644
index 9f7cf6dc7..000000000
--- a/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-require 'spec_helper'
-
-describe Gitlab::Git::GitlabProjects do
- include TestRepo
-
- let(:repository) { gitlab_git_from_gitaly(test_repo_read_only) }
- let(:repo_name) { 'gitlab-test.git' }
- let(:gl_projects) { build_gitlab_projects(DEFAULT_STORAGE_DIR, repo_name) }
- let(:hooks_path) { File.join(tmp_repo_path, 'hooks') }
- let(:tmp_repo_path) { TEST_REPO_PATH }
- let(:tmp_repos_path) { DEFAULT_STORAGE_DIR }
-
- if $VERBOSE
- let(:logger) { Logger.new(STDOUT) }
- else
- let(:logger) { double('logger').as_null_object }
- end
-
- def build_gitlab_projects(*args)
- described_class.new(
- *args,
- global_hooks_path: hooks_path,
- logger: logger
- )
- end
-
- def stub_spawn(*args, success: true)
- exitstatus = success ? 0 : nil
-
- expect(gl_projects)
- .to receive(:popen_with_timeout)
- .with(*args)
- .and_return(["output", exitstatus])
- end
-
- def stub_unlimited_spawn(*args, success: true)
- exitstatus = success ? 0 : nil
-
- expect(gl_projects)
- .to receive(:popen)
- .with(*args)
- .and_return(["output", exitstatus])
- end
-
- def stub_spawn_timeout(*args)
- expect(gl_projects).to receive(:popen_with_timeout).with(*args)
- .and_raise(Timeout::Error)
- end
-
- describe '#initialize' do
- it { expect(gl_projects.shard_path).to eq(tmp_repos_path) }
- it { expect(gl_projects.repository_relative_path).to eq(repo_name) }
- it { expect(gl_projects.repository_absolute_path).to eq(File.join(tmp_repos_path, repo_name)) }
- it { expect(gl_projects.logger).to eq(logger) }
- end
-
- describe '#push_branches' do
- let(:remote_name) { 'remote-name' }
- let(:env) { { 'GIT_SSH_COMMAND' => 'foo-command bar' } }
- let(:force) { false }
- let(:branch_names) { 20.times.map { |i| "branch#{i}" } }
- let(:cmd1) do
- %W(#{Gitlab.config.git.bin_path} push -- #{remote_name}) + branch_names[0, 10]
- end
- let(:cmd2) do
- %W(#{Gitlab.config.git.bin_path} push -- #{remote_name}) + branch_names[10, 10]
- end
-
- subject { gl_projects.push_branches(remote_name, 600, force, branch_names, env: env) }
-
- it 'executes the command' do
- stub_spawn(cmd1, 600, tmp_repo_path, env, success: true)
- stub_spawn(cmd2, 600, tmp_repo_path, env, success: true)
-
- is_expected.to be_truthy
- end
-
- it 'fails' do
- stub_spawn(cmd1, 600, tmp_repo_path, env, success: true)
- stub_spawn(cmd2, 600, tmp_repo_path, env, success: false)
-
- is_expected.to be_falsy
- end
-
- context 'with --force' do
- let(:branch_names) { ['master'] }
- let(:cmd) { %W(#{Gitlab.config.git.bin_path} push --force -- #{remote_name} #{branch_names[0]}) }
- let(:force) { true }
-
- it 'executes the command' do
- stub_spawn(cmd, 600, tmp_repo_path, env, success: true)
-
- is_expected.to be_truthy
- end
- end
- end
-end
diff --git a/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb b/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb
index 8375a0187..15943495e 100644
--- a/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb
+++ b/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb
@@ -40,7 +40,7 @@ describe Gitlab::Git::GitalyRemoteRepository do
end
end
- let(:repository) { gitlab_git_from_gitaly_with_gitlab_projects(new_mutable_test_repo) }
+ let(:repository) { gitlab_git_from_gitaly(new_mutable_test_repo) }
describe 'certs' do
let(:client) { get_client("tls://localhost:#{GitalyConfig.dynamic_port('tls')}") }
@@ -113,7 +113,7 @@ describe Gitlab::Git::GitalyRemoteRepository do
end
context 'unix' do
- let(:client) { get_client("unix:#{File.join(TMP_DIR_NAME, SOCKET_PATH)}") }
+ let(:client) { get_client("unix:#{File.join(TMP_DIR, SOCKET_PATH)}") }
it 'Should connect over unix' do
expect(client).not_to be_empty
diff --git a/ruby/spec/lib/gitlab/git/repository_spec.rb b/ruby/spec/lib/gitlab/git/repository_spec.rb
index 03e9147d4..b6e140cb1 100644
--- a/ruby/spec/lib/gitlab/git/repository_spec.rb
+++ b/ruby/spec/lib/gitlab/git/repository_spec.rb
@@ -88,33 +88,6 @@ describe Gitlab::Git::Repository do # rubocop:disable Metrics/BlockLength
it { expect(repository).not_to be_empty }
end
- describe '#delete_refs' do
- let(:repository) { mutable_repository }
-
- it 'deletes the ref' do
- repository.delete_refs('refs/heads/feature')
-
- expect(repository_rugged.references['refs/heads/feature']).to be_nil
- end
-
- it 'deletes all refs' do
- refs = %w[refs/heads/wip refs/tags/v1.1.0]
- repository.delete_refs(*refs)
-
- refs.each do |ref|
- expect(repository_rugged.references[ref]).to be_nil
- end
- end
-
- it 'does not fail when deleting an empty list of refs' do
- expect { repository.delete_refs }.not_to raise_error
- end
-
- it 'raises an error if it failed' do
- expect { repository.delete_refs('refs\heads\fix') }.to raise_error(Gitlab::Git::Repository::GitError)
- end
- end
-
describe '#merge_base' do
where(:from, :to, :result) do
'570e7b2abdd848b95f2f578043fc23bd6f6fd24d' | '40f4a7a617393735a95a0bb67b08385bc1e7c66d' | '570e7b2abdd848b95f2f578043fc23bd6f6fd24d'
@@ -334,67 +307,6 @@ describe Gitlab::Git::Repository do # rubocop:disable Metrics/BlockLength
end
end
- describe "#commit_patches" do
- let(:repository) { gitlab_git_from_gitaly(new_mutable_test_repo) }
- let(:testdata_dir) { File.join(File.dirname(__FILE__), '../../../../../internal/gitaly/service/operations/testdata') }
- let(:patches) { File.foreach(File.join(testdata_dir, patch_file_name)) }
-
- def apply_patches(branch_name)
- repository.commit_patches(branch_name, patches)
- end
-
- context 'when the patch applies' do
- let(:patch_file_name) { '0001-A-commit-from-a-patch.patch' }
-
- it 'creates a new rev with the patch' do
- new_rev = apply_patches(repository.root_ref)
- commit = repository.commit(new_rev)
-
- expect(new_rev).not_to be_nil
- expect(commit.message).to eq("A commit from a patch\n")
-
- # Ensure worktree cleanup occurs
- result, status = repository.send(:run_git, %w[worktree list --porcelain])
- expect(status).to eq(0)
- expect(result).to eq("worktree #{repository_path}\nbare\n\n")
- end
- end
-
- context 'when the patch does not apply' do
- let(:patch_file_name) { '0001-This-does-not-apply-to-the-feature-branch.patch' }
-
- it 'raises a PatchError' do
- expect { apply_patches('feature') }.to raise_error Gitlab::Git::PatchError
- end
- end
- end
-
- describe '#update_submodule' do
- let(:new_oid) { 'db97db76ecd478eb361f439807438f82d97b29a5' }
- let(:repository) { gitlab_git_from_gitaly(new_mutable_test_repo) }
- let(:submodule) { 'gitlab-grack' }
- let(:head_commit) { repository.commit(branch) }
- let!(:head_submodule_reference) { repository.blob_at(head_commit.id, submodule).id }
- let(:committer) { repository.user_to_committer(user) }
- let(:message) { 'Update submodule' }
- let(:branch) { 'master' }
-
- subject do
- repository.update_submodule(submodule,
- new_oid,
- branch,
- committer,
- message)
- end
-
- it 'updates the submodule oid' do
- blob = repository.blob_at(subject, submodule)
-
- expect(blob.id).not_to eq head_submodule_reference
- expect(blob.id).to eq new_oid
- end
- end
-
describe '#head_symbolic_ref' do
subject { repository.head_symbolic_ref }
diff --git a/ruby/spec/lib/gitlab/git/worktree_spec.rb b/ruby/spec/lib/gitlab/git/worktree_spec.rb
deleted file mode 100644
index e240622f2..000000000
--- a/ruby/spec/lib/gitlab/git/worktree_spec.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-describe Gitlab::Git::Worktree do
- context '#initialize' do
- let(:repo_path) { '/tmp/test' }
- let(:prefix) { 'rebase' }
-
- it 'generates valid path' do
- worktree = described_class.new(repo_path, prefix, 12345)
-
- expect(worktree.path).to match('/tmp/test/gitlab-worktree/rebase-12345-.{16}')
- end
-
- it 'rejects bad IDs' do
- expect { described_class.new(repo_path, prefix, '') }.to raise_error(ArgumentError)
- expect { described_class.new(repo_path, prefix, '/test/me') }.to raise_error(ArgumentError)
- end
- end
-end
diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb
index 631cb0a78..52296007e 100644
--- a/ruby/spec/spec_helper.rb
+++ b/ruby/spec/spec_helper.rb
@@ -7,8 +7,7 @@ require 'factory_bot'
require 'pry'
GITALY_RUBY_DIR = File.expand_path('..', __dir__).freeze
-TMP_DIR_NAME = 'tmp'.freeze
-TMP_DIR = File.join(GITALY_RUBY_DIR, TMP_DIR_NAME).freeze
+TMP_DIR = Dir.mktmpdir('gitaly-ruby-tests-').freeze
GITLAB_SHELL_DIR = File.join(TMP_DIR, 'gitlab-shell').freeze
# overwrite HOME env variable so user global .gitconfig doesn't influence tests
@@ -24,4 +23,8 @@ RSpec.configure do |config|
config.before(:suite) do
FactoryBot.find_definitions
end
+
+ config.after(:suite) do
+ FileUtils.rm_rf(TMP_DIR)
+ end
end
diff --git a/ruby/spec/support/helpers/integration_helper.rb b/ruby/spec/support/helpers/integration_helper.rb
index bd2af3fd8..5d3a1ef7c 100644
--- a/ruby/spec/support/helpers/integration_helper.rb
+++ b/ruby/spec/support/helpers/integration_helper.rb
@@ -34,7 +34,7 @@ module IntegrationClient
klass = Gitaly.const_get(service).const_get(:Stub)
addr = case type
when 'unix'
- "unix:#{File.join(TMP_DIR_NAME, SOCKET_PATH)}"
+ "unix:#{File.join(TMP_DIR, SOCKET_PATH)}"
when 'tcp', 'tls'
"#{type}://localhost:#{GitalyConfig.dynamic_port(type)}"
end
@@ -101,7 +101,7 @@ def start_gitaly
gitaly_pid = spawn(File.join(build_dir, 'bin/gitaly'), config_path, options)
at_exit { Process.kill('KILL', gitaly_pid) }
- wait_ready!(File.join(TMP_DIR_NAME, SOCKET_PATH))
+ wait_ready!(File.join(TMP_DIR, SOCKET_PATH))
end
def wait_ready!(socket)
diff --git a/ruby/spec/test_repo_helper.rb b/ruby/spec/test_repo_helper.rb
index 682476fad..811108637 100644
--- a/ruby/spec/test_repo_helper.rb
+++ b/ruby/spec/test_repo_helper.rb
@@ -10,7 +10,7 @@ Gitlab.config.git.test_global_ivar_override(:bin_path, ENV.fetch('GITALY_TESTING
Gitlab.config.git.test_global_ivar_override(:hooks_directory, File.join(GITALY_RUBY_DIR, "hooks"))
Gitlab.config.gitaly.test_global_ivar_override(:bin_dir, __dir__)
-DEFAULT_STORAGE_DIR = File.expand_path('../tmp/repositories', __dir__)
+DEFAULT_STORAGE_DIR = File.join(TMP_DIR, 'repositories', __dir__)
DEFAULT_STORAGE_NAME = 'default'.freeze
TEST_REPO_PATH = File.join(DEFAULT_STORAGE_DIR, 'gitlab-test.git')
TEST_REPO_ORIGIN = '../_build/testrepos/gitlab-test.git'.freeze
@@ -81,27 +81,15 @@ module TestRepo
File.join(DEFAULT_STORAGE_DIR, gitaly_repo.relative_path)
end
- def gitlab_git_from_gitaly(gitaly_repo, gitlab_projects: nil)
+ def gitlab_git_from_gitaly(gitaly_repo)
Gitlab::Git::Repository.new(
gitaly_repo,
repo_path_from_gitaly(gitaly_repo),
'project-123',
- gitlab_projects,
''
)
end
- def gitlab_git_from_gitaly_with_gitlab_projects(gitaly_repo)
- gitlab_projects = Gitlab::Git::GitlabProjects.new(
- DEFAULT_STORAGE_DIR,
- gitaly_repo.relative_path,
- global_hooks_path: '',
- logger: Rails.logger
- )
-
- gitlab_git_from_gitaly(gitaly_repo, gitlab_projects: gitlab_projects)
- end
-
def repository_from_relative_path(relative_path)
gitlab_git_from_gitaly(
Gitaly::Repository.new(storage_name: DEFAULT_STORAGE_NAME, relative_path: relative_path)