Sitelet https://web.archive.org/web/20201130225027/https://github.com/pre-commit/pre-commit/issues/776
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git apply error when using git-crypt #776

Open
ranimufid opened this issue Jul 2, 2018 · 10 comments
Open

git apply error when using git-crypt #776

ranimufid opened this issue Jul 2, 2018 · 10 comments
Labels
bug

Comments

@ranimufid
Copy link

@ranimufid ranimufid commented Jul 2, 2018 •

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
@asottile asottile added the bug label Jul 2, 2018
@asottile
Copy link
Member

@asottile asottile commented Jul 2, 2018

Thanks for the report! could you provide some more information (if possible) for me?

it appears from the output that you committed with an unstaged change, to which file was that and if possible could you share the patch file mentioned?

I suspect the diff=git-crypt is somehow throwing off the way we generate and then reapply patches and will need to be worked around.

@ranimufid
Copy link
Author

@ranimufid ranimufid commented Jul 2, 2018

thanks for your prompt response @asottile.

Context:
I'm trying to encrypt some ansible secrets using git-crypt. I'm also doing ansible-lint as a pre-commit hook.

it appears from the output that you committed with an unstated change

The change which triggered the error is creating an empty file /hosts/testing/secret-vars.yml with a corresponding entry in .gitattributes

.gitattributes

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

to which file was that and if possible could you share the patch file mentioned

The patch file corresponding to the above change

diff --git a/hosts/testing/secret-vars.yml b/hosts/testing/secret-vars.yml
GIT binary patch
literal 0
HcmV?d00001

literal 22
saefs@_sfdsfsdfsdf?>t6Gdv(psdfsdfsdfsf345`ISOHcp2nGNE

Do let me know if you need further info

@asottile
Copy link
Member

@asottile asottile commented Jul 2, 2018

ok, I have a reproduction:

#!/usr/bin/env bash
set -euxo pipefail

rm -rf repo

git init repo
cd repo
git commit --allow-empty -m 'Initial empty commit'
git-crypt init

cat > .pre-commit-config.yaml << EOF
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v1.3.0
    hooks:
    -   id: trailing-whitespace
EOF
git add .pre-commit-config.yaml
pre-commit install
git commit -m "Add pre-commit config"

echo 'f filter=git-crypt diff=git-crypt' > .gitattributes
git add .gitattributes
echo 'hello world' > f
git add f
rm f && touch f

git commit -m "test"

which ends with:

+ git commit -m test
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/asottile/.cache/pre-commit/patch1530539647.
Trim Trailing Whitespace.................................................Passed
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: Command: ('/usr/lib/git-core/git', '-c', 'core.autocrlf=false', 'apply', '--whitespace=nowarn', '/home/asottile/.cache/pre-commit/patch1530539647')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: binary patch to 'f' creates incorrect result (expecting 9dc8b7b3fd7e85892019cbf77d888ce9d9f37b15, got e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)
    error: f: patch does not apply
    

Check the log at /home/asottile/.cache/pre-commit/pre-commit.log

Almost certainly due to the smudge filter though I'm not exactly sure what the correct approach is here -- for now you can work around this by making sure you stage the encrypted files before committing.

@ranimufid
Copy link
Author

@ranimufid ranimufid commented Jul 2, 2018

This is how i've worked around it for the time being:

touch hosts/production/secret-vars.yml
echo "#prod vars" >> hosts/production/secret-vars.yml
git add hosts/production/secret-vars.yml
git commit -m "add production secret vars"

echo 'hosts/production/secret-vars.yml filter=git-crypt diff=git-crypt' >> .gitattributes
git add .gitattributes
git commit -m "add production secret vars"

git push
@asottile
Copy link
Member

@asottile asottile commented Jul 2, 2018

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

@Lin-Buo-Ren
Copy link

@Lin-Buo-Ren Lin-Buo-Ren commented Aug 14, 2018

I encountered a similar issue as well, here's the log. This is reproduced in some repos that have implemented some clean filters, with some test cases files intentionally committed uncleaned for testing.

@marcjay
Copy link
Contributor

@marcjay marcjay commented Aug 20, 2019

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

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2019

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)

@marcjay
Copy link
Contributor

@marcjay marcjay commented Aug 20, 2019

Many thanks @asottile, that all sounds very non-trivial

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2019

yeah :'( that's why I haven't dug into it yet beyond the repro above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.