Module: GitDossier

Defined in:
lib/git_dossier.rb,
lib/git_dossier/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.generate(branch = "master") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git_dossier.rb', line 19

def self.generate(branch = "master")
	repo_name = %x[git remote -v | head -n1 | awk '{print $2}' | sed -e 's,.*:\(.*/\)\?,,' -e 's/\.git$//']

	project_name = repo_name.lines.first.split('/').pop

	response = %x[git rev-parse --verify --quiet #{branch}]

	if (response.lines.first =~ /\b([a-f0-9]{40})\b/) == 0
		response = %x[git rev-list --pretty=format:'%H|%ai|%an|%aE|%s' #{branch} | grep -v commit]

		commits = [ ]
		authors = [ ]

		response.lines.map do |line|
			sha, timestamp, author_name, author_email, subject = line.split('|').map(&:strip)

			commits.push({
				sha: sha,
				timestamp: DateTime.parse(timestamp),
				subject: subject.strip,
				author: {
					name: author_name,
					email: author_email
				}
			})

			if authors.any? {|a| a[:email] == author_email}
				authors.each do |a|
					if a[:email] == author_email
						a[:number_of_commits] = a[:number_of_commits] + 1
					end
				end
			else
				authors.push({
					name: author_name,
					email: author_email,
					gravatar: Digest::MD5.hexdigest(author_email.downcase),
					number_of_commits: 1,
					percentage_of_commits: 0
				})
			end
		end

		normalization_bound = 0.0

		authors.each do |a|
			a[:percentage_of_commits] = a[:number_of_commits].to_f / commits.count

			if a[:percentage_of_commits] > normalization_bound
				normalization_bound = a[:percentage_of_commits]
			end
		end

		authors.each do |a|
			if a[:percentage_of_commits] == normalization_bound
				a[:percentage_normalized] = 10
			else
				a[:percentage_normalized] = (((a[:percentage_of_commits] / normalization_bound) * 10) % 10).to_i
			end
		end

		Dir.mkdir('dossier') unless Dir.exist?('dossier')
		Dir.mkdir('dossier/assets') unless Dir.exist?('dossier/assets')

		File.open("dossier/index.html", 'w') { |f|
			@project_name = project_name
			@commits = commits
			@authors = authors.sort_by { |a| a[:number_of_commits] }.reverse

			erb = ERB.new(File.open("#{__dir__}/../app/views/index.html.erb").read)

			f.write(erb.result binding)
		}

		File.open("dossier/assets/dossier.min.css", 'w') { |f|
			engine = Sass::Engine.new(File.open("#{__dir__}/../app/assets/stylesheets/dossier.scss").read, :syntax => :scss, :load_paths => ["#{__dir__}/../app/assets/stylesheets/vendor"])

			f.write(CSSminify.compress(engine.render))
		}

		FileUtils.copy_entry "#{__dir__}/../app", "dossier/"
		
	else
		puts "\e[31mThere is no branch called '#{branch}'.\e[0m"
	end
end