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

Only format git diff (similar to clang-format-diff) #245

Closed
Rikorose opened this issue May 22, 2018 · 6 comments
Closed

Only format git diff (similar to clang-format-diff) #245

Rikorose opened this issue May 22, 2018 · 6 comments

Comments

@Rikorose
Copy link

Relevant yapf issue: google/yapf#190

It would be really helpfull to only format code that was changed which also helps to minimize the diff.

clang-format-diff example:

git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i
svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
@ambv
Copy link
Collaborator

ambv commented May 22, 2018

Black is unable to format line ranges. This would be rather problematic given how it's currently implemented. More importantly, this goes against PEP 8's philosophy of staying consistent within a file (and more generally, within a project). See #134 for a longer explanation.

@Lukas0907
Copy link

You can format the whole file and only commit relevant changes via git add -p and revert what you don't want to change.

@ambv
Copy link
Collaborator

ambv commented May 29, 2018

This is not a planned feature.

@ambv ambv closed this as completed May 29, 2018
@wbolster
Copy link
Contributor

wbolster commented Feb 6, 2019

fyi, i wrote https://github.com/wbolster/black-macchiato

@akaihola
Copy link

Also FYI, I wrote darker which can apply Black formatting only to lines which have changed in the Git working tree since the last commit.

@sieu-n
Copy link

sieu-n commented Apr 27, 2023

For quick solution, run the following script.

#!/bin/bash

# Get a list of changed Python files in the diff
changed_files=$(git diff --name-only --diff-filter=d HEAD | grep '\.py$')

# Apply isort and black to the changed files
for file in $changed_files; do
  isort "$file"
  black "$file"
done

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

No branches or pull requests

6 participants