我想警告用户,如果他们的提交消息不遵循某组指南,然后给他们选项来编辑他们的提交消息,忽略警告或取消提交。问题是我似乎无法访问stdin。
下面是我的commit-msg文件:
function verify_info {
if [ -z "$(grep '$2:.*[a-zA-Z]' $1)" ]
then
echo >&2 $2 information should not be omitted
local_editor=`git config --get core.editor`
if [ -z "${local_editor}" ]
then
local_editor=${EDITOR}
fi
echo "Do you want to"
select CHOICE in "edit the commit message" "ignore this warning" "cancel the commit"; do
case ${CHOICE} in
i*) echo "Warning ignored"
;;
e*) ${local_editor} $1
verify_info "$1" $2
;;
*) echo "CHOICE = ${CHOICE}"
exit 1
;;
esac
done
fi
}
verify_info "$1" "Scope"
if [ $# -ne 0 ];
then
exit $#
fi
verify_info "$1" "Affects"
if [ $# -ne 0 ];
then
exit $#
fi
exit 0
当我将Scope信息留空时,这是输出:
Scope information should not be omitted
Do you want to:
1) edit the commit message 3) cancel the commit
2) ignore this warning
#?
消息是正确的,但实际上并没有停止输入。我也尝试使用更简单的“读取”命令,它也有同样的问题。似乎问题在于,此时git控制了stdin并提供了自己的输入。我该如何解决?
更新:似乎这可能是this question的重复,不幸的是,这似乎表明我运气不好。
调用exec < /dev/tty
为键盘分配标准输入。在post-commit git hook中为我工作:
#!/bin/sh
echo "[post-commit hook] Commit done!"
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
while true; do
read -p "[post-commit hook] Check for outdated gems? (Y/n) " yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] ) bundle outdated --pre; break;;
[Nn] ) exit;;
* ) echo "Please answer y or n for yes or no.";;
esac
done
commit-msg
钩子不在交互式环境中运行(正如您所注意到的)。
可靠地通知用户的唯一方法是向stdout写入错误,将提交消息的副本放在BAD_MSG
文件中并指示用户编辑文件和git commit --file=BAD_MSG
如果您对环境有一些控制权,则可以使用备用编辑器,该编辑器是一个检查建议消息的包装脚本,并且可以使用额外的注释消息重新启动编辑器。
基本上,您运行编辑器,检查根据您的规则保存的文件。如果失败,请将警告消息(带有前导#
)添加到文件中,然后重新启动编辑器。
您甚至可以允许他们在消息中放入#FORCE=true
行,这将阻止检查并继续。
为了让select
停止输入,你也可以尝试从stdin
重定向select
的/dev/fd/3
(参见:Read input in bash inside a while loop)。
# sample code using a while loop to simulate git consuming stdin
{
echo 'fd 0' | while read -r stdin; do
echo "stdin: $stdin"
echo "Do you want to"
select CHOICE in "edit the commit message" "ignore this warning" "cancel the commit"; do
case ${CHOICE} in
i*) echo "Warning ignored"
;;
e*) echo ${local_editor} $1
echo verify_info "$1" $2
;;
*) echo "CHOICE = ${CHOICE}"
exit 1
;;
esac
done 0<&3 3<&-
done
} 3<&- 3<&0
从命令行运行git commit时,这很好用。在Windows上(没有试过linux),如果你使用gitk或git-gui,你将无法提示,因为你在“exec </ dev / tty”行上收到错误。
解决方案是在你的钩子中调用git-bash.exe:
.git / hooks / post-commit包含:
#!/bin/sh
exec /c/Program\ Files/Git/git-bash.exe /path/to/my_repo/.git/hooks/post-checkout.sh
.git / hooks / post-commit.sh文件包含:
# --------------------------------------------------------
# usage: f_askContinue "my question ?"
function f_askContinue {
local myQuestion=$1
while true; do
read -p "${myQuestion} " -n 1 -r answer
case $answer in
[Yy]* ) printf "\nOK\n"; break;;
[Nn]* ) printf "\nAbandon\n";
exit;;
* ) printf "\nAnswer with Yes or No.\n";;
esac
done
}
f_askContinue "Do you want to continue ?"
echo "This command is executed after the prompt !"
如何在Node.js或TypeScript中执行此操作
编辑:我做了一个npm package
我看到有人在Eliot Sykes answer上评论如何为其他语言做这件事,但JavaScript解决方案有点长,所以我会单独回答。
我不确定是否需要O_NOCTTY
,但它似乎没有任何影响。我真的不明白控制终端是什么。 GNU docs description。我认为这意味着,如果使用O_NOCTTY
,你将无法向进程发送CTRL+C
(如果它还没有控制终端)。在这种情况下,我会保留它,所以你不控制衍生的进程。我认为主节点进程应该已经有一个控制终端。
我改编了这个GitHub issue的答案
我没有看到任何关于如何使用tty.ReadStream
构造函数的文档,所以我通过Node.js source code做了一些试验和错误/挖掘。
你必须使用Object.defineProperty
,因为Node.js internals也使用它,并没有定义一个setter。另一种方法是做process.stdin.fd = fd
,但我得到了重复输出。
无论如何,我想用Husky.js使用它,它似乎工作到目前为止。当我有空的时候,我应该把它变成一个npm包。
Node.js的
#!/usr/bin/env node
const fs = require('fs');
const tty = require('tty');
if (!process.stdin.isTTY) {
const { O_RDONLY, O_NOCTTY } = fs.constants;
let fd;
try {
fd = fs.openSync('/dev/tty', O_RDONLY + O_NOCTTY);
} catch (error) {
console.error('Please push your code in a terminal.');
process.exit(1);
}
const stdin = new tty.ReadStream(fd);
Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: true,
get: () => stdin,
});
}
...Do your stuff...
process.stdin.destroy();
process.exit(0);
打字稿:
#!/usr/bin/env ts-node
import fs from 'fs';
import tty from 'tty';
if (!process.stdin.isTTY) {
const { O_RDONLY, O_NOCTTY } = fs.constants;
let fd;
try {
fd = fs.openSync('/dev/tty', O_RDONLY + O_NOCTTY);
} catch (error) {
console.error('Please push your code in a terminal.');
process.exit(1);
}
// @ts-ignore: `ReadStream` in @types/node incorrectly expects an object.
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37174
const stdin = new tty.ReadStream(fd);
Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: true,
get: () => stdin,
});
}
...Do your stuff...
process.stdin.destroy();
process.exit(0);
read -p "Question? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null; then
#do if Yes
else
#do if No
fi