pre-commit: git apply error when using git-crypt

Hi. I am currently using git-crypt in tandem with some pre-commit hooks

Upon adding a new file for encryption --> a corresponding .gitattributes entry --> git commit -m "blah" I encounter the following error:

[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /Users/bob/.cache/pre-commit/patch1530518673.
Trim Trailing Whitespace.................................................Passed
Check Yaml...............................................................Passed
Fix End of Files.........................................................Passed
Ansible-lint.............................................................Passed
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: Command: ('/usr/local/Cellar/git/2.18.0/libexec/git-core/git', '-c', 'core.autocrlf=false', 'apply', '--whitespace=nowarn', '/Users/bob/.cache/pre-commit/patch1530518673')
Return code: 128
Expected return code: 0
Output: (none)
Errors:
    error: unrecognized input

git version: 2.18.0 pre-commit version: 1.10.2 .pre-commit-config.yaml

repos:
-   repo: git://github.com/pre-commit/pre-commit-hooks
    rev: v1.3.0
    hooks:
    -   id: trailing-whitespace
    -   id: check-yaml
    -   id: end-of-file-fixer
-   repo: https://github.com/willthames/ansible-lint.git
    rev: v3.4.23
    hooks:
    - id: ansible-lint
      entry: ansible-lint bootstrap.yml

.gitattributes:

hosts/testing/secret-vars.yml filter=git-crypt diff=git-crypt

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I submitted a more thorough investigation to the git mailing list – https://marc.info/?l=git&m=166733577332408&w=2

I can confirm that this issue was resolved for me by making sure I wasn’t staging empty files that matched my .gitattributes git-crypt smudge/filter settings.

Hi! I’ve got the same issue:

git version 2.24.3 (Apple Git-128)
MacOs BigSur 11.1
git-crypt 0.6.0
pre-commit 2.11.1
pre-commit run --show-diff-on-failure terraform_validate
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /Users/yulia/.cache/pre-commit/patch1616497389.
Terraform validate.......................................................Passed
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: command: ('/usr/bin/git', '-c', 'core.autocrlf=false', 'apply', '--whitespace=nowarn', '/Users/yulia/.cache/pre-commit/patch1616497389')
return code: 128
expected return code: 0
stdout: (none)
stderr:
    error: unrecognized input
    

.pre-commit-config.yml

repos:
  - repo: git://github.com/antonbabenko/pre-commit-terraform
    rev: v1.46.0
    hooks:
      - id: terraform_fmt
      - id: terraform_docs
      # - id: terraform_tflint
      #   args:
      #     - '--args=--only=terraform_deprecated_interpolation'
      #     - '--args=--only=terraform_deprecated_index'
      #     - '--args=--only=terraform_unused_declarations'
      #     - '--args=--only=terraform_comment_syntax'
      #     - '--args=--only=terraform_documented_outputs'
      #     - '--args=--only=terraform_documented_variables'
      #     - '--args=--only=terraform_typed_variables'
      #     - '--args=--only=terraform_module_pinned_source'
      #     - '--args=--only=terraform_naming_convention'
      #     - '--args=--only=terraform_required_version'
      #     - '--args=--only=terraform_required_providers'
      #     - '--args=--only=terraform_standard_module_structure'
      #     - '--args=--only=terraform_workspace_remote'
  - repo: git://github.com/pre-commit/pre-commit-hooks
    rev: v3.4.0
    hooks:
      - id: check-merge-conflict
  - repo: git://github.com/gruntwork-io/pre-commit
    rev: v0.1.12
    hooks:
      - id: terraform-validate

it solved for me

as it turned out there were a couple of empty files. I took a look inside /Users/yulia/.cache/pre-commit/pre-commit.log and noticed that 1 commit ago I committed 2 totally empty secrets files. So I filled them in. then

0 git add .
1 pre-commit install
2 git commit -m 'Your commit msg'
Everything works as expected :))

Terraform fmt............................................................Passed
Terraform docs.......................................(no files to check)Skipped
Terraform validate.......................................................Passed
Check for merge conflicts................................................Passed
git-crypt-check-status...................................................Passed

yeah 😢 that’s why I haven’t dug into it yet beyond the repro above

I started looking into this from the git side but never got a definitive response – it comes down to unstaged things in a smudge filter generating bogus git diff that cannot be later git applyd

the current known workaround is to make sure such files are git added before committing, but isn’t ideal

I’m not sure what it would take to fix this, I suspect a fix from git is necessary (or some workaround where pre-commit identifies smudge-filter files and handles them separately)

Wondering if there was an update on this issue? If there are any hints on what the issue might be, I’d be keen to offer help fixing - it’s something we run into frequently

hmm, my crash is actually different from yours – I wonder there’s two issues here :S