当我还原文件时,如何防止 git post-checkout hook 执行?

How can I prevent git post-checkout hook from executing when I revert files?

我有一个 git post-checkout 钩子,每当我检出到不同的提交时,它会自动重新编译我的 Visual Studio 项目。这很好用,但我注意到当我在 TortoiseGit 中执行恢复时,挂钩也会执行,包括日志-window 和提交 window(我右键单击我想要的文件还原,然后在弹出菜单中单击“还原”)。有什么办法可以防止这种情况发生吗?

根据post-checkout

The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0).

您可以测试第三个参数。如果为 0,则不编译。例如 Bash,

#!/bin/bash

flag=
if [[ "$flag" -eq 0 ]];then
    exit 0
fi

在Python,

#!/usr/bin/env python

import sys

flag = sys.argv[3]
if flag == '0':
    sys.exit(0)