2017-09-10 17:25:29 +05:30
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
class CommitService
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# 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
|
|
|
|
|
|
|
|
def initialize(repository)
|
|
|
|
@gitaly_repo = repository.gitaly_repository
|
|
|
|
@repository = repository
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def ls_files(revision)
|
|
|
|
request = Gitaly::ListFilesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: encode_binary(revision)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :list_files, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
response.flat_map do |msg|
|
|
|
|
msg.paths.map { |d| EncodingHelper.encode!(d.dup) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ancestor?(ancestor_id, child_id)
|
2017-09-10 17:25:29 +05:30
|
|
|
request = Gitaly::CommitIsAncestorRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
ancestor_id: ancestor_id,
|
|
|
|
child_id: child_id
|
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :commit_is_ancestor, request, timeout: GitalyClient.fast_timeout).value
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff(from, to, options = {})
|
|
|
|
from_id = case from
|
|
|
|
when NilClass
|
|
|
|
EMPTY_TREE_ID
|
|
|
|
else
|
|
|
|
if from.respond_to?(:oid)
|
|
|
|
# This is meant to match a Rugged::Commit. This should be impossible in
|
|
|
|
# the future.
|
|
|
|
from.oid
|
|
|
|
else
|
|
|
|
from
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
to_id = case to
|
|
|
|
when NilClass
|
|
|
|
EMPTY_TREE_ID
|
|
|
|
else
|
|
|
|
if to.respond_to?(:oid)
|
|
|
|
# This is meant to match a Rugged::Commit. This should be impossible in
|
|
|
|
# the future.
|
|
|
|
to.oid
|
|
|
|
else
|
|
|
|
to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request_params = diff_between_commits_request_params(from_id, to_id, options)
|
|
|
|
|
|
|
|
call_commit_diff(request_params, options)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_from_parent(commit, options = {})
|
2018-03-17 18:26:18 +05:30
|
|
|
request_params = diff_from_parent_request_params(commit, options)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
call_commit_diff(request_params, options)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def commit_deltas(commit)
|
2018-03-17 18:26:18 +05:30
|
|
|
request = Gitaly::CommitDeltaRequest.new(diff_from_parent_request_params(commit))
|
2017-09-10 17:25:29 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_delta, request)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
response.flat_map { |msg| msg.deltas }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def tree_entry(ref, path, limit = nil)
|
|
|
|
request = Gitaly::TreeEntryRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: ref,
|
2018-03-17 18:26:18 +05:30
|
|
|
path: encode_binary(path),
|
2017-09-10 17:25:29 +05:30
|
|
|
limit: limit.to_i
|
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :tree_entry, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
|
|
|
|
entry = nil
|
|
|
|
data = ''
|
|
|
|
response.each do |msg|
|
|
|
|
if entry.nil?
|
|
|
|
entry = msg
|
|
|
|
|
|
|
|
break unless entry.type == :BLOB
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
data << msg.data
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
entry.data = data
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
entry unless entry.oid.blank?
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def tree_entries(repository, revision, path, recursive)
|
2017-09-10 17:25:29 +05:30
|
|
|
request = Gitaly::GetTreeEntriesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2018-03-17 18:26:18 +05:30
|
|
|
revision: encode_binary(revision),
|
2018-03-27 19:54:05 +05:30
|
|
|
path: path.present? ? encode_binary(path) : '.',
|
|
|
|
recursive: recursive
|
2017-09-10 17:25:29 +05:30
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :get_tree_entries, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
response.flat_map do |message|
|
|
|
|
message.entries.map do |gitaly_tree_entry|
|
|
|
|
Gitlab::Git::Tree.new(
|
|
|
|
id: gitaly_tree_entry.oid,
|
|
|
|
root_id: gitaly_tree_entry.root_oid,
|
|
|
|
type: gitaly_tree_entry.type.downcase,
|
|
|
|
mode: gitaly_tree_entry.mode.to_s(8),
|
2018-03-17 18:26:18 +05:30
|
|
|
name: File.basename(gitaly_tree_entry.path),
|
|
|
|
path: encode_binary(gitaly_tree_entry.path),
|
|
|
|
flat_path: encode_binary(gitaly_tree_entry.flat_path),
|
2017-09-10 17:25:29 +05:30
|
|
|
commit_id: gitaly_tree_entry.commit_oid
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_count(ref, options = {})
|
|
|
|
request = Gitaly::CountCommitsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2018-03-27 19:54:05 +05:30
|
|
|
revision: encode_binary(ref),
|
|
|
|
all: !!options[:all]
|
2017-09-10 17:25:29 +05:30
|
|
|
)
|
|
|
|
request.after = Google::Protobuf::Timestamp.new(seconds: options[:after].to_i) if options[:after].present?
|
|
|
|
request.before = Google::Protobuf::Timestamp.new(seconds: options[:before].to_i) if options[:before].present?
|
2018-03-17 18:26:18 +05:30
|
|
|
request.path = encode_binary(options[:path]) if options[:path].present?
|
|
|
|
request.max_count = options[:max_count] if options[:max_count].present?
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :count_commits, request, timeout: GitalyClient.medium_timeout).count
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit_for_path(revision, path)
|
|
|
|
request = Gitaly::LastCommitForPathRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2018-03-17 18:26:18 +05:30
|
|
|
revision: encode_binary(revision),
|
|
|
|
path: encode_binary(path.to_s)
|
2017-09-10 17:25:29 +05:30
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
gitaly_commit = GitalyClient.call(@repository.storage, :commit_service, :last_commit_for_path, request, timeout: GitalyClient.fast_timeout).commit
|
2017-09-10 17:25:29 +05:30
|
|
|
return unless gitaly_commit
|
|
|
|
|
|
|
|
Gitlab::Git::Commit.new(@repository, gitaly_commit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def between(from, to)
|
|
|
|
request = Gitaly::CommitsBetweenRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
from: from,
|
|
|
|
to: to
|
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :commits_between, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_commits(opts = {})
|
|
|
|
request = Gitaly::FindAllCommitsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: opts[:ref].to_s,
|
|
|
|
max_count: opts[:max_count].to_i,
|
|
|
|
skip: opts[:skip].to_i
|
|
|
|
)
|
|
|
|
request.order = opts[:order].upcase if opts[:order].present?
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :find_all_commits, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_commits_by_oid(oids)
|
|
|
|
request = Gitaly::ListCommitsByOidRequest.new(repository: @gitaly_repo, oid: oids)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :list_commits_by_oid, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
consume_commits_response(response)
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue GRPC::NotFound # If no repository is found, happens mainly during testing
|
|
|
|
[]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def commits_by_message(query, revision: '', path: '', limit: 1000, offset: 0)
|
|
|
|
request = Gitaly::CommitsByMessageRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
query: query,
|
|
|
|
revision: revision.to_s.force_encoding(Encoding::ASCII_8BIT),
|
|
|
|
path: path.to_s.force_encoding(Encoding::ASCII_8BIT),
|
|
|
|
limit: limit.to_i,
|
|
|
|
offset: offset.to_i
|
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :commits_by_message, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def languages(ref = nil)
|
|
|
|
request = Gitaly::CommitLanguagesRequest.new(repository: @gitaly_repo, revision: ref || '')
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :commit_languages, request)
|
|
|
|
|
|
|
|
response.languages.map { |l| { value: l.share.round(2), label: l.name, color: l.color, highlight: l.color } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_blame(revision, path)
|
|
|
|
request = Gitaly::RawBlameRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2018-03-17 18:26:18 +05:30
|
|
|
revision: encode_binary(revision),
|
|
|
|
path: encode_binary(path)
|
2017-09-10 17:25:29 +05:30
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :raw_blame, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
response.reduce("") { |memo, msg| memo << msg.data }
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def find_commit(revision)
|
|
|
|
if RequestStore.active?
|
|
|
|
# We don't use RequeStstore.fetch(key) { ... } directly because `revision`
|
|
|
|
# can be a branch name, so we can't use it as a key as it could point
|
|
|
|
# to another commit later on (happens a lot in tests).
|
|
|
|
key = {
|
|
|
|
storage: @gitaly_repo.storage_name,
|
|
|
|
relative_path: @gitaly_repo.relative_path,
|
|
|
|
commit_id: revision
|
|
|
|
}
|
|
|
|
return RequestStore[key] if RequestStore.exist?(key)
|
|
|
|
|
|
|
|
commit = call_find_commit(revision)
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
key[:commit_id] = commit.id
|
|
|
|
RequestStore[key] = commit
|
|
|
|
else
|
|
|
|
call_find_commit(revision)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def patch(revision)
|
|
|
|
request = Gitaly::CommitPatchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: encode_binary(revision)
|
|
|
|
)
|
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_patch, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
|
|
|
|
response.sum(&:data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_stats(revision)
|
|
|
|
request = Gitaly::CommitStatsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: encode_binary(revision)
|
|
|
|
)
|
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :commit_stats, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_commits(options)
|
|
|
|
request = Gitaly::FindCommitsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
limit: options[:limit],
|
|
|
|
offset: options[:offset],
|
|
|
|
follow: options[:follow],
|
|
|
|
skip_merges: options[:skip_merges],
|
2018-03-27 19:54:05 +05:30
|
|
|
all: !!options[:all],
|
2018-03-17 18:26:18 +05:30
|
|
|
disable_walk: true # This option is deprecated. The 'walk' implementation is being removed.
|
|
|
|
)
|
|
|
|
request.after = GitalyClient.timestamp(options[:after]) if options[:after]
|
|
|
|
request.before = GitalyClient.timestamp(options[:before]) if options[:before]
|
|
|
|
request.revision = encode_binary(options[:ref]) if options[:ref]
|
|
|
|
|
|
|
|
request.paths = encode_repeated(Array(options[:path])) if options[:path].present?
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :find_commits, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
|
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_shas_with_signatures(shas)
|
|
|
|
request = Gitaly::FilterShasWithSignaturesRequest.new(repository: @gitaly_repo)
|
|
|
|
|
|
|
|
enum = Enumerator.new do |y|
|
|
|
|
shas.each_slice(20) do |revs|
|
|
|
|
request.shas = encode_repeated(revs)
|
|
|
|
|
|
|
|
y.yield request
|
|
|
|
|
|
|
|
request = Gitaly::FilterShasWithSignaturesRequest.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :filter_shas_with_signatures, enum)
|
|
|
|
|
|
|
|
response.flat_map do |msg|
|
|
|
|
msg.shas.map { |sha| EncodingHelper.encode!(sha) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_signature(commit_id)
|
|
|
|
request = Gitaly::ExtractCommitSignatureRequest.new(repository: @gitaly_repo, commit_id: commit_id)
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :extract_commit_signature, request)
|
|
|
|
|
|
|
|
signature = ''.b
|
|
|
|
signed_text = ''.b
|
|
|
|
|
|
|
|
response.each do |message|
|
|
|
|
signature << message.signature
|
|
|
|
signed_text << message.signed_text
|
|
|
|
end
|
|
|
|
|
|
|
|
return if signature.blank? && signed_text.blank?
|
|
|
|
|
|
|
|
[signature, signed_text]
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def get_commit_signatures(commit_ids)
|
|
|
|
request = Gitaly::GetCommitSignaturesRequest.new(repository: @gitaly_repo, commit_ids: commit_ids)
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :get_commit_signatures, request)
|
|
|
|
|
|
|
|
signatures = Hash.new { |h, k| h[k] = [''.b, ''.b] }
|
|
|
|
current_commit_id = nil
|
|
|
|
|
|
|
|
response.each do |message|
|
|
|
|
current_commit_id = message.commit_id if message.commit_id.present?
|
|
|
|
|
|
|
|
signatures[current_commit_id].first << message.signature
|
|
|
|
signatures[current_commit_id].last << message.signed_text
|
|
|
|
end
|
|
|
|
|
|
|
|
signatures
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
private
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def call_commit_diff(request_params, options = {})
|
|
|
|
request_params[:ignore_whitespace_change] = options.fetch(:ignore_whitespace_change, false)
|
|
|
|
request_params[:enforce_limits] = options.fetch(:limits, true)
|
|
|
|
request_params[:collapse_diffs] = request_params[:enforce_limits] || !options.fetch(:expanded, true)
|
|
|
|
request_params.merge!(Gitlab::Git::DiffCollection.collection_limits(options).to_h)
|
|
|
|
|
|
|
|
request = Gitaly::CommitDiffRequest.new(request_params)
|
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_diff, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
GitalyClient::DiffStitcher.new(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_from_parent_request_params(commit, options = {})
|
2017-09-10 17:25:29 +05:30
|
|
|
parent_id = commit.parent_ids.first || EMPTY_TREE_ID
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
diff_between_commits_request_params(parent_id, commit.id, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_between_commits_request_params(from_id, to_id, options)
|
2017-09-10 17:25:29 +05:30
|
|
|
{
|
|
|
|
repository: @gitaly_repo,
|
2018-03-17 18:26:18 +05:30
|
|
|
left_commit_id: from_id,
|
|
|
|
right_commit_id: to_id,
|
|
|
|
paths: options.fetch(:paths, []).compact.map { |path| encode_binary(path) }
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def consume_commits_response(response)
|
|
|
|
response.flat_map do |message|
|
|
|
|
message.commits.map do |gitaly_commit|
|
|
|
|
Gitlab::Git::Commit.new(@repository, gitaly_commit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def encode_repeated(a)
|
|
|
|
Google::Protobuf::RepeatedField.new(:bytes, a.map { |s| encode_binary(s) } )
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_find_commit(revision)
|
|
|
|
request = Gitaly::FindCommitRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: encode_binary(revision)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :find_commit, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
|
|
|
|
response.commit
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|