Sha256: 6e48b52889caa8422fb67cb54b437ed76699353c2da1d52632484bd4adc1240e

Contents?: true

Size: 1.76 KB

Versions: 38

Compression:

Stored size: 1.76 KB

Contents

module Gitlab
  module Git
    class Stats
      attr_accessor :repo, :ref

      def initialize repo, ref
        @repo, @ref = repo, ref
      end

      def authors
        @authors ||= collect_authors
      end

      def commits_count
        @commits_count ||= repo.commit_count(ref)
      end

      def files_count
        args = [ref, '-r', '--name-only' ]
        repo.git.run(nil, 'ls-tree', nil, {}, args).split("\n").count
      end

      def authors_count
        authors.size
      end

      def graph
        @graph ||= build_graph
      end

      protected

      def collect_authors
        shortlog = repo.git.shortlog({e: true, s: true }, ref)

        authors = []

        lines = shortlog.split("\n")

        lines.each do |line|
          data = line.split("\t")
          commits = data.first
          author = Grit::Actor.from_string(data.last)

          authors << OpenStruct.new(
            name: author.name,
            email: author.email,
            commits: commits.to_i
          )
        end

        authors.sort_by(&:commits).reverse
      end

      def build_graph n = 4
        from, to = (Date.today.prev_day(n*7)), Date.today
        args = ['--all', "--since=#{from.to_s}", '--format=%ad' ]
        rev_list = repo.git.run(nil, 'rev-list', nil, {}, args).split("\n")

        commits_dates = rev_list.values_at(* rev_list.each_index.select {|i| i.odd?})
        commits_dates = commits_dates.map { |date_str| Time.parse(date_str).to_date.to_s(:date) }

        commits_per_day = from.upto(to).map do |day|
          commits_dates.count(day.to_date.to_s)
        end

        OpenStruct.new(
          labels: from.upto(to).map { |day| day.strftime('%b %d') },
          commits: commits_per_day,
          weeks: n
        )
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
gitlab_git-5.5.0 lib/gitlab_git/stats.rb
gitlab_git-5.4.0 lib/gitlab_git/stats.rb
gitlab_git-5.3.0 lib/gitlab_git/stats.rb
gitlab_git-5.2.0 lib/gitlab_git/stats.rb
gitlab_git-5.1.0 lib/gitlab_git/stats.rb
gitlab_git-5.0.0 lib/gitlab_git/stats.rb
gitlab_git-4.1.0 lib/gitlab_git/stats.rb
gitlab_git-4.0.0 lib/gitlab_git/stats.rb
gitlab_git-4.0.0.pre lib/gitlab_git/stats.rb
gitlab_git-3.1.0 lib/gitlab_git/stats.rb
gitlab_git-3.0.1 lib/gitlab_git/stats.rb
gitlab_git-3.0.0 lib/gitlab_git/stats.rb
gitlab_git-1.3.1 lib/gitlab_git/stats.rb
gitlab_git-3.0.0.rc2 lib/gitlab_git/stats.rb
gitlab_git-3.0.0.rc1 lib/gitlab_git/stats.rb
gitlab_git-3.0.0.beta1 lib/gitlab_git/stats.rb
gitlab_git-3.0.0.pre lib/gitlab_git/stats.rb
gitlab_git-2.3.1 lib/gitlab_git/stats.rb
gitlab_git-2.3.0 lib/gitlab_git/stats.rb
gitlab_git-2.2.0 lib/gitlab_git/stats.rb