# Class Gitlab::GitStats < Object

- lib/gitlab/git_stats.rb

Methods

A
  - authors,
  - authors_count
B
  - build_graph
C
  - collect_authors,
  - commits_count
F
  - files_count
G
  - graph
N
  - new

Attributes

[RW] | ref |
[RW] | repo |

Class Public methods
new(repo, ref) Link

Source: show

    # File lib/gitlab/git_stats.rb, line 5
    def initialize repo, ref
      @repo, @ref = repo, ref
    end

Instance Public methods
authors() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 9
    def authors
      @authors ||= collect_authors
    end

authors_count() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 22
    def authors_count
      authors.size
    end

commits_count() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 13
    def commits_count
      @commits_count ||= repo.commit_count(ref)
    end

files_count() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 17
    def files_count
      args = [ref, '-r', '--name-only' ]
      repo.git.run(nil, 'ls-tree', nil, {}, args).split("\n").count
    end

graph() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 26
    def graph
      @graph ||= build_graph
    end

Instance Protected methods
build_graph(n = 4) Link

Source: show

    # File lib/gitlab/git_stats.rb, line 54
    def build_graph n = 4
      from, to = (Date.today - n.weeks), Date.today
      args = ['--all', "--since=#{from.to_s(:date)}", '--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(:date))
      end

      OpenStruct.new(
        labels: from.upto(to).map { |day| day.stamp('Aug 23') },
        commits: commits_per_day,
        weeks: n
      )
    end

collect_authors() Link

Source: show

    # File lib/gitlab/git_stats.rb, line 32
    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