在 git 上 bash 运行 的不同版本在 Ubuntu 上预提交?
Different version of bash running on git pre-commits on Ubuntu?
我正在使用 Ubuntu 18.04 以及一些 Git 2.17.1 预提交。我已经使用 chsh 安装 zsh 作为我的默认 shell。任何时候发生提交,一组 yarn 命令 运行s 到 lint 和单元测试 Javascript 代码。 yarn 中执行 lint 检查的 package.json 脚本是:
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/"
[[ -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 运行ning 其他版本的 bash/dash 会产生重定向错误,如果是这样,我可以更改它吗?
您正在使用的 yarn 脚本可能实际上是 /bin/sh
运行,在 Ubuntu 上是 dash
,而不是 bash
或 zsh
。大多数调用 shell 的程序只会调用 /bin/sh
,而忽略您个人的 shell。这很重要,因为用户可能有 shell,例如 tcsh
而不是 POSIX-compatible,否则脚本会失败。
在 dash
中,here-strings(<<<
语法)不可用,因此您可能需要将该语法切换为使用 echo。另请注意,double-brackets 符号是一种 bashism,同样不太可能起作用,因此您应该将其替换为单括号和变量周围的引号。
鉴于 bk2204 的回答,我进一步深入研究了如何更改任何进程调用的 shell。我找到了关于更改 Ubuntu shell 的 AskUbuntu answer。一旦我这样做了,pre-commits 就可以正常工作了。
我正在使用 Ubuntu 18.04 以及一些 Git 2.17.1 预提交。我已经使用 chsh 安装 zsh 作为我的默认 shell。任何时候发生提交,一组 yarn 命令 运行s 到 lint 和单元测试 Javascript 代码。 yarn 中执行 lint 检查的 package.json 脚本是:
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/"
[[ -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 运行ning 其他版本的 bash/dash 会产生重定向错误,如果是这样,我可以更改它吗?
您正在使用的 yarn 脚本可能实际上是 /bin/sh
运行,在 Ubuntu 上是 dash
,而不是 bash
或 zsh
。大多数调用 shell 的程序只会调用 /bin/sh
,而忽略您个人的 shell。这很重要,因为用户可能有 shell,例如 tcsh
而不是 POSIX-compatible,否则脚本会失败。
在 dash
中,here-strings(<<<
语法)不可用,因此您可能需要将该语法切换为使用 echo。另请注意,double-brackets 符号是一种 bashism,同样不太可能起作用,因此您应该将其替换为单括号和变量周围的引号。
鉴于 bk2204 的回答,我进一步深入研究了如何更改任何进程调用的 shell。我找到了关于更改 Ubuntu shell 的 AskUbuntu answer。一旦我这样做了,pre-commits 就可以正常工作了。