module Idonethis::UseCases module Git class << self def apply(credential, args={}) log = args[:log] || fail("You need to supply :log adapter") log.call args opts = args[:opts] dir = opts.any? ? File.expand_path(opts.first) : File.expand_path(".") if opts.any? all_dirs = directories_in(dir) log.call("All directories in <#{dir}>: #{all_dirs}") dirs_that_have_changed_today = all_dirs.select{|it| modified_today?(it, log) } puts "Scanning dir <#{dir}>, which has <#{dirs_that_have_changed_today.size}> repositories that have changed today\n\n" summarise *dirs_that_have_changed_today puts "" else puts "Scanning the current directory <#{dir}>\n\n" summarise dir end end def summarise(*dirs) dirs.each do |dir| summary = commit_summary(dir) puts %Q{#{Pathname.new(dir).basename} (#{summary.size}):\n-- #{summary.join("\n-- ")}} end end def directories_in(dir) Dir.entries(dir). reject{|it| ["..", "."].include?(it) }. select{|it| File.directory?(File.join(dir, it))}. map{ |it| File.expand_path(File.join(dir, it))} end def modified_today?(dir, log) time = File.ctime(dir) now = Time.now any_file_changed_today?(dir, time, now, log).tap do |result| log.call "[#{dir}] Comparing mtime <#{time}> to today <#{now}>. Today? <#{result}>" end end def any_file_changed_today?(dir, time, now, log) Dir["#{dir}/**/**"].select do |file| mtime = File.ctime(file) log.call "File <#{file}> has mtime <#{mtime}>" return true if today?(mtime, now) end return false end def today?(time, now) time.year == now.year && time.month == now.month && time.day == now.day end def commit_summary(dir) require 'git' git = ::Git.open(dir) git.log.since('1am').map{|it| %Q{[#{it.date.strftime("%H:%M")}] #{it.message}}} end end end end