Git 挂钩。如何从用户输入中读取变量?
Git Hooks. How to read variables from user input?
我正在尝试创建预提交挂钩。我希望它与用户互动。所以,我发现我可以使用
read -p "Enter info: " info
或者只是
read info
我创建了一个文件:
预提交:
#!/bin/sh
read -r input
echo $input
它只是应该读取变量并输出它。但它不起作用。我的意思是它不能作为钩子。如果我 运行 它使用带有 ./.githooks/pre-commit
的终端,一切都很好。但是当我使用 git commit -am "Hook"
时,它回显空字符串并且不读取任何内容。我做错了什么吗?
Git 版本为 2.28.0.windows.1
如in this gist, you might need to take into account stderr (used by Git commands, as )
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
这些行之后的脚本示例:
consoleregexp='console.log'
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0; #THE USER WANTS TO CONTINUE
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
我正在尝试创建预提交挂钩。我希望它与用户互动。所以,我发现我可以使用
read -p "Enter info: " info
或者只是
read info
我创建了一个文件:
预提交:
#!/bin/sh
read -r input
echo $input
它只是应该读取变量并输出它。但它不起作用。我的意思是它不能作为钩子。如果我 运行 它使用带有 ./.githooks/pre-commit
的终端,一切都很好。但是当我使用 git commit -am "Hook"
时,它回显空字符串并且不读取任何内容。我做错了什么吗?
Git 版本为 2.28.0.windows.1
如in this gist, you might need to take into account stderr (used by Git commands, as
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
这些行之后的脚本示例:
consoleregexp='console.log'
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0; #THE USER WANTS TO CONTINUE
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi