Class: Arb::Cli::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/arb/cli/shared/git.rb

Class Method Summary collapse

Class Method Details

.commit!(filenames:, message:) ⇒ Object



92
93
94
95
# File 'lib/arb/cli/shared/git.rb', line 92

def self.commit!(filenames:, message:)
  `git add #{filenames.join(" ")}`
  `git commit -m "#{message}"`
end

.commit_all!(message:) ⇒ Object



97
98
99
100
# File 'lib/arb/cli/shared/git.rb', line 97

def self.commit_all!(message:)
  `git add -A`
  `git commit -m "#{message}"`
end

.commit_countInteger

Returns:

  • (Integer)


48
49
50
# File 'lib/arb/cli/shared/git.rb', line 48

def self.commit_count
  `git rev-list HEAD --count`.to_i
end

.committed_by_yearObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arb/cli/shared/git.rb', line 52

def self.committed_by_year
  committed_solution_files = `git log --diff-filter=A --name-only --pretty=format: src`

  previous_days = (2015..Date.today.year - 1).map { [it, (1..25).to_a] }.to_h
  if Date.today.month == 12
    previous_days[Date.today.year] = (1..Date.today.day)
  end

  committed_days = committed_solution_files
    .split("\n")
    .reject(&:empty?)
    .map {
      match = it.match(/(?<year>\d{4})\/(?<day>\d\d).rb$/)
      year, day = match[:year], match[:day]
      [year.to_i, day.to_i]
    }
    .group_by(&:first)
    .transform_values { it.map(&:last) }

  previous_days.map { |year, days|
    days_hash = days.map { |day|
      [
        day.to_s.rjust(2, "0"),
        committed_days.has_key?(year) && committed_days[year].include?(day)
      ]
    }.to_h

    [year.to_s, days_hash]
  }.to_h
end

.init!Object



88
89
90
# File 'lib/arb/cli/shared/git.rb', line 88

def self.init!
  `git init`
end

.last_committed_puzzle(year: nil, exclude_year: nil) ⇒ Array(String, String)?

Year and day of the latest-date solution in the most recent commit, or nil.

Returns:

  • (Array(String, String), nil)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/arb/cli/shared/git.rb', line 33

def self.last_committed_puzzle(year: nil, exclude_year: nil)
  if exclude_year
    output = `git log -n 1 --diff-filter=A --name-only --pretty=format: -- src spec ':!src/#{exclude_year}' ':!spec/#{exclude_year}'`
  elsif year && !Dir.exist?(File.join("src", year))
    return nil
  else
    output = `git log -n 1 --diff-filter=A --name-only --pretty=format: #{File.join("src", year || "")} #{File.join("spec", year || "")}`
  end

  return nil if output.empty?

  output.scan(/(?<year>\d{4})\/(?<day>\d\d)(?:_spec)?.rb$/).last
end

.modified_puzzlesHash{Array(String, String), Array<String>}

A hash whose keys are [“<year>”, “<day>”] of modified existing solutions or specs, and whose values are the names of the modified files (solution, spec, or both).

Returns:

  • (Hash{Array(String, String), Array<String>})


21
22
23
24
25
26
27
28
29
# File 'lib/arb/cli/shared/git.rb', line 21

def self.modified_puzzles
  output = `git status -su | grep -e "^ M src" -e "^ M spec" -e "^M  src" -e "^M  spec"`
  filenames = output.scan(/(?:src|spec)\/\d{4}\/\d\d(?:_spec)?.rb/).uniq
  filenames.group_by { |filename|
    filename
      .match(/(?<year>\d{4})\/(?<day>\d\d)(?:_spec)?.rb$/)
      .captures
  }
end

.repo_exists?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/arb/cli/shared/git.rb', line 84

def self.repo_exists?
  `git rev-parse --is-inside-work-tree 2> /dev/null`.chomp == "true"
end

.uncommitted_puzzlesArray<Array(String, String)>

A hash whose keys are [“<year>”, “<day>”] of new solutions or specs, and and whose values are the names of the modified files (solution, spec, or both).

Returns:

  • (Array<Array(String, String)>)


7
8
9
10
11
12
13
14
15
# File 'lib/arb/cli/shared/git.rb', line 7

def self.uncommitted_puzzles
  output = `git status -su | grep -e "^?? src" -e "^?? spec" -e "^A  src" -e "^A  spec"`
  filenames = output.scan(/(?:src|spec)\/\d{4}\/\d\d(?:_spec)?.rb/).uniq
  filenames.group_by { |filename|
    filename
      .match(/(?<year>\d{4})\/(?<day>\d\d)(?:_spec)?.rb$/)
      .captures
  }
end