Class: Octostat::Git

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/octostat/git.rb

Constant Summary collapse

LOG_FORMAT =
<<~FORMAT.strip
  %h
  %ae
  %an
  %aI
  %P
  %s
FORMAT
ENTRY_LENGTH =
LOG_FORMAT.lines.size
LIST_COMMAND =
["git", "log", "--pretty=format:#{LOG_FORMAT}"]
COUNT_COMMAND =
["git", "rev-list", "--count", "HEAD"]
CLONE_COMMAND =
["git", "clone"]

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Git

Returns a new instance of Git.



23
24
25
# File 'lib/octostat/git.rb', line 23

def initialize path
  @path = Dir.exist?(path) ? path : clone_repo(path)
end

Instance Method Details

#countObject



31
32
33
# File 'lib/octostat/git.rb', line 31

def count
  @count ||= Open3.capture2(*COUNT_COMMAND, chdir: path).first.to_i
end

#eachObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/octostat/git.rb', line 35

def each
  return enum_for(:each) unless block_given?
  Open3.popen2e(*LIST_COMMAND, chdir: path) do |input, output, wait_thr|
    output.each_slice(ENTRY_LENGTH) do |commit|
      commit.each(&:strip!)
      hash, email, name, date, parents, subject = commit
      merge_commit = parents.split(" ").size > 1
      yield({
        hash:,
        email:,
        name:,
        date:,
        merge_commit:,
        subject:
      })
    end
  end
end

#envObject



27
28
29
# File 'lib/octostat/git.rb', line 27

def env
  {"GIT_DIR" => path}
end