20
22

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 5 years have passed since last update.

git commit 前に cpplint を走らせる

Last updated at Posted at 2015-09-03

C++ の lint ツールとして cpplint がよさそう。

キレイなコードを書くためにこれを強制したいんだけど、 editor の設定やなんかだと使ってるものによって方法が違うので逆にめんどくさくなってしまう。
じゃあ必ず通るパスはどこだということで git commit は絶対やるわけだからそこで cpplint をかけてしまおうという流れ。
cpplint に通らないと commit できなくする。

こういうときは pre-commit hooks を使うらしい。

というわけで設定を作ってみた。

cpplint を使うリポジトリーの直下に tools ディレクトリーを作ってその下に cpplint.py を置く。
で、次のファイルを .git/hooks/ の下におくと git commit 時に勝手に cpplint が走ってくれる。

pre-commit
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

cpplint=$(git rev-parse --show-toplevel)/tools/cpplint.py
filters='-whitespace/line_length,-readability/todo,-runtime/reference'
sum=0

# for cpp
for file in $(git diff-index --name-status $against -- | grep -E '\.[ch](pp)?$' | awk '{print $2}'); do
    $cpplint --filter=$filters $file
    sum=$(expr ${sum} + $?)
done

if [ ${sum} -eq 0 ]; then
    exit 0
else
    exit 1
fi

ファイルの位置を変えたいときや cpplint で見てほしい設定を変えたい場合は次の行をいじればいい。

cpplint=$(git rev-parse --show-toplevel)/tools/cpplint.py
filters='-whitespace/line_length,-readability/todo,-runtime/reference'

20
22
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
20
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?