From 8656434ec0627190109c07e10b9ba14c90f51500 Mon Sep 17 00:00:00 2001 From: Kaoru Maeda Date: Wed, 9 May 2018 17:32:48 +0900 Subject: [PATCH] Support for deleted files --- lib/git_diff_parser/patches.rb | 9 ++++++++- spec/git_diff_parser/patches_spec.rb | 12 ++++++++++++ spec/support/fixtures/deleted.body | 2 ++ spec/support/fixtures/deleted.diff | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 spec/support/fixtures/deleted.body create mode 100644 spec/support/fixtures/deleted.diff diff --git a/lib/git_diff_parser/patches.rb b/lib/git_diff_parser/patches.rb index ea3c5b2..50eae20 100644 --- a/lib/git_diff_parser/patches.rb +++ b/lib/git_diff_parser/patches.rb @@ -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 @@ -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/(?.*)} + orig_file_name = Regexp.last_match[:file_name] when %r{^\+\+\+ b/(?.*)} file_name = Regexp.last_match[:file_name] body = true + when %r{^\+\+\+ /dev/null} + file_name = orig_file_name + body = true when /^(?[\ @\+\-\\].*)/ 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 diff --git a/spec/git_diff_parser/patches_spec.rb b/spec/git_diff_parser/patches_spec.rb index 117f204..9c17d3b 100644 --- a/spec/git_diff_parser/patches_spec.rb +++ b/spec/git_diff_parser/patches_spec.rb @@ -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) @@ -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 diff --git a/spec/support/fixtures/deleted.body b/spec/support/fixtures/deleted.body new file mode 100644 index 0000000..4467d0f --- /dev/null +++ b/spec/support/fixtures/deleted.body @@ -0,0 +1,2 @@ +@@ -1 +0,0 @@ +-Hello, world! diff --git a/spec/support/fixtures/deleted.diff b/spec/support/fixtures/deleted.diff new file mode 100644 index 0000000..f49e866 --- /dev/null +++ b/spec/support/fixtures/deleted.diff @@ -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!