Skip to content

Detect git commit --amend in pre-commit hooks so staged files include everything in commit #146

@sds

Description

@sds

Running pre-commit hooks when amending a commit currently behaves a little strangely in that only the changes being added to the commit (not all changes in the commit) are checked by Overcommit.

This sometimes results in weird behavior where depending on the order you add files and amend them to a commit you can get around certain pre-commit hooks. For example, with Rubocop's MethodLength cop set to 20 lines, if you add a method that is 19 lines long, commit it, then amend the commit to add another couple of lines to the method body, the Rubocop lint message will now become a warning because it is reported on a line that is not currently staged, since it's already in the commit.

Benefits

  • Prevents potential bugs/problems being introduced due to order of changes being amended
  • Assuming we can detect when a pre-commit hook is being executed as part of an "amend", this allows us to write a hook that prevents amending commits on certain branches (e.g. master) to enforce certain workflows

Costs

  • Unclear if there is a definitive way to detect an amend on pre-commit (if it requires a non-reliable hack, this feature may be dead in the water)
  • Significant behavior change, which may be surprising to existing users

Feel free to comment or ask questions in this issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions