class DocumentGenerator::DiffFile
Attributes
git_diff_file[RW]
Public Class Methods
new(git_diff_file)
click to toggle source
# File lib/document_generator/diff_file.rb, line 11 def initialize(git_diff_file) @git_diff_file = git_diff_file end
Public Instance Methods
action_type()
click to toggle source
# File lib/document_generator/diff_file.rb, line 84 def action_type { new: 'Create file', modified: 'Update file', deleted: 'Remove file' }.fetch(type.to_sym, type) end
content()
click to toggle source
# File lib/document_generator/diff_file.rb, line 29 def content if type == 'deleted' return "####{patch_heading}\n\n" end temp = [] temp << "####{patch_heading}" outputs = markdown_outputs if outputs.any? outputs.each do |output| if output.escaped_content.length > 0 temp << "\n\n" temp << "#####{output.description}" temp << "\n```\n" if output.description == "Becomes" temp << output.content.join("\n") + "\n" else temp << output.escaped_content end temp << "\n```\n" end end end temp << "\n\n" temp.join end
ending_code()
click to toggle source
# File lib/document_generator/diff_file.rb, line 60 def ending_code # The escaped end result code for the whole diff file returned as a string clean_hunks = [] git_diff_file_hunks.each do |hunk| clean_hunks << ending_code_for(hunk).join("\n") end clean_hunks.join("\n") end
ending_code_for(hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 68 def ending_code_for(hunk) # The unescaped end result code for a particular hunk returned as array clean_lines = [] git_diff_lines_for(hunk).each_with_index do |line, index| if (line[0]) == "-" || ignore_line?(line) next end if (line[0]) == "+" line = remove_first_character(line) end clean_lines << line end clean_lines end
git_diff_file_hunks()
click to toggle source
# File lib/document_generator/diff_file.rb, line 19 def git_diff_file_hunks hunks = git_diff_file.patch.split(/@@.*@@.*\n/) hunks.shift # Shift to pop first element off array which is just git diff header info hunks end
git_diff_lines_for(hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 25 def git_diff_lines_for(hunk) hunk.split("\n") end
markdown_outputs()
click to toggle source
# File lib/document_generator/diff_file.rb, line 90 def markdown_outputs outputs = [] git_diff_file_hunks.each do |hunk| outputs << markdown_outputs_for(hunk) end outputs.flatten end
markdown_outputs_for(hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 98 def markdown_outputs_for(hunk) # returns an array of outputs for a particular hunk outputs = [] last_line = -1 git_diff_lines_for(hunk).each_with_index do |line, index| next if index <= last_line case line.strip[0] when "+" last_line = last_same_line(index, hunk) output = Output.new output.description = "Add" output.content = line_block(index, last_line, hunk) outputs << output when "-" if line_sign(index + 1, hunk) == "+" output = Output.new output.description = "Change" output.content = line_block(index, last_same_line(index, hunk), hunk) outputs << output last_line = last_same_line(last_same_line(index, hunk) + 1, hunk) output = Output.new output.description = "To" output.content = line_block(last_same_line(index, hunk) + 1, last_line, hunk) outputs << output last_line = last_same_line(last_same_line(index, hunk) + 1, hunk) else output = Output.new output.description = "Remove" last_line = last_same_line(index, hunk) output.content = line_block(index, last_line, hunk) outputs << output end end end if git_diff_file.type == 'modified' output = Output.new output.description = "Becomes" output.content = ending_code_for(hunk) outputs << output end outputs end
patch_heading()
click to toggle source
# File lib/document_generator/diff_file.rb, line 15 def patch_heading "#{action_type} `#{git_diff_file.path}`" end
type()
click to toggle source
# File lib/document_generator/diff_file.rb, line 7 def type git_diff_file.type end
Private Instance Methods
ignore_line?(line)
click to toggle source
# File lib/document_generator/diff_file.rb, line 148 def ignore_line?(line) line.strip == 'No newline at end of file' end
last_same_line(line_index, hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 152 def last_same_line(line_index, hunk) starting_sign = line_sign(line_index, hunk) git_diff_lines_for(hunk)[line_index..-1].each_with_index do |line, index| if line_sign(index + 1 + line_index, hunk) != starting_sign return (index + line_index) end end end
line_block(beginning, ending, hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 162 def line_block(beginning, ending, hunk) lines = [] git_diff_lines_for(hunk)[beginning..ending].each do |line| if ["+", "-"].include?(line[0..0]) line = remove_first_character(line) end if !ignore_line?(line) lines << line end end lines end
line_sign(line_number, hunk)
click to toggle source
# File lib/document_generator/diff_file.rb, line 175 def line_sign(line_number, hunk) (git_diff_lines_for(hunk)[line_number] || '').strip[0] end
remove_first_character(line)
click to toggle source
# File lib/document_generator/diff_file.rb, line 179 def remove_first_character(line) " " + line[1..-1] end