Class: Github::Pulse::GitAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/github/pulse/git_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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

#repositoryObject (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)
  
  commits_by_author = 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
    
    author = commit.author[:email]
    commits_by_author[author] << {
      sha: commit.oid,
      message: commit.message.lines.first&.strip,
      time: commit_time,
      additions: 0,
      deletions: 0
    }
  end
  
  commits_by_author.each do |author, 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
  
  commits_by_author
end

#commit_activity_by_dayObject



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_authorObject



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 lines_of_code_by_author
  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|
        author = hunk[:final_signature][:email]
        lines = hunk[:lines_in_hunk]
        blame_data[author] += lines
      end
    rescue StandardError
      next
    end
  end
  
  blame_data
end

#remote_urlObject



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