0
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?

変更があったディレクトリで pre-commit を実行する

Posted at

1つのリポジトリで複数のサービスやパッケージを作っているときに、ディレクトリごとの環境でリントとかしたいですよね

探したら
https://blog.physalis.net/2021/git-hooks-dispatch
とかあったんですけど、余計な依存無しに、 git や シェルスクリプトだけで完結したかったので、雑に作りました

#!/bin/sh

files_changed=$(git diff --cached --name-only)
dirs_changed=()
for file in ${files_changed[@]}; do
  _dir=$(echo $file | sed 's/\/.*//')
  dirs_changed+=($_dir)
done

dirs_unique=($(echo "${dirs_changed[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

if [ ${#dirs_unique[@]} -eq 0 ]; then
  echo "No directories to lint"
  exit 0
fi

base=$(pwd)
echo ${dirs_unique[@]}
for _dir in ${dirs_unique[@]}; do
  echo "cd $_dir, and run linting"
  cd $base/$_dir
  pwd
  poetry run black . --check
  poetry run isort . --check-only
  poetry run flake8 .
done

ただし、リポジトリ直下のディレクトリのみに対応しています
もちろん sed をいじればどうとでもなりますが

0
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
0
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?