问题
Following an answer to a previous question, I implemented a Git hook script which needs to fire whenever the working directory is updated. I linked this script to the following in .git/hooks:
- post-checkout
- post-commit
- post-merge
This mostly works, but not always. One case I found is git stash
. This is a problem because my hook generates a text file wihch I also mark with git update-index --assume-unchanged
to tell Git that I don't want to check in changes (an empty version is checked in). However, git stash
will revert the assume-unchanged file (to the empty file), which means the hook needs to run again, yet the hook is not invoked after git stash
.
I suspect a similar problem may exist with git rebase
too, but that's not as easy to explain.
I want a hook which Git will always run after updating the working directory. Is this possible?
回答1:
Not sure about git stash pop
(I offer some alternative in "Is there a way with to prevent “git stash pop” from marking files as modified?").
git update-index comes with another option --skip-worktree
, which might avoid the git stash issue.
See "Git - Difference Between 'assume-unchanged' and 'skip-worktree'"
But for git rebase
, you could be interested in the post-rewrite hook:
This hook is invoked by commands that rewrite commits (
git commit --amend
,git-rebase
; currentlygit-filter-branch
does not call it!).
Its first argument denotes the command it was invoked by: currently one of amend or rebase. Further command-dependent arguments may be passed in the future.The hook receives a list of the rewritten commits on stdin, in the format
<old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
The
extra-info
is again command-dependent. If it is empty, the preceding SP is also omitted. Currently, no commands pass any extra-info.
来源:https://stackoverflow.com/questions/18393567/git-hook-for-any-action-that-updates-the-working-directory