Mercurial 和 Git 中 JSLint 的预提交挂钩

发布于 2024-08-13 06:19:32 字数 882 浏览 8 评论 0原文

我想在提交到 Mercurial 或 Git 存储库之前运行 JSLint。

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

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

GIT 的类似问题,作为预提交脚本的一部分提供的默认信息似乎有限。

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

更新:我有一些工作,可以在这里看到:http://github.com /jrburke/dvcs_jslint/

I want to run JSLint before a commit into either a Mercurial or Git repo is done.

I want this as an automatic step that is set up instead of relying on the developer (mainly me) remembering to run JSLint before-hand. I normally run JSLint while developing, but want to specify a contract on JS files that they pass JSLint before being committed to the repo.

For Mercurial, this page spells out the precommit syntax, but the only variables that seem to be available are the parent1 and parent2 changeset IDs involved in the commit. What I really want are a list of file names that are involved with the commit, so that I can then choose the .js file and run jslint over them.

Similar issue for GIT, the default info available as part of the precommit script seems limited.

What might work is calling hg status/git status as part of the precommit script, parse that output to find JS files then do the work that way. I was hoping for something easier though, and I am not sure if calling status as part of a precommit hook reflect the correct information. For instance in Git if the changes files have not been added yet, but the git commit uses -a, would the files show up in the correct section of the git status output as being part of the commit set?

Update: I got something working, it is visible here: http://github.com/jrburke/dvcs_jslint/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千柳 2024-08-20 06:19:32

以下是 @Bitbieger 的 Git 解决方案的变体,可与 Node.jsnode-jslint (即您需要在根存储库目录中npm install jslint)。

此外,该脚本:

  • 对所有 .html 和 .json 文件以及 .js 运行 jslint
  • 仅对已添加、复制或修改的文件运行 jslint。这可以防止 jslint 在已重命名或删除的文件上出错。
  • 复制任何 jslint 错误供用户查看
  • 使用 --indent 4 --white true jslint 选项确保源代码一致性

要使其正常工作,请将以下内容复制到 .git/hooks/预提交并且不要忘记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

The following is a variation of @Bitbieger's Git solution that works with Node.js and a local copy of node-jslint (i.e. you need to npm install jslint in your root repository directory).

Additionally the script:

  • Runs jslint over all .html and .json files as well as .js
  • Only runs jslint over files that have been added, copied or modified. This prevents jslint from erroring on files that have been renamed or deleted.
  • Replicates any jslint errors for the user to see
  • Uses the --indent 4 --white true jslint options to ensure source code consistency

To get it to work copy the following to .git/hooks/pre-commit and don't forget to 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
痴梦一场 2024-08-20 06:19:32

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

For git, there are examples in the .git/hooks directory. If you just need the file names for JSLint, you could use git diff --name-only, which in my example will list the names of the files that differ from the current HEAD.

深海夜未眠 2024-08-20 06:19:32

JSLint 与 SpiderMonkey

for js in $(git diff-index --name-only --cached HEAD -- | grep '\.js
); do
    if jslint.sh $js 2>&1 | grep 'Lint at line' ; then
        echo $js
        exit 1
    else
        echo "js files validated"
        exit 0
    fi  
done

JSLint with SpiderMonkey

for js in $(git diff-index --name-only --cached HEAD -- | grep '\.js
); do
    if jslint.sh $js 2>&1 | grep 'Lint at line' ; then
        echo $js
        exit 1
    else
        echo "js files validated"
        exit 0
    fi  
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文