Class: Github::Pulse::GitAnalyzer
- Inherits:
-
Object
- Object
- Github::Pulse::GitAnalyzer
- Defined in:
- lib/github/pulse/git_analyzer.rb
Instance Attribute Summary collapse
-
#repo_path ⇒ Object
readonly
Returns the value of attribute repo_path.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
- #analyze_commits(since: nil, until_date: nil) ⇒ Object
- #commit_activity_by_day ⇒ Object
-
#initialize(repo_path) ⇒ GitAnalyzer
constructor
A new instance of GitAnalyzer.
- #lines_of_code_by_author ⇒ Object
- #remote_url ⇒ Object
Constructor Details
#initialize(repo_path) ⇒ GitAnalyzer
Returns a new instance of GitAnalyzer.
11 12 13 14 15 16 |
# File 'lib/github/pulse/git_analyzer.rb', line 11 def initialize(repo_path) @repo_path = repo_path @repository = Rugged::Repository.new(repo_path) rescue Rugged::RepositoryError => e raise Error, "Not a valid git repository: #{repo_path}" end |
Instance Attribute Details
#repo_path ⇒ Object (readonly)
Returns the value of attribute repo_path.
9 10 11 |
# File 'lib/github/pulse/git_analyzer.rb', line 9 def repo_path @repo_path end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
9 10 11 |
# File 'lib/github/pulse/git_analyzer.rb', line 9 def repository @repository end |
Instance Method Details
#analyze_commits(since: nil, until_date: nil) ⇒ Object
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 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/github/pulse/git_analyzer.rb', line 18 def analyze_commits(since: nil, until_date: nil) walker = Rugged::Walker.new(repository) walker.push(repository.head.target_id) = Hash.new { |h, k| h[k] = [] } walker.each do |commit| commit_time = Time.at(commit.time) if since && commit_time < Time.parse(since) break end if until_date && commit_time > Time.parse(until_date) next end = commit.[:email] [] << { sha: commit.oid, message: commit..lines.first&.strip, time: commit_time, additions: 0, deletions: 0 } end .each do |, commits| commits.each do |commit_data| stats = calculate_commit_stats(commit_data[:sha]) commit_data[:additions] = stats[:additions] commit_data[:deletions] = stats[:deletions] end end end |
#commit_activity_by_day ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/github/pulse/git_analyzer.rb', line 81 def commit_activity_by_day walker = Rugged::Walker.new(repository) walker.push(repository.head.target_id) activity = Hash.new(0) walker.each do |commit| date = Time.at(commit.time).to_date activity[date] += 1 end activity.sort.to_h end |
#lines_of_code_by_author ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/github/pulse/git_analyzer.rb', line 56 def blame_data = Hash.new(0) repository.head.target.tree.walk(:preorder) do |root, entry| next unless entry[:type] == :blob file_path = root.empty? ? entry[:name] : "#{root}/#{entry[:name]}" next if binary_file?(file_path) begin blame = Rugged::Blame.new(repository, file_path) blame.each do |hunk| = hunk[:final_signature][:email] lines = hunk[:lines_in_hunk] blame_data[] += lines end rescue StandardError next end end blame_data end |
#remote_url ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/github/pulse/git_analyzer.rb', line 95 def remote_url remotes = repository.remotes return nil if remotes.count == 0 origin = remotes["origin"] return nil unless origin url = origin.url extract_github_repo(url) end |