2017-02-24 10:53:44 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-07-18 03:59:36 -04:00
|
|
|
class CommitService
|
2017-02-24 10:53:44 -05:00
|
|
|
# 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
|
|
|
|
|
2017-04-11 15:23:00 -04:00
|
|
|
def initialize(repository)
|
|
|
|
@gitaly_repo = repository.gitaly_repository
|
2017-05-05 10:55:12 -04:00
|
|
|
@repository = repository
|
2017-04-11 15:23:00 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 00:16:02 -04:00
|
|
|
def ls_files(revision)
|
|
|
|
request = Gitaly::ListFilesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: GitalyClient.encode(revision)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :list_files, request)
|
|
|
|
response.flat_map do |msg|
|
|
|
|
msg.paths.map { |d| d.dup.force_encoding(Encoding::UTF_8) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 13:52:43 -04:00
|
|
|
def ancestor?(ancestor_id, child_id)
|
2017-04-11 15:23:00 -04:00
|
|
|
request = Gitaly::CommitIsAncestorRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
ancestor_id: ancestor_id,
|
|
|
|
child_id: child_id
|
|
|
|
)
|
|
|
|
|
2017-07-18 03:59:36 -04:00
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :commit_is_ancestor, request).value
|
2017-05-05 10:55:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_from_parent(commit, options = {})
|
|
|
|
request_params = commit_diff_request_params(commit, options)
|
|
|
|
request_params[:ignore_whitespace_change] = options.fetch(:ignore_whitespace_change, false)
|
2017-07-13 18:22:09 -04:00
|
|
|
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)
|
|
|
|
|
2017-06-16 12:42:41 -04:00
|
|
|
request = Gitaly::CommitDiffRequest.new(request_params)
|
2017-07-18 03:59:36 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_diff, request)
|
2017-07-25 17:26:52 -04:00
|
|
|
GitalyClient::DiffStitcher.new(response)
|
2017-04-11 15:23:00 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 10:55:12 -04:00
|
|
|
def commit_deltas(commit)
|
2017-06-16 12:42:41 -04:00
|
|
|
request = Gitaly::CommitDeltaRequest.new(commit_diff_request_params(commit))
|
2017-07-18 03:59:36 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_delta, request)
|
2017-07-25 17:26:52 -04:00
|
|
|
|
|
|
|
response.flat_map { |msg| msg.deltas }
|
2017-02-24 10:53:44 -05:00
|
|
|
end
|
2017-05-05 10:55:12 -04:00
|
|
|
|
2017-06-12 14:55:28 -04:00
|
|
|
def tree_entry(ref, path, limit = nil)
|
|
|
|
request = Gitaly::TreeEntryRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: ref,
|
2017-07-25 17:33:06 -04:00
|
|
|
path: GitalyClient.encode(path),
|
2017-06-12 14:55:28 -04:00
|
|
|
limit: limit.to_i
|
|
|
|
)
|
|
|
|
|
2017-07-18 03:59:36 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :tree_entry, request)
|
2017-06-12 14:55:28 -04:00
|
|
|
|
2017-08-16 08:29:27 -04:00
|
|
|
entry = nil
|
|
|
|
data = ''
|
|
|
|
response.each do |msg|
|
|
|
|
if entry.nil?
|
|
|
|
entry = msg
|
|
|
|
|
|
|
|
break unless entry.type == :BLOB
|
|
|
|
end
|
|
|
|
|
|
|
|
data << msg.data
|
2017-06-12 14:55:28 -04:00
|
|
|
end
|
2017-08-16 08:29:27 -04:00
|
|
|
entry.data = data
|
2017-06-12 14:55:28 -04:00
|
|
|
|
2017-08-16 08:29:27 -04:00
|
|
|
entry unless entry.oid.blank?
|
2017-06-12 14:55:28 -04:00
|
|
|
end
|
|
|
|
|
2017-07-18 01:02:56 -04:00
|
|
|
def tree_entries(repository, revision, path)
|
|
|
|
request = Gitaly::GetTreeEntriesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-08-21 14:13:40 -04:00
|
|
|
revision: GitalyClient.encode(revision),
|
|
|
|
path: path.present? ? GitalyClient.encode(path) : '.'
|
2017-07-18 01:02:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :get_tree_entries, request)
|
|
|
|
|
|
|
|
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),
|
2017-09-06 16:47:25 -04:00
|
|
|
name: File.basename(gitaly_tree_entry.path),
|
|
|
|
path: GitalyClient.encode(gitaly_tree_entry.path),
|
|
|
|
flat_path: GitalyClient.encode(gitaly_tree_entry.flat_path),
|
2017-07-18 01:02:56 -04:00
|
|
|
commit_id: gitaly_tree_entry.commit_oid
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-12 14:55:28 -04:00
|
|
|
|
2017-07-27 02:45:04 -04:00
|
|
|
def commit_count(ref, options = {})
|
2017-07-06 14:34:25 -04:00
|
|
|
request = Gitaly::CountCommitsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: ref
|
|
|
|
)
|
2017-07-27 02:45:04 -04:00
|
|
|
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?
|
|
|
|
request.path = options[:path] if options[:path].present?
|
2017-07-06 14:34:25 -04:00
|
|
|
|
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :count_commits, request).count
|
|
|
|
end
|
|
|
|
|
2017-07-31 13:38:36 -04:00
|
|
|
def last_commit_for_path(revision, path)
|
|
|
|
request = Gitaly::LastCommitForPathRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-07-25 17:33:06 -04:00
|
|
|
revision: GitalyClient.encode(revision),
|
|
|
|
path: GitalyClient.encode(path.to_s)
|
2017-07-31 13:38:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
gitaly_commit = GitalyClient.call(@repository.storage, :commit_service, :last_commit_for_path, request).commit
|
|
|
|
return unless gitaly_commit
|
|
|
|
|
2017-07-25 16:48:17 -04:00
|
|
|
Gitlab::Git::Commit.new(@repository, gitaly_commit)
|
2017-07-31 13:38:36 -04:00
|
|
|
end
|
|
|
|
|
2017-06-23 17:52:51 -04:00
|
|
|
def between(from, to)
|
|
|
|
request = Gitaly::CommitsBetweenRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
from: from,
|
|
|
|
to: to
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :commits_between, request)
|
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
2017-07-18 04:06:49 -04:00
|
|
|
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?
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :find_all_commits, request)
|
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
2017-08-03 04:22:01 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :commits_by_message, request)
|
|
|
|
consume_commits_response(response)
|
|
|
|
end
|
|
|
|
|
2017-07-31 09:23:05 -04:00
|
|
|
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
|
|
|
|
|
2017-07-28 08:16:26 -04:00
|
|
|
def raw_blame(revision, path)
|
|
|
|
request = Gitaly::RawBlameRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-08-23 07:44:54 -04:00
|
|
|
revision: GitalyClient.encode(revision),
|
|
|
|
path: GitalyClient.encode(path)
|
2017-07-28 08:16:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :raw_blame, request)
|
|
|
|
response.reduce("") { |memo, msg| memo << msg.data }
|
|
|
|
end
|
|
|
|
|
2017-07-25 17:33:06 -04:00
|
|
|
def find_commit(revision)
|
|
|
|
request = Gitaly::FindCommitRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: GitalyClient.encode(revision)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :commit_service, :find_commit, request)
|
|
|
|
|
|
|
|
response.commit
|
|
|
|
end
|
|
|
|
|
2017-08-09 17:47:11 -04:00
|
|
|
def patch(revision)
|
|
|
|
request = Gitaly::CommitPatchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: GitalyClient.encode(revision)
|
|
|
|
)
|
|
|
|
response = GitalyClient.call(@repository.storage, :diff_service, :commit_patch, request)
|
|
|
|
|
|
|
|
response.sum(&:data)
|
|
|
|
end
|
|
|
|
|
2017-09-06 06:55:16 -04:00
|
|
|
def commit_stats(revision)
|
|
|
|
request = Gitaly::CommitStatsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
revision: GitalyClient.encode(revision)
|
|
|
|
)
|
|
|
|
GitalyClient.call(@repository.storage, :commit_service, :commit_stats, request)
|
|
|
|
end
|
|
|
|
|
2017-05-05 10:55:12 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def commit_diff_request_params(commit, options = {})
|
2017-07-25 16:48:17 -04:00
|
|
|
parent_id = commit.parent_ids.first || EMPTY_TREE_ID
|
2017-05-05 10:55:12 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
left_commit_id: parent_id,
|
|
|
|
right_commit_id: commit.id,
|
|
|
|
paths: options.fetch(:paths, [])
|
|
|
|
}
|
|
|
|
end
|
2017-06-23 17:52:51 -04:00
|
|
|
|
|
|
|
def consume_commits_response(response)
|
2017-07-18 04:06:49 -04:00
|
|
|
response.flat_map do |message|
|
|
|
|
message.commits.map do |gitaly_commit|
|
2017-07-25 16:48:17 -04:00
|
|
|
Gitlab::Git::Commit.new(@repository, gitaly_commit)
|
2017-07-18 04:06:49 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-23 17:52:51 -04:00
|
|
|
end
|
2017-02-24 10:53:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|