Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何进行git预提交代码检查?_Git_Pre Commit Hook_Pre Commit - Fatal编程技术网

如何进行git预提交代码检查?

如何进行git预提交代码检查?,git,pre-commit-hook,pre-commit,Git,Pre Commit Hook,Pre Commit,第一个问题。。。甚至可以用git实现这一点吗?:) 我想要的是: 有时,出于调试目的,我会将代码中的一个变量切换到true(localMode=true;)。但这永远不应该被承诺。我应该只提交变量设置为false的代码。当然,有时我会忘记做这个改变。如果我即将提交“错误”代码,git是否可能以某种方式停止或警告我 UPD: 谢谢你的帮助!我最终得到了以下shell脚本: #!/bin/bash git diff --cached --name-only | while read FILE; do

第一个问题。。。甚至可以用git实现这一点吗?:)

我想要的是:

有时,出于调试目的,我会将代码中的一个变量切换到
true
localMode=true;
)。但这永远不应该被承诺。我应该只提交变量设置为
false
的代码。当然,有时我会忘记做这个改变。如果我即将提交“错误”代码,git是否可能以某种方式停止或警告我

UPD:
谢谢你的帮助!我最终得到了以下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
#/bin/bash
git diff--缓存--仅限名称|读取文件时;做
如果[$(echo“$FILE”| grep-E“^.+main \-controller\.js$”)];然后
内容=$(&2)
出口1
fi
fi
完成

是的,您可以使用
预提交
钩子

只需在“.git/hooks”文件夹中放置一个名为
pre-commit
(不带扩展名)的shell脚本,该脚本具有检查变量的逻辑,并且:

  • 将其更改为
    false
    并继续执行提交或
  • 打印一条消息,告诉用户手动更正该值,并使用非零代码退出以中止提交
hooks文件夹应该包含一些示例,例如“pre-commit.sample”,您可能会发现这些示例很有用

从:

预提交
钩子在您输入提交消息之前首先运行。它用于检查即将提交的快照,查看您是否忘记了什么,确保测试运行,或者检查代码中需要检查的任何内容。从该钩子退出非零将中止提交,尽管您可以使用git commit传递它——无需验证。您可以执行以下操作:检查代码样式(运行lint或类似的代码)、检查尾随空格(默认钩子正是这样做的),或者检查有关新方法的适当文档


下面的链接呢

我认为您可以在链接处的示例代码中添加一些正则表达式来检测
localMode=true

这是为希望找到以前解决方案的替代方案的人提供的更新

现在有很多资源可以使用git预提交钩子检查内容

最著名的可能是

Git钩子有时很难维护和共享(即使Git2.9引入了
core.hooksPath
config使之更容易)

我主要使用JavaScript,发现了一个名为的流行模块,它通过projet管理可共享的钩子。我喜欢它,因为它通过我的
包.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)"
  }
]
“预提交检查”:[
{
“过滤器”:“\\.js$”,
“非阻塞”:“真”,
“消息”:“您有剩余的`console.log`”,
“regex”:“控制台\\.log”
},
{
“消息”:“您有剩余的冲突标记”,
“正则表达式”:“/^[|=]{4,}/m”
},
{
“消息”:“您有未完成的开发人员”,
“非阻塞”:“真”,
“regex:”(?:FIXME | TODO)”
}
]
我终于做了我自己的:。 如果您愿意,您可以尝试它,否则您仍然可以寻找替代方案(特别是如果您不使用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
#如果遇到诸如“declare:-A:无效选项”之类的错误`
#然后您必须将bash版本升级到v4。
#有关Mac OS,请参阅http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
#将其键用作搜索正则表达式,并将其值用作关联的错误消息
声明-A模式;
模式['^[|=]{4,}']=“您有剩余的冲突标记”;
模式['focus:\s*true']=“你有一个焦点规范”;
#声明空错误数组
声明错误;
#在暂存文件上循环并检查PATTERNS键中列出的任何特定模式
#仅筛选添加(A)、复制(C)、修改(M)文件
对于$(git diff--staged--name only--diff filter=ACM--no color--unified=0)中的文件,执行以下操作
对于${!PATTERNS[*]};do中的元素
{git show:0:“$file”| grep-Eq${elem};}| | continue;
错误+=(${file}中的“${PATTERNS[${elem}]}…”);
完成
完成
#打印错误
#author=$(git config--get user.name)
对于“${errors[@]}”中的错误,请执行以下操作
echo-e“\033[1;310M${error}\033[0m”
#仅限Mac OS:使用可审核语音
#哪个-s say&&say-v Samantha-r 250“$author$error”
完成
#如果有任何错误,请停止提交创建
如果![${错误[@]}-eq 0];那么
出口1
fi

以下是我根据之前的答案得出的结论:

!/bin/bash
#
#这是一个git钩子。它以非零状态退出,因此中止
#如果处理的文件包含不需要的模式,则运行“git commit”。
#
#要启用此挂钩,请将此文件重命名为.git/hooks/pre-commit并运行:
#chmoda+x.git/hooks/pre-commit
# 
#要使其全球化:
#git config——global core.hooksPath~/git-central-hooks
#
#资料来源:https://stackoverflow.com/questions/26992576/how-to-make-a-git-pre-commit-code-check
# 
#已知问题:
#如果遇到错误`declare:-A:invalid option`或类似的升级bash
#版本到v4。有关Mac OS,请参阅http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
#
#声明空数组
声明-A模式
声明-a错误
#自定义它:[“您的grep模式”]=>“找到时的错误消息”
模式['^[|=]{4,}']=“您有剩余的冲突标记”
模式['FIXME']=“您已经挂起了FIXME(考虑更改为TODO)”
读取文件时
做
对于${!PATTERNS[*]}中的元素
做
如果git显示:0:“$file”| grep-Eq“$elem”
然后
错误+=(“${PATTERNS[${elem}]}在文件“$file”中)
fi
D