0
我使用GIT pre-commit
挂钩来查找我的README.md文件中的字符串模式前。 {{STRING_PATTERN}},然后将字符串模式更改为所需的文本/代码输出,其中还包含当前分支名称。GIT预提交挂钩更改文件,但不添加到提交
这是问题。 pre-commit
脚本的工作原理是,它定位字符串模式并用正确的文本/代码替换它,但在提交更改之前似乎没有这样做......这意味着在运行git commit -m "Updated README.md"
并执行git status
检查README.md文件显示为正在修改,如果我要运行git push origin branch_name
,则README.md文件包含实际的{{STRING_PATTERN}}标识符(不需要),而不是更新的文本(这是我想要)。
这里是pre-commit
代码,就像我说的,请注意,这确实从它所处的字符串的观点工作{{STRING_PATTERN}}标识,且具有动态创建的文本/代码更新它 - 它只是不实际上承诺这些变化,这是问题所在。
#!/usr/bin/env bash
# Echo out text to ensure pre-commit is running
echo "pre-commit working"
# Get the current branch name
function git_branch {
git rev-parse --abbrev-ref HEAD
}
# Set branch variable to current branch
branch=$(git_branch)
# Define the string/pattern marker to search for
# This pattern/string serves as a marker that
# gets replaced with the dynamically created text/code
id="{{STRING_PATTERN}}"
# Dynamically create text/code
# Inject the $branch variable into the correct location of the text
updated_text="Example text containing that is being added to the $branch branch"
# Find the string/pattern and replace it with the text/code
# Then replace it with the build status image
sed -i '' -e "s%{{STRING_PATTERN}}%$updated_text%g" README.md
当你说添加'git add'你的意思是它应该添加在脚本的末尾?所以在我运行'sed'代码块之后?也不意味着我也需要重新运行'git commit'? – corey
是的,确切地说,脚本末尾的'git add' –