Skip to content

Support for --staged-only Flag #608

@hsjobeki

Description

@hsjobeki

Feature Request: Support for --staged-only Flag

Is your feature request related to a problem? Please describe.

Yes. When running treefmt natively on a project with partially staged changes, the formatter operates on both staged and unstaged content. For example:

  • Staged:
    foo.nix (lines 10–20) — has formatting issues
  • Unstaged:
    foo.nix (lines 80–81) — contains the formatting fix, but is unstaged

Because treefmt formats the entire file, it may end up accepting files with unstages formatting fixes as well. Which makes it unsutable as a pre-commit tool on its own.

This becomes an issue when using treefmt as part of a pre-commit hook. Tools like [git-hooks.nix](https://github.com/cachix/git-hooks.nix) are capable of extracting only staged changes, but they bring their own ecosystem. This adds overhead, complexity, and potential integration friction.

Describe the solution you'd like

I'd like treefmt to support a native --staged-only flag, which restricts formatting to only the staged portions of files. This would enable native use of treefmt in pre-commit workflows without (mentioned) external wrappers.

Describe alternatives you've considered

Running treefmt directly is far simpler — if it supported "staged-only" formatting that would be awesome.

Example of interactions

# Initial state
$ git status

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   foo.nix

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   foo.nix
  1. Run
treefmt . --staged-only

Expected:

=> treefmt fails because it checks the staged change only.

  1. Stage the formatted files
    This makes sense, since we want to build a pre-commit check. Which should check the content of the commit, not local files.
git add foo.nix
treefmt . --staged-only

Expected:

treefmt runs fine. Because we have staged the formatting changes.

EDIT: Maybe we should call this --check-staged-only which fails on change

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions