Created
March 22, 2020 15:03
-
-
Save bfichter/4cb40793d315025d6e0bb3aa980aaecc to your computer and use it in GitHub Desktop.
Bash script to make a commit excluding comments
This file contains 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 | |
paths="$(git diff --name-only --cached --diff-filter=MA)" | |
echo "$paths" | | |
while IFS= read -r path; do | |
cp "$path" "$path.precommentstrip" | |
git diff --cached "$path" | awk '($1 ~ /^\+/) && (/\/\//)' | cut -c 2- | | |
while IFS= read -r addition; do | |
commentFreeAddition="$(echo "$addition" | awk -F'//' '{print $1}' | sed 's|[[:blank:]]*$||')" | |
if [[ -z "$commentFreeAddition" ]] | |
then | |
sed -i '' "\|$addition|d" "$path" | |
else | |
sed -i '' "s|$addition|$commentFreeAddition|" "$path" | |
fi | |
done | |
git add "$path" | |
done | |
git commit | |
echo "$paths" | | |
while IFS= read -r path; do | |
mv "$path.precommentstrip" "$path" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment