【问题标题】:Different version of bash running on git pre-commits on Ubuntu?在 Ubuntu 上预提交的 git 上运行的不同版本的 bash?
【发布时间】:2019-06-11 16:11:42
【问题描述】:

我正在使用 Ubuntu 18.04 以及一些 Git 2.17.1 预提交。我已经使用 chshzsh 安装为我的默认 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 上运行吗? yarngit 是否运行其他版本的 bash/dash 会产生重定向错误,如果是,我可以更改吗?

【问题讨论】:

  • 你说你在 zsh 中交互式地测试了你的命令,但是你有没有在 bash 中测试过它,这是你的脚本运行的,根据他们的 she-bang 行:#!/bin/bash

标签: bash git zsh yarnpkg ubuntu-18.04


【解决方案1】:

很可能您正在使用的 yarn 脚本实际上是由 /bin/sh 运行的,在 Ubuntu 上是 dash,而不是 bashzsh。大多数调用 shell 的程序只会调用 /bin/sh,而忽略您的个人 shell。这很重要,因为用户可能有一个不兼容 POSIX 的 shell,例如 tcsh,否则脚本会失败。

dash 中,此处的字符串(&lt;&lt;&lt; 语法)不可用,因此您可能需要切换该语法以使用回显。另请注意,双括号表示法是一种 bashism,同样不太可能起作用,因此您应该将其替换为单括号并在变量周围加上引号。

【讨论】:

    【解决方案2】:

    鉴于 bk2204 的回答,我对如何更改任何进程调用的 shell 进行了更多挖掘。我发现了一个关于更改 Ubuntu shell 的AskUbuntu answer。一旦我这样做了,预提交就会正常工作。

    【讨论】:

    • 虽然这可能适用于您的系统,但请注意,如果其他人正在处理此项目,他们将遇到同样的问题。在 Debian 或 Ubuntu 上假设 /bin/sh 的任何行为不是 POSIX plus a few extensions 是不安全的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2014-01-03
    • 2020-09-20
    • 1970-01-01
    • 2022-12-15
    • 2017-11-08
    • 2020-02-02
    相关资源
    最近更新 更多