Class: GitStats::GitData::Repo

Inherits:
Object
  • Object
show all
Includes:
HashInitializable, Inspector
Defined in:
lib/git_stats/git_data/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspector

#inspect, #pretty_print, #to_s

Constructor Details

#initialize(params) ⇒ Repo

Returns a new instance of Repo.



15
16
17
18
19
# File 'lib/git_stats/git_data/repo.rb', line 15

def initialize(params)
  super
  @path = File.expand_path(@path)
  @tree_path ||= '.'
end

Instance Attribute Details

#first_commit_shaObject (readonly)

Returns the value of attribute first_commit_sha.



25
26
27
# File 'lib/git_stats/git_data/repo.rb', line 25

def first_commit_sha
  @first_commit_sha
end

Instance Method Details

#==(other) ⇒ Object



154
155
156
# File 'lib/git_stats/git_data/repo.rb', line 154

def ==(other)
  path == other.path
end

#activityObject



118
119
120
# File 'lib/git_stats/git_data/repo.rb', line 118

def activity
  @activity ||= Activity.new(commits)
end

#add_command_observer(proc = nil, &block) ⇒ Object



149
150
151
152
# File 'lib/git_stats/git_data/repo.rb', line 149

def add_command_observer(proc = nil, &block)
  command_observers << block if block
  command_observers << proc if proc
end

#authorsObject



43
44
45
46
47
# File 'lib/git_stats/git_data/repo.rb', line 43

def authors
  @authors ||= run_and_parse("git shortlog -se #{commit_range} #{tree_path}").map do |author|
    Author.new(repo: self, name: author[:name], email: author[:email])
  end
end

#command_parserObject



145
146
147
# File 'lib/git_stats/git_data/repo.rb', line 145

def command_parser
  @command_parser ||= CommandParser.new
end

#command_runnerObject



141
142
143
# File 'lib/git_stats/git_data/repo.rb', line 141

def command_runner
  @command_runner ||= CommandRunner.new
end

#comment_statsObject



114
115
116
# File 'lib/git_stats/git_data/repo.rb', line 114

def comment_stats
  @comment_stats ||= commits.map(&:comment_stat)
end

#comment_stringObject



35
36
37
# File 'lib/git_stats/git_data/repo.rb', line 35

def comment_string
  @comment_string ||= '//'
end

#comments_count_by_dateObject



93
94
95
96
97
98
99
100
# File 'lib/git_stats/git_data/repo.rb', line 93

def comments_count_by_date
  sum = 0
  @comments_count_by_date ||= commits.map do |commit|
    sum += commit.comment_stat.insertions
    sum -= commit.comment_stat.deletions
    [commit.date.to_date, sum]
  end.to_h.fill_empty_days!(aggregated: true)
end

#commit_rangeObject



106
107
108
# File 'lib/git_stats/git_data/repo.rb', line 106

def commit_range
  first_commit_sha.blank? ? last_commit_sha : "#{first_commit_sha}..#{last_commit_sha}"
end

#commitsObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/git_stats/git_data/repo.rb', line 49

def commits
  command = "git rev-list --pretty=format:'%H|%at|%ai|%aE' #{commit_range} #{tree_path} | grep -v commit"
  @commits ||= run_and_parse(command).map do |commit_line|
    Commit.new(
      repo: self,
      sha: commit_line[:sha],
      stamp: commit_line[:stamp],
      date: Time.parse(commit_line[:date]),
      author: authors.first! { |a| a.email == commit_line[:author_email] }
    )
  end.sort_by!(&:date)
end

#commits_count_by_author(limit = 4) ⇒ Object

TODO: This method is called from nowhere



67
68
69
# File 'lib/git_stats/git_data/repo.rb', line 67

def commits_count_by_author(limit = 4)
  (authors.map { |author| [author, author.commits.size] }.sort_by { |_author, commits| -commits }[0..limit]).to_h
end

#commits_periodObject



62
63
64
# File 'lib/git_stats/git_data/repo.rb', line 62

def commits_period
  commits.map(&:date).minmax
end

#files_count_by_dateObject



78
79
80
81
82
# File 'lib/git_stats/git_data/repo.rb', line 78

def files_count_by_date
  @files_count_by_date ||= commits.map do |commit|
    [commit.date.to_date, commit.files_count]
  end.to_h
end

#last_commitObject



102
103
104
# File 'lib/git_stats/git_data/repo.rb', line 102

def last_commit
  commits.last
end

#last_commit_shaObject



27
28
29
# File 'lib/git_stats/git_data/repo.rb', line 27

def last_commit_sha
  @last_commit_sha ||= 'HEAD'
end

#lines_count_by_dateObject



84
85
86
87
88
89
90
91
# File 'lib/git_stats/git_data/repo.rb', line 84

def lines_count_by_date
  sum = 0
  @lines_count_by_date ||= commits.map do |commit|
    sum += commit.short_stat.insertions
    sum -= commit.short_stat.deletions
    [commit.date.to_date, sum]
  end.to_h
end

#pathObject



21
22
23
# File 'lib/git_stats/git_data/repo.rb', line 21

def path
  @path ||= '.'
end

#project_nameObject



126
127
128
# File 'lib/git_stats/git_data/repo.rb', line 126

def project_name
  @project_name ||= File.expand_path(File.join(path, tree_path)).sub(File.dirname(File.expand_path(path)) + File::SEPARATOR, '')
end

#project_versionObject



122
123
124
# File 'lib/git_stats/git_data/repo.rb', line 122

def project_version
  @project_version ||= run("git rev-parse #{commit_range}").strip
end

#run(command) ⇒ Object



130
131
132
133
134
# File 'lib/git_stats/git_data/repo.rb', line 130

def run(command)
  result = command_runner.run(path, command)
  invoke_command_observers(command, result)
  result
end

#run_and_parse(command) ⇒ Object



136
137
138
139
# File 'lib/git_stats/git_data/repo.rb', line 136

def run_and_parse(command)
  result = run(command)
  command_parser.parse(command, result)
end

#short_statsObject



110
111
112
# File 'lib/git_stats/git_data/repo.rb', line 110

def short_stats
  @short_stats ||= commits.map(&:short_stat)
end

#treeObject



39
40
41
# File 'lib/git_stats/git_data/repo.rb', line 39

def tree
  @tree ||= Tree.new(repo: self, relative_path: @tree_path)
end

#tree_pathObject



31
32
33
# File 'lib/git_stats/git_data/repo.rb', line 31

def tree_path
  @tree_path ||= '.'
end