我正在尝试编写一个脚本来初始化我的git钩子,无论我的git repo是子模块还是常规存储库。它目前看起来像这样:
# 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:”]]
答案 0 :(得分:0)
if [[ "`pwd`" =~ \.git/modules ]]
反引号意味着运行命令并获取其输出,pwd
是打印当前目录的命令;助记符:打印工作目录&#39 ;; =~
是匹配运算符。或者只使用$PWD
:
if [[ "$PWD" =~ \.git/modules ]]
答案 1 :(得分:0)
这使用POSIX参数扩展(如下所述)来确定当前路径是否以/.git/modules
结尾:
if [ "$PWD" != "${PWD%/.git/modules}" ]
有关参数扩展的更多信息(粘贴自dash(1)
):
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded
to produce a pattern. The parameter expansion then
results in parameter, with the smallest portion of the
suffix matched by the pattern deleted.
所以,例如。
FOO="abcdefgabc"
echo "${FOO%bc}" # "abcdefga"