【发布时间】:2019-06-11 16:11:42
【问题描述】:
我正在使用 Ubuntu 18.04 以及一些 Git 2.17.1 预提交。我已经使用 chsh 将 zsh 安装为我的默认 shell。每当发生提交时,就会运行一组 yarn 命令来对 Javascript 代码进行 lint 和单元测试。 package.json 中执行 lint 检查的 yarn 脚本是:
read -r CHANGES <<< `git diff-index --name-only HEAD | grep 'workspaces/.*/\\(src\\|test\\)/.\\+\\.jsx\\?$' | tr '\\n' ' '`; if [[ $CHANGES ]]; then yarn eslint ${CHANGES}; fi;
当 yarn 尝试运行它时,我收到 Syntax error: redirection unexpected 错误。如果我采用相同的命令并直接从 zsh 提示符运行它,它工作正常。
预提交挂钩文件如下所示:
#!/bin/bash
./node_modules/pre-commit/hook
RESULT=$?
[ $RESULT -ne 0 ] && exit 1
exit 0
node_modules 中的钩子文件如下所示:
#!/bin/bash
HAS_NODE=`which node 2> /dev/null || which nodejs 2> /dev/null || which iojs 2> /dev/null`
#
# There are some issues with Source Tree because paths are not set correctly for
# the given environment. Sourcing the bash_profile seems to resolve this for bash users,
# sourcing the zshrc for zshell users.
#
# https://answers.atlassian.com/questions/140339/sourcetree-hook-failing-because-paths-don-t-seem-to-be-set-correctly
#
function source_home_file {
file="$HOME/$1"
[[ -f "${file}" ]] && source "${file}"
}
if [[ -z "$HAS_NODE" ]]; then
source_home_file ".bash_profile" || source_home_file ".zshrc" || source_home_file ".bashrc" || true
fi
NODE=`which node 2> /dev/null`
NODEJS=`which nodejs 2> /dev/null`
IOJS=`which iojs 2> /dev/null`
LOCAL="/usr/local/bin/node"
BINARY=
#
# Figure out which binary we need to use for our script execution.
#
if [[ -n "$NODE" ]]; then
BINARY="$NODE"
elif [[ -n "$NODEJS" ]]; then
BINARY="$NODEJS"
elif [[ -n "$IOJS" ]]; then
BINARY="$IOJS"
elif [[ -x "$LOCAL" ]]; then
BINARY="$LOCAL"
fi
#
# Add --dry-run cli flag support so we can execute this hook without side affects
# and see if it works in the current environment
#
if [[ $* == *--dry-run* ]]; then
if [[ -z "$BINARY" ]]; then
exit 1
fi
else
"$BINARY" "$("$BINARY" -e "console.log(require.resolve('pre-commit'))")"
fi
还有其他使用 OSX 的用户也运行这些钩子,并且可以为他们找到。有什么方法可以让它在 Ubuntu 上运行吗? yarn 或 git 是否运行其他版本的 bash/dash 会产生重定向错误,如果是,我可以更改吗?
【问题讨论】:
-
你说你在 zsh 中交互式地测试了你的命令,但是你有没有在 bash 中测试过它,这是你的脚本运行的,根据他们的 she-bang 行:
#!/bin/bash。
标签: bash git zsh yarnpkg ubuntu-18.04