Skip to content

Exclude files deleted from the working tree from all_files - #905

Open
ybai08 wants to merge 1 commit into
sds:mainfrom
ybai08:fix-run-all-deleted-files
Open

ybai08 wants to merge 1 commit into
sds:mainfrom
ybai08:fix-run-all-deleted-files

Conversation

@ybai08

@ybai08 ybai08 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

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.

`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.
@ybai08

ybai08 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

The four failing rspec jobs (Ruby 2.6, 2.7, 3.0, 3.1) fail in the "Set up Ruby" step, before any spec runs: Unavailable version 3.1.7 for ruby on ubuntu-26.04 (and the same for 2.6.10, 2.7.8 and 3.0.7). The runner image no longer offers those Rubies. The newer Ruby jobs pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant