Use git to check for empty index.

This commit is contained in:
Sven Weidauer 2022-06-06 11:27:39 +02:00
parent 47363ad14b
commit 2e59062454
3 changed files with 65 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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