diff --git a/lib/overcommit/git_repo.rb b/lib/overcommit/git_repo.rb index 1ab1b6b5..284397be 100644 --- a/lib/overcommit/git_repo.rb +++ b/lib/overcommit/git_repo.rb @@ -131,12 +131,14 @@ def tracked?(path) Overcommit::Utils.execute(%W[git ls-files #{path} --error-unmatch]).success? end - # Returns the names of all files that are tracked by git. + # Returns the names of all files that are tracked by git, excluding files + # that have been deleted from the working tree. # # @return [Array] list of absolute file paths def all_files - `git ls-files`. - split(/\n/). + deleted_files = `git ls-files --deleted`.split(/\n/) + + (`git ls-files`.split(/\n/) - deleted_files). map { |relative_file| File.expand_path(relative_file) }. reject { |file| File.directory?(file) } # Exclude submodule directories end diff --git a/spec/overcommit/git_repo_spec.rb b/spec/overcommit/git_repo_spec.rb index 78f02f98..da4d77d4 100644 --- a/spec/overcommit/git_repo_spec.rb +++ b/spec/overcommit/git_repo_spec.rb @@ -293,6 +293,16 @@ it { should include(*%w[tracked staged].map { |file| File.expand_path(file) }) } it { should_not include File.expand_path('sub') } + + context 'when a tracked file is deleted but the deletion is not staged' do + around do |example| + FileUtils.rm 'tracked' + example.run + end + + it { should_not include File.expand_path('tracked') } + it { should include File.expand_path('staged') } + end end describe '.initial_commit?' do