LoginSignup
13
12

More than 5 years have passed since last update.

pre-commit時にformatterを実行する

Posted at

pre-commit時にformatterを実行する

はじめに

チームでコーディングルールが決ってないので、システム的に解決したいと考えました。
Gitにはhookという機能があり、cimmit時にshellを実行することができます。
commit時にformatを実行することで、リポジトリに綺麗なコードしかアップロードされないようにしようという試みです。

他の方法として、Lint系CIやCIでLintを実行することも考えていますが、別途試してみる予定です。

環境

pipenv
python3.7.1

設定方法

  1. 環境のセットアップ
  2. formatterのインストール
  3. checkerのインストール
  4. pre-commitの設定

環境のセットアップ

pipenv --python 3.7.1
pipenv shell

仮想環境を使用しない場合は、なくても良い

formatterのインストール

pip install black

formatterの設定ファイルを作成

pyproject.toml
[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

checkerのインストール

pip install flake8

checkerの設定ファイルを作成

.flake8
[flake8]
ignore = E203, E266, E501, W503, F403, F401
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9

pre-commitをインストール

pip install pre-commit

pre-commit時に実行する設定を作成

.pre-commit-config.yaml
repos:
- repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v1.2.3
    hooks:
    - id: flake8

pre-commitの設定

pre-commit install

動作

1.雑にダメなコードを書きます

hoge.py
    for i in range(1,10):
      text=''
      if i%3==0:
        text += 'Fizz'
      if i% 5== 0:
        text += "Buzz"
      print(text or i)

2.commitします

(Stady) bash-3.2$ git add hoge.py
(Stady) bash-3.2$ git commit -m'feat: add hoge.py'
black....................................................................Failed
hookid: black

Files were modified by this hook. Additional output:

reformatted hoge.py
All done! ✨ 🍰 ✨
1 file reformatted.

Flake8...................................................................Passed
(Stady) bash-3.2$

commitすると、blackとflake8が実行されます。blackのチェックが通らず、formatされることがあり、失敗しました。
ですが、blackでのformatが行われました。

3.ファイルを再度確認してみます。

hoge.py
for i in range(1, 10):
    text = ""
    if i % 3 == 0:
        text += "Fizz"
    if i % 5 == 0:
        text += "Buzz"
    print(text or i)

ファイルを確認すると、各所のスペースがなかった部分やシングルクォートになっていた部分が修正されており、formatterが実行されたことが確認できます。

4.もう一度addを実行後commitする

(Stady) bash-3.2$ git add hoge.py
(Stady) bash-3.2$ git commit -m'feat: add hoge.py'
black....................................................................Passed
Flake8...................................................................Passed
[master 093697f] feat: add hoge.py
 1 file changed, 7 insertions(+)
 create mode 100644 hoge.py
(Stady) bash-3.2$

blackとflake8ともにチェックが通過し、commitできたことが確認できました。

所感

pre-commitの動作としては、非常におもしろいとは思います。
ですが、目的であるチームのコード修正という意味では、一人一人の環境に対して準備する必要があるので、望ましい方法であるとは言えません。
また別の方法を模索してみます。なにかいい方法があれば御教授ください。

参考サイト

Automate Python workflow using pre-commits: black and flake8

13
12
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
13
12