Class: GitStats::GitData::Commit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashInitializable

#initialize

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



9
10
11
# File 'lib/git_stats/git_data/commit.rb', line 9

def author
  @author
end

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/git_stats/git_data/commit.rb', line 9

def date
  @date
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/git_stats/git_data/commit.rb', line 9

def repo
  @repo
end

#shaObject (readonly)

Returns the value of attribute sha.



9
10
11
# File 'lib/git_stats/git_data/commit.rb', line 9

def sha
  @sha
end

#stampObject (readonly)

Returns the value of attribute stamp.



9
10
11
# File 'lib/git_stats/git_data/commit.rb', line 9

def stamp
  @stamp
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  [self.repo, self.sha, self.stamp, self.date, self.author] ==
      [other.repo, other.sha, other.stamp, other.date, other.author]
end

#binary_filesObject



17
18
19
# File 'lib/git_stats/git_data/commit.rb', line 17

def binary_files
  @binary_files ||= files.select { |f| f.binary? }
end

#comment_statObject



53
54
55
# File 'lib/git_stats/git_data/commit.rb', line 53

def comment_stat
  @comment_stat ||= CommentStat.new(self)
end

#filesObject



11
12
13
14
15
# File 'lib/git_stats/git_data/commit.rb', line 11

def files
  @files ||= repo.run_and_parse("git ls-tree -r #{self.sha} -- #{repo.tree_path}").map do |file|
    Blob.new(repo: repo, filename: file[:filename], sha: file[:sha])
  end
end

#files_by_extensionObject



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

def files_by_extension
  @files_by_extension ||= files.inject({}) { |acc, f| acc[f.extension] ||= []; acc[f.extension] << f; acc }
end

#files_by_extension_countObject



29
30
31
# File 'lib/git_stats/git_data/commit.rb', line 29

def files_by_extension_count
  @files_by_extension_count ||= Hash[files_by_extension.map { |ext, files| [ext, files.count] }]
end

#files_countObject



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

def files_count
  @files_count ||= repo.run("git ls-tree -r --name-only #{self.sha} -- #{repo.tree_path}| wc -l").to_i
end

#lines_by_extensionObject



33
34
35
36
37
# File 'lib/git_stats/git_data/commit.rb', line 33

def lines_by_extension
  @lines_by_extension ||= Hash[files_by_extension.map { |ext, files|
    [ext, files.map(&:lines_count).sum]
  }.delete_if { |ext, lines_count| lines_count == 0 }]
end

#lines_countObject



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

def lines_count
  @lines_count ||= repo.run("git diff --shortstat `git hash-object -t tree /dev/null` #{self.sha} -- #{repo.tree_path}").lines.map do |line|
    line[/(\d+) insertions?/, 1].to_i
  end.sum
end

#short_statObject



49
50
51
# File 'lib/git_stats/git_data/commit.rb', line 49

def short_stat
  @short_stat ||= ShortStat.new(self)
end

#text_filesObject



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

def text_files
  @text_files ||= files - binary_files
end

#to_sObject



57
58
59
# File 'lib/git_stats/git_data/commit.rb', line 57

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