Class: MetaCommit::Message::Commands::DiffIndexExaminer

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_commit/message/commands/diff_index_examiner.rb

Instance Method Summary collapse

Constructor Details

#initialize(parse_command, ast_path_factory, diff_factory) ⇒ DiffIndexExaminer

Returns a new instance of DiffIndexExaminer.



7
8
9
10
11
# File 'lib/meta_commit/message/commands/diff_index_examiner.rb', line 7

def initialize(parse_command, ast_path_factory, diff_factory)
  @parse_command = parse_command
  @ast_path_factory = ast_path_factory
  @diff_factory = diff_factory
end

Instance Method Details

#index_meta(repo) ⇒ Array<MetaCommit::Contracts::Diff>

Creates diff objects with meta information of changes in index staged files

Parameters:

Returns:

  • (Array<MetaCommit::Contracts::Diff>)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/meta_commit/message/commands/diff_index_examiner.rb', line 16

def index_meta(repo)
  diffs = MetaCommit::Models::Changes::Commit.new(repo.last_commit_oid, 'staged')
  repo.index_diff_with_optimized_lines do |old_file_path, new_file_path, patch, line|
    commit_id_old = repo.last_commit_oid
    commit_id_new = 'staged'

    old_file_content = repo.get_blob_at(commit_id_old, old_file_path, '')
    new_file_content = repo.get_content_of(new_file_path, '')

    old_file_ast = @parse_command.execute(old_file_path, old_file_content)
    next if old_file_ast.nil?
    new_file_ast = @parse_command.execute(new_file_path, new_file_content)
    next if new_file_ast.nil?

    old_ast_path = @ast_path_factory.create_ast_path(old_file_ast, line.old_lineno)
    new_ast_path = @ast_path_factory.create_ast_path(new_file_ast, line.new_lineno)

    created_diff = @diff_factory.create_diff_of_type(line.line_origin, {
        :line => line,
        :commit_id_old => commit_id_old,
        :commit_id_new => commit_id_new,
        :old_ast_path => old_ast_path,
        :new_ast_path => new_ast_path,
        :old_file_path => old_file_path,
        :new_file_path => new_file_path,
    })
    diffs.push(created_diff) unless created_diff.nil?
  end
  diffs
end