如何在 .git 文件夹中的每个命令上发送消息,例如 "fatal: This operation must be run in a work tree"

How can I send messages on every single command like "fatal: This operation must be run in a work tree" in a .git folder

.git 文件夹中,邮件

fatal: This operation must be run in a work tree

标题是我输入的,例如lscd .、甚至echo hi等等。

我的问题不是如何解决这个问题,也不是问它为什么会出现。

出于我自己的目的,我想像这样向用户发送消息(在任何命令上)(如果可能,当 PWD 位于指定位置时,例如 /home/user_name/target)。 它是如何安装的?我的环境是 macOS。 (如果我得到任何线索,我会在任何其他操作系统上找到解决方案)

(我的问题可能与Git无关)

(对我而言)不清楚您为什么想要这个。 简短的回答是否定的,bash、cshell、zsh、ksh 等没有这样的钩子来调用函数并决定不执行基于 return 该函数的值。

要执行此操作,您需要自己的 shell(解释器),并且您的代码将执行您在 .git 目录中时要执行的检查并打印出来每当您决定需要打印消息时。

包装 shell 命令似乎微不足道,但完整的 shell 功能却并非如此。 注意:以下未经测试

#!/bin/bash
while [ read line ]
do
   if inGitDir 
   then
      if [ $line != "pwd" ]
      then
          echo "fatal: This operation must be run in a work tree"
      else
          exec $line
      fi
done

这只是一个游戏示例,但是,因为真正的 shell/interpreters 处理具有重定向、sub-shells、终端控制组、暂停、恢复等的多个命令

如果您想(仍然)这样做,请寻找 bash 或您最喜欢的 shell 的来源并实施它。 :-)