2016-06-15 112 views
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 

回答

0

您應該在預提交掛鉤中也執行git add以將更改添加到回購。

+0

當你說添加'git add'你的意思是它應該添加在腳本的末尾?所以在我運行'sed'代碼塊之後?也不意味着我也需要重新運行'git commit'? – corey

+0

是的,確切地說,腳本末尾的'git add' –