Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

リポジトリにpushする前に簡易なコードチェックをする

More than 3 years have passed since last update.

リポジトリにpushする前に最低限、

  • debug用のコードが含まれていないこと
  • swapファイルによる編集の勘違いがないか
  • コードが規約上(rubocop)問題ないか

を徹底したかったので、gitのpre-pushを書いてみました。

$ chmod +x .git/hooks/pre-push

を忘れずに!

.git/hooks/utils
#!/bin/sh

check() {
  echo "< Check $1 >"
  eval $2
  echo "=> OK!\n"
}

finish() {
  echo 'Finish!!'
  sleep 0.5
  exit 0
}

skip_if_needed() {
  if [ -n "$SKIP" ]; then
    echo "Skip checker!!"
    sleep 0.5
    exit 0
  fi
}

declare -F > /dev/null
.git/hooks/pre-push
#!/bin/sh

source ./utils

skip_if_needed

# sawp files check -->
check_swap_files() {
  swap_files=`find . -name ".*.sw*"`

  if [ -n "$swap_files" ]; then
    echo "Failed! This project contain swap files!!!"
    echo "$swap_files"
    exit 1
  fi
}
check "swap files" check_swap_files


# development code check -->
check_dev_code() {
  keywords=(binding.pry debugger focus:)

  for keyword in ${keywords[@]}; do
    invalid_files=`find spec app lib -type f -exec grep -l $keyword {} \;`
    if [ -n "$invalid_files" ]; then
      echo "Failed! \"$keyword\" is contained these files!!!"
      for file in $invalid_files; do
        line_nums=`grep -n $keyword $file | cut -c 1`
        for line_num in $line_nums; do
          echo "\"$keyword\"\t in $file on line $line_num"
        done
      done
      exit 1
    fi
  done
}
check "development code" check_dev_code


# rubocop check -->
check_coding_style() {
  bundle exec rubocop -D

  if [ $? -ne 0 ]; then
    exit 1
  fi
}
check "coding style" check_coding_style


finish
kei-p
iOS/Ruby/cocos2d-x/インフラ当たりを浅く広く。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away