2

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?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Did you try a [`git update-index --skip-worktree`](http://stackoverflow.com/a/13631525/6309), to see if the file if also affected by the git stash? – VonC Aug 23 '13 at 06:05
  • I had no idea about `--skip-worktree`. Now I have tried it, and it does seem to do what I want. If you add that to your answer, I'll accept it. – John Zwinck Aug 23 '13 at 07:08

1 Answers1

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; currently git-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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried post-rewrite, and it does not solve the `git stash` problem (note that even `git stash` is a problem, not only `git stash pop`). So thanks for the tip, I may make use of it for rebase later, but I still need a solution for stash. – John Zwinck Aug 23 '13 at 07:01
  • @JohnZwinck I agree. My answer was for the "`git rebase`" part. I only offer a link for the git stash, but nothing conclusive. – VonC Aug 23 '13 at 07:03