我正在尝试编写一个脚本来以不同方式初始化我的 git Hook ,无论我的 git 存储库是一个子模块还是只是一个常规存储库。目前看起来像这样:
# Get to root directory of client repository
if [[ ":$PATH:" != *":.git/modules:"* ]]; then # Presumed not being used as a submodule
cd ../../
else
cd .. # cd into .git/modules/<nameOfSubmodule> (up one level from hooks)
submoduleName=${PWD##*/} # Get submodule name from current directory
cd ../../../$submoduleName
fi
但是在测试中,即使我在子模块中,它似乎也总是采用 else
路由。
在确定我的路径是否包含预期字符的这一行中我是否遗漏了什么?
if [[ ":$PATH:" != ":.git/modules:" ]]
最佳答案
if [[ "`pwd`" =~ \.git/modules ]]
Backticks 表示运行命令并获取其输出,pwd
是打印当前目录的命令;助记符:打印工作目录'; =~
是匹配运算符。或者简单地使用 $PWD
:
if [[ "$PWD" =~ \.git/modules ]]
关于git - 在 bash 脚本中检测当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855301/