git - 在 bash 脚本中检测当前目录

标签 git bash path git-submodules githooks

我正在尝试编写一个脚本来以不同方式初始化我的 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/

相关文章:

git - 是否可以在 Gitlab(或其他 Git 存储库管理工具)中公开特定分支?

git - 如何重命名分支并删除 GitHub 上的 "old"master 分支?

android - Mediaplayer不会退出

git - 如何获取文件相对于 Git 存储库根目录的路径?

c++ - 如何使用相对路径切换到不同的驱动器

git - 有没有办法 git stash 文件的特定行?

java - 使用 jGit 进行垃圾收集

linux - 使用 awk 命令用逗号替换双引号 csv 文件中的管道

html - 使用 hxselect 在 HTML 中按 ID 标签搜索

bash - 如何重用通配符找到的模式?