Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Mercurial和Git中JSLint的预提交钩子_Git_Mercurial_Jslint_Pre Commit Hook - Fatal编程技术网

Mercurial和Git中JSLint的预提交钩子

Mercurial和Git中JSLint的预提交钩子,git,mercurial,jslint,pre-commit-hook,Git,Mercurial,Jslint,Pre Commit Hook,我想在对Mercurial或Git回购进行提交之前运行JSLint 我希望这是一个自动设置的步骤,而不是依赖于开发人员(主要是我)记住要先运行JSLint。我通常在开发时运行JSLint,但希望在提交到repo之前在JS文件上指定一个契约,让它们通过JSLint 对于Mercurial,说明了预提交语法,但似乎唯一可用的变量是提交中涉及的parent1和parent2变更集ID。我真正想要的是一个与提交相关的文件名列表,这样我就可以选择.js文件并对其运行jslint ,作为预提交脚本的一部分可

我想在对Mercurial或Git回购进行提交之前运行JSLint

我希望这是一个自动设置的步骤,而不是依赖于开发人员(主要是我)记住要先运行JSLint。我通常在开发时运行JSLint,但希望在提交到repo之前在JS文件上指定一个契约,让它们通过JSLint

对于Mercurial,说明了预提交语法,但似乎唯一可用的变量是提交中涉及的parent1和parent2变更集ID。我真正想要的是一个与提交相关的文件名列表,这样我就可以选择.js文件并对其运行jslint

,作为预提交脚本的一部分可用的默认信息似乎有限

可能的工作方式是将hg status/git status作为预提交脚本的一部分调用,解析该输出以查找JS文件,然后以这种方式进行工作。不过,我希望有更简单的方法,我不确定作为预提交挂钩的一部分调用status是否反映了正确的信息。例如,在Git中,如果更改文件尚未添加,但Git提交使用-a,那么这些文件是否会作为提交集的一部分显示在Git状态输出的正确部分


更新:我得到了一些有用的东西,在这里可以看到:

对于git,在.git/hooks目录中有一些示例。如果只需要JSLint的文件名,可以使用
git diff--name only
,在我的示例中,它将列出与当前
头不同的文件名


以下是@Bitbieger的Git解决方案的一个变体,该解决方案可用于和的本地副本(即,您需要
npm在根存储库目录中安装jslint

此外,脚本:

  • 在所有.html和.json文件以及.js上运行jslint
  • 仅对已添加、复制或修改的文件运行jslint。这可以防止jslint在已重命名或删除的文件上出错
  • 复制任何jslint错误以供用户查看
  • 使用
    --indent 4--white true
    jslint选项确保源代码的一致性
要使其正常工作,请将以下内容复制到
.git/hooks/pre-commit
,不要忘记
chmod+x.git/hooks/pre-commit

# Pre-commit hook passing files through jslint
#
# This ensures that all js, html and json files are valid and conform
# to expectations.

ROOT_DIR=$(git rev-parse --show-toplevel)
JSLINT="${ROOT_DIR}/node_modules/.bin/jslint --indent 4 --white true"

for file in $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- | grep -P '\.((js)|(html)|(json))$'); do
    if node $JSLINT $file 2>&1 | grep 'No errors found' ; then
        echo "jslint passed ${file}"
        exit 0
    else
        node $JSLINT $file
        exit 1
    fi  
done

感谢您对git命令的提示。因此,我似乎仍然需要使用dvcs命令来获取文件列表,并解析出要提供给jslint的文件。至少git diff--name only会使解析变得相当容易。是否应该将“出口0”置于循环之外?这样,循环实际上会检查每个文件,而不是返回传递的第一个文件。。。模式[文件]。。。请尝试“grep--help”了解更多信息。
有什么想法吗?我不熟悉grep,但是
-P
似乎是有效的。为了完整起见,这里有一些设置说明。我也有同样的需要,所以写了这个
# Pre-commit hook passing files through jslint
#
# This ensures that all js, html and json files are valid and conform
# to expectations.

ROOT_DIR=$(git rev-parse --show-toplevel)
JSLINT="${ROOT_DIR}/node_modules/.bin/jslint --indent 4 --white true"

for file in $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- | grep -P '\.((js)|(html)|(json))$'); do
    if node $JSLINT $file 2>&1 | grep 'No errors found' ; then
        echo "jslint passed ${file}"
        exit 0
    else
        node $JSLINT $file
        exit 1
    fi  
done