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 =
/^\+(?!\+|\+)/
- REMOVED_LINE =
/^[-]/
- NOT_REMOVED_LINE =
/^[^-]/
- NO_NEWLINE_MESSAGE =
/^\\ No newline at end of file$/
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.
-
#removed_lines ⇒ Array<Line>
Removed lines.
Constructor Details
#initialize(body, options = {}) ⇒ Patch
Returns a new instance of Patch.
51 52 53 54 55 |
# File 'lib/git_diff_parser/patch.rb', line 51 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.
11 12 13 |
# File 'lib/git_diff_parser/patch.rb', line 11 def body @body end |
#file ⇒ String?
Returns file path or nil.
11 12 13 |
# File 'lib/git_diff_parser/patch.rb', line 11 def file @file end |
#secure_hash ⇒ String?
Returns target sha1 hash or nil.
11 12 13 |
# File 'lib/git_diff_parser/patch.rb', line 11 def secure_hash @secure_hash end |
Instance Method Details
#changed_line_numbers ⇒ Array<Integer>
Returns changed line numbers.
106 107 108 |
# File 'lib/git_diff_parser/patch.rb', line 106 def changed_line_numbers changed_lines.map(&:number) end |
#changed_lines ⇒ Array<Line>
Returns changed lines.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/git_diff_parser/patch.rb', line 58 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 NO_NEWLINE_MESSAGE # nop 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.
113 114 115 116 117 |
# File 'lib/git_diff_parser/patch.rb', line 113 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 |
#removed_lines ⇒ Array<Line>
Returns removed lines.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/git_diff_parser/patch.rb', line 84 def removed_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 REMOVED_LINE line = Line.new( content: content, number: line_number, patch_position: patch_position ) lines << line line_number += 1 end lines end end |