LoginSignup
3
6

More than 5 years have passed since last update.

WIP commit の消し忘れを教えてくれる君を作った

Last updated at Posted at 2017-04-14

作業ブランチ切ったら,最初に git commit --allow-empty -m "[WIP]" してプルリク作ってから作業を始めたい
でもこのWIPコミットを消し忘れると後々はずかしいおもいをするので,教えてくれる君をつくった

tl;dr

~/.git_template/hooks/pre-commit
#!/bin/bash
if git log -1 --oneline --pretty=format:'%s' | grep -q -e '^\[WIP\]'
then
  last_commit=`git log -1 --date=raw --pretty=format:"%ad"`
  current_commit=$GIT_AUTHOR_DATE
  if echo "$current_commit" | grep -q -v "$last_commit"
  then
    echo "last commit is wip commit!!"
    exit 1
  fi
fi

ぜんてい

Git Hooks の pre-commit にコミットを hook した時の処理を記述する
参考: gitのhooksを管理する(自分用テンプレートを使う)

ほうしん

  1. 直前のコミットのコミットメッセージが [WIP] ではじまってて
  2. hook したコミットが amend してなかったら
  3. アラートだして exit 1 する

1. 直前のコミットのコミットメッセージ

$ git log -1 --oneline --pretty=format:'%s' | grep -e '^\[WIP\]'

--prety=format のあたりは git logのフォーマットを指定する を参考に

2. amendしてるかどうか

amend すると, hook したコミットの author date は直前のコミットの author date になるので,2つが一致しているかどうかをみます

直前のコミットの author date

前項を参考に

$ git log -1 --date=raw --pretty=format:"%ad"
1491989538 +0900

hook したコミットの auther date

GIT_AUTHOR_DATE でもってこれます
が,これをechoしてみると,

.git/hooks/pre-commit

#!/bin/bash

echo $GIT_AUTHOR_DATE
exit 1
$ git commit --allow-empty -m "hoge"
@1491995878 +0900

文頭に @ がついてしまっているので, 比較ではなく grep をつかいます

last_commit=`git log -1 --date=raw --pretty=format:"%ad"`
current_commit=$GIT_AUTHOR_DATE
echo "$last_commit" | grep -q -v "$current_commit"

3. exit 1 する

します

if echo "$current_commit" | grep -q -v "$last_commit"
then
  echo "last commit is wip commit!!"
  exit 1
fi
3
6
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
3
6