Git挂钩用于更新工作目录的任何操作 [英] Git hook for any action that updates the working directory

查看:101
本文介绍了Git挂钩用于更新工作目录的任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答上一个问题

a>,我实现了一个Git钩子脚本,只要工作目录被更新就需要触发。我在.git / hooks中将此脚本链接到以下内容:


  • 结账后

  • 发布-commit

  • 合并后



这主要适用,但并非总是如此。我找到的一个例子是 git stash 。这是一个问题,因为我的钩子生成一个文本文件,我也用标记git update-index --assume-unchanged 来告诉Git我不想检入更改(检入空白版本)。但是, git stash 会将假设不变的文件恢复到空文件,这意味着钩子需要再次运行,但是钩子不会在 git stash



我怀疑类似的问题可能存在于 git rebase ,但这并不容易解释。



我想要一个Git将总是在更新工作目录之后运行的钩子。这可能吗?

解决方案不知道 git存储pop (I在提供了一些替代方法,以防止git stash pop将文件标记为已修改?)。

git update-index 附带另一个选项 - skip-worktree ,这可以避免git存储问题。

请参阅 Git - '承担 - 不变'和' skip-worktree '




但是对于 git rebase ,您可能对 post-rewrite 钩子


该钩子由重写提交的命令调用( git commit --amend git -rebase ;当前 git-filter-branch 不会调用它!)。
它的第一个参数表示它被调用的命令:目前是修改或重新分配的命令之一。未来可能会传递更多依赖于命令的参数。
$ b 钩子以
格式接收stdin上重写的提交列表



 < old-sha1> SP< new-sha1> [SP< extra-info> ] 




extra-info 又依赖于命令。如果它是空的,则前面的SP也被省略。目前,没有命令会传递任何附加信息。



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?

解决方案

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.

这篇关于Git挂钩用于更新工作目录的任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆