summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/summary/commit.rb
blob: bea78862757183f0c668cba08a8ec2f395a496fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Gitlab
  module CycleAnalytics
    module Summary
      class Commit < Base
        def title
          n_('Commit', 'Commits', value)
        end

        def value
          @value ||= count_commits
        end

        private

        # Don't use the `Gitlab::Git::Repository#log` method, because it enforces
        # a limit. Since we need a commit count, we _can't_ enforce a limit, so
        # the easiest way forward is to replicate the relevant portions of the
        # `log` function here.
        def count_commits
          return unless ref

          repository = @project.repository.raw_repository
          sha = @project.repository.commit(ref).sha

          cmd = %W(git --git-dir=#{repository.path} log)
          cmd << '--format=%H'
          cmd << "--after=#{@from.iso8601}"
          cmd << sha

          output, status = Gitlab::Popen.popen(cmd)

          raise IOError, output unless status.zero?

          output.lines.count
        end

        def ref
          @ref ||= @project.default_branch.presence
        end
      end
    end
  end
end