Skip to content

Instantly share code, notes, and snippets.

@MacoTasu
Last active January 24, 2021 09:52
Show Gist options
  • Save MacoTasu/ab2da6cff8da7ff6b7f0 to your computer and use it in GitHub Desktop.
Save MacoTasu/ab2da6cff8da7ff6b7f0 to your computer and use it in GitHub Desktop.
commitするときに、差分のあったファイルのみgo fmtを実行して、変なフォーマットがないかチェックするためのshellscript
#!/bin/sh
before_diff_bytes=`git diff | wc -c`
diff_files=(`git diff --name-only HEAD ./`)
for diff_file in "${diff_files[@]}"
do
if [[ $diff_file =~ \.go$ ]]; then
gofmt -w $diff_file
fi
done
after_diff_bytes=`git diff | wc -c`
diff_bytes=`expr $after_diff_bytes \- $before_diff_bytes`
if [[ $diff_bytes > 0 ]] ; then
echo "\033[0;31m Check golang code format! \033[0;39m"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment