Class: GitDiffParser::Patch
- Inherits:
-
Object
- Object
- GitDiffParser::Patch
- Defined in:
- lib/git_diff_parser/patch.rb
Overview
Parsed patch
Constant Summary collapse
- RANGE_INFORMATION_LINE =
/^@@ .+\+(?<line_number>\d+),/
- MODIFIED_LINE =
/^\+(?!\+|\+)/
- NOT_REMOVED_LINE =
/^[^-]/
Instance Attribute Summary collapse
-
#body ⇒ String?
Patch section in ‘git diff` or nil.
-
#file ⇒ String?
File path or nil.
-
#secure_hash ⇒ String?
Target sha1 hash or nil.
Instance Method Summary collapse
-
#changed_line_numbers ⇒ Array<Integer>
Changed line numbers.
-
#changed_lines ⇒ Array<Line>
Changed lines.
-
#find_patch_position_by_line_number(line_number) ⇒ Integer?
Patch position.
-
#initialize(body, options = {}) ⇒ Patch
constructor
A new instance of Patch.
Constructor Details
#initialize(body, options = {}) ⇒ Patch
Returns a new instance of Patch.
49 50 51 52 53 |
# File 'lib/git_diff_parser/patch.rb', line 49 def initialize(body, = {}) @body = body || '' @file = [:file] || ['file'] if [:file] || ['file'] @secure_hash = [:secure_hash] || ['secure_hash'] if [:secure_hash] || ['secure_hash'] end |
Instance Attribute Details
#body ⇒ String?
Returns patch section in ‘git diff` or nil.
9 10 11 |
# File 'lib/git_diff_parser/patch.rb', line 9 def body @body end |
#file ⇒ String?
Returns file path or nil.
9 10 11 |
# File 'lib/git_diff_parser/patch.rb', line 9 def file @file end |
#secure_hash ⇒ String?
Returns target sha1 hash or nil.
9 10 11 |
# File 'lib/git_diff_parser/patch.rb', line 9 def secure_hash @secure_hash end |
Instance Method Details
#changed_line_numbers ⇒ Array<Integer>
Returns changed line numbers.
80 81 82 |
# File 'lib/git_diff_parser/patch.rb', line 80 def changed_line_numbers changed_lines.map(&:number) end |
#changed_lines ⇒ Array<Line>
Returns changed lines.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/git_diff_parser/patch.rb', line 56 def changed_lines line_number = 0 lines.each_with_index.inject([]) do |lines, (content, patch_position)| case content when RANGE_INFORMATION_LINE line_number = Regexp.last_match[:line_number].to_i when MODIFIED_LINE line = Line.new( content: content, number: line_number, patch_position: patch_position ) lines << line line_number += 1 when NOT_REMOVED_LINE line_number += 1 end lines end end |
#find_patch_position_by_line_number(line_number) ⇒ Integer?
Returns patch position.
87 88 89 90 91 |
# File 'lib/git_diff_parser/patch.rb', line 87 def find_patch_position_by_line_number(line_number) target = changed_lines.find { |line| line.number == line_number } return nil unless target target.patch_position end |