Class: Obst::GitLog

Inherits:
Object
  • Object
show all
Defined in:
lib/obst/git_log.rb

Defined Under Namespace

Classes: Commit

Constant Summary collapse

EMPTY_LINE =
"\n"

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ GitLog

Returns a new instance of GitLog.



5
6
7
8
9
10
11
# File 'lib/obst/git_log.rb', line 5

def initialize(**opts)
  path = opts[:C] || '.'
  @cmd = ['git', '-C', path, 'log', '--name-status', '--pretty=format:%ad', "--date=format:'%Y-%m-%dT%H:%M:%S'"]
  @cmd << '--after' << opts[:after] if opts[:after]
  @cmd << '--before' << opts[:before] if opts[:before]
  Array(opts[:pathspec]).each{ |s| @cmd << s }
end

Instance Method Details

#commitsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/obst/git_log.rb', line 50

def commits
  Enumerator.new do |y|
    batch = []
    Open3.popen2(*@cmd) do |stdin, stdout, status_thread|
      stdout.each_line do |line|
        next batch << line unless line == EMPTY_LINE
        y << Commit.new(batch)
        batch.clear
      end
      raise 'fail to loop git log' unless status_thread.value.success?
    end
    y << Commit.new(batch)
  rescue => e
    puts @cmd
    raise e
  end
end

#to_sObject



13
14
15
# File 'lib/obst/git_log.rb', line 13

def to_s
  `#{@cmd.join(' ')}`
end