Module: Idonethis::UseCases::Git

Defined in:
lib/idonethis/use_cases/git.rb

Class Method Summary collapse

Class Method Details

.any_file_changed_today?(dir, time, now, log) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/idonethis/use_cases/git.rb', line 54

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

.apply(credential, args = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/idonethis/use_cases/git.rb', line 4

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

.commit_summary(dir) ⇒ Object



67
68
69
70
71
# File 'lib/idonethis/use_cases/git.rb', line 67

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

.directories_in(dir) ⇒ Object



38
39
40
41
42
43
# File 'lib/idonethis/use_cases/git.rb', line 38

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

.modified_today?(dir, log) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/idonethis/use_cases/git.rb', line 45

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

.summarise(*dirs) ⇒ Object



31
32
33
34
35
36
# File 'lib/idonethis/use_cases/git.rb', line 31

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

.today?(time, now) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/idonethis/use_cases/git.rb', line 63

def today?(time, now)
  time.year == now.year && time.month == now.month && time.day == now.day
end