paularmstrong/onerepo

Prevent staging unstaged portions of files when using `--add` in tasks on `pre-commit`

Closed this issue · 0 comments

Problem

Some commands and plugins are intended to operate on pre-commit git hooks, eg, one lint --add. This command is expected to only modify and add the currently staged changes and not all unstaged files.

This mostly works, except when files are partially staged. When this happens, the full file ends up getting included in the stage by the command.

Solution

lint-staged handles this by:

  1. creating a patch file of the unstaged changes
  2. Running tasks
  3. applying the patch file
  4. if there's a failure in the patch apply, it retries with 3-way merge
  • one tasks could have special handling for --lifecycle=pre-commit that stores a patch of the unstaged changes and re-applies it after all tasks are run
  • potentially getFilepaths from @onerepo/builders could handle this in order for it to work on each command
    • unsure how that would deal with cleanup after the fact
    • this may cause conflicts across tasks run in parallel

Code of Conduct

  • I agree to follow this project's Code of Conduct