diff options
author | Ahmad Sherif <me@ahmadsherif.com> | 2017-06-12 20:55:28 +0200 |
---|---|---|
committer | Ahmad Sherif <me@ahmadsherif.com> | 2017-06-21 17:31:20 +0200 |
commit | 0284f01716cfdcbe8d9e7a0281e551414b4c0239 (patch) | |
tree | 6c40ea5dfe9a4ec397da22fa4c30baf0ed3e2047 /lib | |
parent | 78ee24ba53a4a44d829613495ec0d5f1bbb5a9ee (diff) | |
download | gitlab-ce-0284f01716cfdcbe8d9e7a0281e551414b4c0239.tar.gz |
Migrate Gitlab::Git::Blob.find to Gitalyfeature/migrate-blob-find-to-gitaly
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/git/blob.rb | 45 | ||||
-rw-r--r-- | lib/gitlab/gitaly_client/commit.rb | 20 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lib/gitlab/git/blob.rb b/lib/gitlab/git/blob.rb index 33a7624e303..a7aceab4c14 100644 --- a/lib/gitlab/git/blob.rb +++ b/lib/gitlab/git/blob.rb @@ -14,6 +14,51 @@ module Gitlab class << self def find(repository, sha, path) + Gitlab::GitalyClient.migrate(:project_raw_show) do |is_enabled| + if is_enabled + find_by_gitaly(repository, sha, path) + else + find_by_rugged(repository, sha, path) + end + end + end + + def find_by_gitaly(repository, sha, path) + path = path.sub(/\A\/*/, '') + path = '/' if path.empty? + name = File.basename(path) + entry = Gitlab::GitalyClient::Commit.new(repository).tree_entry(sha, path, MAX_DATA_DISPLAY_SIZE) + return unless entry + + case entry.type + when :COMMIT + new( + id: entry.oid, + name: name, + size: 0, + data: '', + path: path, + commit_id: sha + ) + when :BLOB + # 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(entry.data) + new( + id: entry.oid, + name: name, + size: entry.size, + data: entry.data.dup, + mode: entry.mode.to_s(8), + path: path, + commit_id: sha, + binary: detect && detect[:type] == :binary + ) + end + end + + def find_by_rugged(repository, sha, path) commit = repository.lookup(sha) root_tree = commit.tree diff --git a/lib/gitlab/gitaly_client/commit.rb b/lib/gitlab/gitaly_client/commit.rb index 73c1848c95f..b8877619797 100644 --- a/lib/gitlab/gitaly_client/commit.rb +++ b/lib/gitlab/gitaly_client/commit.rb @@ -36,6 +36,26 @@ module Gitlab end end + def tree_entry(ref, path, limit = nil) + request = Gitaly::TreeEntryRequest.new( + repository: @gitaly_repo, + revision: ref, + path: path.dup.force_encoding(Encoding::ASCII_8BIT), + limit: limit.to_i + ) + + response = GitalyClient.call(@repository.storage, :commit, :tree_entry, request) + entry = response.first + return unless entry.oid.present? + + if entry.type == :BLOB + rest_of_data = response.reduce("") { |memo, msg| memo << msg.data } + entry.data += rest_of_data + end + + entry + end + private def commit_diff_request_params(commit, options = {}) |