Class: Ginatra::RepoStats

Inherits:
Object
  • Object
show all
Defined in:
lib/ginatra/repo_stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo, branch_name) ⇒ Ginatra::RepoStats

Parameters:

  • repo (Ginatra::Repo)

    Ginatra::Repo instance

  • branch_name (String)

    Branch name of repository



6
7
8
9
# File 'lib/ginatra/repo_stats.rb', line 6

def initialize(repo, branch_name)
  @repo = repo
  @branch = branch_name
end

Instance Method Details

#commits_countInteger

Commits count in defined branch

Returns:

  • (Integer)

    Commits count



85
86
87
88
89
90
91
# File 'lib/ginatra/repo_stats.rb', line 85

def commits_count
  ref = @repo.ref("refs/heads/#{@branch}")

  walker = Rugged::Walker.new(@repo.to_rugged)
  walker.push(ref.target)
  walker.count
end

#contributorsArray

Contributors to repository

Returns:

  • (Array)

    Information about contributors sorted by commits count



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
# File 'lib/ginatra/repo_stats.rb', line 14

def contributors
  contributors = {}
  ref = @repo.ref("refs/heads/#{@branch}")
  walker = Rugged::Walker.new(@repo.to_rugged)
  walker.push(ref.target)

  walker.each do |commit|
    author = commit.author
    email = author[:email]

    if contributors[email]
      contributors[email] = {
        author: author[:name],
        commits_count: contributors[email][:commits_count] + 1
      }
    else
      contributors[email] = {
        author: author[:name],
        commits_count: 1
      }
    end
  end

  contributors.sort_by {|c| c.last[:commits_count] }.reverse
end

#created_atTime

Repository created at time

Returns:

  • (Time)

    Date of first commit to repository



72
73
74
75
76
77
78
79
80
# File 'lib/ginatra/repo_stats.rb', line 72

def created_at
  ref = @repo.ref("refs/heads/#{@branch}")

  walker = Rugged::Walker.new(@repo.to_rugged)
  walker.sorting(Rugged::SORT_TOPO)
  walker.push(ref.target)
  commit = walker.to_a.last
  Time.at(commit.time)
end

#licenseString

Detect common OSS licenses

Returns:

  • (String)

    License name



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ginatra/repo_stats.rb', line 43

def license
  last_commit = @repo.ref("refs/heads/#{@branch}").target
  license = @repo.blob_at(last_commit.oid, 'LICENSE') || @repo.blob_at(last_commit.oid, 'LICENSE.txt')

  if license.nil?
    'N/A'
  else
    license_text = license.text

    case license_text
    when /Apache License/
      'Apache'
    when /GNU GENERAL PUBLIC LICENSE/
      'GPL'
    when /GNU LESSER GENERAL PUBLIC LICENSE/
      'LGPL'
    when /Permission is hereby granted, free of charge,/
      'MIT'
    when /Redistribution and use in source and binary forms/
      'BSD'
    else
      'N/A'
    end
  end
end