Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/git_diff_parser/patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.[](*ary)
def self.parse(contents)
body = false
file_name = ''
orig_file_name = ''
patch = []
lines = contents.lines
line_count = lines.count
Expand All @@ -24,18 +25,24 @@ def self.parse(contents)
parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
patch.clear
file_name = ''
orig_file_name = ''
end
body = false
when /^\-\-\-/
when %r{^\-\-\- a/(?<file_name>.*)}
orig_file_name = Regexp.last_match[:file_name]
when %r{^\+\+\+ b/(?<file_name>.*)}
file_name = Regexp.last_match[:file_name]
body = true
when %r{^\+\+\+ /dev/null}
file_name = orig_file_name
body = true
when /^(?<body>[\ @\+\-\\].*)/
patch << Regexp.last_match[:body] if body
if !patch.empty? && body && line_count == count + 1
parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
patch.clear
file_name = ''
orig_file_name = ''
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/git_diff_parser/patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module GitDiffParser
let(:sjis_diff) { sjis_file.gsub(/\.csv\z/, '.diff') }
let(:sjis_body) { File.read(sjis_diff) }

let(:deleted_diff) { 'spec/support/fixtures/deleted.diff' }
let(:deleted_file) { 'spec/support/fixtures/hello.txt' }
let(:deleted_body) { File.read(deleted_diff.gsub(/\.diff\z/, '.body')) }

it 'returns parsed patches' do
diff_body = File.read('spec/support/fixtures/d1bd180-c27866c.diff')
patches = Patches.parse(diff_body)
Expand All @@ -36,6 +40,14 @@ module GitDiffParser
expect { patches = Patches.parse(sjis_body) }.not_to raise_error
expect(patches[0].file).to eq sjis_file
end

it 'returns patches for a deleted file' do
diff_body = File.read(deleted_diff)
patches = Patches.parse(diff_body)
expect(patches.size).to eq 1
expect(patches[0].file).to eq deleted_file
expect(patches[0].body).to eq deleted_body
end
end
end
end
2 changes: 2 additions & 0 deletions spec/support/fixtures/deleted.body
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@@ -1 +0,0 @@
-Hello, world!
7 changes: 7 additions & 0 deletions spec/support/fixtures/deleted.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git a/spec/support/fixtures/hello.txt b/spec/support/fixtures/hello.txt
deleted file mode 100644
index af5626b..0000000
--- a/spec/support/fixtures/hello.txt
+++ /dev/null
@@ -1 +0,0 @@
-Hello, world!