Class: Flatito::DiffParser
- Inherits:
-
Object
- Object
- Flatito::DiffParser
- Defined in:
- lib/flatito/diff_parser.rb
Defined Under Namespace
Constant Summary collapse
- HUNK_HEADER =
/\A@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/- INDEX_LINE =
/\Aindex ([0-9a-f]+)\.\.([0-9a-f]+)/
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(content) ⇒ DiffParser
constructor
A new instance of DiffParser.
- #parse ⇒ Object
Constructor Details
#initialize(content) ⇒ DiffParser
Returns a new instance of DiffParser.
32 33 34 35 |
# File 'lib/flatito/diff_parser.rb', line 32 def initialize(content) @lines = content.lines @i = 0 end |
Class Method Details
.diff?(content) ⇒ Boolean
24 25 26 27 28 29 30 |
# File 'lib/flatito/diff_parser.rb', line 24 def self.diff?(content) return false if content.nil? || content.empty? head = content.byteslice(0, 4096).to_s head.start_with?("diff --git ") || (head.include?("\n--- ") && head.include?("\n+++ ") && head.include?("\n@@ ")) end |
.parse(content) ⇒ Object
20 21 22 |
# File 'lib/flatito/diff_parser.rb', line 20 def self.parse(content) new(content).parse end |
Instance Method Details
#parse ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/flatito/diff_parser.rb', line 37 def parse files = [] current = nil while @i < @lines.size line = @lines[@i] if line.start_with?("diff --git ") files << current if current && !current.path.nil? current = new_file_record @i += 1 next end unless current @i += 1 next end if (m = line.match(INDEX_LINE)) current.before_blob = m[1] current.after_blob = m[2] elsif line.start_with?("new file mode") current.new_file = true elsif line.start_with?("deleted file mode") current.deleted_file = true elsif line.start_with?("--- ") handle_minus_header(line, current) elsif line.start_with?("+++ ") handle_plus_header(line, current) elsif (m = line.match(HUNK_HEADER)) @i += 1 consume_hunk(current, m) next end @i += 1 end files << current if current && !current.path.nil? files end |