Skip to content

feat: add --staged flag to lint only files staged in the index - #49

Open
PrasannaMishra001 wants to merge 2 commits into
omegaup:mainfrom
PrasannaMishra001:fix/add-staged-flag
Open

PrasannaMishra001 wants to merge 2 commits into
omegaup:mainfrom
PrasannaMishra001:fix/add-staged-flag

Conversation

@PrasannaMishra001

@PrasannaMishra001 PrasannaMishra001 commented Feb 16, 2026 •

Copy link
Copy Markdown

adds --staged to 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

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.
@PrasannaMishra001

Copy link
Copy Markdown
Author

@pabo99 this is with reference to omegaup/omegaup#8958,
don't know who is maintainer here,can u guide pls?

Comment on lines +252 to +256
return subprocess.run(
['/usr/bin/git', 'show', f':{filename}'],
check=True,
stdout=subprocess.PIPE,
cwd=root).stdout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@PrasannaMishra001 PrasannaMishra001 Nov 14, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +330 to +331
fix_parser.add_argument(
'--staged', action='store_true',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

[FEATURE] Add support for --staged flag to lint only staged files

2 participants