Module: Flatito::DiffSource
- Defined in:
- lib/flatito/diff_source.rb
Constant Summary collapse
- NULL_BLOB =
"0000000"
Class Method Summary collapse
- .blob_content(blob) ⇒ Object
- .contents_for(file) ⇒ Object
- .load_side(blob, path, file, after_side: false) ⇒ Object
- .pad_until(lines, target_index) ⇒ Object
- .reconstruct_from_hunks(file, after_side:) ⇒ Object
- .working_tree_content(path) ⇒ Object
Class Method Details
.blob_content(blob) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/flatito/diff_source.rb', line 27 def blob_content(blob) return nil if blob.nil? || blob.start_with?(NULL_BLOB) output = IO.popen(["git", "cat-file", "-p", blob], err: File::NULL, &:read) return nil unless $CHILD_STATUS&.exitstatus&.zero? output rescue Errno::ENOENT nil end |
.contents_for(file) ⇒ Object
11 12 13 14 15 |
# File 'lib/flatito/diff_source.rb', line 11 def contents_for(file) before = file.new_file ? nil : load_side(file.before_blob, file.before_path, file) after = file.deleted_file ? nil : load_side(file.after_blob, file.after_path, file, after_side: true) [before, after] end |
.load_side(blob, path, file, after_side: false) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/flatito/diff_source.rb', line 17 def load_side(blob, path, file, after_side: false) content = blob_content(blob) return content if content content = working_tree_content(path) if after_side return content if content reconstruct_from_hunks(file, after_side: after_side) end |
.pad_until(lines, target_index) ⇒ Object
66 67 68 |
# File 'lib/flatito/diff_source.rb', line 66 def pad_until(lines, target_index) lines << "\n" while lines.size < target_index end |
.reconstruct_from_hunks(file, after_side:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/flatito/diff_source.rb', line 47 def reconstruct_from_hunks(file, after_side:) lines = [] file.hunks.each do |hunk| target_line = after_side ? hunk.new_start : hunk.old_start pad_until(lines, target_line - 1) hunk.lines.each do |entry| case entry[:kind] when :context lines << entry[:text] when :add lines << entry[:text] if after_side when :del lines << entry[:text] unless after_side end end end lines.join end |
.working_tree_content(path) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/flatito/diff_source.rb', line 38 def working_tree_content(path) return nil if path.nil? || path.empty? return nil unless ::File.file?(path) ::File.read(path) rescue StandardError nil end |