module GitDossier

Constants

VERSION

Public Class Methods

generate(branch = "master") click to toggle source
# 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