bash 中的更改目录不会影响 post-commit 挂钩
change directory in bash is not affecting the post-commit hook
我已经制作了以下 bash 脚本来在子模块中进行一些更改后提交父仓库。
这都是关于脚本想要 cd .. 检查父 repo 当前分支但问题是 cd .. 不影响即将到来的命令,因为我猜 subshell
我试过 运行
1- cd ../ && 在每个命令之前
2-制作别名但没有成功
3- 运行 执行但脚本没有继续
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "post-commit".
commit_msg= git log -1 --pretty=%B
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "int1177/next" ]]; then
cd ..
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "B0/next" ]]; then
git add 6_Tests
git commit -m "bs esss"
echo "development branch B0/next has now new commit"
else
echo "development branch isn't B0/next"
fi
else
echo "current branch isn't int1177/next"
fi
实际上,这个特定问题不是 bash 问题,而是 Git 问题。
Why doesn't "cd" work in a shell script? 一般有效,是许多 other 问题的合适答案。但是这个特定的 post-commit
挂钩试图 chdir
从子模块进入其父超级项目,然后在父超级项目中进行提交。 是可能的。由于其他原因,这可能不是一个好主意——一般来说,让 Git 提交挂钩创建提交是不明智的,即使在其他存储库中也是如此1——但在这种特殊情况下,你是运行了解 Git 通过环境变量找到 其目录的事实。
特别是,有一个环境变量 GIT_DIR
告诉 Git:包含存储库的 .git
目录位于此路径。当 Git 运行 是 Git 钩子时,Git 通常将 $GIT_DIR
设置为 .
或 .git
。如果 $GIT_DIR
未设置 ,Git 将通过 directory-tree 搜索找到 .git
目录,但如果 $GIT_DIR
是设置,Git假定$GIT_DIR
设置正确。
解决方法是取消设置 GIT_DIR
:
unset GIT_DIR
cd ..
其余的sub-shell命令会运行在one-step-up目录下,现在$GIT_DIR
不再设置,Git会搜索超级项目的 work-tree 用于超级项目的 .git
目录。
顺便说一句,这个:
$(git branch | grep \* | cut -d ' ' -f2)
是获取当前分支名称的笨拙方法。使用:
git rev-parse --abbrev-ref HEAD
相反,在这里。 (另一个选项是 git symbolic-ref --short HEAD
,但它会因分离的 HEAD 而大声失败,而您可能希望安静的结果只是 HEAD
一词,rev-parse
方法将产生该词。)
1这个案例中的主要危险是超级项目存储库现在不一定处于任何状态来处理提交。 编辑: 或者,正如在 中发现的那样,甚至没有设置为 成为 该子模块的超级项目,更不用说了添加 submodule-updating 提交。
我已经制作了以下 bash 脚本来在子模块中进行一些更改后提交父仓库。 这都是关于脚本想要 cd .. 检查父 repo 当前分支但问题是 cd .. 不影响即将到来的命令,因为我猜 subshell
我试过 运行 1- cd ../ && 在每个命令之前 2-制作别名但没有成功 3- 运行 执行但脚本没有继续
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "post-commit".
commit_msg= git log -1 --pretty=%B
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "int1177/next" ]]; then
cd ..
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "B0/next" ]]; then
git add 6_Tests
git commit -m "bs esss"
echo "development branch B0/next has now new commit"
else
echo "development branch isn't B0/next"
fi
else
echo "current branch isn't int1177/next"
fi
实际上,这个特定问题不是 bash 问题,而是 Git 问题。
Why doesn't "cd" work in a shell script? 一般有效,是许多 other 问题的合适答案。但是这个特定的 post-commit
挂钩试图 chdir
从子模块进入其父超级项目,然后在父超级项目中进行提交。 是可能的。由于其他原因,这可能不是一个好主意——一般来说,让 Git 提交挂钩创建提交是不明智的,即使在其他存储库中也是如此1——但在这种特殊情况下,你是运行了解 Git 通过环境变量找到 其目录的事实。
特别是,有一个环境变量 GIT_DIR
告诉 Git:包含存储库的 .git
目录位于此路径。当 Git 运行 是 Git 钩子时,Git 通常将 $GIT_DIR
设置为 .
或 .git
。如果 $GIT_DIR
未设置 ,Git 将通过 directory-tree 搜索找到 .git
目录,但如果 $GIT_DIR
是设置,Git假定$GIT_DIR
设置正确。
解决方法是取消设置 GIT_DIR
:
unset GIT_DIR
cd ..
其余的sub-shell命令会运行在one-step-up目录下,现在$GIT_DIR
不再设置,Git会搜索超级项目的 work-tree 用于超级项目的 .git
目录。
顺便说一句,这个:
$(git branch | grep \* | cut -d ' ' -f2)
是获取当前分支名称的笨拙方法。使用:
git rev-parse --abbrev-ref HEAD
相反,在这里。 (另一个选项是 git symbolic-ref --short HEAD
,但它会因分离的 HEAD 而大声失败,而您可能希望安静的结果只是 HEAD
一词,rev-parse
方法将产生该词。)
1这个案例中的主要危险是超级项目存储库现在不一定处于任何状态来处理提交。 编辑: 或者,正如在