From ba54c407cbad615f468dbb69112b3b536e9198ea Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 6 Jun 2022 10:18:05 +0200 Subject: [PATCH] [#1] Fail if all files are reset to committed version. --- Gemfile | 2 +- bin/git-format-staged | 3 ++- lib/format-staged/job.rb | 17 ++++++++++++++--- spec/test_spec.rb | 27 +++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index b37c960..26ea26a 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gemspec group :ci do gem 'code-scanning-rubocop' - gem "rspec_junit_formatter" + gem 'rspec_junit_formatter' end diff --git a/bin/git-format-staged b/bin/git-format-staged index a9454e3..5195cfa 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -73,4 +73,5 @@ if !parameters[:formatter] || parameters[:patterns].empty? exit end -FormatStaged.run(**parameters) +success = FormatStaged.run(**parameters) +exit success ? 0 : 1 diff --git a/lib/format-staged/job.rb b/lib/format-staged/job.rb index 4955e3a..6ad0b44 100644 --- a/lib/format-staged/job.rb +++ b/lib/format-staged/job.rb @@ -28,9 +28,15 @@ module FormatStaged end def run - matching_files(repo_root).each do |file| - format_file(file) + files = matching_files(repo_root) + if files.empty? + info 'No staged file matching pattern. Done' + return true end + + formatted = files.filter { |file| format_file file } + + !formatted.empty? end def repo_root @@ -57,7 +63,7 @@ module FormatStaged if new_hash == file.dst_hash info "Unchanged #{file.src_path}" - return false + return true end if object_is_empty new_hash @@ -68,6 +74,11 @@ module FormatStaged replace_file_in_index file, new_hash update_working_copy file, new_hash + if new_hash == file.src_hash + info "File #{file.src_path} equal to comitted" + return false + end + true end diff --git a/spec/test_spec.rb b/spec/test_spec.rb index 2b3ef4c..1c30107 100644 --- a/spec/test_spec.rb +++ b/spec/test_spec.rb @@ -19,8 +19,9 @@ describe FormatStaged do it 'updates staged file and working copy' do repo.set_content 'test.test', 'a=b' repo.stage 'test.test' - repo.run_formatter + success = repo.run_formatter + expect(success).to be_truthy expect(repo.get_staged('test.test')).to eq('a = b') expect(repo.get_content('test.test')).to eq('a = b') end @@ -29,8 +30,9 @@ describe FormatStaged do repo.set_content 'test.test', "x=y\na=b\n" repo.stage 'test.test' repo.set_content 'test.test', 'abc' - repo.run_formatter + success = repo.run_formatter + expect(success).to be_truthy expect(repo.get_content('test.test')).to eq('abc') expect(repo.get_staged('test.test')).to eq("x = y\na = b") end @@ -39,8 +41,9 @@ describe FormatStaged do repo.set_content 'test.test', "x=y\n#stuff\n" repo.stage 'test.test' repo.set_content 'test.test', "x=y\n#stuff\nmore stuff\n" - repo.run_formatter + success = repo.run_formatter + expect(success).to be_truthy expect(repo.get_content('test.test')).to eq("x = y\n#stuff\nmore stuff") expect(repo.get_staged('test.test')).to eq("x = y\n#stuff") end @@ -48,8 +51,24 @@ describe FormatStaged do it 'only touches files matching the given pattern' do repo.set_content 'test.other', 'x=y' repo.stage 'test.other' - repo.run_formatter + success = repo.run_formatter + expect(success).to be_truthy expect(repo.get_content('test.other')).to eq('x=y') end + + it 'fails if files are changed to already comitted version' do + repo.file_in_tree 'test.test', 'x = y' + repo.set_content 'test.test', 'x=y' + repo.stage 'test.test' + + success = repo.run_formatter + + expect(success).to be_falsy + expect(repo.get_content('test.test')).to eq('x = y') + end + + it 'succeeds if there are no staged files' do + expect(repo.run_formatter).to be_truthy + end end