lib/idonethis/use_cases/git.rb in idonethis-cli-0.19.1 vs lib/idonethis/use_cases/git.rb in idonethis-cli-0.20.0

- old
+ new

@@ -6,45 +6,68 @@ def apply(args={}) log = args[:log] || fail("You need to supply :log adapter") git = args[:git] || fail("You need to supply :git adapter") view = args[:view] || fail("You need to supply :view adapter") fs = args[:fs] || fail("You need to supply :fs adapter") - since = args[:since] || 'today' + since = Internal::Dates.from(args[:since]) log.call args opts = args[:opts] || [] dir = opts.any? ? File.expand_path(opts.first) : File.expand_path(".") - dirs = dir - - if dir == FileUtils.pwd - view.call "Scanning the current directory <#{dir}>\n\n" - else - dirs = fs.modified_today?(dir).select{|dir| git.repo?(dir) } + dirs = fs.modified({since: since, dir: dir, log: log}).select{|dir| git.repo?(dir) } + + view.call "Scanning dir <#{dir}>, which has <#{dirs.size}> repositories that have changed since <#{since}>\n\n" - view.call "Scanning dir <#{dir}>, which has <#{dirs.size}> repositories that have changed\n\n" - end - - view.call summarise(git, view, since, *dirs) + view.call summarise(git, view, since, log, *dirs) view.call "" end - def summarise(git, view, since, *dirs) + def summarise(git, view, since, log, *dirs) dirs.map do |dir| - commits = git.commits(dir, since).map{|it| %Q{[#{date_from(it)}] (#{it.author.name}) #{it.message}}} - %Q{#{Pathname.new(dir).basename} (#{commits.size}):\n\n-- #{commits.join("\n-- ")}} + begin + commits = git.commits({dir: dir, since: since, log: log}).map{|it| %Q{[#{date_from(it)}] (#{it.author.name}) #{it.message}}} + %Q{#{Pathname.new(dir).basename} (#{commits.size}):\n\n-- #{commits.join("\n-- ")}} + rescue Exception => e + "An error occured trying to query git repo at <#{dir}>" + end end.join "\n\n" end def date_from(commit) return commit.date.strftime("%d %b, %H:%M") unless today?(commit.date, Time.now) commit.date.strftime("%H:%M") end def today?(time, now) time.year == now.year && time.month == now.month && time.day == now.day + end + end + end + + module Internal + module Dates + class << self + def from(text) + case text + when nil + Date.today + when 'yesterday' + (Date.today - 1) + when 'today' + Date.today + when 'one-week-ago' + (Date.today - 7) + else + begin + Date.parse(text) + rescue + Date.today + end + end + end end end end end