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



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

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

#activityObject



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

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

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



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

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

#authorsObject



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

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



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

def command_parser
  @command_parser ||= CommandParser.new
end

#command_runnerObject



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

def command_runner
  @command_runner ||= CommandRunner.new
end

#comment_statsObject



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

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



89
90
91
92
93
94
95
96
# File 'lib/git_stats/git_data/repo.rb', line 89

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



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

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

#commitsObject



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

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



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

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



60
61
62
# File 'lib/git_stats/git_data/repo.rb', line 60

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

#files_count_by_dateObject



74
75
76
77
78
# File 'lib/git_stats/git_data/repo.rb', line 74

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



98
99
100
# File 'lib/git_stats/git_data/repo.rb', line 98

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_count_by_dateObject



80
81
82
83
84
85
86
87
# File 'lib/git_stats/git_data/repo.rb', line 80

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



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

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



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

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

#run(command) ⇒ Object



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

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

#run_and_parse(command) ⇒ Object



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

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

#short_statsObject



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

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

#to_sObject



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

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