Skip to content

Commit c9f6b35

Browse files
ybai08sds
andauthored
Exclude files deleted from the working tree from all_files (#905)
Refs #699: this fixes the `overcommit --run` case narrowed down in #699 (comment) and #699 (comment). The renamed-file behaviour in the original report (during a normal commit) is not changed here. `GitRepo.all_files` returns everything `git ls-files` lists. That includes a tracked file that has been deleted from the working tree when the deletion is not staged yet. `overcommit --run` uses `all_files` as its list of modified files, so every hook that reads its `applicable_files` then fails on that path. With `JsonSyntax` enabled: ``` $ rm a.json # tracked, deletion not staged $ overcommit --run Running pre-commit hooks Validate JSON syntax.....................................[JsonSyntax] FAILED Hook raised unexpected error No such file or directory @ rb_sysopen - .../a.json ``` `all_files` now drops the paths `git ls-files --deleted` reports. This uses Git's own view rather than `File.exist?`, so a tracked broken symlink is still listed (the `BrokenSymlinks` hook needs it). `included_files` (used by the pre-push `Minitest` hook) goes through `all_files` too, so it no longer passes a deleted file either. ### Tests A new context in `spec/overcommit/git_repo_spec.rb` deletes a committed file without staging the deletion and checks that `all_files` no longer includes it but still includes the other files. Run on macOS with Ruby 4.0.7 and Git 2.54.0: - `bundle exec rspec spec/overcommit/git_repo_spec.rb -e '.all_files'`: `4 examples, 1 failure` with `lib/overcommit/git_repo.rb` from main (the new "is expected not to include .../tracked" example), `4 examples, 0 failures` with the change. - Manual check in a scratch repository with only `JsonSyntax` enabled, a committed `a.json` deleted from the working tree, then `overcommit --run`: main fails with `No such file or directory @ rb_sysopen`; with the change, `[JsonSyntax] OK`. - `bundle exec rspec`: `1659 examples, 4 failures`. The same 4 examples fail on main: the git alias `#amendment?` examples in `hook_context/commit_msg_spec.rb:134,140` and `hook_context/pre_commit_spec.rb:43,49`, which look specific to this machine's Git (Apple Git). - `bundle exec rubocop`: no offenses. Not run: anything on Windows or Linux. Co-authored-by: ybai08 <[email protected]> Co-authored-by: Shane da Silva <[email protected]>
1 parent 7f5d449 commit c9f6b35

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎lib/overcommit/git_repo.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ def tracked?(path)
131131
Overcommit::Utils.execute(%W[git ls-files #{path} --error-unmatch]).success?
132132
end
133133

134-
# Returns the names of all files that are tracked by git.
134+
# Returns the names of all files that are tracked by git, excluding files
135+
# that have been deleted from the working tree.
135136
#
136137
# @return [Array<String>] list of absolute file paths
137138
def all_files
138-
`git ls-files`.
139-
split(/\n/).
139+
deleted_files = `git ls-files --deleted`.split(/\n/)
140+
141+
(`git ls-files`.split(/\n/) - deleted_files).
140142
map { |relative_file| File.expand_path(relative_file) }.
141143
reject { |file| File.directory?(file) } # Exclude submodule directories
142144
end

‎spec/overcommit/git_repo_spec.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@
293293

294294
it { should include(*%w[tracked staged].map { |file| File.expand_path(file) }) }
295295
it { should_not include File.expand_path('sub') }
296+
297+
context 'when a tracked file is deleted but the deletion is not staged' do
298+
around do |example|
299+
FileUtils.rm 'tracked'
300+
example.run
301+
end
302+
303+
it { should_not include File.expand_path('tracked') }
304+
it { should include File.expand_path('staged') }
305+
end
296306
end
297307

298308
describe '.initial_commit?' do

0 commit comments

Comments
 (0)