diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 10fd158..8d3bf5d 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -17,6 +17,17 @@ module FormatStaged output end + def get_status(*args) + puts "> #{args.join(' ')}" if @verbose + result = system(*args) + + raise 'Failed to run command' if result.nil? + + puts "? #{$CHILD_STATUS.exitstatus}" if @verbose + + $CHILD_STATUS.exitstatus + end + def pipe_command(*args, source: nil) puts (source.nil? ? '> ' : '| ') + args.join(' ') if @verbose r, w = IO.pipe diff --git a/lib/format-staged/job.rb b/lib/format-staged/job.rb index 6ad0b44..bc6acd9 100644 --- a/lib/format-staged/job.rb +++ b/lib/format-staged/job.rb @@ -36,7 +36,10 @@ module FormatStaged formatted = files.filter { |file| format_file file } - !formatted.empty? + return false unless formatted.size == files.size + + quiet = @verbose ? [] : ['--quiet'] + return get_status('git', 'diff-index', '--cached', '--exit-code', *quiet, 'HEAD') != 0 end def repo_root @@ -76,7 +79,7 @@ module FormatStaged if new_hash == file.src_hash info "File #{file.src_path} equal to comitted" - return false + return true end true diff --git a/spec/test_spec.rb b/spec/test_spec.rb index 1c30107..54edcb2 100644 --- a/spec/test_spec.rb +++ b/spec/test_spec.rb @@ -57,7 +57,7 @@ describe FormatStaged do expect(repo.get_content('test.other')).to eq('x=y') end - it 'fails if files are changed to already comitted version' do + it 'fails if all 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' @@ -68,7 +68,55 @@ describe FormatStaged do expect(repo.get_content('test.test')).to eq('x = y') end + it 'succeeds if there are excluded files to commit' do + repo.file_in_tree 'test.test', 'x = y' + repo.set_content 'test.test', 'x=y' + repo.stage 'test.test' + repo.set_content 'test.other', 'abc' + repo.stage 'test.other' + + success = repo.run_formatter + + expect(success).to be_truthy + 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 + + it 'succeeds if only excluded files are changed' do + repo.set_content 'test.other', 'abc' + repo.stage 'test.other' + + expect(repo.run_formatter).to be_truthy + end + + it 'succeeds if one file is changed' do + repo.file_in_tree 'test.test', 'x = y' + repo.set_content 'test.test', 'x=y' + repo.stage 'test.test' + repo.set_content 'other.test', 'a=b' + repo.stage 'other.test' + + success = repo.run_formatter + + expect(success).to be_truthy + expect(repo.get_content('test.test')).to eq('x = y') + expect(repo.get_content('other.test')).to eq('a = b') + end + + it 'fails if a single file becomes empty' do + repo.file_in_tree 'test.test', 'x = y' + repo.set_content 'test.test', '#clear' + repo.stage 'test.test' + repo.set_content 'other.test', 'a=b' + repo.stage 'other.test' + + success = repo.run_formatter + + expect(success).to be_falsy + expect(repo.get_content('test.test')).to eq('#clear') + expect(repo.get_content('other.test')).to eq('a = b') + end end