Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/word_diff/parser.rb')
-rw-r--r--lib/gitlab/word_diff/parser.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/gitlab/word_diff/parser.rb b/lib/gitlab/word_diff/parser.rb
new file mode 100644
index 00000000000..3b6d4d4d384
--- /dev/null
+++ b/lib/gitlab/word_diff/parser.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+# Converts git diff --word-diff=porcelain output to Gitlab::Diff::Line objects
+# see: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-porcelain
+module Gitlab
+ module WordDiff
+ class Parser
+ include Enumerable
+
+ def parse(lines, diff_file: nil)
+ return [] if lines.blank?
+
+ # By returning an Enumerator we make it possible to search for a single line (with #find)
+ # without having to instantiate all the others that come after it.
+ Enumerator.new do |yielder|
+ @chunks = ChunkCollection.new
+ @counter = PositionsCounter.new
+
+ lines.each do |line|
+ segment = LineProcessor.new(line).extract
+
+ case segment
+ when Segments::DiffHunk
+ next if segment.first_line?
+
+ counter.set_pos_num(old: segment.pos_old, new: segment.pos_new)
+
+ yielder << build_line(segment.to_s, 'match', parent_file: diff_file)
+
+ when Segments::Chunk
+ @chunks.add(segment)
+
+ when Segments::Newline
+ yielder << build_line(@chunks.content, nil, parent_file: diff_file)
+
+ @chunks.reset
+ counter.increase_pos_num
+ end
+ end
+ end
+ end
+
+ private
+
+ attr_reader :counter
+
+ def build_line(content, type, options = {})
+ Gitlab::Diff::Line.new(
+ content, type,
+ counter.line_obj_index, counter.pos_old, counter.pos_new,
+ **options).tap do
+ counter.increase_obj_index
+ end
+ end
+ end
+ end
+end