[英]How to make a git pre-commit code check?
第一个问题......甚至有可能用git来完成这个吗? :)
我想要的是这个:
有时localMode = true;
为了自己的调试目的,我将代码中的一个变量切换为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
是的,您可以使用pre-commit
挂钩。
只需在您的“.git/hooks”文件夹中放置一个名为pre-commit
(无扩展名)的 shell 脚本,并使用逻辑检查您的变量,或者:
false
并继续提交或hooks 文件夹应该包含一些示例,例如“pre-commit.sample”,您可能会发现它们很有帮助。
从文档:
pre-commit
钩子首先运行,甚至在你输入提交消息之前。 它用于检查即将提交的快照,查看您是否忘记了某些内容,确保测试运行,或检查您需要在代码中检查的任何内容。 从这个钩子退出非零会中止提交,尽管你可以使用 git commit --no-verify 绕过它。 您可以执行诸如检查代码样式(运行 lint 或等效的东西)、检查尾随空格(默认挂钩正是这样做的)或检查有关新方法的适当文档之类的操作。
下面的链接呢?
http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/
我认为您可以在链接的示例代码中添加一些正则表达式来检测localMode = true
。
这是针对想要寻找先前解决方案替代方案的人的更新。
现在有很多资源可以使用 git pre-commit hook 检查内容。
最“有名”的大概是https://pre-commit.com/
Git 钩子有时难以维护和共享(即使 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.