1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

goエンジニア向けgitのpre-commitとpre-pushファイルのオススメ

Posted at

commit時にはgo fmt、 push時にはgo vetが走るようにしてます。

.git/hooks/pre-push
linterr="$(go vet ./... 2>&1 )"
if [ -n "$linterr" ] ; then
    echo "go vet error:"
    echo $linterr
    exit 1
fi
echo "finish go vet"

branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
       "$(git describe --contains --all HEAD)"

if [ "${branch##refs/heads/}" = "main" ]; then
    echo "do not push to main"
    exit 1
fi

.git/hooks/pre-commit
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

if test "`git symbolic-ref HEAD | sed -e 's:^refs/heads/::'`" = main; then
        echo "cannot commit on main branch."
        exit 1
fi

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?