Skip to content

Instantly share code, notes, and snippets.

@bfichter
Created March 22, 2020 15:03
Show Gist options
  • Save bfichter/4cb40793d315025d6e0bb3aa980aaecc to your computer and use it in GitHub Desktop.
Save bfichter/4cb40793d315025d6e0bb3aa980aaecc to your computer and use it in GitHub Desktop.
Bash script to make a commit excluding comments
#!/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