Git 预提交:获取用户 y/n 选项以继续

问题描述 投票:0回答:1

在进行 git hook 时有没有办法获取用户 y/n 选项?例如,在运行预提交

ruff
linter 检查后,用户可能决定不添加建议的更改。

如果是这样,有没有办法通过添加

pre-commit
选项来通过 shell
would you like to continue with the commit?
文件(在 .git/hooks 中)本身继续进行更改?

我知道 vscode 中有一个

No verify commit
选项,但这不是我要找的。

git pre-commit-hook pre-commit linter
1个回答
0
投票

你的预提交挂钩可能是这样的:

#!/bin/bash

# 获取用户输入
read -p "would you like to continue with the commit? [Y/n] " answer

# 判断用户输入
if [[ $answer =~ ^[Yy]$ || $answer == "" ]]; then
    exit 0 # 退出并允许提交
else
    echo "Commit aborted."
    exit 1 # 退出并阻止提交
fi
© www.soinside.com 2019 - 2024. All rights reserved.