Class: Jekyll::GitMetadata::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll/git_metadata/generator.rb

Instance Method Summary collapse

Instance Method Details

#authors(file = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll/git_metadata/generator.rb', line 52

def authors(file = nil)
  results = []
  cmd = 'git shortlog -se'
  cmd << " -- #{file}" if file
  result = %x{#{cmd}}
  result.lines.each do |line|
    commits, name, email = line.scan(/(.*)\t(.*)<(.*)>/).first.map(&:strip)
    results << { 'commits' => commits.to_i, 'name' => name, 'email' => email }
  end
  results
end

#commit(sha) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll/git_metadata/generator.rb', line 75

def commit(sha)
  result = %x{git show --format=fuller -q #{sha}}
  author_name, author_email, author_date, commit_name, commit_email, commit_date, message = result
    .scan(/.*Author:(.*)<(.*)>\nAuthorDate:(.*)\nCommit:(.*)<(.*)>\nCommitDate:(.*)\n\n(.*)/)
    .first
    .map(&:strip)
  {
    'author_name' => author_name,
    'author_email' => author_email,
    'author_date' => author_date,
    'commit_name' => commit_name,
    'commit_date' => commit_date,
    'message' => message
  }
end

#files_countObject



95
96
97
# File 'lib/jekyll/git_metadata/generator.rb', line 95

def files_count
  %x{ git ls-tree -r HEAD | wc -l }.strip.to_i
end

#generate(site) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll/git_metadata/generator.rb', line 9

def generate(site)
  raise "Git is not installed" unless git_installed?

  Dir.chdir(site.source) do
    site.config['git'] = site_data
    site.pages.each do |page|
      page.data['git'] = page_data(page.relative_path)
    end
    site.posts.each do |page|
      page.data['git'] = page_data(page.relative_path)
    end
  end
  
end

#git_installed?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/jekyll/git_metadata/generator.rb', line 99

def git_installed?
  void = RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw/ ? 'NUL' : '/dev/null'
  system "git --version >>#{void} 2>&1"
end

#lines(file = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll/git_metadata/generator.rb', line 64

def lines(file = nil)
  cmd = "git log --numstat --format='%h'"
  cmd << " -- #{file}" if file
  result = %x{#{cmd}}
  results = result.scan(/(.*)\n\n(.*)\t(.*)\t(.*)\n/)
  results.map { |a| a[1] = a[1].to_i; a[2] = a[2].to_i }
  results.map do |line|
    { 'sha' => line[0], 'additions' => line[1], 'subtractions' => line[2], 'file' => line[3] }
  end
end

#page_data(relative_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll/git_metadata/generator.rb', line 39

def page_data(relative_path)
  authors = self.authors(relative_path)
  lines = self.lines(relative_path)
  {
    'authors' => authors,
    'total_commits' => authors.inject(0) { |sum, h| sum += h['commits'] },
    'total_additions' => lines.inject(0) { |sum, h| sum += h['additions'] },
    'total_subtractions' => lines.inject(0) { |sum, h| sum += h['subtractions'] },
    'first_commit' => commit(lines.last['sha']),
    'last_commit' => commit(lines.first['sha'])
  }
end

#project_nameObject



91
92
93
# File 'lib/jekyll/git_metadata/generator.rb', line 91

def project_name
  File.basename(%x{git rev-parse --show-toplevel}.strip)
end

#site_dataObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll/git_metadata/generator.rb', line 24

def site_data
  authors = self.authors
  lines = self.lines
  {
    'project_name' => project_name,
    'files_count' => files_count,
    'authors' => authors,
    'total_commits' => authors.inject(0) { |sum, h| sum += h['commits'] },
    'total_additions' => lines.inject(0) { |sum, h| sum += h['additions'] },
    'total_subtractions' => lines.inject(0) { |sum, h| sum += h['subtractions'] },
    'first_commit' => commit(lines.last['sha']),
    'last_commit' => commit(lines.first['sha'])
  }
end