2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
# Gitlab::Git::CommitStats counts the additions, deletions, and total changes
|
|
|
|
# in a commit.
|
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class CommitStats
|
2018-10-30 09:37:40 -04:00
|
|
|
include Gitlab::Git::WrapsGitalyErrors
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
attr_reader :id, :additions, :deletions, :total
|
|
|
|
|
|
|
|
# Instantiate a CommitStats object
|
2017-07-12 10:31:36 -04:00
|
|
|
#
|
|
|
|
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/323
|
2017-09-06 06:55:16 -04:00
|
|
|
def initialize(repo, commit)
|
2017-01-04 13:43:06 -05:00
|
|
|
@id = commit.id
|
|
|
|
|
2021-08-06 11:10:05 -04:00
|
|
|
additions, deletions = fetch_stats(repo, commit)
|
2017-09-06 06:55:16 -04:00
|
|
|
|
2021-08-06 11:10:05 -04:00
|
|
|
@additions = additions.to_i
|
|
|
|
@deletions = deletions.to_i
|
2017-09-06 06:55:16 -04:00
|
|
|
@total = @additions + @deletions
|
|
|
|
end
|
2021-08-06 11:10:05 -04:00
|
|
|
|
|
|
|
def fetch_stats(repo, commit)
|
|
|
|
Rails.cache.fetch("commit_stats:#{repo.gl_project_path}:#{@id}") do
|
|
|
|
stats = wrapped_gitaly_errors do
|
|
|
|
repo.gitaly_commit_client.commit_stats(@id)
|
|
|
|
end
|
|
|
|
|
|
|
|
[stats.additions, stats.deletions]
|
|
|
|
end
|
|
|
|
end
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|