第一个问题...是否可以使用 git 来完成此任务? :)
我想要的是这个:
有时,出于我自己的调试目的,我会将代码中的一个变量切换为
true
(localMode = true;
)。但这绝对不应该发生。我应该只提交将变量设置为 false
的代码。当然,有时我会忘记进行此更改。如果我要提交“错误”代码,git 是否可以以某种方式停止或警告我?
更新:
感谢您的帮助!我最终得到了以下 shell 脚本:
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ $(echo "$FILE" | grep -E "^.+main\-controller\.js$") ]]; then
content=$(<"$FILE")
if [[ $(echo "$content" | grep -E "rootScope\.localMode = true") ]]; then
echo -e "\e[1;31m\tCommit contains localMode set to true.\e[0m" >&2
exit 1
fi
fi
done
以下链接怎么样?
http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/
我认为您可以在链接的示例代码中添加一些正则表达式来检测
localMode = true
。
是的,您可以使用
pre-commit
挂钩。
只需将名为
pre-commit
(无扩展名)的 shell 脚本放入“.git/hooks”文件夹中,其中包含检查变量的逻辑,并且:
false
并继续提交或hooks 文件夹应包含一些示例,例如“pre-commit.sample”,您可能会发现它很有帮助。
来自文档:
在您输入提交消息之前,首先运行
钩子。它用于检查即将提交的快照,查看您是否忘记了某些内容,确保测试运行,或者检查代码中需要检查的任何内容。从此挂钩退出非零会中止提交,尽管您可以使用 git commit --no-verify 绕过它。您可以执行诸如检查代码样式(运行 lint 或等效操作)、检查尾随空格(默认挂钩正是执行此操作)或检查有关新方法的适当文档之类的操作。pre-commit
这是针对那些想要寻找先前解决方案替代方案的人的更新。
现在有大量资源可以使用 git pre-commit hook 检查内容。
最“出名”的大概是https://pre-commit.com/
Git hooks 有时很难维护和共享(即使 git 2.9 引入了
core.hooksPath
配置使其变得更容易)。
我主要使用 JavaScript,我发现了一个名为 Husky 的流行模块,它通过项目管理可共享的钩子。我喜欢它,因为它通过我的
package.json
在我的项目中集成、共享和配置。
我还尝试找到一个补充模块来在提交之前检查我的内容,但我发现没有什么令人满意的。我想要类似的共享配置(在
package.json
中),如下所示:
"precommit-checks": [
{
"filter": "\\.js$",
"nonBlocking": "true",
"message": "You’ve got leftover `console.log`",
"regex": "console\\.log"
},
{
"message": "You’ve got leftover conflict markers",
"regex": "/^[<>|=]{4,}/m"
},
{
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
]
我终于做了自己的:git-precommit-checks。 如果你愿意,你可以尝试一下,否则你仍然可以寻找替代方案(特别是如果你不使用 JS)。
如果您仍然想使用 bash 脚本检查内容,您可以尝试更通用的方法并循环遍历一组搜索到的模式,这应该会停止您的提交。
#! /bin/bash
# If you encounter any error like `declare: -A: invalid option`
# then you'll have to upgrade bash version to v4.
# For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
# Hash using its key as a search Regex, and its value as associated error message
declare -A PATTERNS;
PATTERNS['^[<>|=]{4,}']="You've got leftover conflict markers";
PATTERNS['focus:\s*true']="You've got a focused spec";
# Declare empty errors array
declare -a errors;
# Loop over staged files and check for any specific pattern listed in PATTERNS keys
# Filter only added (A), copied (C), modified (M) files
for file in $(git diff --staged --name-only --diff-filter=ACM --no-color --unified=0); do
for elem in ${!PATTERNS[*]} ; do
{ git show :0:"$file" | grep -Eq ${elem}; } || continue;
errors+=("${PATTERNS[${elem}]} in ${file}…");
done
done
# Print errors
# author=$(git config --get user.name)
for error in "${errors[@]}"; do
echo -e "\033[1;31m${error}\033[0m"
# Mac OS only: use auditable speech
# which -s say && say -v Samantha -r 250 "$author $error"
done
# If there is any error, then stop commit creation
if ! [ ${#errors[@]} -eq 0 ]; then
exit 1
fi
这是我根据之前的答案得出的看法:
#! /bin/bash
#
# This is a git hook. It exits with non-zero status and thus aborts
# a running `git commit` if the processed files contain undesirable PATTERNS.
#
# To enable this hook, rename this file to .git/hooks/pre-commit and run:
# chmod a+x .git/hooks/pre-commit
#
# To make it global:
# git config --global core.hooksPath ~/git-central-hooks
#
# Source: https://stackoverflow.com/questions/26992576/how-to-make-a-git-pre-commit-code-check
#
# Known problem:
# If you encounter error `declare: -A: invalid option` or similar upgrade bash
# version to v4. For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
#
# Declare empty arrays
declare -A PATTERNS
declare -a errors
# Customize it: ['your grep pattern'] ===> "Error message when found"
PATTERNS['^[<>|=]{4,}']="You've got leftover CONFLICT markers"
PATTERNS['FIXME']="You've got FIXME hanging (consider changing to TODO)"
while read file
do
for elem in ${!PATTERNS[*]}
do
if git show :0:"$file" | grep -Eq "$elem"
then
errors+=( "${PATTERNS[${elem}]} in file '$file'" )
fi
done
# The post-loop expression generates only filenames added (A) or modified (M)
done < <( git diff --staged --name-only --diff-filter=AM --no-color --unified=0 )
# Print errors
for error in "${errors[@]}"
do
echo -e "\033[1;31m${error}\033[0m"
# Mac OS only: use auditable speech
# author=$(git config --get user.name)
# which -s say && say -v Samantha -r 250 "$author $error"
done
# Fail if there is an error
if [[ ${#errors[@]} -ne 0 ]]
then
exit 1
fi