gpt4 book ai didi

git - Git Push 上的 JSHint(更新 Hook )

转载 作者:太空狗 更新时间:2023-10-29 14:40:01 24 4
gpt4 key购买 nike

当客户端推送到远程 git 存储库(裸)时,我想要一个钩子(Hook),它可以自动对传入的更改文件运行 JSHint,并在 JSHint 返回错误时拒绝提交。我只关心确保 master 分支符合我们的 JSHint 配置。所以我有这个脚本:

#!/bin/bash

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
branch=${refname#refs/heads/}

echo ${refname}
echo ${oldrev}
echo ${newrev}
echo ${branch}

if [ "$branch" == "master" ]
then
echo "Need to JSHint" >&2
exit 1
fi

# Not updating master
exit 0

我想我有两个问题:

  1. 如何获取推送中已更改的文件列表?
  2. 如何将这些文件传递给 JSHint?

最佳答案

我不认为这是完成任务的最佳方式。基本上,代码生成 repo 中每个 JavaScript 文件的文件,然后分别调用每个 JavaScript 文件。奖励它实际上使用项目的 .jshintrc 文件(如果存在)。 Also on Gist

任何建议、指示、替代方案???

#!/bin/bash

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
branch=${refname#refs/heads/}

# Make a temp directory for writing the .jshintrc file
TMP_DIR=`mktemp -d`
EXIT_CODE=0

# If commit was on the master branch
if [ "$branch" == "master" ]
then
# See if the git repo has a .jshintrc file
JSHINTRC=`git ls-tree --full-tree --name-only -r HEAD -- | egrep .jshintrc`

JSHINT="jshint"
if [ -n "$JSHINTRC" ]
then
# Create a path to a temp .jshintrc file
JSHINTRC_FILE="$TMP_DIR/`basename \"$JSHINTRC\"`"

# Write the repo file to the temp location
git cat-file blob HEAD:$JSHINTRC > $JSHINTRC_FILE

# Update the JSHint command to use the configuration file
JSHINT="$JSHINT --config=$JSHINTRC_TMP_DIR/$JSHINTRC"
fi

# Check all of the .js files
for FILE in `git ls-tree --full-tree --name-only -r ${newrev} -- | egrep *.js`; do
FILE_PATH=`dirname ${FILE}`
FULL_PATH=${TMP_DIR}/${FILE_PATH}
mkdir -p ${FULL_PATH}
git cat-file blob ${newrev}:${FILE} > "$TMP_DIR/$FILE"
${JSHINT} ${TMP_DIR}/${FILE} >&2
# Exit status of last command
EXIT_CODE=$((${EXIT_CODE} + $?))
if [[ $EXIT_CODE -ne 0 ]]
then
rm -rf ${TMP_DIR}
exit $EXIT_CODE
fi
done
rm -rf ${TMP_DIR}
fi

# Not updating master
exit 0

关于git - Git Push 上的 JSHint(更新 Hook ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com