【问题标题】:Git Hooks. How to read variables from user input?Git 钩子。如何从用户输入中读取变量?
【发布时间】:2020-11-25 05:37:15
【问题描述】:

我正在尝试创建一个预提交挂钩。我希望它与用户互动。所以,我发现我可以使用
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

【问题讨论】:

  • 来自文档:Hooks can get their arguments via the environment, command-line arguments, and stdin. See the documentation for each hook below for details.,标准输入不适用于带有钩子的用户输入。
  • read -r input < /dev/tty 呢?

标签: bash git git-bash githooks


【解决方案1】:

作为in this gist,您可能需要考虑stderr(由Git 命令使用,作为for instance here

#!/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

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 2023-04-06
    • 2011-10-11
    • 1970-01-01
    • 2023-04-08
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多