This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<<<<<< HEAD | |
hoge | |
======= | |
fuga | |
>>>>>>> another-branch |
しかし、複数部分が conflict していると、たまにこの線を commit することがあります。つらい。
それを阻止したいのですが、人の手でやることでは無いので自動化します。
man githooks
- $ man git
- とりあえず man を読む。
- /hook とかすると githooks というものがあると。
- $ man githooks
- $GIT_DIR/hooks か core.hooksPath/* のファイルを読み込むと書いてあります。
- hooksPath を設定してみます。~/.gitconfig あたりに
- を追記。
- たぶん git confing --global とかでもできる。
- 実際やることは ~/.gitconfig への書き込みだから同じなのだけれど。
- これで全リポジトリに対する設定終わり。
- ~/.config/git/hooks/pre-commit を書く
- こんな感じかな。参考とか。
- これで commit 前に <<<<<< などの線があったら警告してくれます。
- 実際は単なる shell script ですね。hook なので特定の action があった時に起動してれる感じ。
- ちなみに git init したリポジトリの .git/hooks/* あたりを見ると sample があったりします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[core] | |
hooksPath = ~/.config/git/hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
FORBIDDEN='(<<<<<<<|=======|>>>>>>>)' | |
GREP_COLOR='4;5;37;41' | |
cd $PWD | |
git diff --cached --name-only | xargs -L1 grep --color --with-filename -n -E $FORBIDDEN | |
if [[ $? -eq 0 ]]; then | |
echo "Merge lines was detected.\nPlease fix files, or run 'git commit --no-verify' to skip this check." | |
exit 1 | |
fi |
動作サンプル
さて実際はどう動くかと言うと、こんな感じ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git commit -m 'perfect merge' | |
e:1:<<<<<<< HEAD | |
e:3:======= | |
e:5:>>>>>>> o | |
Merge lines was detected. | |
Please fix files, or run 'git commit --no-verify' to skip this check. | |
$ git status | |
On branch master | |
All conflicts fixed but you are still merging. | |
(use "git commit" to conclude merge) | |
Changes to be committed: | |
modified: hoge.txt |
commit したのに status が clean になってませんね。
これでミスって merge を解決し忘れたファイルを commit することも無いでしょう。めでたしめでたし。
これでミスって merge を解決し忘れたファイルを commit することも無いでしょう。めでたしめでたし。
おまけ
ソースにどうしても <<<<<< などの線を入れたい場合は
- $ git commit --no-verify
とか使うと良いようです。
この hook を足す commit そのものが hook に引っかかったので使いました。
0 件のコメント:
コメントを投稿