Skip to content

--diff output does not git apply cleanly when the diff context includes EOF #526

@benkuhn

Description

@benkuhn

Thanks for making an awesome tool! I ran across this issue while trying to make a script that asks before applying changes. (Obviously I could reapply the changes by running black again so it's not too important, but being able to cache the diff so the apply is faster would be nice, and avoid a race condition if someone edits a file in the middle)

Steps to reproduce issue:

# setup
git init black-repro && cd black-repro && echo "x = '1'" > foo.py

black --diff . | git apply
# error: patch failed: foo.py:1
# error: foo.py: patch does not apply

The problem is that the diff includes an extra newline at the end compared to what git expects:

black --diff . | sed s/1,2/1,1/g | grep -Ev '^ $' | git apply
# works

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions