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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2018-04-06 19:14:05 +0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2018-04-06 19:25:07 +0300
commit76cd2d7881da69697f5c6584be04abbfb9411a22 (patch)
tree6c7c665420ddabca752024d4b92951358c0fd4f7 /lib
parent8ee16ed684a75c7b31e4a69874d4b591899a2bf1 (diff)
Move checksum calculation to Gitlab::Git::Repository
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/git/checksum.rb97
-rw-r--r--lib/gitlab/git/repository.rb40
2 files changed, 40 insertions, 97 deletions
diff --git a/lib/gitlab/git/checksum.rb b/lib/gitlab/git/checksum.rb
deleted file mode 100644
index 218651906bf..00000000000
--- a/lib/gitlab/git/checksum.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-module Gitlab
- module Git
- class Checksum
- include Gitlab::Git::Popen
-
- EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'.freeze
-
- Failure = Class.new(StandardError)
-
- attr_reader :path, :relative_path, :storage, :storage_path, :gl_repository
-
- def initialize(storage, relative_path, gl_repository)
- @storage = storage
- @storage_path = Gitlab.config.repositories.storages[storage].legacy_disk_path
- @relative_path = "#{relative_path}.git"
- @path = File.join(storage_path, @relative_path)
- @gl_repository = gl_repository
- end
-
- def calculate
- unless repository_exists?
- failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path')
- end
-
- raw_repository.gitaly_migrate(:calculate_checksum) do |is_enabled|
- if is_enabled
- calculate_checksum_gitaly
- else
- calculate_checksum_by_shelling_out
- end
- end
- end
-
- private
-
- def repository_exists?
- raw_repository.exists?
- end
-
- def calculate_checksum_gitaly
- gitaly_repository_client.calculate_checksum
- end
-
- def calculate_checksum_by_shelling_out
- args = %W(--git-dir=#{path} show-ref --heads --tags)
- output, status = run_git(args)
-
- if status&.zero?
- refs = output.split("\n")
-
- result = refs.inject(nil) do |checksum, ref|
- value = Digest::SHA1.hexdigest(ref).hex
-
- if checksum.nil?
- value
- else
- checksum ^ value
- end
- end
-
- result.to_s(16)
- else
- # Empty repositories return with a non-zero status and an empty output.
- if output&.empty?
- EMPTY_REPOSITORY_CHECKSUM
- else
- failure!(Gitlab::Git::Checksum::Failure, output)
- end
- end
- end
-
- def failure!(klass, message)
- Gitlab::GitLogger.error("'git show-ref --heads --tags' in #{path}: #{message}")
-
- raise klass.new("Could not calculate the checksum for #{path}: #{message}")
- end
-
- def circuit_breaker
- @circuit_breaker ||= Gitlab::Git::Storage::CircuitBreaker.for_storage(storage)
- end
-
- def raw_repository
- @raw_repository ||= Gitlab::Git::Repository.new(storage, relative_path, gl_repository)
- end
-
- def gitaly_repository_client
- raw_repository.gitaly_repository_client
- end
-
- def run_git(args)
- circuit_breaker.perform do
- popen([Gitlab.config.git.bin_path, *args], path)
- end
- end
- end
- end
-end
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 5678c28cf3a..79cacd9f6f5 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -23,6 +23,7 @@ module Gitlab
SQUASH_WORKTREE_PREFIX = 'squash'.freeze
GITALY_INTERNAL_URL = 'ssh://gitaly/internal.git'.freeze
GITLAB_PROJECTS_TIMEOUT = Gitlab.config.gitlab_shell.git_timeout
+ EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'.freeze
NoRepository = Class.new(StandardError)
InvalidBlobName = Class.new(StandardError)
@@ -31,6 +32,7 @@ module Gitlab
DeleteBranchError = Class.new(StandardError)
CreateTreeError = Class.new(StandardError)
TagExistsError = Class.new(StandardError)
+ ChecksumError = Class.new(StandardError)
class << self
# Unlike `new`, `create` takes the repository path
@@ -1502,6 +1504,16 @@ module Gitlab
FileUtils.rm_rf(worktree_git_path) if worktree_git_path && File.exist?(worktree_git_path)
end
+ def checksum
+ gitaly_migrate(:calculate_checksum) do |is_enabled|
+ if is_enabled
+ gitaly_repository_client.calculate_checksum
+ else
+ calculate_checksum_by_shelling_out
+ end
+ end
+ end
+
private
def local_write_ref(ref_path, ref, old_ref: nil, shell: true)
@@ -2420,6 +2432,34 @@ module Gitlab
def sha_from_ref(ref)
rev_parse_target(ref).oid
end
+
+ def calculate_checksum_by_shelling_out
+ raise NoRepository unless exists?
+
+ args = %W(--git-dir=#{path} show-ref --heads --tags)
+ output, status = run_git(args)
+
+ if status.nil? || !status.zero?
+ # Empty repositories return with a non-zero status and an empty output.
+ return EMPTY_REPOSITORY_CHECKSUM if output&.empty?
+
+ raise ChecksumError, output
+ end
+
+ refs = output.split("\n")
+
+ result = refs.inject(nil) do |checksum, ref|
+ value = Digest::SHA1.hexdigest(ref).hex
+
+ if checksum.nil?
+ value
+ else
+ checksum ^ value
+ end
+ end
+
+ result.to_s(16)
+ end
end
end
end