'is it possible to apply code format before git diff command
I was working on a old repo, I have config my editor to auto apply black and isort while save, but the repo code not. So while I try to diff, there will always be so much diff message to views. So I wonder if it possible to apply black and isort while call git diff, So I can only check the logic different, but not the format.
Solution 1:[1]
You can use the options to avoid spaces/eol change. Try git diff -w
but there are others. Check git help diff
Solution 2:[2]
This feature was rejected from black (see https://github.com/psf/black/issues/245) but on Unix systems you can use the below black-diff
function to get the basic functionality (allowing to compare a single file against the latest version, or two revisions of a single file):
Check changes since last commit:
black-diff test.py
Compare two versions of a file between branches:
black-diff main:test.py feature_branch:test.py
The function itself (you can add it to your ~/.bashrc
for convenience):
black-diff () {
if [[ $# -eq 0 ]]; then
echo "Please provide path to the modified file (or two revisions of a file) to compare"
return 1
fi
if [[ $# -eq 1 ]]; then
# compare latest revision against current version
cat $1 | black -q - | diff --color -u <(git show "$(git branch --show-current):$1" | black -q -) -
else
# compare two revisions
git show $1 | black -q - | diff --color -u <(git show $2 | black -q -) -
fi
}
Solution 3:[3]
There is a diff tool based of syntax that support over 20 programming languages (Python is one of them). This tool is experimental and you can review in Github, DiffStatic.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | eftshift0 |
Solution 2 | krassowski |
Solution 3 | cosmoscalibur |