Repository navigation
feat: add --staged flag to lint only files staged in the index - #49
PrasannaMishra001 wants to merge 2 commits into
Conversation
When --staged is passed to the validate or fix subcommands, only files that are staged in the git index (git diff --cached) are considered. This is useful for pre-commit hooks that should only lint staged changes rather than the entire diff against a branch. The flag is incompatible with --all-files, commits, and explicit file arguments. When --staged is active, file_contents reads from the index (git show :filename) instead of the working tree.
|
@pabo99 this is with reference to omegaup/omegaup#8958, |
| return subprocess.run( | ||
| ['/usr/bin/git', 'show', f':{filename}'], | ||
| check=True, | ||
| stdout=subprocess.PIPE, | ||
| cwd=root).stdout |
There was a problem hiding this comment.
Reading staged content makes sense for validation. However, fix --staged eventually writes the formatted content back to the working tree, which could overwrite unstaged changes in partially staged files.
Could you make sure those changes are preserved, or reject fix --staged when the working tree differs from the index?
There was a problem hiding this comment.
right, as fixes write to disk, reading from the index won't be safe. I added a check before any writes. Now fix --staged rejects files that differ from index.
| yield path | ||
|
|
||
|
|
||
| def _get_staged_files() -> Iterator[bytes]: |
There was a problem hiding this comment.
Could we add regression tests for staged file selection, including modified, added, deleted, and renamed files, as well as partially staged changes?
It would also be useful to verify that validate --staged reads the index content rather than the working tree.
There was a problem hiding this comment.
added git and cli tests for those cases, included checking renamed destn. , excluded deletions. For partial staging, i tested in both directions and removing index read makes both cases fail.
| fix_parser.add_argument( | ||
| '--staged', action='store_true', |
There was a problem hiding this comment.
Could you also preserve --staged when generating the suggested fix command? Otherwise, a failed validate --staged may suggest a command that no longer uses staged-only selection.
There was a problem hiding this comment.
kept --staged in shared command builder, so now both the suggested command and auto fix path have it. I also left out explicit filenames as they conflict with staged selection.
adds
--stagedto validate or fix files in the git index. validation reads staged content, and fixes are rejected before writing if a selected file has unstaged changes. suggested fix commands keep--staged.covers staged additions, edits, deletions, renames, partial staging and the first commit with real git and cli tests. 20 tests and the full repository lint pass locally on Python 3.10.
Fixes #50