Last active
December 9, 2023 22:38
-
-
Save mmore500/be54c0b3891ec41ab2dba2eebcc7f720 to your computer and use it in GitHub Desktop.
Scheme to trim trailing whitespace from diffs via git filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* text eol=lf filter=cleanWhitespace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[filter "cleanWhitespace"] | |
clean = /home/mmore500/clean.sh %f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a file name is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <filename>" | |
exit 1 | |
fi | |
# Assign the filename to a variable | |
FILE="$1" | |
# Perform the operations | |
git show :"$FILE" > /tmp/from | |
cp "$FILE" /tmp/to | |
GIT_CONFIG_NOSYSTEM=1 HOME=/nonexistent git diff --no-index -- "/tmp/from" "/tmp/to" | sed '/^+/s/[[:space:]]*$//' > /tmp/patch | |
cat /tmp/patch | GIT_CONFIG_NOSYSTEM=1 HOME=/nonexistent git apply --allow-empty --unsafe-paths --directory=/ --whitespace=nowarn | |
cat /tmp/to | sed ':a;/^[ \n]*$/{$d;N;ba}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment