Skip to content

Instantly share code, notes, and snippets.

@mmore500
Last active December 9, 2023 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmore500/be54c0b3891ec41ab2dba2eebcc7f720 to your computer and use it in GitHub Desktop.
Save mmore500/be54c0b3891ec41ab2dba2eebcc7f720 to your computer and use it in GitHub Desktop.
Scheme to trim trailing whitespace from diffs via git filter
* text eol=lf filter=cleanWhitespace
[filter "cleanWhitespace"]
clean = /home/mmore500/clean.sh %f
#!/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