为什么 Git 预推送不允许我运行输入选择,但在交互式脚本中执行 select 命令时失败

问题描述 投票:0回答:1

我制作了一个交互式预推送 git hook,在推送之前允许我在 npm 包中修改版本。

#!/bin/bash

current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

echo "Pushing ${current_branch}"

current_version=$(npm run version -s)

echo "Current version: $current_version"

patch_version=$(npx semver $current_version -i patch)
minor_version=$(npx semver $current_version -i minor)
major_version=$(npx semver $current_version -i major)

# Choose the version bump type
PS3="Select the version bump type: "
select bump_type in "patch - Bump into ${patch_version}" "minor - Bump into ${minor_version}" "major - Bump into ${major_version}" "none - Keep Same"; do

  case $bump_type in
    "patch"*)
      # Increment version based on the chosen bump type
      new_version="patch"
      break
      ;;
    "minor"*)
        new_version="minor"
        break
        ;;
    "major"*)
        new_version="major"
        break
        ;;
    "none"*)
      echo "No version selected, skipping bump."
      exit 0
      ;;
    *)
      echo "Invalid selection, please choose a valid option."
      ;;
  esac
done

echo "Bumping version to $new_version"

# Update package.json with the new version
new_version=$(npm version ${new_version} --force --silent)

echo "Version bumped to $new_version"

echo "Keep package-lock.json up to spec"
npm install
git commit -m "AutoBump Version" package.json package-lock.json

但是一旦我进入我的分支:

Pushing dev
Current version: 1.0.3
.husky/pre-push: 17: Syntax error: "do" unexpected
husky - pre-push script failed (code 2)
error: απέτυχε η δημοσίευση κάποιων ref στο 'github.com:ellakcy/fa-checkbox.git'

似乎无法运行,但如果手动运行则可以正常工作:

bash .husky/pre-push 
Pushing dev
Current version: 1.0.3
1) patch - Bump into 1.0.4
2) minor - Bump into 1.1.0
3) major - Bump into 2.0.0
4) none - Keep Same
Select the version bump type: 

作为解决问题的方法,我尝试了这个答案:https://stackoverflow.com/a/22585746/4706711

但似乎没有解决问题。

bash git npm githooks git-husky
1个回答
0
投票

git help hooks
表示预推送调用是

有关要推送的内容的信息在钩子的标准输入上提供,其形式为:

<local ref> SP <local object name> SP <remote ref> SP <remote object name> LF

因此您的

select
语句正在从标准输入读取第一个推送的引用。

<&2
select
终止符 stmt 之后添加一个
done
,让它尝试读取 fd 2,我收集的 stderr 仍然是你的终端。

© www.soinside.com 2019 - 2024. All rights reserved.