我如何在每个命令上发送消息,例如“致命:必须在工作树中运行此操作”。在.git文件夹中

发布于 2025-01-25 04:22:14 字数 367 浏览 2 评论 0原文

.git文件夹中,消息

致命:该操作必须在标题中的工作树中运行

在我的输入中,例如lscd。,甚至echo hi等等。

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

我想出于我自己的目的将消息(在任何命令上)发送给用户(pwd在指定的位置中,例如/home/home/user_name/target )。 如何安装?我的环境是macos。 (如果我得到任何线索,我会在任何其他OSS上找到解决方案)

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

In a .git folder, the message

fatal: This operation must be run in a work tree

in the title comes on my input such as ls, cd ., even echo hi and so on.

My question is NOT how to solve this or NOT asking why it comes out.

I want to send messages to users (on any commands) like this for my own purpose (when PWD is in a specified location such as /home/user_name/target if possible).
How is it installed? My environment is macOS. (If I get any clues, I will find solutions on any other OSs)

(My question may not be related to Git)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2025-02-01 04:22:14

(对我来说)并不清楚您为什么想要这个。
简短的答案是否,bash,cshell,zsh,ksh等,没有这样的钩子来调用函数,并决定不根据该函数的返回值执行命令。

要执行这样的事情,您需要自己的外壳(解释器),并且您的代码将执行您在.git目录中的检查并在您决定需要打印时打印该消息时要执行的检查。

包装外壳命令似乎很微不足道,但完整的外壳功能不是。
注意:下面未测试

#!/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

这只是一个播放示例,因为真实的外壳/口译员处理多个带有重定向,子壳,终端对照组,暂停,简历等的

命令要执行此操作(仍然),然后寻找bash或您喜欢的外壳的来源并实现它。 :-)

It's not (to me) clear why you would want this.
The short answer is no, the bash, cshell, zsh, ksh, etc, do not have such a hook to make a call to a function and decide to not execute a command based on the return value of that function.

To do such a thing you would need your own shell (interpreter), and your code would perform the checks that you want to perform when your in the .git directory and print that message whenever you decide it needs to be printed.

Wrapping shell commands appears to be trivial, but full shell functionality is not.
NOTE: Below was not tested

#!/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

This is only a play example, however, because real shell/interpreters handle multiple commands with redirection, sub-shells, terminal control groups, Suspend, Resume, etc.

If you wanted to do this (still) then look for the source for bash or your favorite shell and implement it. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文