lib/idonethis/use_cases/git.rb in idonethis-cli-0.5.0 vs lib/idonethis/use_cases/git.rb in idonethis-cli-0.6.0

- old
+ new

@@ -2,34 +2,73 @@ module Git class << self def apply(credential, args={}) log = args[:log] || fail("You need to supply :log adapter") - dir = File.expand_path("..") + log.call args - all_dirs = Dir.entries(dir).reject{|it| ["..", "."].include?(it) }.select{|it| File.directory?(File.join(dir, it))}.map{|it| File.join(dir, it)} + opts = args[:opts] + + dir = opts.any? ? File.expand_path(opts.first) : File.expand_path(".") - dirs_that_have_changed_today = all_dirs.select{|it| today?(File.mtime(it)) } + 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\n\n" + puts "Scanning dir <#{dir}>, which has <#{dirs_that_have_changed_today.size}> repositories that have changed today\n\n" - dirs_that_have_changed_today.each do |dir| + 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-- ")}"} + puts %Q{#{Pathname.new(dir).basename} (#{summary.size}):\n-- #{summary.join("\n-- ")}} end + end - puts "" + 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 today?(time) + 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').take(10).map{|it| %Q{[#{it.date.strftime("%H:%M")}] #{it.message}}} + git.log.since('1am').map{|it| %Q{[#{it.date.strftime("%H:%M")}] #{it.message}}} end end end end