Skip to content

Instantly share code, notes, and snippets.

@takenoco82
Last active March 1, 2019 11:46
Show Gist options
  • Save takenoco82/20dbcaf490ba4f3fe3cca38272a6a89d to your computer and use it in GitHub Desktop.
Save takenoco82/20dbcaf490ba4f3fe3cca38272a6a89d to your computer and use it in GitHub Desktop.
gitのコミット時に静的解析する

gitのコミット時に静的解析する

./git/hooks/pre-commit を作成する

  • サンプルをコピーして作成
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
  • pre-commit の内容は以下のような感じ
  • 関数にしておくと取り回しやすい
#!/bin/bash
#
# 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.

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

# [チームで共有しておきたい!オススメ githooks まとめ - Qiita](https://qiita.com/shibukk/items/714c656c2c4de34ed504)
function lint_python() {
  flake8 --version >/dev/null 2>&1 || { echo >&2 '[ERROR] command not found: flake8'; exit 1; }

  local FILES="$(git diff --cached --name-only --diff-filter=AMCR | grep "\.py$" | tr '\n' ' ')"
  if [ -n "$FILES" ]; then
    flake8 ${FILES} >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo >&2 '[ERROR] Python lint error. run `make lint`.'
      exit 1
    fi
  fi
}

function validate_swagger_spec() {
  git diff --cached --name-only --diff-filter=AMCR | grep swagger.yaml >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    make validate_swagger_spec >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo >&2 '[ERROR] Swagger SPEC validate error.'
        exit 1
    fi
  fi
}

lint_python
validate_swagger_spec
  • 有効にする
chomd +x .git/hooks/pre-commit
git init

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment