简体   繁体   English

如何仅在运行 git 工作树添加命令时运行 git 挂钩

[英]How to run a git hook only when running git worktree add command

I am wanting to run a shell script when I invoke the command git worktree add .当我调用命令git worktree add时,我想运行 shell 脚本。 Reading the docs for post-checkout seems like it would run for git worktree add but it would also run for other commands which I don't want to use it for, such as git checkout .阅读用于post-checkout的文档似乎适用于git worktree add但它也适用于我不想使用它的其他命令,例如git checkout

Is there any other hook I could use?我可以使用其他任何钩子吗? Or perhaps I could use post checkout but have the script setup so it exits if it isn't the git worktree add command?或者也许我可以使用后结帐但有脚本设置,所以如果它不是git worktree add命令,它就会退出?

The reason I want to do this is to run a set of commands to set up my directory that is required when I run git worktree add , but I wouldn't need to do this setup for a normal git repository that is just using git checkout commands.我想这样做的原因是运行一组命令来设置我运行git worktree add时所需的目录,但我不需要为仅使用git checkout出的普通 git 存储库执行此设置命令。

I have come up with a solution, though I am not sure how rebust it is.我想出了一个解决方案,虽然我不确定它是如何被拒绝的。 I use the post-checkout hook as a bash script, and I have the following lines at the top.我将post-checkout挂钩用作 bash 脚本,顶部有以下几行。

if ! [[ "$0" =~ ".bare/hooks/post-checkout" ]] \
    || ! [[ "$1" == "0000000000000000000000000000000000000000" ]]; then
    echo "WARNING: post-checkout hook running for reason otherwise than git worktree add, bailing";
    exit 0;
fi

# Do something down here every time we run `git worktree add`

How this works: $0 is the path of the script and $1 is the reference of the previous head.这是如何工作的:$0 是脚本的路径,$1 是前一个头的引用。

By convention I have been cloning my bare repository into a directory called .bare which is what the first statement is checking.按照惯例,我一直将我的裸存储库克隆到一个名为.bare的目录中,这是第一条语句正在检查的内容。 The second statement is checking that the previous ref of HEAD is that string of 0s.第二个语句是检查 HEAD 的前一个 ref 是那个 0 字符串。 This catches and exits if you are just using other checkout commands such as git checkout because in that case the previous HEAD is not the string of 0s because it was pointing to a commit.如果您只是使用其他签出命令(例如git checkout出),则会捕获并退出,因为在这种情况下,前一个 HEAD 不是 0 的字符串,因为它指向一个提交。 However, it seems that because I create a new HEAD every time we run git worktree add it sets the previous HEAD ref to that string of 0s, which allows me to assert on that condition.但是,似乎因为我每次运行git worktree add时都会创建一个新 HEAD,所以它会将前一个 HEAD ref 设置为该字符串 0,这允许我在该条件下断言。

This does work for my use case so I am not sure if there is a better way or not.这确实适用于我的用例,所以我不确定是否有更好的方法。 If anyone has a better suggestion then let me know.如果有人有更好的建议,请告诉我。

PS I can get the directory of the new branch simply using pwd in my post-checkout script, which is useful for the commands that I want to run in the hook. PS 我可以简单地在我post-checkout脚本中使用pwd来获取新分支的目录,这对于我想在钩子中运行的命令很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM