Class: GitStats::GitData::Repo

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Repo

Returns a new instance of Repo.


12
13
14
15
16
# File 'lib/git_stats/git_data/repo.rb', line 12

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

Instance Method Details

#==(other) ⇒ Object


201
202
203
# File 'lib/git_stats/git_data/repo.rb', line 201

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

#activityObject


156
157
158
# File 'lib/git_stats/git_data/repo.rb', line 156

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

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


192
193
194
195
# File 'lib/git_stats/git_data/repo.rb', line 192

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

#authorsObject


46
47
48
49
50
# File 'lib/git_stats/git_data/repo.rb', line 46

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_memoizationObject


42
43
44
# File 'lib/git_stats/git_data/repo.rb', line 42

def command_memoization
  @command_memoization_map ||= {}
end

#command_parserObject


188
189
190
# File 'lib/git_stats/git_data/repo.rb', line 188

def command_parser
  @command_parser ||= CommandParser.new
end

#command_runnerObject


184
185
186
# File 'lib/git_stats/git_data/repo.rb', line 184

def command_runner
  @command_runner ||= CommandRunner.new
end

#comment_statsObject


152
153
154
# File 'lib/git_stats/git_data/repo.rb', line 152

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

#comment_stringObject


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

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
  @comment_count_each_day ||= Hash[commits.map { |commit|
    sum += commit.comment_stat.insertions
    sum -= commit.comment_stat.deletions
    [commit.date.to_date, sum]
  }].fill_empty_days!(aggregated: true)
end

#commit_rangeObject


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

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

#commitsObject


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

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

#commits_count_by_author(limit = 4) ⇒ Object


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

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

#commits_periodObject


64
65
66
# File 'lib/git_stats/git_data/repo.rb', line 64

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

#files_by_extension_by_dateObject


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/git_stats/git_data/repo.rb', line 102

def files_by_extension_by_date
  file_counts_by_date_by_extension = {}
  extensions_sums = {}
  commits.map do |commit|
    commit.files_by_extension_count.map do |ext, count|
      extensions_sums[ext] ||= 0;
      extensions_sums[ext] = count;
      file_counts_by_date_by_extension[ext] ||= {};
      file_counts_by_date_by_extension[ext][commit.date.to_date] = extensions_sums[ext]
    end
  end
  @multi_data_file_counts_by_date ||=  file_counts_by_date_by_extension.map { |ext, data|
    {
      name: ext || "NO EXTENSION",
      data: data.fill_empty_days!(aggregated:true)
      }
    }
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_each_day ||= Hash[commits.map { |commit|
    [commit.date.to_date, commit.files_count]
  }]
end

#first_commit_shaObject


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

def first_commit_sha
  @first_commit_sha
end

#last_commitObject


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

def last_commit
  commits.last
end

#last_commit_shaObject


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

def last_commit_sha
  @last_commit_sha ||= 'HEAD'
end

#lines_by_extension_by_dateObject


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/git_stats/git_data/repo.rb', line 121

def lines_by_extension_by_date
  lines_by_date_by_extension = {}
  extensions_sums = {}
  commits.map do |commit|
    commit.lines_by_extension.map do |ext, count|
      extensions_sums[ext] ||= 0;
      extensions_sums[ext] = count;
      lines_by_date_by_extension[ext] ||= {};
      lines_by_date_by_extension[ext][commit.date.to_date] = extensions_sums[ext]
    end
  end
  @multi_data_lines_by_date ||=  lines_by_date_by_extension.map { |ext, data|
    {
      name: ext || "NO EXTENSION",
      data: data.fill_empty_days!(aggregated:true)
      }
    }
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_each_day ||= Hash[commits.map { |commit|
    sum += commit.short_stat.insertions
    sum -= commit.short_stat.deletions
    [commit.date.to_date, sum]
  }]
end

#pathObject


18
19
20
# File 'lib/git_stats/git_data/repo.rb', line 18

def path
  @path ||= '.'
end

#project_nameObject


164
165
166
# File 'lib/git_stats/git_data/repo.rb', line 164

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

#project_versionObject


160
161
162
# File 'lib/git_stats/git_data/repo.rb', line 160

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

#run(command) ⇒ Object


168
169
170
171
172
173
174
175
176
177
# File 'lib/git_stats/git_data/repo.rb', line 168

def run(command)
  if (command_memoization[command])
    command_memoization[command]
  else
    result = command_runner.run(path, command)
    invoke_command_observers(command, result)
    command_memoization[command] = result
    result
  end
end

#run_and_parse(command) ⇒ Object


179
180
181
182
# File 'lib/git_stats/git_data/repo.rb', line 179

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

#short_statsObject


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

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

#to_sObject


197
198
199
# File 'lib/git_stats/git_data/repo.rb', line 197

def to_s
  "#{self.class} #@path"
end

#treeObject


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

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

#tree_pathObject


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

def tree_path
  @tree_path ||= '.'
end