【问题标题】:How do i add linting to pre-commit hook如何将 linting 添加到预提交挂钩
【发布时间】:2017-11-09 06:05:25
【问题描述】:

大家好,我有一个 git repo,里面有 3 个文件夹(单个 js 项目)。

这是我的pre-commit 钩子

#!/bin/bash
echo -e "\033[1;92m Linting Staged Files . . . "

files=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(js|jsx)$')
path=(${files[0]//// })


if [[ $files = "" ]] ; then
  echo -e "\033[1;31m You have no staged file, exiting . . "
  exit 1
fi

for file in $files
do
git show ":$file" | $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")
 if [ $? -ne 0 ]; then
   echo -e "\033[1;31m ESLint failed on staged file '$file'. \n Code will not be committed, Please fix your code and try again."
exit 1
 fi
done

BRANCH=`git rev-parse --abbrev-ref HEAD`

if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
 echo "\033[1;31m commiting to $BRANCH is illegal."
 echo "\033[1;31m If you really know what you're doing, then add the argument '-n' to skip this git hook."
 exit 1
fi

exit 0

但它在这一行一直失败git show ":$file" | $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")

有错误

.git/hooks/pre-commit: line 17: ./node_modules/.bin/eslint --stdin --stdin-filename sfa/src/SFAApp.js: No such file or directory

我不知道我做错了什么,请帮忙。

【问题讨论】:

    标签: git bash githooks pre-commit-hook pre-commit


    【解决方案1】:

    你可以使用 npm 包 husky : https://www.npmjs.com/package/husky

    通过利用 git 钩子进行预提交,您可以通过下面的行对所有暂存文件排除已删除的文件进行 lint

    git diff --cached --name-only --diff-filter=d | grep ".js$" | xargs ./node_modules/.bin/eslint

    你的 .huskyrc 看起来会像下面这样:

    module.exports = {
     'hooks': {
       'pre-commit': 'git diff --cached --name-only --diff-filter=d | grep ".js$" | xargs ./node_modules/.bin/eslint'
      }
    }
    

    【讨论】:

      【解决方案2】:

      你正在尝试运行一个字面上命名的命令:

      ./node_modules/.bin/eslint --stdin --stdin-filename sfa/src/SFAApp.js
      

      而不是尝试运行名为:

      的命令
      ./node_modules/.bin/eslint
      

      带参数:

      --stdin
      

      和:

      --stdin-filename
      

      和:

      sfa/src/SFAApp.js
      

      解决方法是修复双引号位置。一旦你这样做了,你可能会遇到第二个问题:

      git show ":$file" | $(./$path/node_modules/.bin/eslint --stdin --stdin-filename "$file")
      

      将运行带有参数的命令,但是因为它在$(...) 中,所以会将其输出作为命令。我对 eslint 一无所知,但似乎应该这样写:

      git show ":$file" | ./$path/node_modules/.bin/eslint --stdin --stdin-filename "$file"
      

      (我也完全不确定$path 的愚蠢之处。path=(${files[0]//// }) 行将path 设置为git diff 列出的文件的第一个文件路径名组件,例如,如果输出为a/b/c.js,则path 设置为a。)

      【讨论】:

      • 就像我说的 git repo 没有保存实际项目,但其他文件夹中有不同的项目,因此我必须获取修改文件的文件夹名称并执行 that 文件夹,以及该文件夹的配置。
      • 啊哈。好吧,如果a/dir1/file1.js b/dir2/file2.js 中有更改或新文件,这将不起作用。您需要选择正确的路径每个文件(或者可能禁止影响多个子项目的提交)。
      • 我可能需要获取每个文件的文件夹路径,而不是只获取第一个。非常感谢。
      【解决方案3】:

      没关系,我通过更改解决了问题

      $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")

      ./$path/node_modules/.bin/eslint --stdin --stdin-filename $file

      TODO:研究 $() 在 bash 中的真正含义。

      【讨论】:

        猜你喜欢
        • 2019-09-21
        • 1970-01-01
        • 2016-10-16
        • 2020-08-15
        • 2012-08-23
        • 2019-06-02
        • 2015-04-20
        • 1970-01-01
        相关资源
        最近更新 更多