module HappyCommit module OnCommit def self.included(base) base.class_eval do base.extend ClassMethods end end module ClassMethods def on_commit order, commits = self.commits pos = order.index(current_name)+1 if pos == 1 puts ">> Congrats!! you are the most committer of all time with #{commits[current_name]} commits!" elsif pos == 2 puts ">> You need #{commits[order.first] - commits[current_name]} commits to reach #{order.first}(the champion)" end order, commits = self.commits_today pos = order.index(current_name)+1 if pos == 1 puts ">> Congrats!! you are the most committer today with #{commits[current_name]} commits!" elsif pos == 2 puts ">> You need #{commits[order.first] - commits[current_name]} commits to reach #{order.first}(the champion)" elsif pos puts ">> You have committed #{commits[current_name]} times today, congrats!" end end def current_name @current_name||=`git config user.name`.strip end def commits commits = {} order = [] count = 0 `git shortlog -s -n -m HEAD`.each_line do |line| if line =~ /\s+(\d+)\s+(.+)$/ name = $2.strip commits[name] = $1.to_i order << name count += 1 break if count >= 5 end end [order, commits] end def commits_today hours = Time.now.strftime("%k").to_i-5 order = [] count = 0 commits = {} `git shortlog -s -n -m --since="#{hours} hours ago" HEAD`.each_line do |line| if line =~ /\s+(\d+)\s+(.+)$/ name = $2.strip commits[name] = $1.to_i order << name count += 1 break if count >= 5 end end [order, commits] end end end end