Class: Repoman::DiffParse

Inherits:
Object
  • Object
show all
Defined in:
lib/repoman/diff_parse.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo, file, branch = 'master') ⇒ DiffParse

Returns a new instance of DiffParse.



4
5
6
7
8
9
# File 'lib/repoman/diff_parse.rb', line 4

def initialize(repo, file, branch = 'master')
  @repo        = Grit::Repo.new(repo)
  @rugged_repo = Rugged::Repository.new(repo)
  @file        = file
  @branch      = branch
end

Instance Method Details

#info_and_diff(commit) ⇒ Object



30
31
32
33
# File 'lib/repoman/diff_parse.rb', line 30

def info_and_diff(commit)
  commit.match(/(^commit .+)(?:\n)(diff .+)/m)[1..2]
  [Grit::Commit.list_from_string(@repo, $1).first, Grit::Diff.list_from_string(@repo, $2).first]
end

#logObject



11
12
13
# File 'lib/repoman/diff_parse.rb', line 11

def log
  @repo.git.log({ :pretty => 'raw' }, '--follow', '--topo-order', '-p', '-U1', @branch, "--", @file)
end

#parseObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/repoman/diff_parse.rb', line 15

def parse
  split_commits(log).map do |commit|
    info, diff = info_and_diff(commit)
    {
      info: info.to_hash,
      file: @rugged_repo.file_at(info.to_s, @file).split(/\n/),
      diff_parts: split_diff(diff.diff)
    }
  end.reverse
end

#split_commits(log) ⇒ Object



26
27
28
# File 'lib/repoman/diff_parse.rb', line 26

def split_commits(log)
  log.split(/\ncommit /).map { |commit| "commit #{commit}" }
end

#split_diff(diff) ⇒ Object



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

def split_diff(diff)
  header_matcher = /(@@ [-+](?:\d+)(?:,(?:\d+))? [-+](?:\d+)(?:,(?:\d+))? @@)/
  diff.split(header_matcher).drop(1).each_slice(2).map do |part|
    hunk= {
      start_line: part[0].match(/\b(\d+),\d+/)[1].to_i,
      number_of_lines: part[0].match(/\b\d+,(\d+)/)[1].to_i,
    }
    hunk[:lines] = part[1].split(/\n/).map do |line|
      next if line.length == 0
      {
        type: line_type(line)
      }
    end.compact
    hunk
  end
end