From 6022ccab7ea813afa6f2afd0d60b47fa5d386fa0 Mon Sep 17 00:00:00 2001 From: ybai08 <271969568+ybai08@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:51:26 -0500 Subject: [PATCH] Exclude files deleted from the working tree from all_files `overcommit --run` checks every file `git ls-files` lists, which includes tracked files that were deleted but whose deletion is not staged. Hooks then fail trying to read them (e.g. JsonSyntax raises 'No such file or directory'). Skip the files `git ls-files --deleted` reports. --- lib/overcommit/git_repo.rb | 8 +++++--- spec/overcommit/git_repo_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) 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