Repository navigation
Commit c9f6b35
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
| 135 | + | |
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
138 | | - | |
139 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
140 | 142 | | |
141 | 143 | | |
142 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
296 | 306 | | |
297 | 307 | | |
298 | 308 | | |
| |||
0 commit comments