顯示具有 commit 標籤的文章。 顯示所有文章
顯示具有 commit 標籤的文章。 顯示所有文章

2014年5月6日 星期二

commit and checkpatch.pl and hooks

checkpatch.pl 是一個 perl script。
越來檢查有沒有符合該專案的 coding style.

所以可以叫 git commit 時,自動 run 這個 script。
防止不合style 的 code 被 commit 進 source code

這個動作通常是放在 git 的 hooks 裡:
 .git/hooks/prepare-commit-msg

這個 hooks 是一個 shell script,在 git commit 時,書入完 commit 時會被執行。

所以可以這樣寫:
#!/bin/sh
if test -r "checkpatch.pl"; then
  git diff --cached | ./checkpatch.pl --no-signoff --no-tree -
fi
檢查project 目錄是不是有 checkpatch.pl 好決定是不是要 執行

這樣改完後,如果 checkpatch.pl 結果是 warning 或 error, commit 就不會進行 commit

這個 script 也可以吃參數,$1 就是 commit comment。
所以可以利用 commit comment 來控制他的動作。
例如:
 ! grep -qs "\[NO_STYLE_CHECK\]" "$1" || exit 0
就可以在 commit 時,加入 "[NO_STYLE_CHECK]" bypass 掉 checkpatch:
$ git commit -am "[NO_STYLE_CHECK] try commit dirty codes"


ref:
  • http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
  • https://answers.atlassian.com/questions/169399/sourcetree-and-git-prepare-commit-msg
  • http://www.fluff.org/ben/linux/githooks/pre-commit

#!/bin/sh
#
# pre-commit hook to run check-patch on the output and stop any commits
# that do not pass. Note, only for git-commit, and not for any of the
# other scenarios
#
# Copyright 2010 Ben Dooks, 

if git rev-parse --verify HEAD 2>/dev/null >/dev/null
then
 against=HEAD
else
 # Initial commit: diff against an empty tree object
 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

git diff --cached $against -- | ./scripts/checkpatch.pl --no-signoff -

2014年2月20日 星期四

revert 某個 commit..

有時候想把某個 commit 消除。
要作 :
$ git revert COMMIT-SHA

然後就會出現 comment editor 要你confirm,

這個動作不會把那個 commit 從 commit log 中拿掉,
而是產生一個相反動作的 commit, 把那個 commit 消除。

所以 revert 完是可以 push 到 remote server 的。



所以要把最新的 3 個 commit 都 revert 的話:
git log
commit A
 ...
commit B
 ...
commit C
..
就要反過來:
 git revert A
 git revert B
 git revert C

2013年11月5日 星期二

看某一個 commit 的 diff

只要看某一個 commit 的 diff,就用
$ git show 2e6f1155dd56512d5f848f4b7e6198c38aa256f2

2011年3月22日 星期二

merge --squash

git merge 會保留分之的 commit log,所以 merge 之後,主線會留下 分支的 commit log。
所以 merge 完後就可以砍掉分支。不用擔心分支的 commit log 會不見。

但是 git merge 會自動 commit,並且在最後加上一個 "merge" 的 commit log。

如果只是要 merge,不要 commit ,就要用 --squash 這個 option
$ git merge --squash branchname
這樣就 code merge 完就不會作 commit,但是 .. branch 的 commit log 也不會被加到主線來。

加上